Based on this answer, it says that Arduino and ESP32 cannot do 2-way communication at the same time cause it will do collision as software serial cannot handle simultaneous 2 way communication. Even if i used level shifter to convert 3,3v esp to 5v arduino, is it i still cannot do 2 way comms at the same time?
Note: My project is create Automatic AC based on Desired Temperature with IR transmitter. I used Arduino MEGA 2560 to do the proccessing and send the ir data (its just because esp32 cannot do ir transmit). Then Arduino MEGA 2560 will send some data to be uploaded by ESP32 to Blynk or ThingSpeak. Below is my hardware sequence:
Very simple: manufacturer's device specifications. You MAY NOT connect a 3.3V input to a 5V source, or damage to one or both devices can be expected. The damage may not be enough to notice in the short term, but it can also be instantly fatal.
Do i really need point no.2? or i can do it simultaneously by just using point no.1? Can you help me show the right way to code simultaneous two way comms?
Why do you need "simultaneous" exchange and what do you mean on it?
I recommended you to read a "Serial input basics" tutorial before you going further.
note the potential divider to convert the Mega Tx 5V logic signal to a suitable level for the ESP32 3.3V logic - there no problem with the ESP32 3.3V Tx signal to the Mega RX
Yes, If you're using a Mega and an ESP32, then both these MCU's have additional hardware ( full-duplex) serial UART ports. if available, HW ports should always be used over SW ports.
Therefore, you don't need to worry about my previous answer, as this involved the OP using a software serial ports (half-duplex) on the UNO.
In your case, you can communicate both ways simultaneously, without needing to program collision avoidance, as you have 2 full-duplex ports.
So, thats the pinout. should i use the gpio 16 and 15 as you said? or i should just use the 16 and 17 (rx2 ang tx2)? Cause i find different on your example board
I have used both 15 and 16 and 16 and 17 in various tests
this works using Serial2 (pins 16 and 17)
// 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()));
}
}