ESP8266 WiFi Module for Dummies

106,531

680

75

Published

Introduction: ESP8266 WiFi Module for Dummies

About: I've been in IT and Software Development forever! My real passion though is where Software and Industrial Design meet. Especially micro-controller based gadgets and 3D Printing. I would love my day job to be...

Overview of the ESP8266 WiFi Module

The ESP8266 is a really useful, cheap WiFi module for controlling devices over the Internet. It can work with a micro-controller like the Arduino or it can be programmed to work on its own.

The Internet of Things (IoT) has just been made a whole lot cheaper and easier!

The ESP8266 comes with factory installed firmware allowing you to control it with standard “AT commands”. You can also easily create and upload your own code and this makes it hugely powerful and flexible.

The ESP8266 was launched in 2014 and is rapidly growing in popularity. There is only one ESP8266 processor but it is found on many different breakout boards. These all differ in terms of which pins are exposed and the size of the flash memory etc. These breakout boards have evolved rapidly over the years and there is a lot of information to be found on the web. This is both a blessing and a curse as some of the advice is outdated or just plain wrong.

As a result it took me 3 days before I could get it to do anything! Some people are lucky but for me it was a battle to get anything working. At times I thought I had broken it but with some perseverance it always came back to life!

I’ve finally got it singing and dancing and it turns out to be easier than I thought!

I’ve consolidated all the useful info I found into one place and I thought I’d share this in an instructable. This will hopefully get you quickly up and running with all the basic functionality that you’ll need.

In this instructable were are looking at the ESP-01 breakout board.

This same approach should however apply to most (if not all) of the variants of the ESP out there.

Step 1: The ESP-01 in a Nutshell

  1. It supports the 802.11 b/g/n protocol
  2. It can connect to your router and work as a client or it can be an access point itself or both!
  3. It is IP addressable and can be a Web Server
  4. The “standard” version has 2 digital pins that can be used for input or output. Eg: to drive LED’s or relays. These pins can also be used for PWM. Other versions have more pins exposed. For example the ESP-12, which is a good option if you need more pins. Either way the programming is still the same.
  5. Analog input is also available on the ESP8266 chip (ADC/TOUT) but it’s not wired up on the ESP-01.
  6. It can be combined with an Arduino or it can be programmed to work on its own
  7. There are various tools and development environments (IDE’s) to program it.
  8. And it has lots more….

In this demo the ESP8266 acts as a Webserver hosting a Web Page that you can interact with over WiFi.

With this demo you can start your own home automation project and control it from the web!

Step 2: What You’ll Need

Firstly let’s see if we can get it up and running.

A lot of sites recommend re-flashing the firmware and using various tools. You don’t need to do that just yet.

We’ll be replacing the firmware with our own code. In another instructable: The Simple Guide to Flashing Your ESP8266 Firmware I show you how to revert it back to an updated factory version. If you'll only be programming the ESP8266 with your own code - as we will be doing in this demo - then you don't need factory firmware. Your code is the firmware.

We'll be using the Arduino IDE as a programming tool but we don't need an Arduino itself. The ESP8266 has all the functionality we need.

Note: that after uploading your own code, your ESP8266 won't respond to AT commands any more. Your code has replaced the firmware. Using your code and the ESP8266 library in the Arduino IDE you'll have access to a richer set of commands. See the documentation here: https://www.arduino.cc/en/Reference/WiFi

You can restore the firmware on the ESP8266 to go back to AT commands if you wish. See my other instructable: The Simple Guide to Flashing Your ESP8266 Firmware.

NB: It’s a 3.3V module and not 5V. The ESP-01 is more robust than I thought but you should set it up properly, especially if you’re using it with a 5V USB to Serial Programmer or micro-controller, otherwise you may fry it!

