RS232 data via MAX3232 [SOLVED]

I am using 2 Arduino Nano ESP32's with 2 MAX3232 IDR modules, along with a gender changer. I have connected the circuit like this:

Receiver Circuit:

MAX3232:

GND - GND
VCC - VCC
Tx - D8
Rx - D9

Transmitter Circuit:

MAX3232:

GND - GND
VCC - VCC
Tx - D8
Rx - D9

Code for receiver circuit:

#include <HardwareSerial.h>

HardwareSerial MySerial(1);  // Use UART1

String receivedData = "";  // Declare receivedData as a global variable

void setup() {
    Serial.begin(9600);  // Monitor
    MySerial.begin(9600, SERIAL_8N1, 17, 18);  // TX=17, RX=18
}

void loop() {
    if (MySerial.available() > 0) {  // Check if data is available
        char incomingChar = MySerial.read();  // Read character
        receivedData += incomingChar;  // Append to string
        Serial.print("Data received: ");
        Serial.println(incomingChar);  // Print the incoming character
    }

    if (receivedData.length() > 0) {
        Serial.println("Received: " + receivedData);  // Print full message
    }
    delay(5);  // Small delay to allow buffer filling
}

Code for Transmitter Circuit:

#include <HardwareSerial.h>

HardwareSerial MySerial(1);  // Use UART1

void setup() {
    Serial.begin(115200);  // Monitor
    MySerial.begin(9600, SERIAL_8N1, 17, 18);  // TX=17, RX=18
}

void loop() {
    MySerial.println("H");  // Send "Hello" via RS232
}

I have a DB9 Male to female cable with a male-male gender changer to connect the two MAX3232 IDR modules. D8 and D9 (GPIO 17 and 18) are UART 1 pins on the Arduino Nano ESP32, so I thought I could use these pins for communication. I'm sure I can use the MAX3232 with 3.3v as it can operate between 3v and 5v. I am not receiving any data from this. Can someone help me with this?

Hi,
How have you then connected the two MAX ICs.
TX to Rx and Rx to Tx?

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

This is pretty essential, a picture can save a thousand posts.

Thanks.. Tom.... :smiley: :+1: :coffee: :australia:

Link please.

I think you should be using the Arduino pin numbers, as this is an Arduino IDE.

MySerial.begin(9600, SERIAL_8N1, 8, 9);  // TX=8, RX=9

Do your codes compile?

Tom.... :smiley: :+1: :coffee: :australia:

Yes they both compile and upload, and I have changed the IDE to use GPIO pin numbers instead, as I have had issues before in the past with using the arduino pin configuration

Right here

Hi,
This might help;

Tom.... :smiley: :+1: :coffee: :australia:

It says nothing about 3.3 V operation*, ask the supplier. Otherwise you may be troubleshooting something for nothing.

* The IC datasheet mentions 3-5.5 V, but it needs to be configured with the proper capacitors.

I am powering the receiver Nano ESP32 via USB through my PC so I can use the serial port. I am powering the other Nano ESP32 with a USB cable connected to a plug connecting to a socket.

I have written on the modules because it's a lot easier to remember the pins that way lol

What does the gender changer do Rx and Tx pin wise?

Do you have a DMM? Digital MultiMeter?

Tom.... :smiley: :+1: :coffee: :australia:

The datasheet is here.

I do have a digital multimeter

if you connect GPIO 17 to 18 to form a loopback does text entered on the keyboard echo back to the display?

this works on a ESP32-S3-DevKitC-1

// ESP32-S3 DevkitC-1  Serial1 test - for loopback test connect pin GPIO17 U1TXD to pin GPIO18 U1RXD"
// ss https://docs.espressif.com/projects/esp-idf/en/latest/esp32s3/_images/ESP32-S3_DevKitC-1_pinlayout_v1.1.jpg

#define RXD1 18
#define TXD1 17

void setup() {
  // initialize both serial ports:
  Serial.begin(115200);
  Serial1.begin(9600, SERIAL_8N1, RXD1, TXD1);
  Serial.println();
  Serial.println("\n\nESP32-S3 DevkitC-1 serial1  test pin GPIO17 U1TXD pin GPIO18 U1RXD");
  Serial.write("   for loopback test connect pin GPIO17 U1TXD to pin GPIO18 U1RXD\n");
}

void loop() {
  // read from port 1, send to port 0:
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.write(inByte);
  }

  // read from port 0, send to port 1:
  if (Serial.available()) {
    int inByte = Serial.read();
    //Serial.write(inByte);
    Serial1.write(inByte);
  }
}

Nothing prints to the serial monitor using this code

You have your RX/TX connections backwards. You have TX-TX and RX-RX
See notes 4 and 5

I was just having an issue with the serial monitor for some reason. When I enter text the text it does loopback, but none of the text above prints to the serial monitor (Output example):

11:15:28.980 -> dw
11:15:29.915 -> ge
11:18:26.081 -> ge
11:18:48.061 -> test

edit: I added a delay after the serial.begins and the text now prints and loops back

@chrisd79
See post #14

try connecting the RS232 module so

RS232: ESP32S3 pin 18 RXD1 to TTL/RS232 Rx and pin 17 TXD1 to TTL/RS232 Tx
RS232 - loopback connect 9-pin D-type pin 2 Tx to pin 3 Rx

I've wired it like this:

1 nano ESP32 and 2 MAX3232 IDR modules

MAX3232 1:

VCC VCC
GND GND
Tx ESP32 Rx
Rx ESP32 Tx

DB9 cable connecting MAX3232 1 to MAX3232 2:

MAX3232 2:

VCC VCC
GND GND
Tx & Rx connected to echo data

I've changed the BAUD rate and found that I receive data slightly more consistently at higher BAUD rates, which is weird. I haven't found a BAUD rate that has gave me consistent results.

EDIT:

For example, when I change the BAUD rate to 115200 on both serial connections, I can receive data kind of consistently:

13:34:58.395 -> hi
13:35:00.237 -> hello
13:35:04.098 -> �test
13:35:34.250 -> test 3

Also if I have the physical wiring connections closer together, it's giving me more accurate results. Does anyone know why this is happening?

how long is the DB9 cable? are the GNDs connected (pin 5 to pin 5)?
what is the environment?
have you an oscilloscope to look at the signals - how noisy are they?

The GNDs are connected. The DB9 cable is 0.5m and brand new. I do not have an oscilloscope.