Page 1 of 1

Unable to Receive RS485 MAX data after switching to Receive mode of esp32s3

Posted: Fri Apr 04, 2025 6:36 am
by devhari
Hi
I'm working on communicating from esp32-c3-wroom-2 module to MAX chip.
From MAX2 to I'll send one time data to MAX1(Connected to esp module).
Initially I'm sending data from esp to MAX2 side. This is working fine. But after sending data my esp switching to receive mode by making RE&DE to low. So, This one is not working.
Below I have attached my esp source code and connections. Kindly let me know if any modifications required.

Code: Select all

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "esp_log.h"
#include "driver/uart.h"
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#define BUFFER_SIZE 256

#define UART_NUM UART_NUM_1  // Use UART1 for communication
#define TXD_PIN GPIO_NUM_4   // TX pin for UART1 (change as needed)
#define RXD_PIN GPIO_NUM_5   // RX pin for UART1 (change as needed)
#define BAUD_RATE 9600       // Baud rate (match with TTL device)
#define RS485_ENABLE_PIN GPIO_NUM_2  // RS485 Enable (Connect to DE & RE)

uint8_t rxBuff_from_device[32];
uint8_t send_rs485_bytes[32];

// Function prototypes
void process_rs485_data(uint8_t *DeviceRxBuff, int len);

// Function to initialize UART
void init_uart() {
    uart_config_t uart_config = {
        .baud_rate = BAUD_RATE,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
    };

    // Install UART driver with TX and RX buffer
    uart_driver_install(UART_NUM, 256, 256, 0, NULL, 0);
    uart_param_config(UART_NUM, &uart_config);
    uart_set_pin(UART_NUM, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);

    gpio_set_direction(RS485_ENABLE_PIN, GPIO_MODE_OUTPUT);

    // Set default state for RS485 (RX mode)
    gpio_set_level(RS485_ENABLE_PIN, 1);

}



void get_data_from_device(void *pvParameters) {
    while (1) {
        int len = uart_read_bytes(UART_NUM, rxBuff_from_device, sizeof(rxBuff_from_device), 1000 / portTICK_PERIOD_MS);

        if (len > 0) {
             printf("Response : ");
             for (int k = 0; k < len; k++) {
                 printf("%02X ", rxBuff_from_device[k]);
             }
             printf("\n");
            memset(rxBuff_from_device, 0, sizeof(rxBuff_from_device));
        }
    }
}


// Function to send RS485 data to devices
void send_rs485_to_device(uint8_t *data, size_t length) {
    gpio_set_level(RS485_ENABLE_PIN, 1);  // Enable TX mode
   vTaskDelay(10 / portTICK_PERIOD_MS);  // Ensure GPIO switch before sending

    uart_write_bytes(UART_NUM, (const char *)data, length);

    vTaskDelay(30 / portTICK_PERIOD_MS);  // Allow transmission to complete
    gpio_set_level(RS485_ENABLE_PIN, 0);  // Switch back to RX mode

     vTaskDelay(50 / portTICK_PERIOD_MS);  // Allow some time before reading response
}

// Task to send RS485 data every 2 minutes and check response
void periodic_send_task(void *pvParameters) {
    uint8_t buftx[] =  {0x84, 0xBA, 0x20, 0xFF, 0xFE, 0x92, 0x5E, 0x5F, 0x05, 0x01, 0x01, 0x3D, 0x23};  // Example RS485 command
    printf("Sending data...\n");
    send_rs485_to_device(buftx, sizeof(buftx));
    vTaskDelete(NULL);
}

void app_main(void)
{
    // Initialization for RS485
    init_uart();
    xTaskCreate(get_data_from_device, "get_data_from_device", 4096, NULL, 7, NULL);
    xTaskCreate(periodic_send_task, "periodic_send_task", 4096, NULL, 4, NULL);
}
This is the esp32s3 idf source code I'm running.
Initially data send to another device(MAX-2, which contains MAX chip) onetime successfully. And then I switched to Receiver mode by enabling RE/DE to low.
When I try to send data from another device to esp chip, why data not received at esp side ?

I checked response at esp RX gpio by connecting TTL-USB Rx pin and response printing in realterm. Only esp side issue is there.

Connections

ESP MAX-1
VCC VCC
GND GND
TX(GPIO-4) DI
RX(GPIO-5) RO
DE_RE(GPIO-2) DE_RE

MAX-1 MAX-2
VCC VCC
GND GND
A A
B B

Re: Unable to Receive RS485 MAX data after switching to Receive mode of esp32c3

Posted: Fri Apr 04, 2025 8:37 am
by egionet
Based on this reference, ESP32-C3 usable pins, GPIO 2 is strapping and shouldn't be used.

https://pcbartists.com/design/embedded/ ... EL23IZA5RS

Re: Unable to Receive RS485 MAX data after switching to Receive mode of esp32c3

Posted: Fri Apr 04, 2025 8:58 am
by devhari
I apologize for the mistake

Board is esp32-s3-wroom-2 not esp32-c3

Re: Unable to Receive RS485 MAX data after switching to Receive mode of esp32s3

Posted: Fri Apr 04, 2025 5:22 pm
by egionet
No worries! I ran into the same problem integrating an Analog Devices LTC2873 with an ESP32-S3.

Here is a detailed RS-485 loopback example utilizing two UARTs with TTL to RS-485 converter boards: https://github.com/K0I05/ESP32-S3_UART2X.