What you’ll need:

  1. An ESP8266 of course (use the ESP-01 variant or adapt your wiring accordingly)
  2. Breadboard
  3. 3.3V USB to Serial Programmer like the FTDI Basic (sometimes known as a UART: Universal Asynchronous Receiver/Transmitter). Some come with a jumper to switch between 5V and 3.3V.
  4. Breadboard Jumper Wires
  5. A separate 3.3V Power Supply. You do need one as the ESP8266 can chew a lot of mA (>250mA at times). The 3.3V output from the Serial Programmer or the Arduino is not sufficient. Without a separate power supply your ESP may be erratic and you will waste time trying to upload code.
  6. Led, push button, 330 ohm resistor, 10k ohm resistor, .1µF capacitor across your power supply (as standard practice).
  7. Add a Logic Level Converter into the circuit if you’re using a micro-controller or FTDI programmer at 5V

If you have an ESP8266 with a built in USB port then you don't need to go through any of this hassle. The Wemos D1 Mini is a good alternative. See this: https://www.wemos.cc/product/d1-mini.html

Step 3: The Components in More Detail

ESP-01

I got my ESP-01 from: http://netram.co.za/2720-wifi-serial-transceiver-module-esp8266.html

You can have a look at the various versions here: http://www.esp8266.com/wiki/doku.php?id=esp8266-module-family


Breadboard Power Supply

A good power supply seems to be a crucial aspect for the ESP8266. A breadboard power supply is really useful, especially when you’re dealing with mixed voltages. The one shown here supplies 3.3V and 5.5V and plugs right into your breadboard. I should have got one a long time ago! The ESP8266 can chew a lot of mA (>300mA at times). If your ESP reboots or seems unreliable then you need a separate power supply for the ESP (keep a common ground though). Also connect a capacitor across 3.3V and ground to minimise spikes and to provide a small reservoir of power.

http://netram.co.za/938-breadboard-power-supply-5v33v.html


USB to Serial programmer

Get one that supports 3.3V or has a 3.3V/5V jumper otherwise if you only have 5V then you should use a bi-directional Logic Level Converter in between the programmer and the ESP8266. The FTDI Basic has a 3.3V/5V jumper on the back:

http://netram.co.za/2842-ftdi-basic-usb-to-ttl-programmer.html

http://netram.co.za/2679-logic-level-converter-bi-directional.html


Logic Level Converter

Bi-Directional 5V & 3.3V. This is only required if you are using a 5V serial programmer.


Breadboard Jumpers

Include some JST jumpers as they’re handy for plugging onto modules with pins and then joining them to the breadboard. Include some male to female cables for this purpose. The ESP-01 isn't breadboard friendly so you'll need these.

Step 4: The Breadboard Layout

Looking at the breadboard, the FTDI programmer is the red board on top and the ESP8266-01 at the bottom. The FTDI programmer only needs 3 wires connected: the ground wire and the RX to TX and TX to RX on the ESP. On the left is the breadboard power supply (it would normally plug into the breadboard).

NB: The quality of the power supply to the ESP is one of the most important factors. There is a small capacitor (.1uF) across 3.3V and Ground to minimise spikes. If you are having problems with programming your ESP it may well be your power supply. What will help is to add another larger capacitor across 3.3V and Ground. eg: 10uF.

Step 5: The ESP-01 Pinouts

The picture above of the ESP8266 pinouts is found all over the net. I think it may have originated from Neil “Kolban’s Book on ESP8266”. This is a fantastic resource and is available as a pdf download from: https://leanpub.com/ESP8266_ESP32. It’s 400 pages long, was updated in November 2016, and if you’re into serious ESP8266 development then this is the book to get. It’s free but make a donation if you can.

Breadboard wiring notes:

  • The wires are shown connecting to the front of the ESP-01 for clarity. Attach them to the same pins from behind. The ESP-01 is not breadboard friendly and JST connector male-female jumpers are handy for connecting it.
  • GPIO 0 must be held low for programming. This should be removed if you remove the FTDI and operate the ESP8266 on its own power supply.
  • CH_PD should be held high at all times.
  • The RESET pin is held high with a 10k ohm resistor and brought to ground by the RESET button which will reboot the chip. Press RESET every time before you upload code, and every time you connect or disconnect GPIO 0. The reset button will save you a lot of hassle.

