ESP32 as serial reader using MAX3232

Good afternoon,

I want to use an ESP32 board as a serial reader for a scale, using the UART2 port with pins 16 and 17 as RX and TX.

The code I am using is the following:

#include <HardwareSerial.h>

// Pines de RX y TX en el ESP32
const byte txPin = 17;  // TX2
const byte rxPin = 16;  // RX2

void setup() {
  Serial.begin(1200);

  // Configuración de los pines para Serial2
  Serial2.begin(1200, SERIAL_8N1, rxPin, txPin);

  while (!Serial2) {
    Serial.println("Esperando inicialización del lector");
    delay(1000);
  }
  Serial.println("Lector inicializado");
}

void loop() {
  Serial.println("Esperando a la balanza:");

  for (int loops = 0; loops < 120; ++loops) {
    if (Serial2.available()) {
      Serial.println("Datos recibidos desde la balanza");
      break;
    }
    delay(1000);
  }

  char result[100] = "-1, -1";
  if (Serial2.available()) {
    Serial.println("Leyendo...");
    int i = 0;
    while (Serial2.available()) {
      result[i++] = Serial2.read();
      Serial.print((char)result[i - 1]);  
    }
    result[i] = '\n'; 
  } else {
    Serial.println("Tiempo de espera agotado");
  }

  Serial.println(result);
}

The code was made based on the following tutorial for an ESP8266 board:

The code works using SoftwareSerial and an ESP8266 board but when I wanted to use an ESP32 board and HardwareSerial I couldn't get any data to arrive. The program compiles and does not throw errors, I simply touch the print button on the scale and it does nothing.

I have already tried changing Tx and Rx, I have my doubts about the voltage used by the scale's RS232 communication since it does not specify the value in the manual. As I understand it, the ESP32 board pins work up to 3.3V while the ESP8266 supports 5V so it occurred to me that the error may come from there.

Thank you very much in advance!

I don't see any reason your code would not work for esp32 if it's working on esp8266.
They are both 3.3V boards, question if abusing one with 5V is giving magic smoke before another is probably not relevant here.
What exact esp32 board you have? Are you powering Max the same way with both boards?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.