Interfacing ESP8266 with PIC16F877A Microcontroller

Interfacing PIC

Interfacing PIC

Interfacing PIC16F877A Microcontroller with ESP8266

In this article, let us discuss how to interface WIFI module ESP8266 with a PIC microcontroller. So far you might have been using the ESP8266 module as a standalone microcontroller or might have been using it with the Arduino library. But, when it comes to hardcore embedded system projects we should know how to use it with PIC microcontrollers as well. This will help you to customize your projects in design perspective and at the same time also making it cheap.

The ESP8266 modules comes with a default firmware loaded into it, hence we can program the module using AT commands. These commands have to be sent through a serial communication channel. This channel is established between the PIC and the ESP8266 module by using the USART module in the PIC microcontroller. The whole working will be monitored and reported to the user by using a 16×2 LCD display. Hence, this tutorial assumes that you have some basic knowledge about the USART module in PIC, Interfacing LCD with PIC and using AT commands in ESP8266. If you do not then you can fall back to the linked tutorials to learn them beforehand.

Materials Required:

You would need the following hardware to complete this tutorial

  1. PIC16F877A
  2. 20MHz crystal oscillator
  3. 7805
  4. LM317
  5. ESP8266
  6. 16*2 LCD display
  7. PicKit3 programmer
  8. Resistors (1K,220ohm,360ohm)
  9. Capacitors (1uF, 0.1uF, 33pF)
  10. Jumper wires
  11. 12V Adapter to power the PIC and ESP module


The complete schematic of the project is shown here belowPIC16F877A ESP8266 interface

The schematics consists of two voltage regulator circuits, one is a +5V regulator which is used to power the PIC microcontroller and the other is a 3.3V regulator which powers the ESP8266 module. The +5V is regulated by using a 7805(Linear Voltage Regulator IC). The 3.3V is regulated by using LM317 (Variable Voltage Regulator). The ESP8266 module consumes a lot of current (~800mA) hence if you are designing your own power supply make sure it can source such high current. Also make sure that the ground pins of the PIC and the ESP8266 module is connected together.

So now we know that the PIC operates on +5V and the ESP8266 operates at 3.3V volts. In order to establish a USART communication between these two modules we need have a 5V – 3.3V logic converter circuit as shown in the above figure. This circuit is nothing but a potential divider which simply converts the incoming +5V to 3.3V. This will prevent the 3.3V tolerable RX pin of ESP8266 from getting +5V.

I have made the PIC and ESP modules on two separate perf boards, as shown in these tutorials. This way I can use them universally for more similar projects

  1. LED hardware using PIC
  2. Getting started with ESP8266

You can follow the same, or build your own board in your style or simply connect the above circuit to a breadboard.

Programming the PIC microcontroller:

In order to program the PIC microcontroller to send “AT commands” serially using USART to the ESP8266 module we have to use a library. This library will save you a lot of hassle, like using the ESP8266 instruction modules to check for each and every AT command and then find a way to transmit them to the ESP module. This library is free software originally developed by Camil Staps and later it was improved and modified by Circuit Digest so that it can be used with our PIC16F877A   Microcontroller. You can download it here

The library is still under development, but you can use most of the important AT commands in the ESP8266 firmware. If you find any of the command you need is missing, then let me know it in the comment section and I will try to add it for you. This tutorial will explain you all the commands(so far) that can be used by through this library. Further will also guide you to add your own functions to the library.

Functions in ESP8266 Library:

  • Initialize_ESP8266(): This function will Initialize the USART module of the PIC to communicate with the ESP8266 module. It sets the baud rate at 115200 and prepares the Rx and Tx pin of PIC for USART communication.
  • _esp8266_putch(): This function is used to send a single character serially to the ESP8266 module. For example, _esp8266_putch(‘a’) will send the character a serially to the ESPmodule.
  • _esp8266_getch(): This function is used to get a single character from the ESP module. For example if the ESP is printing “OK” and we use char a =_esp8266_getch(). Then the char ‘o’ will be stored in the variable a.
  • ESP8266_send_string(): This function is the string version of _esp8266_putch(). It can send one complete string to the ESP8266 module. For example, ESP8266_send_string(“AT/r/n”) will send the command “AT” to the ESP8266 module.
  • esp8266_isStarted(): It is used to check if the PIC can communicate with the ESP module. It send the command “AT” and waits for “OK” if received it returns true else it returns false.
  • esp8266_restart(): Resets the ESP8266 module and returns true is reset successful and returns false if not successful.
  • esp8266_mode(): Used to set the working mode of the ESP8266 module. As we know it can work in three different modes.
