Been trying to do UART communication for learning purposes. For the program written below, i get no outputs while giving inputs. what seems to be wrong?
#include <HardwareSerial.h>
String user_input = "What is your name";
HardwareSerial SerialPort(2);
void setup() {
Serial.begin(115200);// put your setup code here, to run once:
SerialPort.begin(9600,SERIAL_8N1, 16, 17);
}
void loop() {
if (Serial.available()) {
user_input = Serial2.readString();
}
Serial.println(Serial2.readString());
if (user_input = "What is your name"){
Serial.println(Serial.readString());
}
}
#include <HardwareSerial.h>
String user_input = "Hi";
HardwareSerial SerialPort(2);
void setup() {
Serial.begin(9600);
SerialPort.begin(115200,SERIAL_8N1, 16, 17 );// put your setup code here, to run once:
}
void loop() {
Serial2.println("hello");
//delay(1000);
if (Serial.available()) {
user_input = Serial.readString();
}
//user_input = Serial2.readString();
if (user_input ="Hi"){
Serial2.println("Hello");
} //Serial.println(Serial2.readString());
}
You defined inter-board port as SerialPort, but call it in the code as Serial2.
Change one or another to use the same name in object name and in the code
// ESP32 serial2 hardware loop back test - jumper GPIO16 (Rx) and GPIO17 (Tx)
// see https://circuits4you.com/2018/12/31/esp32-hardware-serial2-example/
/* There are three serial ports on the ESP known as U0UXD, U1UXD and U2UXD.
*
* U0UXD is used to communicate with the ESP32 for programming and during reset/boot.
* U1UXD is unused and can be used for your projects. Some boards use this port for SPI Flash access though
* U2UXD is unused and can be used for your projects.
*/
#define RXD2 16
#define TXD2 17
void setup() {
// Note the format for setting a serial port is as follows: Serial2.begin(baud-rate, protocol, RX pin, TX pin);
Serial.begin(115200);
Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2);
Serial.println("ESP32 hardware serial test on Serial2");
Serial.println("Serial2 Txd is on pin: "+String(TXD2));
Serial.println("Serial2 Rxd is on pin: "+String(RXD2));
}
void loop() { //Choose Serial1 or Serial2 as required
while (Serial2.available()) {
Serial.print(char(Serial2.read()));
}
while (Serial.available()) {
Serial2.print(char(Serial.read()));
}
}
jumpering pins 16 and 17 gives a loopback test - characters entered on serial monitor are echoed back