0

I have an Arduino UNO connected to ESP32 through voltage divider with TX0 from Uno to RX2 to ESP32. My goal is to send data from my Uno to my ESP32 through HardwareSerial. Here's my code for the Uno:

void setup() {
Serial.begin(9600);
}

void loop() {
int t = 100;
String myString = String(t);
Serial.println(t);
delay(5000);
}

Here I assign an int to 100 and then cast it to String so I can send it on my ESP32's side. My code for ESP32 is:

#define RXD2 16
#define TXD2 -1

HardwareSerial Serial2(1); // Use UART channel 1

void setup() {
Serial.begin(9600);
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
}

void loop() {
if(Serial2.available() >= 1){
Serial.print("Data received:");
Serial.print(Serial2.readString());
}
}

The serial monitor on ESP's side is printing only "100" on every 5 seconds but if I disconnect the Uno from my Port, the ESP's monitor keeps printing "100" even tho it shouldn't. However, IF I type Serial.println("100"); then I get "Data received: 100" on ESP's side and if I unplug the Uno, the ESP's monitor doesn't print anything or with other words it works properly. Is there a way I can fix this?

  • The whole String thing is irrelevant. You're not using it. You're printing the integer. Delete the string rubbish. – Majenko Aug 25 at 12:54
  • you print to Serial. Serial prints to Serial Monitor. on what port is your Serial Monitor listening? – Juraj Aug 25 at 13:00
  • I'm well aware of that but my goal here is to "pass" a variable to the Serial communication. With the String thing, the ESP's side doesn't act properly and that's what I'm trying to fix. – anthino12 Aug 25 at 13:00
  • Uno's port is COM3 and ESP's port is COM4 @Juraj – anthino12 Aug 25 at 13:01
  • the output you see depends on which COM port is in Serial Monitor, not on code changes. the port changes as you change it for upload – Juraj Aug 25 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.