ESP8266 - Serial Communication from Arduino is not printing??

Hi,
I'm a newbie to Arduino and would like your help!

I want to make serial communication work between Arduino UNO and ESP8266-01.

To be exact, I want to send an analog input value (from a potentiometer) from Arduino to ESP8266, and print that or write that on the Serial Monitor.

What I did:

  • As you can see below - I connected Arduino and ESP8266 via the digital pin 2 and 3 of the Arduino to allow serial communication.
  • I changed the baud rate of ESP8266 to 9600 so that part is ok.
  • I uploaded the ESP8266's code in Flash mode, connecting its GPIO0 and RESET to Ground, and the code uploads ok.
  • Then I unplugged GPIO0 and RESET from Ground, making sure it's not in flash mode. As soon as I do this the blue led on the ESP8266 starts to flash every 1000 msec (I'm guessing this means the serial communication itself is successful, and ESP8266 is receiving the values??).
  • I wired my potentiometer to analog pin 0 of Arduino, and I've checked the pot is working ok.
  • I tried both board mode - Arduino UNO and the Generic ESP8266, but that does nothing.

I want the ESP8299 to print out values between 0 and 255 (from the pot) on the Serial Monitor every 1000 msec, but it prints out nothing. Why? How can I fix this?

Arduino UNO (sender)'s code:

#include <SoftwareSerial.h>
SoftwareSerial ESPserial(2,3); // RX | TX
 
void setup() {
  //Serial Begin at 9600 Baud 
  Serial.begin(9600); //This must always be 9600
  ESPserial.begin(9600);  //This must match the baud rate of the Serial.begin for ESP8266

}

void loop() {
  int sensorValue = analogRead(A0); //Getting values from potentiometer
  // print out the value you read:
  sensorValue = map(sensorValue, 0, 800, 0, 255); 
  ESPserial.write(sensorValue); 

  delay(1000);        // delay in between reads for stability
  
}

ESP8266 (receiver)'s code:

byte data; //Initialised variable to store received data

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

void loop() {

  if(Serial.available()){  //When there is data from Arduino available...
    data = Serial.read(); //Read the serial data and store it
    Serial.println(data); //Prints data value - THIS IS NOT WORKING

  }
  delay(1000);
}

Here is how I set it up:

"I changed the baud rate of ESP8266 to 9600 so that part is ok." why? you deleted the AT firmware after then. now your sketch sets the baud rate.

You connected esp8266 to Uno, not to Serial Monitor. What you print to Serial is send to Uno. Uno doesnt't read Serial

If you 'read' a byte then 'write' a byte, not 'print' it.

Juraj:
"I changed the baud rate of ESP8266 to 9600 so that part is ok." why? you deleted the AT firmware after then. now your sketch sets the baud rate.

Thank you for your reply Juraj!
Oh my gosh, I was not supposed to do that...? I used "AT+UART_DEF=9600,8,1,0,0" command, following this tutorial for beginners: https://www.hackster.io/user16726/configuring-the-esp8266-using-an-arduino-0ab2e6
But why shouldn't I have changed the baud rate? (sorry it might not directly relate to my question but it would be great if you can teach me)

Juraj:
You connected esp8266 to Uno, not to Serial Monitor. What you print to Serial is send to Uno. Uno doesnt't read Serial

If you 'read' a byte then 'write' a byte, not 'print' it.

Ahh okay - but I'm confused by what you mean by "Uno doesnt't read Serial"?
And, do you mean - printing anything from the ESP8622 is impossible in this setup? (since I was thinking of printing out strings in between the pot values later, like so:
Serial.println(potValue + "," potValue2 + "," potValue3);

  • I'm worried this is not possible?)

cloudurchin:
Oh my gosh, I was not supposed to do that...? I used "AT+UART_DEF=9600,8,1,0,0" command, following this tutorial for beginners: https://www.hackster.io/user16726/configuring-the-esp8266-using-an-arduino-0ab2e6
But why shouldn't I have changed the baud rate? (sorry it might not directly relate to my question but it would be great if you can teach me)

it was for nothing. it is useful if you want to use the AT firmware. it is obsolete if you overwrite AT firmware with your sketch.

cloudurchin:
Ahh okay - but I'm confused by what do you mean by "Uno doesnt't read Serial"?
And, do you mean - printing anything from the ESP8622 is impossible in this setup? (since I was thinking of printing out strings in between the pot values later, like so:
Serial.println(potValue + "," potValue2 + "," potValue3);

  • I'm worried this is not possible?)

add to loop() in Uno:

while (ESPserial.available()) {
 Serial.write(ESPserial.read());
}

Juraj:
it was for nothing. it is useful if you want to use the AT firmware. it is obsolete if you overwrite AT firmware with your sketch.

Juraj,
Ah I see.
I also got what you meant by I connected esp8266 to Uno just now - stupid me!
ok so - I changed the code for the Uno, as well as esp8266:

byte data; //Initialized variable to store received data

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

void loop() {

  if(Serial.available()){  //When there is data from Arduino available...
    data = Serial.read(); //Read the serial data and store it
    Serial.write(data); 
    //^Changed. This writes data to Arduino - since Serial of ESP8266
    //is Arduino's RX/TX.

  }
  delay(100);

So I was supposed to change Serial.println(data); to Serial.write(data); . Am I correct?

Juraj:
add to loop() in Uno:

while (ESPserial.available()) {

Serial.write(ESPserial.read());
}

This is great! I was also trying out something like

if(ESPserial.available()){  //When there is data from ESP8266 available...
    int x = ESPserial.read();
    Serial.print(x); //Print the value gotten from ESP8266
    Serial.print("\n");
  }

But I guess it's the same thing - your loop is very neat!
One question - why did you suggest using Serial.write instead of Serial.print (for this loop in Uno)? Or it doesn't really matter which one I use?

again, if 'read' then 'write', not 'print'

Ah okay, thank you so much for your help :slight_smile: