Esp8266 Serial Connection
Dec 16, 2016 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. Serial-to-WiFi Tutorial using ESP8266. The ESP8266 is a low cost Serial-to-WiFi module that interfaces nicely to any microcontroller. However, a word of caution - it is highly undocumented (primary reason for writing this document), and more importantly, it is frequently updated and not backward compatible.
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.
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.
Esp8266 Usb Connection
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 Connection Esp8266 01
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.
2 4 6 8 10 12 14 16 18 20 22 24 | ESP8266 Serial Communication The blue LED on the ESP-12 module is connected to GPIO2 (which is also the TXD pin; so we cannot use Serial.print() at the same time) Serial.begin(9600);// Initialize the Serial interface with baud rate of 9600 // the loop function runs over and over again forever if(Serial.available()>0)//Checks is there any data in buffer Serial.print('We got:'); Serial.print(char(Serial.read()));//Read serial data byte and send back to serial monitor else Serial.println('Hello World..');//Print Hello word every one second } |
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.