ESP8266 and Arduino Interfacing

3.4k views Asked by At

I have connected Arduino with ESP8266 with

Arduino pin 2 connected to ESP's Tx Arduino pin 3 connected to ESP's Rx via Voltage Divider Arduino GND connected to ESP's GND Arduino 3v3 connected to ESP's CH_PD

I have powered ESP8266 using 1117 Voltage regulator

When I initially bought ESp8266 it working but now it shows an endless stream of garbage values...

The arduino is programmed with the following code

#include <SoftwareSerial.h>

SoftwareSerial esp8266(2,3); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.
                             // This means that you need to connect the TX line from the esp to the Arduino's pin 2
                             // and the RX line from the esp to the Arduino's pin 3
void setup()
{
  Serial.begin(9600);
  esp8266.begin(9600); // your esp's baud rate might be different
}

void loop()
{
  if(esp8266.available()) // check if the esp is sending a message 
  {
    while(esp8266.available())
    {
      // The esp has data so display its output to the serial window 
      char c = esp8266.read(); // read the next character.
      Serial.write(c);
    }  
  }



  if(Serial.available())
  {
    // the following delay is required because otherwise the arduino will read the first letter of the command but not the rest
    // In other words without the delay if you use AT+RST, for example, the Arduino will read the letter A send it, then read the rest and send it
    // but we want to send everything at the same time.
    delay(1000); 

    String command="";

    while(Serial.available()) // read the command character by character
    {
        // read one character
      command+=(char)Serial.read();
    }
    esp8266.println(command); // send the read character to the esp8266
  }
}
7

There are 7 answers

0
LucasRT On

As a compliment of the other answers, try replacing the voltage divider with a logical level converter, because esp has 3.3v logic, and arduino 5v logic.

0
Venkat Sai On

Check out logic value as esp8266 works on 3.3v and serial port baud rate. In few cases ESP8266 can have internal faults and produce garbage values. As far ESP8266 is concerned, checkout here It helped me a lot

1
Amal Anjula On

remove voltage divider and series 330ohm between Arduino TX to NodeMCU Rx pin Thanks

0
John Carlo Catilo On

when establishing bidirectional communication between esp8266 (which has tx and rx pins only 3.3V tolerant, not 5v) and arduino atmegaXXXX based chips(which gives and accepts 5v), take note of the following:

  1. make sure baud rates are similar and neccesary voltage adjustments are made. this includes the a use of a voltage divider circuit or some other thing you can find on online blogs.
  2. when programming each device, make sure to disconnect the rx and tx connections of the two devices
  3. remove unneccesarry Serial.printX()s since these are going to affect the tx rx comms

I suggest study softwareserial library so you can use the GPIO pins as your tx rx pins for other lines of communication. this will have kinda messy coding but is good for testing the logic of your code.

example: use pin 2 and pin 3 of arduino and pin 2 and pin 3 of esp8266 for arduino to esp8266 communication then... use pin 4 and pin 5 of arduino and pin 4 and pin 5 of esp8266 for esp8266 to arduino communication

0
Victor Quintana On

The code seems to be ok, but you should checkout your ESP8266 baud rate maybe different. Checkout the following:

  1. Check the ESP8266 baud rate alone, once you have it, declare the same baud rate into your Arduino.

  2. Checkout your Arduino model, some clones like nano one drive different voltages than the original one,

0
Visual Micro On

Your esp8266 is probably working at 56000 or 115200 baud rate and not 9600. This would cause garbage to be read.

If 115200 it won't work on normal digital pins with softwareSerial.

If an older board, then you can try changing to 56000:-

 esp8266.begin(56000); // your esp's baud rate might be different

Otherwise you will need to hook the esp8266 up to the HardwareSerial port

 Serial.begin(115200);
0
vallabh On

Upload the code and check the serial monitor for any response with a specific baud rate,if you didn't get any response on a specific baud rate then change the baud rate till you get the response. For few modules the default baud rate would be 57600.So check according to it.

You can use the given code above and change the baud rate of esp8266.begin(56000); change the baud rate like 9600,56000,112500 etc and check the serial monitor at 9600 baud rate.

Serial.begin(9600);

you will get the response on the monitor,and also try to reset the wifi module by connecting 3.3v to RST pin for 1-2 seconds. Hope it helps.