Make the ESP8266 talk 9600 baud

Surprise videoThe default serial speed on the ESP8266 WiFi chip is 115200 baud. If you connect this chip to an Arduino and want to use SoftSerial to talk to it, it will not work. The SoftSerial library is limited to 9600 baud because of hardware limitations of the Arduino. If you want to talk to the ESP8266 with an Arduino at lower speeds, you need to tell the ESP8266 to slow down.

Remco.org noted that sending 115200 baud with SoftSerial works just well enough to throw a command to the ESP8266. So below is a sketch that will do just that for you.

  1. Load or copy/paste the sketch in your Arduino IDE.
  2. Connect the ESP8266 board to the Arduino as follows:
    – ESP TX pin to Arduino softRX pin 4
    – ESP RX pin to Arduino softTx pin 5
  3. Open the Serial Monitor of the Arduino IDE and set it to 9600 bps and CR/LF.
  4. Compile and upload this sketch to the  Arduino.
  5. Wait for the Serial monitor to show:
    AT
    OK
    — Sketch stopped.

You can now use your ESP8266 at 9600 baud. The setting is stored in non-volatile memory, so you only need to do this once.

Here’s the sketch:

/*
 * This sketch switches the ESP8266 from its
 * factory-default setting of 115200 bps to
 * 9600 bps so that we can use SoftSerial to
 * communicate with it.
 * 
 * 1. Connect the ESP8266:
 *    - ESP TX pin to Arduino softRX pin 4
 *    - ESP RX pin to Arduino softTx pin 5
 * 2. Open the Serial Monitor of the 
 *    Arduino IDE and set it to 9600 bps and
 *    CR/LF.
 * 3. Compile and upload this sketch to the   
 *    Arduino.
 * 4. Sit back and watch the magic.
 * 5. If you see "AT OK", it worked.
 * 
 * Settings are stored in Flash memory, you
 * only need to execute this sketch once.
 * 
 * Please note: Although softserial is used
 * to transmit at 115200 bps, you'll notice
 * we never wait for an answer. That is
 * because SoftSerial is not built to receive
 * at speeds above 9600 bps.
 * You have been warned.
 */
#include <SoftwareSerial.h>

const long sourcebaudrate = 115200;
const long targetbaudrate = 9600;

const int softRX = 4;
const int softTX = 5;

SoftwareSerial softSerial(softRX, softTX);

void setup() {
  Serial.begin(targetbaudrate);

  Serial.println("Tell the ESP8266 to switch from "
                  + String(sourcebaudrate) + " to " 
                  + String(targetbaudrate) + " bps.");

  softSerial.begin(sourcebaudrate);
  softSerial.print("AT+IPR=" 
                  + String(targetbaudrate)
                  + "\r\n");

  delay(500);

  Serial.println("Talk to the ESP8266 at "
                  + String(targetbaudrate) + " bps.");
  softSerial.begin(targetbaudrate);
  softSerial.print("AT\r\n");

  delay(500);

  Serial.println("Reply from the ESP8266:");
  while(softSerial.available() > 0 ) {
    char a = softSerial.read();
    if(a == '\0') continue;
    if(a != '\r' && a != '\n' && (a < 32)) continue;
    Serial.print(a);
  }

  Serial.println("-- Sketch stopped.");
}

void loop() {
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s