Giter Site home page Giter Site logo

uart-communication-stm32-esp32's Introduction

How to configure UART communication between STM32 and ESP32

This tutorial will show you how to configure a UART communication between STM32 board and ESP32 board. You have to take into account that in this example I will configure STM32 as the transmitter board and ESP32 as the receiver board, so this configuration only works in this scenario.

Requirements

STM32 board pinout configuration

We will use STM32CubeMX sofware in order to configure pins of STM32 board. By default UART2 is enable, but in this case I will enable UART6 and configure in Single Wire (Half-Duplex) mode. It's important that the Baud Rate is configured at 115200 bits/s in both boards.

182590756-f9dc89ee-fae9-4c8b-99e9-f6bbefd62427

Now, in the main loop of the code (main.c) you have to add this code in order to send data. This will send the word "Hello" each 2 seconds through PC_6 pin (UART6).

  while (1)
  {
    /* USER CODE END WHILE */
    HAL_UART_Transmit(&huart6, "Hello", 6, 1000);
    HAL_Delay(2000);
    /* USER CODE BEGIN 3 */
  }

ESP32 board configuration

You have to add this configuration to your ESP32 code. This code will configure pin 17 as a transmitter and pin 16 as a receiver.

#define TXD_PIN (GPIO_NUM_17)
#define RXD_PIN (GPIO_NUM_16)

void setup() {
    Serial.begin(115200);
    const uart_port_t uart_num = UART_NUM_2;
    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_CTS_RTS,
        .rx_flow_ctrl_thresh = 122,
    };
    // Configure UART parameters
    ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config));
    ESP_ERROR_CHECK(uart_set_pin(UART_NUM_2, TXD_PIN, RXD_PIN, 18, 19));
    const int uart_buffer_size = (1024 * 2);
    QueueHandle_t uart_queue;
    // Install UART driver using an event queue here
    ESP_ERROR_CHECK(uart_driver_install(UART_NUM_2, uart_buffer_size, \
                                        uart_buffer_size, 10, &uart_queue, 0));
}

Once this is configure, you have to add a code that constantly check if there is any data to receive.

void loop() {
    // Read data from UART.
    const uart_port_t uart_num = UART_NUM_2;
    uint8_t data[128];
    int length = 0;
    ESP_ERROR_CHECK(uart_get_buffered_data_len(uart_num, (size_t*)&length));
    length = uart_read_bytes(uart_num, data, length, 100);
    
    if (length > 0) {
        char * test = (char*) data;
        Serial.println(test);
    }
}

Circuit diagram

Connect a single cable through which the STM32 board will transmit the data (Pin PC_6) and the ESP32 will receive it (Pin IO16). Check out the pinout images of this repository.

Untitled Sketch_bb

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.