When you power the circuit up the red LED on the ESP-01 should light up and the blue LED should flash briefly. Later when uploading code to the ESP the blue LED should flash continuously during the process. Any other LED combinations suggests that there is a problem.

NB: If you want to remove the Serial programmer and run the ESP on its own, make sure you disconnect GPIO 0 from ground and push the RESET button otherwise the ESP may not work properly.

Step 6: Schematic

Step 7: Using the Arduino Instead of a Serial Programmer

You can also use an Arduino instead of your USB to Serial programmer.

A few things to note:

  1. On a 5V Arduino the digital output will be at 5V and this could fry your ESP2866. So either use a 3.3V Arduino or a Logic Converter chip
  2. The 3.3V Vout on the Arduino is insufficient to drive your ESP8266 and you could get unreliable results. Preferably use another 3.3V supply.
  3. As a recommended practice always upload a blank sketch to your Arduino before connecting any other devices to avoid any unpredictable results. A blank sketch has empty setup() and loop() functions.
  4. If you are using an Arduino Uno it only has one hardware TX&RX pair. To communicate with both the ESP8266 and the Serial Monitor at the same time you will also need to use the SoftwareSerial include file. It used to have a limitation on the baud rate but can now theoretically support a baud rate of up to 115200. A Mega 2560 has more hardware TX and RX pins so this could be a better way to go.
  5. And finally and importantly: Look closely at the TX and RX pins on the Arduino. You will notice that they have arrows next to them. The TX pin has an arrow pointing away from it. What this says is TX IN! What this really means is that the TX pin is actually the RX pin! And likewise the RX pin is actually the TX pin! So when using the Arduino as a serial adapter with another device you must connect RX to RX and TX to TX. I only discovered this nuance after several hours of pain! Why-oh-why did they label it like this?

Whilst I’m asking questions: why the ESP-01 was produced with so few of the useful pinouts exposed also baffles me! It is still a great starting point but if you need something more then go for the ESP-12.

To make things really simple, try an ESP-12 with a built in USB like:

https://www.wemos.cc/product/d1-mini.html

Step 8: Programming Using the Arduino IDE

There are various tools to program the ESP8266 but the Arduino IDE (Integrated Development Environment) is by far the easiest for most folk.

To program the ESP8266 with the Arduino IDE you’ll need to download the latest version if you don’t already have it and then install the ESP8266 support. Note that you are only using the Arduino programming environment you don't need any Arduino hardware itself.

To download the Arduino IDE go to: https://www.arduino.cc/en/Main/Software

Start up the Arduino IDE and select the Boards Manager

Step 9: Setup the ESP2866 Board Manager

Start the Arduino IDE and select the Boards Manager under Tools / Board ..

Step 10: Install the ESP2866 Board Manager

Find the esp8266, select it and click Install.

Step 11: Arduino Preference Setup

Go to File / Preferences

Step 12: Boards Manager URL's

In the Additional Boards Manager URLs field enter the URL for the ESP8266 package which is:

http://arduino.esp8266.com/stable/package_esp8266com_index.json

Step 13: ESP8266 Setup in IDE

Select the “Generic ESP8266 Module” and go back to the Boards Manager again

Step 14: ESP8266 Options

Setup you ESP-01 options. Select the appropriate COM port according to your PC.

Step 15: Talking to ESP8266

If you still have the original firmware on your ESP8266 you can try talking to it with the Serial Monitor.

  1. Open the Arduino Serial Monitor:
  2. Select the appropriate baud rate which is usually 115200 but could be anything down to 9600 depending on the firmware on the ESP.
  3. Also select “Both NL & CR”. Ie: New Line and Carriage Return characters after each “Send”.
  4. Type AT and press enter. The ESP should come back with OK. If not press the RESET button or try unplugging the USB cable and starting again. Make sure also that you have the right COM port selected. Also if you don’t have the “standard” firmware on your ESP it may not understand the AT command set. This is not necessarily a problem yet as you’ll be overwriting this with Arduino code later in this instructable.
  5. Type AT+GMR. This should come back with the version numbers of the firmware on your ESP. In another instructable I show you how you can restore the latest version of the “factory” firmware (if you want to).

Step 16: ESP8266 Arduino Code

Download code file (ESP8266_LED_Control.zip), unzip it and open it in the Arduino IDE.

Change the Router setting to yours. ie: change the SSID and PASSWORD fields.

Make sure your breadboard is setup correctly, and connect your USB cable to the Serial Programmer.

NB: When addressing the GPIO Pins in your code you should use the GPIO number. eg: 2 for GPIO 2 and not the physical pin number which will be different.

Step 17: Uploading Your Code to the ESP8266

Make sure you’ve closed ALL Serial port monitors on the same port, if you you’re using them. Eg: tools like CoolTerm. Open the Arduino Serial Monitor again, as you will need to see the IP address of the ESP after it loads.

NB: To upload your code you must have GPIO 0 grounded. You can leave it there whilst running your code in the browser. Press the RESET button before each load. If the upload fails, check your settings and try disconnecting and reconnecting the USB cable. Try Ctrl&U a couple of times. If all else fails try rebooting your PC. Sometimes the COM ports get a bit messed up.

The blue LED on the ESP should flash if the code is being uploaded successfully. The progress of the upload should look like the images above.

NB: If you want to remove the Serial programmer and run the ESP on its own, make sure you disconnect GPIO 0 from ground and push the RESET button otherwise the ESP may not work properly.

If your ESP8266 uploads are behaving erratically, improve your power supply.


Step 18: Let See What It's Doing

Switch over to the Arduino Serial Monitor and you should see the IP Address of the ESP. You could also log into your Router and check what’s connected there. The device will show up in the list of connected IP’s with a name like: ESP_1A6C4A.

Whilst you are in your router you could always reserve the ESP's IP address and set up some Port Forwarding so that you can connect to the ESP from outside your network - like the office..

You should see something like the above. Use that IP address in your browser.

Step 19: Let's Try It Out

After clicking on a few links you’ll hopefully see the LED going on and off!

You also see that it can do PWM and dim the LED.

The ESP8266 can also handle Interrupts on the digital pins and also has analog input. The analog input pin though is not exposed on the ESP-01,

Internet Explorer works fine but for some reason FireFox seems very slow with the ESP. This may have something to do with the setup of the HTTP header.

Note: that after uploading your own code, your ESP8266 won't respond to AT commands any more. Your code has replaced the firmware. Using your code and the ESP8266 library in the Arduino IDE you'll have access to a richer set of commands. See the documentation here: https://www.arduino.cc/en/Reference/WiFi.

You can restore the firmware on the ESP8266 to go back to AT commands if you wish. See my other instructable: The Simple Guide to Flashing Your ESP8266 Firmware.

Step 20: ESP Resources

"Kolban’s Book on ESP8266" updated in November 2016 is for serious enthusiasts.

It is 470+ pages long and has an incredible amount of detail. You can get it for free but make a donation if you can.

https://leanpub.com/ESP8266_ESP32

He has now separated out the ESP32 and published a new book:

https://leanpub.com/kolban-ESP32

ESP32

"Like the ESP8266, the ESP32 is a WiFi compatible microcontroller, plus it adds support for Bluetooth low-energy (i.e BLE, BT4.0, Bluetooth Smart), and nearly 30 I/O pins."

See: https://www.sparkfun.com/products/13907

ESP Sites

http://www.esp8266.com/wiki/doku.php?id=esp8266-module-family

http://www.esp8266.com/

https://iotbytes.wordpress.com/esp8266-pinouts/

http://espressif.com/en/products/hardware/esp8266ex/resources


AT Commands

https://www.itead.cc/wiki/ESP8266_Serial_WIFI_Module#AT_Commands

https://espressif.com/sites/default/files/documentation/4a-esp8266_at_instruction_set_en.pdf


Arduino Boards Manager

http://randomnerdtutorials.com/how-to-install-esp8266-board-arduino-ide/

https://github.com/esp8266/Arduino


Notes on Flashing the Firmware:

I have published an instructable showing you how to flash the factory firmware onto your ESP8266.

See: https://www.instructables.com/id/The-Simple-Guide-to-Flashing-Your-ESP8266-Firmware/