esp8266_mode(1): Station mode
esp8266_mode(2): Soft AP mode
esp8266_mode(3): Both Station and AP mode
  • esp8266_connect(): Allows you to connect to a wifi signal. For example esp8266_connect(“home”,”12345678”), will allow your module to connect to the wifi signal named home whose password is 12345678.
  • esp8266_disconnect(): This function disconnects you module from any wifi connection that was previously connected
  • esp8266_ip(): Gets the IP address and returns it. Use this function if you wanna know the IP address of the ESP8266 module.
  • esp8266_start(): This function is used to start a TCP or UDP communication. For example esp8266_start(“TCP”, “”, 80). Will start a TCP network in that IP and port 80.
  • esp8266_send(): This function is used to send information to the TCP/UDP network. The HTML script will be sent using  this command. Then this script will appear in the IP address in which the communication was established in prior.
  • esp8266_config_softAP(): This function is used to configure the softAP. For example esp8266_config_softAP(“office”,”12345678”); will create a Wifi signal named office and the password 12345678 should be used to access it.
  • esp8266_get_stationIP(): This function will return you the IP/MAC address of the clients who are connected to your softAP.

Sample Program:

Now that we have understood the functions of each and every command in the library let us look into a small sample program. In this program we will check if the connection between ESP8266 and PIC is successful and then create a WIFI network (SoftAP) with a preferred name and password. The complete program and the simulation of the same will be explained for your understanding.

Again if you have not read our PIC interfacing with LCD and PIC USART tutorial please read the, before proceeding because only then it will make sense to you.

Since we are just getting started to interface PIC with the ESP8266, I have used an LCD to make sure things are working properly.




    Lcd_Print_String("ESP not found");

    }while (!esp8266_isStarted()); //wait till the ESP send back "OK"


    Lcd_Print_String("ESP is connected");



When we send the “AT” to the ESP8266 module it replies back with a “OK”. This ensures us that the ESP8266 module is connected successfully. The function esp8266_isStarted() is used for the same. We send the signal AT from the PIC and we wait till the ESP module to get alive and send us an OK. If we get an OK we display that the “ESP is connected” on the LCD.



    Lcd_Print_String("ESP set as AP");



The above lines of code are used to set the ESP module to work in “soft AP” mode. The function esp8266_mode(2);   sends the AT commands “AT+CWMODE=3” to the module and waits for the module to respond with “OK”

/*Configure the AP name and Password*/



    Lcd_Print_String("AP configured");



    /*AP configured*/

This segment of the code is used to configure the softAP. Here we have named the SSID as “CircuitDigest” and the password as “619007123”. To indicate that the process is complete we will wait for the module to respond with “OK” and then print AP configured onto the LCD screen.

That is it now we have interfaced the ESP8266 module with the PIC MCU and have configured the softAP with a name and password of our choice. As usual lets simulate this code and see how it works.

Simulation output:

We are using the Proteus software to simulate the output. The design file for the same can be found in the attachment.

Since we do not have an ESP8266 module in Proteus library we have use the Serial terminal and respond back as a user to the PIC module. The simulation once completed screen will look like belowpic esp interfacing simulation

The output of our code is shown in the Virtual terminal. The complete working of the simulation will be explained in the video below.

Output Verification:

Once the program is verified using the simulation, dump it into your PIC microcontroller. Make the connections as shown in the schematics above (Hardware section). You should be able to track your progress through the LCD display.

Once the LCD says that the AP is configured we can check that out using the WIFI settings in Phone or Laptop. My laptop shows the following signal as per our program.WiFi signal

That is it guys we have successfully interfaced the ESP8266 module with the PIC Microcontroller. This is a very basic interface and if you want to do any complicated projects using the ESP8266 you might have to add your own libraries or at least add your own functions. Trust me it is very much easy to do so, I will give a brief insight for the same.

Adding functions to the ESP8266 Library:

Adding your own function will help you to send any “AT” command to the ESP8266 module. To proceed with this you need to read the instruction set documentation of the ESP8266 module. You can directly send any AT command that you find in that instruction set manual. But always remember to append “/r/n” at the end of every AT command. For example if your want to establish multiple connections with your ESP module. Then open the instruction set documentation and find our which AT command will do this job for you. Here the command “AT+CIPMUX=1” will allow you to establish multiple connections with your ESP module.

Now all you have to do is send this “AP+CIPMUX=1” to your ESP8266 module using the serial port. The hardcore way of doing this is by simply using the command


This will work but is not the best way of doing it. You have to read back what your ESP8266 responds to your command. In our case it will respond with “OK”. So you have to read the incoming data from ESP8266 module and confirm that it is a “OK”. Also you can make this function where the “1” or “0” can be passed as arguments.

Go ahead and try to make your own functions for the library. But if you need help please fell free to use the comment section and I will help you out.


Leave a Comment

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