Error in compiling esp32 in arduino ide

Hello,
I am getting this error while compiling code for esp32.
Error:
Arduino: 1.8.15 (Windows Store 1.8.49.0) (Windows 10), Board: "ESP32 Dev Module, Disabled, Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS), 240MHz (WiFi/BT), QIO, 80MHz, 4MB (32Mb), 115200, None"

C:\Users\bilal\AppData\Local\Temp\arduino_cache_87525\core\core_6929dad97967b40f7e760b2df9456f93.a(HardwareSerial.cpp.o):(.bss.Serial2+0x0): multiple definition of `Serial2'

sketch\sketch_may14a.ino.cpp.o:(.bss.Serial2+0x0): first defined here

collect2.exe: error: ld returned 1 exit status

exit status 1

Error compiling for board ESP32 Dev Module.

ESP32 Code:
#include <HardwareSerial.h>
#define RXD2 16
#define TXD2 17
HardwareSerial Serial2(2);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);

}

void loop() {
Serial.print("Data Received: ");
Serial.println(Serial2.read());
delay(200);
// put your main code here, to run repeatedly:

}
I hope someone can help me in this. Thank you.

This means Serial2 has already been defined somewhere else.

The definition is in the header file you included and the cpp file of the same name.

Any solution to this issue?
Would be glad if some help could be provided.
Thanks

Remove the following line from your code. It is already in the cpp file. Have a look.

HardwareSerial Serial2(2);



I got this output on the serial monitor. I don't know what is wrong with the code but I want to transfer the value "50" from Arduino to ESP32 so that the value should be shown on the serial monitor of ESP32.
Any help in that?
Thank you.

First image is of Arduino Uno and second one is of ESP32.

Have a look at ASCII codes. You are printing the characters 5 and 0.

Please use code tags to post code. Reading codes on images in no fun and does not allow to copy code.

Also have a look at serial examples. You should check whether there are any available characters first.

As a general tip I recommend you avoid combining function calls for now. Think about the data types and store all values in local variables first and then use them. This will help you understand better what is going on and tells other more about what you think happens vs what actually happens in your code. e.g.

Serial.println( Serial2.read() ); // make this two lines of code

Everything works exactly as you have programmed it.
Though if this is something different than you expected you don't yet understand your own program.

You are printing the bytes received on the software-serial-interface to the serial-monitor
your

Serial.print(Serial2.read());

53 is the ASCII-Code of character "5"
and
48 is the ASCII-Code of character "0"

I think if you assign the received character to a variable of type char and then do a serial.print with that char it should do what you want.

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

so something like

char myRcvChar;

and then

myRcvChar = Serial2.read();
Serial.print(myRcvChar);

will do the job.

Work through this tutorial

best regards Stefan

Thank you for the reply.
Can you also tell me that if it is possible to have bi-directional communication between Arduino and ESP32? If it is, then what code/documentation I should see for an example?
Also "myRcvChar = Serial2.read();
Serial.print(myRcvChar);"
Should I write these lines for ESP32 or Arduino?
Any help in that would be helpful.

Hi Bilal,

you can answer this questions yourself if you look into both codes to find which of the two codes uses serial2. ?
and which one does not.

Which of you two codes shows
53
48
??

programming is very logical and you have some logical thinking inside of you. Just use it

best regards Stefan

Thank you Stefan for your help. I was able to achieve what I wanted to.
I just wanted a bit more help from you in, how can i send multiple sensor data from Arduino Uno to ESP32 via serial communication.
I have already tried sending only one value like 100 or a string like "Hello" but I want to send more than one sensor value to esp32.
Basically, I will try to send EEG and EMG data from Arduino to Esp32.
I hope you can guide me in this regards.
Thanks

There is a library called SafeString,
which has functions to make life easier about send/receive strings
and to extract parts of a string.

The variabletype String (with capital "S") is dangerous to use with the Arduino if the strings are defined global.

I guess you do not yet understand what defining as "global" means.
Well that's one of the basic things you have to learn.

The name of SafeStrings is program. They are safe to use.
What I'm gonna do for you is "teach fishing" WHich means I can show you some programming-techniques that will make you yourself able to analyse in details what a program does.

ANd based on that you can modify your code to see what happens through the modification.

There are various learning-types like learning by reading or learning by watching (videos)

here is a basic course that teaches important basic things you need to know for every program including sending single or multiple values

Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

If you are not the reading-texttype of learner just say so.

best regards Stefan