4 People Made This Project!

Recommendations

  • Creative Misuse Contest

    Creative Misuse Contest
  • Clocks Contest

    Clocks Contest
  • Oil Contest

    Oil Contest

75 Discussions

Hi TonesN

I'm working on a projet were i need to collect data from a web "data base" and turn it into an image on screen. I have some problemes with the ESP8266 wen trying to connect. Here is the error

Archiving built core (caching) in: C:\Users\GR741003\AppData\Local\Temp\arduino_cache_324429\core\core_esp8266_esp8266_d1_CpuFrequency_80,FlashSize_4M1M,LwIPVariant_v2mss536,Debug_Disabled,DebugLevel_SSL,FlashErase_sdk,UploadSpeed_115200_408b080357398332d92905650febc357.a

warning: espcomm_sync failed
error: espcomm_open failed
error: espcomm_open failed

Please help me fix it!!!!

thanks

Be careful on saying "for dummies"! I can say without a doubt that I could really challenge you!

Hello TonesB,
All I can say is Thank You, unless I can E-Mail you a drink to say thanks!
Very well done and it will be a great help to me for my next project.
Dave

1 more answer

Hi Dave, thanks for the feedback and thanks to you and everyone for sharing good information.
That's my reward. We all do good stuff and share it and people appreciate it.

Hello, I wanted to ask if it would be possible to have the ESP8266 to receive input from a distance sensor like GP2Y0A60SZLF and send it to Arduino/Raspberry Pi ?

1 more answer

Hello Daniel,
Sure - Very easy and there are sample programs available everywhere, check out the Arduion code.
Just make sure the ESP version you get has I2 s available. The sharp sensor is quite easy to connect.

I appreciate you taking the effort to improve ESP2866 resources on the web!

I've followed your directions, and everything is running smoothly until step 15 (talking to ESP2866). My serial monitor is blank, unless I hit the reset button, then I get a line of random characters. I uploaded your code successfully as well, but again, a blank serial monitor. I've tried all the baud rates, and I've checked my router. The device is not connected to the internet. I know I entered the SSID and password correctly. I believe my power supply is sufficient: two AAs connected to a boost power module. Any input would be appreciated! Again, thank you so much for making such detailed instructables!

1 reply

Nevermind, I found the problem. Rather than delete my post, I'll just add that one of my pins was fried (perhaps GPIO 0 or TX or RX likely). Fortunately I had two chips available. Using my second ESP8266 was a complete success, thank you!

1.hola alguna informacion para ser usado en Pic18f4550 ?

0
user
ArsS1

10 months ago

its great and i have a question

i wanted a code to cheak connection , if connection with wifi is true set any port to high and In reverse set low this port

plz help me to creat this code :))))

tanx so much

1 reply

Hi ArsS1

You'll see in the code this loop:

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

The property: WiFi.status() will tell what the connection status is. The While loop will continue looping and keep printing dots to your Serial Monitor screen until the ESP8266 has connected to your WiFi router.

ie: until WiFi.status() == WL_CONNECTED

So before this loop starts you can set a port to light up a Red LED, to show it hasn't connected yet, and after the code exits this loop you will know the WiFi has connected so you can now light up a Green LED instead or use an RGB LED to show different colours and statuses.

The various values of the status as defined in the Library are below, so you can play with these:

WL_IDLE_STATUS = 0,
WL_NO_SSID_AVAIL = 1,
WL_SCAN_COMPLETED = 2,
WL_CONNECTED = 3,
WL_CONNECT_FAILED = 4,
WL_CONNECTION_LOST = 5,
WL_DISCONNECTED = 6

I hope this helps?

Hello Tones - trying to upload your code to my ESP-01. Not sure of the firmware version. Does this matter? Below is the error message....

Error Message

Arduino: 1.8.1 (Windows 10), Board: "Generic ESP8266 Module, 80 MHz, 40MHz, DIO, 115200, 1M (512K SPIFFS), ck, Disabled, None"

Sketch uses 232249 bytes (46%) of program storage space. Maximum is 499696 bytes.

