Trouble With Arduino to Arduino Communication Via RX/TX

i am testing out communication between an arduino nano and arduino nano esp32 and am having issues. I've tested both independently and they work. Example the nano sends on/off commands to serial and the esp32 turns the led on or off if i type "ON" or "OFF" in serial. However when connected the two and powering at the same time the led doesn't turn on when the button is selected. They are both connected correctly (Arduino RX to esp32 TX and Arduino TX to the esp32 RX & share a ground). Is there something im missing on how to accept serial commands different if not sent via the IDE? Here is the scripts for reference:

//nano

const int buttonPin = 13; // Button connected to pin 13
int buttonState = 0;      // Variable to store button state
int lastButtonState = LOW;  // For edge detection, assuming LOW as the unpressed state
bool toggleState = false;   // Toggle state for switching between ON and OFF

void setup() {
  pinMode(buttonPin, INPUT_PULLUP); // Initialize the button pin as an input with an internal pull-up resistor
  Serial.begin(9600);               // Start serial communication at 9600 bps
}

void loop() {
  buttonState = digitalRead(buttonPin); // Read the current state of the button

  // Check for button press with edge detection (transition from LOW to HIGH)
  if (buttonState == HIGH && lastButtonState == LOW) {
    // Delay for debouncing
    delay(200);

    // Ensure that the button is still pressed after debounce delay
    if(digitalRead(buttonPin) == HIGH) {
      if (toggleState) {
        // If toggle state is true, print "OFF"
        Serial.println("OFF");
      } else {
        // If toggle state is false, print "ON"
        Serial.println("ON");
      }
      toggleState = !toggleState; // Toggle the state
    }
  }
  lastButtonState = buttonState; // Store the last button state
}

//nano esp32

void setup() {
  Serial.begin(9600);        // Initialize serial communication at 9600 bps
  pinMode(13, OUTPUT);       // Set the digital pin as output
}

void loop() {
  // Check if data is available to read from the serial port
  if (Serial.available() > 0) {
    // Read the incoming byte
    String command = Serial.readStringUntil('\n'); // Adjusted to read string until newline character

    // Trim any whitespace or new line characters
    command.trim();
    
    // Check if the command is "ON"
    if (command == "ON") {
      digitalWrite(13, HIGH); // Turn the LED on
    }
    // Check if the command is "OFF"
    else if (command == "OFF") {
      digitalWrite(13, LOW); // Turn the LED off
    }
  }
}

Aren't these 5V and 3.3V controllers? Different signal levels can cause problems.

The nano is 5v and the esp32 is 3.3v so different indeed. I've checked but there really isn't much online I've found regarding signal levels from rx/tx being different due to the different voltage. That being said because i haven't seen anything does not mean its true. You have any reference to point towards?

Hi @boonyboy ,

search the internet for "level shifter" ...

And/or feel free to have a look here:

https://randomnerdtutorials.com/how-to-level-shift-5v-to-3-3v/

BTW: How are you powering both boards? Are both connected to a PC via USB?

I was powering both via computer through usb but realized that both were communicating to serial on the IDE. I read in a couple places that the IDE serial would take priority vs sending via rx/tx so stopped using that and have tried via wall outlet. Still not dice on that either.

As for level shifter, I'm assuming the rx/tx from the Nano (5v) is needed to level shift down to 3.3v for the esp32 to recognize the rx/tx response? Just buy a level shifter and attempt the bridge via that?

IT is mostly the TX 5V to RX 3.3V that might damage the input pin ... That can be handled by a voltage divider (see the link I posted).

I would recommend to use a level shifter and a SoftwareSerial connection so that you can leave the IDE running and check what's going on via standard Serial ...

See here https://docs.arduino.cc/learn/built-in-libraries/software-serial/

SoftwareSerial should do it for the requirements your application has.

haha! I've tried software serial thinking the rx/tx weren't working for some reason. That makes sense I'll swing by a pickup a level shifter at a radio shack nearby and see if that helps. thanks

Good luck! I'm off for today as it's already 1100 pm here in Europe ... :wink:

1 Like

i was able to pickup a BSS138. I've been testing with a multi-meter and a bit confused. When i run a 5v to HV and gnd to HV sides gnd. If i test the other LV and gnd with the multimeter im not really getting a reading.

On the opposite end if i run a 3.3v to LV and gnd to gnd. Im only getting a 3.3v on the multimeter? Does this require an actual device to pull up the 5v measurement? Or pull up to 3.3 on the opposite end?

That's a bidirectional level shifter for an I2C bus. For UART you can try to add pullup resistors to the receiver end.

maybe a silly goof on picking it up, but reading online i saw it works with TTL serial. Would this not also work?

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