ESP8266 wifi module interfacing with pic microcontroller

ESP8266 wifi module interfacing with pic microcontroller: Hi everyone I hope you are fine and doing well. In this tutorial, I will guide you how to interface ESP8266 wifi module with pic microcontroller. We will be using pic18f46k22 microcontroller in this tutorial. I have seen many tutorials on internet on various websites related to interfacing of ESP8266 wifi module with Arduino But I have not found a single tutorial on how to interface ESP8266 wifi module with pic microcontroller. This module can be used to make internet of things based pic microcontroller projects. I have made a project on wifi based home automation system in which I also used esp8266 module. This module can be used in many applications. For example you can use it to send data to server and you can also use to control servo motor from website. It is very easy to interface this module with arduino because there are many resources available online related to Arduino. But in case of pic microcontroller you have to develop your library yourself.

ESP8266 wifi module configuration

So lets start with pin configuration of this module and how to connect this module with pic microcontroller. This module works on 3.3V voltage range. So make sure you do not connect more than 5 volt with this module otherwise your module will get damaged. It works on serial communication. I will further explain it in later part of this article.  This module has six pins as shown in picture below:

Explanation of each pin along with their operating voltage range is given below.

  • Vcc pin: This pin is used to provide power supply. you should connect 3.3V with this pin and voltage more than 3.3 volt burns this wi-fi module.
  • GND pin: Connect ground terminal of power supply with this pin
  • RX pin: This is a receiver pin of ESP8266 wifi module. This module works on UART serial communication. So this pin is used to receive data from microcontroller or any other device to which you want to send data.
  • TX pin: This is a transmitter pin of ESP8266 wifi module. It is used to send or transmit data to microcontroller or any other device which works on serial communication principle.
  • GPIO_0 and GPIO_2 : These are general purpose input output pins which are used to directly interact with external digital world. This module can also be used as a stand along device to make IOT based projects. But I will not discuss this aspect in this article. I will write a separate article on it. But in today’s article, I will write only on how to interface ESP8266 wifi module with pic microcontroller.
  • RESET pin: It is used to reset this module. It is active high pin and it will reset the module when you apply 3.3 volt.
  • CH_PD pin:  Connect 3.3 volt of this pin.

So now you got basic understanding and pin configuration of this module. Now lets move to interfacing part that is how to interface esp8266 wifi module with pic microcontroller.

ESP8266 wifi module interfacing with pic microcontroller

To interface this module with any microcontroller and any digital device you should know about operating voltages of this device and microcontroller. As I mentioned earlier,  operating voltages of ESP8266 wifi module is 3.3 volt. So its transmission and receiver signal will be also be 3.3 volt amplitude.  So when you are connecting this module with any pic microcontroller, you need to make sure either both have same operating voltages or you need to connect any circuit with esp8266 and pic microcontroller.  This circuit should be cable of converting 3.3 volt signal to 5 volt and 5 volt signal to 3.3 volt.  you can also use voltage divider for this purpose. I will explain it in more details in coming paragraphs.

We are using pic18f46k22 pic microcontroller in this tutorial. Operating voltages of this pic microcontroller is 5 volt. So we need to use any circuit between ESP8266 wifi module and pic microcontroller. We can use a voltage divider between Tx pin of pic microcontroller and Rx pin of ESP8266 wifi module. Voltage divider will step down 5 volt signal of pic microcontroller transmitter to 3.3 volt which is in range of operating voltages of wifi module. We can connect transmit of wi-fi module directly with pic18f46k22 microcontroller, because this pic microcontroller have wide range of operating voltages between 3.3V and 5 volt. So there is not need of voltage divider between Rx pin of esp8266 and pic 18f46k22 microcontroller, because microcontroller will read 3.3 volt as a high signal. But we need to connect voltage divider between Tx pin of microcontroller and Rx pin of esp8266, Because wi-fi module can not read 5 volt and it will burn our module.  Interfacing connections are shown below:

ESP8266 wifi module interfacing with pic microcontroller

As shown in above circuit diagram, we have connected 1k and 2k resistor between Tx pin of microcontroller and Rx pin of esp8266.  I assume you already familiar with voltage divider working principle. If you do not know, you can use a dedicated chip as 5 volt to 3.3 volt converter. Pic microcontroller and esp8266 wifi module communicate with each other through UART serial communication. So you should know how to use serial communication of pic microcontroller. ESP8266 wifi module responds to “AT” commands.  you should check data sheet of this module to know more about AT commands, you will send at commands to this wifi module from pic microcontroller through serial communication.  Before using AT commands you should set the Baud rate of this module to 9600 because by default baud rate of this wifi module is 115200.

AT+ UART_DEF = 9600, 8,1,0,0

This command is used to set baud rate of ESP8266 wifi module to 9600 permanently.  For more information on how to use AT commands check this video tutorial:

9 thoughts on “ESP8266 wifi module interfacing with pic microcontroller”

  1. At the moment I am trying to send 15 bytes from the PIC16F877A (RC6 TxD) to a module ESP8266 to pin 4 (RxD) but the data does not arrive, I performed several tests and I do not get the data to arrive.

    For pin 25 of PIC16F877A send “L01Z01C01P01B61” like this:

    The pin 25 of the PIC, is connected pin 11 of the MAX232 and pin 14 to the pin 2 of an industrial server SENA LS100 and this sends the data to an IP and port, the socket server, reads the data and stores it in a table in MySQL , very good.

    I perform the above procedure, but I change the industrial server SENA LS100 by the module ESP8266, the pin 4 of the RxD module to ping 14 of the MAX232 and the data does not reach the database ..

    If I execute the procedure, by CMD “type dato1.txt 1> COM4:” connecting the serial port pin 2 with 14 MAX232 and pin 3 with 13 MAX232 and the module ESP8266 pin 4 RxD with the pin 12 MAX232 the data reaches the base of data.

    But when I connect the pin 25 of the PIC16F877A to pin 4 RxD of the module the data does not reach the database.
    Codigo en PIC16F877A

    #include
    #use delay(clock=4M)
    #fuses XT,NOWDT,HS,NOPROTECT,NOLVP,NODEBUG,NOBROWNOUT,PUT
    #use rs232(baud=9600,xmit=pin_c6,rcv=pin_c7,parity=N,bits=8)

    #Byte PORTB = 0X06
    #Byte PORTC = 0X07

    #Byte TRISB = 0X86
    #Byte TRISC = 0X87

    #Bit RB0 = PORTB.0

    #Bit RC5 = PORTC.5

    int1 zb0=0;

    void main(VOID) {
    port_b_pullups(FALSE);
    TRISB=0B11111111;

    TRISC=0B00000000;
    RC5=1;
    CICLO:
    IF(RB0==1){IF(zb0==0){zb0=1;printf(“%s”,”L01Z01C01P01B01″);}} delay_ms(5); IF(RB0==0){IF(zb0==1){zb0=0;printf(“%s”,”L01Z01C01P01B02″);}};
    GOTO CICLO;
    }
    Codigo en modulo ESP8266-01

    #include
    SoftwareSerial SerialESP8266(4,5); // RX, TX
    #include
    #include

    ESP8266WiFiMulti WiFiMulti;

    String inputString = “”; // a string to hold incoming data
    boolean stringComplete = false; // whether the string is complete
    boolean newData = false;

    void setup() {
    // initialize serial:
    Serial.begin(9600);
    delay(10);
    // reserve 15 bytes for the inputString:
    inputString.reserve(15);

    // We start by connecting to a WiFi network
    WiFiMulti.addAP(“dlink”, “”);

    //Serial.println();
    //Serial.print(“Wait for WiFi… “);
    //Serial.print(“1 IP : “);
    //Serial.println(WiFi.localIP());

    while(WiFiMulti.run() != WL_CONNECTED) {
    Serial.print(“2 IP : “);
    Serial.println(WiFi.localIP());
    delay(500);
    }

    //Serial.println(“”);
    //Serial.println(“WiFi connected”);
    //Serial.println(“IP address: “);
    //Serial.println(WiFi.localIP());

    delay(500);

    }
    //Fin Setup

    void loop() {
    // print the string when a newline arrives:
    //Serial.print(” Dios “);
    while (Serial.available() > 0 ) {
    //Serial.print(” Dato available() > 0 “);
    // get the new byte:
    //char inChar = (char)Serial.read();
    char inChar = Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == ‘\n’) { stringComplete = true;Serial.print(” Evento “); }
    }

    if (stringComplete) {
    //Serial.println(inputString);
    //Serial.print(“Recibio RxD\n”);
    //Codigo copia ejemplo ClienteBasic
    const uint16_t port = 1961;
    const char * host = “192.168.0.100”; // ip or dns

    //Serial.print(“connecting to “);
    //Serial.println(host);

    // Use WiFiClient class to create TCP connections
    WiFiClient client;

    if (!client.connect(host, port)) {
    Serial.println(“connection failed”);
    Serial.println(“wait 3 sec…”);
    delay(3000);
    return;
    }
    //http://forum.arduino.cc/index.php?topic=409456.0
    //Serial.print(buffer);

    // This will send the request to the server
    // “Send this data to server”
    client.print(inputString);

    //read back one line from server
    String line = client.readStringUntil(‘\r’);
    client.println(line);

    //Serial.println(“closing connection”);
    client.stop();

    //Serial.println(“wait 5 sec…”);
    //delay(5000);

    //Fin codigo copia
    // clear the string:
    inputString = “”;
    stringComplete = false;

    }
    //Serial.print(“Loop\n”);
    }
    //Fin Loop

    I am very grateful for the support you can give me.

    Reply
  2. sir, how can send data to server through wifi module for measure instant value of voltage unit in a single phase .we are connect voltage sensor to arduino board and then connect arduino to wifi module and send data on web page.
    sir give me porogramming solution.

    Reply
  3. Dear Sir,

    Why do we need to change the baud rate from 115200 to 9600. Is it compulsory for pic18f to communicate with esp8266 at baud rate 9600.

    Kindly please reply. I really need to understand it.

    Thanks

    Reply
  4. Dear Sir,
    I wanted to connect ESP8266 to a 4G module (SIM7500e). I will be grateful if you gave me some help and also if it’s possible I would like to see your code for this project.

    Thank you

    Reply

Leave a Comment