ESP8266 Serial Communication

Serial interface is common requirement for most of the application development. We are discussing on how to do 3.3V to 5V level conversion for converting serial TTL to RS232 level from 3.3V you can use MAX3232 it operates at 3.3V levels.

On ESP8266 we have one hardware serial i.e. GPIO2 (Tx) and GPIO3 (Rx).

Hardware Serial Programming is similar to the Arduino Serial. Remember that few USB to Serial converter does not support higher baud rate. It is better to keep baud rate below 115200.

Serial interface is useful for debugging the programs by sending some debug info to serial.

Hardware Serial Communication

Level conversion 3.3V to 5V

For Level conversion from 3.3V to 5V we need only two components.

Logic Level Conversion
Logic Level Conversion

Assuming that you have already connected serial with your USB to Serial converter or You are using ESP Witty, Node MCU.

Serial communication on pins TX/RX uses TTL logic levels 3.3V. Don’t connect these pins directly to an RS232 serial port; they operate at +/- 12V and can damage your ESP8266 board.

Serial is used for communication between the Arduino board and a computer or other devices. All ESP boards have at least one serial port (also known as a UART or USART): Serial. It communicates on RX and TX.

 You can use the Arduino IDE environment’s built-in serial monitor to communicate with an ESP board. Click the Tools>>Serial monitor button in the toolbar and select the same baud rate used in the call to begin().

Before we start our program lets understand commonly used serial commands

Serial.begin(9600);

This command is used to initialize serial port with 9600 baud rate. It is used only when you initialize serial i.e. in Setup() or when you want to change the baud rate. Baud rate is number of bits transmitted per second. Higher the baud rate higher the speed of communication.

Use lower baud rate if cable length is more. Standard baud rates are 300, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 250000.

Serial.print(); and Serial.println();

Serial.print prints text on same line. Seial.println puts carriage return at the end of line.

Prints data to the serial port as human-readable ASCII text. This command can take many forms. Numbers are printed using an ASCII character for each digit. Floats are similarly printed as ASCII digits, defaulting to two decimal places. Bytes are sent as a single character. Characters and strings are sent as is. For example:

Serial.print(65) gives “65”

Serial.print(char(65)) gives “A”, 65 is ASCII code of A

 

Serial.print(1.23456) gives “1.23”

Serial.print(‘N’) gives “N”

Serial.print(“Hello world.”) gives “Hello world.”

An optional second parameter specifies the base (format) to use; permitted values are BIN (binary, or base 2), OCT (octal, or base 8), DEC (decimal, or base 10), HEX (hexadecimal, or base 16). For floating point numbers, this parameter specifies the number of decimal places to use. For example:

Serial.print(78, BIN) gives “1001110”

Serial.print(78, OCT) gives “116”

Serial.print(78, DEC) gives “78”

Serial.print(78, HEX) gives “4E”

Serial.println(1.23456, 0) gives “1”

Serial.println(1.23456, 2) gives “1.23”

Serial.println(1.23456, 4) gives “1.2346”

Serial.print(“\t”);              // prints a tab

Serial.print(“\n”);              // inserts line break

Serial.write();

Writes binary data to the serial port. This data is sent as a byte or series of bytes; to send the characters representing the digits of a number use the print() function instead.

Serial.print(65); gives “65”

Serial.print(char(65)); gives “A”, 65 is ASCII code of A

Serial.write(65);  gives “A”, 65 is ASCII code of A

Serial.available();

Get the number of bytes (characters) available for reading from the serial port. This is data that’s already arrived and stored in the serial receive buffer (which holds 64 bytes).

Serial.read();

Reads incoming serial data as single byte.

Hardware Serial Program for ESP8266

Programming of ESP is same as arduino.

Results

Open serial monitor and observe the data we get “Hello World…”  every second and when we send any data it shows “We got:data”. as shown in below figure.

ESP8266 Serial Communication
ESP8266 Serial Communication

 

Leave a Reply

Your email address will not be published. Required fields are marked *