This is the code I uploaded in Arduino UNO:

SoftwareSerial ESP(2,3); // RX, TX
//ESP RX-->3(UNO) TX-->2(UNO)

void setup() {
// Open serial communications and wait for port to open:

    while (!Serial) {
        ; // wait for serial port to connect. Needed for native USB port only

    Serial.println("Goodnight moon!");

    // set the data rate for the SoftwareSerial port


void loop() {
    if (ESP.available()) {
        char inData = ESP.read();
        Serial.println("Got reponse from ESP8266: ");

    ESP.write("AT\r\n"); //Normally ESP responds to AT command with "OK"

But ESP is not able to receive the AT command properly...sometimes it recieves A, sometime T. It then responds with garbage value. Though we are able to give command to ESP through Serial Monitor and it responds pretty well. But when trying to communicate through code, the communication is not OK.

  • 2
    This site is about electrical engineering. Extra-sensory perception is off topic here.
    – Olin Lathrop
    Jun 2, 2016 at 11:05
  • 4
    Why is this question downvoted and voted to close? MCVE code is provided, sympthoms of failure are described. What is the problem? Jun 2, 2016 at 12:00
  • Are you aware that you can program the ESP8266 directly from the Arduino IDE using proper Arduino C rather than AT commands. This means that it might be possible to eliminate the need for a 32MHz Arduino and just rely on the 160MHz ESP82366 instead. Also you need to have a look at the SO Arduino forum. Jun 2, 2016 at 12:04
  • 1
    @Matt yeah it can be done...but am using arduino in my project to control servos and all, so I am relying on arduino for the processing work. :)
    – abhimanyu bishnoi
    Jun 2, 2016 at 12:27
  • 1
    @Matt do you mean WeMos D1? There is no Webbo D1.
    – gre_gor
    Jun 2, 2016 at 15:42

1 Answer 1


The problem is in your loop() function. On each iteration, you will receive at most one character from the SoftwareSerial, while writing the same AT command again and again. As a result, your slave device (ie. ESP8266) will overflow its UART buffers, resulting in garbage being sent and received.

You should keep receiving data from the slave for as long as it has data:

while (ESP.available()) {
    char inData = ESP.read();
    Serial.println("Got response from ESP8266: ");
  • Thanx it works fine now! :)
    – abhimanyu bishnoi
    Jun 2, 2016 at 11:41
  • @abhimanyubishnoi Note that you can click o that "check" mark to accept my answer if it really solved your problem. That way, other people will see your problem was solved, and move on to answer unsolved questions. Jun 2, 2016 at 11:50
  • Dmitry Grigoryev - i have tried u r solution, but still i am not able to get response 'OK' from ESP 01(my device)....here is the screen shot with response i.stack.imgur.com/mOklg.png....
    – 3bu1
    Jun 20, 2017 at 12:54
  • @3bu1 Judging by your screenshot, you still have failed to replace if with while. Jun 20, 2017 at 13:13
  • still the same, here is the screen shot(i.stack.imgur.com/3nAC2.png) and i have posted question on this url(arduino.stackexchange.com/questions/39729/…), kindly help me with this...thank u in advance
    – 3bu1
    Jun 20, 2017 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.