uart newbie question
Posts: 5
Joined: Wed Oct 16, 2019 11:31 am

uart newbie question

Postby » Thu Oct 17, 2019 6:44 pm

Hello! This is my first post. I'm starting to develop with esp-idf and ESP32. I have experience with AVR and ARM Cortex-M0+ microcontrollers.

I installed the esp-idf stable without any issue, downloaded a clean project, coded a blink led and works fine. The board that I'm using is ESP32-S 1.1 board "Nodemcu".

I addded the code for a echo in the UART2 port, connected the board to a typical usb to uart (FTDI, tested) but the echo doesn't works. Also tested writing hello world to the uart2 and didnt work.

I think that must be a really easy solution but I can't find it.

This is my code:

Code: Select all

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
#include "driver/gpio.h"
#include "sdkconfig.h"

 * This is an example which echos any data it receives on UART1 back to the sender,
 * with hardware flow control turned off. It does not use UART driver event queue.
 * - Port: UART1
 * - Receive (Rx) buffer: on
 * - Transmit (Tx) buffer: off
 * - Flow control: off
 * - Event queue: off
 * - Pin assignment: see defines below


#define BLINK_GPIO 2

#define BUF_SIZE (1024)

static void echo_task()
    /* Configure parameters of an UART driver,
     * communication pins and install the driver */
    uart_config_t uart_config = {
            .baud_rate = 115200,
            .data_bits = UART_DATA_8_BITS,
            .parity    = UART_PARITY_DISABLE,
            .stop_bits = UART_STOP_BITS_1,
            .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
    uart_param_config(UART_NUM_2, &uart_config);
    uart_driver_install(UART_NUM_2, BUF_SIZE * 2, 0, 0, NULL, 0);

    // Configure a temporary buffer for the incoming data
    uint8_t *data = (uint8_t *) malloc(BUF_SIZE);

    while (1) {
        // Read data from the UART
        int len = uart_read_bytes(UART_NUM_2, data, BUF_SIZE, 20 / portTICK_RATE_MS);
        // Write data back to the UART
        uart_write_bytes(UART_NUM_2, (const char *) data, len);

static void blink_task()
    while(1) {
        gpio_set_level(BLINK_GPIO, 1);
        vTaskDelay(20 / portTICK_PERIOD_MS);
        gpio_set_level(BLINK_GPIO, 0);
        vTaskDelay(1000 / portTICK_PERIOD_MS);

void app_main()
    gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);

    xTaskCreate(echo_task, "uart_echo_task", 1024, NULL, 10, NULL);
    xTaskCreate(blink_task, "blink_task", 1024, NULL, 10, NULL);
the ftdi rx and tx are connected to pin 16 and 17 (yes, I tried inverting the tx/rx pin :lol: )

Any help?
thank you very much!

Posts: 471
Joined: Fri Jan 12, 2018 9:10 pm

Re: uart newbie question

Postby mikemoy » Fri Oct 18, 2019 2:13 am

What pins are you using for UART2?

Posts: 2951
Joined: Tue Dec 01, 2015 7:35 am

Re: uart newbie question

Postby WiFive » Fri Oct 18, 2019 5:34 am

Don't use UART_PIN_NO_CHANGE, default function is not tx/rx

Posts: 92
Joined: Sat Mar 09, 2019 2:56 am

Re: uart newbie question

Postby wevets » Sat Oct 19, 2019 10:25 pm

I had the same problem initially when working with UARTs. The ESP32 has 3 UARTs build in, UART0, 1 and 2. I notice in your code that you seem to be using UART 2. Try changing all those references to UART0, the UART to which the USB port is connected. Also, if you use UART0, I don't believe you have to use a You can get to those other UARTs, but you have to get to them through GPIO pins. Here's the code I use to set up UART0, shamelessly stolen from the uart_events example in the esp-idf. It worked for me right out of the box.
  1. #include "driver/uart.h"
  2.  #define EX_UART_NUM UART_NUM_0
  4.   uart_config_t uart_config = {
  5.         .baud_rate = 115200,
  6.         .data_bits = UART_DATA_8_BITS,
  7.         .parity = UART_PARITY_DISABLE,
  8.         .stop_bits = UART_STOP_BITS_1,
  9.         .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
  10.     };
  11.     uart_param_config(EX_UART_NUM, &uart_config);
  13.     //Set UART pins (using UART0 default pins ie no changes.)
  15.     //Install UART driver, and get the queue.
  16.     uart_driver_install(EX_UART_NUM, BUF_SIZE * 2, BUF_SIZE * 2, 20, &uart0_queue, 0);
  18.     //Create a task to handle UART event from ISR
  19.     xTaskCreatePinnedToCore(uart_task, "uart_task", 2048, NULL, 5, NULL, APP_CPU_NUM);

Posts: 92
Joined: Sat Mar 09, 2019 2:56 am

Re: uart newbie question

Postby wevets » Sat Oct 19, 2019 10:31 pm

Forgot to mention that writing back to the terminal can be done either with uart_write_byte() statements or with printf().
Posts: 5
Joined: Wed Oct 16, 2019 11:31 am

Re: uart newbie question

Postby » Sun Oct 20, 2019 3:33 pm

Hello! Thanks everyone for your replys. I was out some days, sorry for the late reply.

The problem was that I assumed the TX and RX pins had to work with UART_PIN_NO_CHANGE, but they didn't. I changed back to uart 2, used GPIO_NUM_17 and GPIO_NUM_16 for tx and rx, flashed and worked.

thank you very much!! :mrgreen: :mrgreen:

Who is online

Users browsing this forum: Bing [Bot] and 31 guests