Global variables use 33264 bytes (40%) of dynamic memory, leaving 48656 bytes for local variables. Maximum is 81920 bytes.

Uploading 236400 bytes from C:\Users\DJC-LI~1\AppData\Local\Temp\arduino_build_962768/ESP8266_Led_Control_PWM.ino.bin to flash at 0x00000000

................................................................................ [ 34% ]

................................................................................ [ 69% ]

......................................................................warning: espcomm_send_command: didn't receive command response

warning: espcomm_send_command(FLASH_DOWNLOAD_DATA) failed

warning: espcomm_send_command: wrong direction/command: 0x01 0x03, expected 0x01 0x04

error: espcomm_upload_mem failed

error: espcomm_upload_mem failed

This report would have more information with

"Show verbose output during compilation"

option enabled in File -> Preferences.

-----------

Was able to get it to load once... and the Arduino IDE Monitor show this... keeps repeating...

ets Jan 8 2013,rst cause:4, boot mode:(3,7)

wdt reset

load 0x4010f000, len 1384, room 16

tail 8

chksum 0x2d

csum 0x2d

v09f0c112

~ld

------ End -----

Thanks for taking a look, not sure what is going on - Dan

2 replies

Hi Dan

It looks like a power supply
issue. Whenever the ESP8266 behaves erratically it's usually because
your power supply is not sufficient. To be on the safe side, whilst
prototyping, use a power supply that can provide at least 1A. Keep the
leads short and also put a 10uF and a .1uF capacitor across the + and -
to smooth it.

Otherwise, the upload is starting which shows that
everything else seems to be configured ok. On the firmware: any code
that you upload to the ESP8266 becomes the new firmware, so versioning
is not an issue. (Except in the unlikely event that you've bricked the
ESP8266 and it's a boot loader issue.)

So sort out the power supply and give it another go.

So-far, I've been finding the same problem.. Most projects seem to be wired for the device to be powered off whatever it's plugged into (USB<-->FTDI , Arduino, etc.) and the regulators on those devices are at best going to give you 250mA, not even scratching the requirements if they intend on using the WiFi in any way.. ) I kinda cheat... Most of my breadboard projects, I've powered off reclaimed ATX Supplies.. I have 2, where the 3.3V output is rated up as high as 28A!! (that's 28000mA!!!!).. Absolutely no Brown-out (power loss), but also playing with fire if there is a short or wired in reverse.

I usually wire the supply to a separate rail on the breadboard, and common-GND everything.. VCC from the USB-FTDI and +3.3V from the supply, are _NEVER_ connected together! I've also run into quite a few FTDI modules, where, despite the switch being set to 3.3V, the VCC is still punching +5V. (Deek-Robot's version, with the slide switch, as example.)

0
user
nixrod

1 year ago

Thanks for this great instructable!

I tried hooking up my esp01s a few months ago with no success using various tutorials from the web, which all contradicted each other slightly.
This one was very clear and concise. Thank you for having all the required information in one place.

1 reply

Hi nixrod, thanks for the feedback. I had exactly the same problem trying to get my esp01 going and that spurred me on to write this instructable.

0
user
scanos

1 year ago

Hi, thanks for the great instructable. It worked fine for me except I cannot get the led to dim. I am using an ESP-01 ESP8266, i.e. the one with 8 pins / 2 GPIOs 0 and ". Should it work? Can you clarify what you said about analogue signals., please

2 replies

Hi Scanos

I'm puzzled. The ESP-01
definitely has PWM as that's what I used in the original build and it
does work. Does the LED turn fully on and off ok? Maybe try other PWM values. ie: change the 512 (which is half way) to a lower value?

Hi, I set the 512 to 128 and it worked. I also guess that I could have used a higher rated resistor. Thanks for that. I assume that I can use your program to read a voltage from a DSB1820 temperature sensor?

0
user
smeem

1 year ago

Tried doing this but it wont send back AT commands so i tried uploading the code anyways and it seemed to work but when it tries to connect and i watch it in serial monitor i get what i pasted below and nothing more

any thoughts?

Connecting to ASUS

..............................