ESP8266 is the most popular connected microcontroller out there. If you want to take advantage of it, you need to flash a firmware first. There is more than one option when it comes to firmware but flashing ES8266 (and ESP32) method remains the same regardless. This tutorial will show you how to flash ESP8266 (and other boards like ESP32 & ESP8285) and how to program it.
ESP8266 Flash Guide
Before we start with the ESP8266 flash guide, I would like to highlight 2 most common issues that you may encounter. If you follow this guide with these in mind, you should have no issues whatsoever if you keep these points in mind
Issue no 1
Always double-check the jumper cables. Regardless of how good and clear a guide is, if you fail to connect the cables correctly – you won’t be able to flash the microcontroller. Trust me when I say this. These mistakes are far too easy to make.
Put aside 5 jumper cables in different colours and use it just for flashing. Be consistent with how you use the colour coding (Vcc-Red, GND-Black, RX-Green, TX-Blue, GPIO00-Yellow). It will help you with keeping things organized!
Issue no 2
ESP8266 is a 3.3V board. Unless it comes as a dev board with power regulator or similar, make sure to use a correct power supply. While we are talking electricity, the current must be sufficient to flash the board successfully. 500mA supply is recommended. USB 2.0 port should be enough to deliver it.
There is more than one tool out there to complete the process. If you are going to flash Tasmota on your ESP chip, consider using Tasmotizer (more about it in this post). It will save you time and there is no need for the software requirements below.
Start with the software, mostly because the download will take you a few moments, this will give you some time to hook up the jumper cables correctly. You will need several programs. Once the firmware is flashed, you will use only one.
- NodeMCU flasher (flashes ESP8266 with the image)
- ESPlorer (Script flasher, an impressive tool really)
- Firmware builder (start here, as it takes few about 10-15 min to compose it)
- ESPtool (if NodeMCU fails, we need it to do fixes)
- Python 2.7 (needed for esptool)
- PythonSerial Library (you may need it as well)
Get serial port adapter (FTD1232) if possible, you can also use Arduino Uno to flash the firmware.
Take a closer look at your serial port adapter. There are 2 ways to connect your ESP8266, but before you do this make sure your adapter is not running at 5V. Mine has a bridge which selects the 3.3V and 5V. You want to make sure that ESP8266 is running on 3.3V.
Another thing to note is the DTR pin. The FTD1232 has that pin available, other adapters may not.
|ESP8266||FTD1232 DTR||FTD1232 no DTR||Arduino Uno|
|VCC & CHPD||VCC||VCC||3.3V|
|GPIO 00||DTR or GND||GND||GND|
|GPIO 16/RST||GND (don’t connect)||GND (don’t connect)||GND (don’t connect)|
Don’t use external power, it can mess up the flash process. Connect cables as per the table above. For GPIO16/RST – reset pin use female-male cable and leave it hanging from the ESP8266. You will need it to touch the GND pin to reset the ESP.
If you have more than 2 ESP8266 consider making an adapter, reconnecting the cables each time is annoying. I have a nice build log here.
If you are using the Arduino as the serial adapter you have to also connect the RESET pin on Arduino Uno to the GND. This will put the microcontroller into the adapter mode.
Lastly, check the cables again. It’s easy to get something wrong.
I will show you how to erase the flash, which could be the issue of all your troubles. It’s a good point to start and not many sources mention this step. If you are on Windows, you need Python 2.7 installed and the pyserial library (links above). If you are on Linux or a Raspberry Pi, things are a little easier for you – just:
git clone https://github.com/espressif/esptool.git cd esptool
The easiest way to get the ESP8266 into the flash mode is:
- Pull down the GPIO 0 (connect it to GND or DTR, DTR may not work with esptool)
- Start the flash (press Flash button or execute the cmd command)
- Reset the ESP8266 by pulling down the GPIO16/RST pin (touch any of the GND pins with a male end)
Open the folder with esptool and bring up the cmd (right click +shift > Open command window here). Check what port has been assigned to your FDT1232 by bringing up a device manager.
If you are on Linux or Raspberry Pi use:
$ dmesg | grep tty. This should give you a device port name:
/dev/ttyUSB0. Run the command (
python.exe path + esptool.py --port [your com port here] erase_flash)
>>c:\Python27\python esptool.py --port COM5 erase_flash
Execute the line and then immediately reset the ESP. You should see this:
If you on the Linux or Raspberry Pi, you can continue with the esptool, if you are using Windows you can now switch to the NodeMCU flasher tool.
I’ll continue with the esptool as we are one command away from the firmware flash:
>>c:\Python27\python esptool.py --port COM5 write_flash -fm dio -fs 8m 0x00000 C:\node1.bin
The command is built in the following way: (path to python) esptool.py — port [your com port] write_flash -fm [mode: dio for 4MB+, qio for <4Mb]) -fs (flash size 8Mb in my case) 0x00000 (path to bin file)
As before, execute the command and reset the ESP8266 by shorting the GPIO16/RST pin to GND.
If you are using Windows and NodeMCU flash tool the procedure is similar:
Then press flash and reset the ESP8266 by shorting the GPIO16/RST to GND:
The flash will take few moments and you will see the progress:
Once the flash is complete you are ready to upload your Lua files. This is where the ESPlorer comes in handy. Disconnect the GPIO 0, we don’t need it any more. Set the baud rate to 115200 open the COM port and reset the ESP8266 by shorting the GPIO16/RST and GND. Your screen should look like this:
Congratulations. You are nearly done! While this is not a Lua script tutorial I will provide you with a init script. Init is the file that loads on boot. It should contain the vital set up of your script. Avoid entering the entire project into the init file, get the init file to set up your environment, connect to the network and launch the needed scripts. It’s a good idea to create a wait in the init file in case you made a mistake. This way you will have a few seconds to flash new init file even if your ESP8266 ends with a boot loop.
function startup() if file.open("init.lua") == nil then print("init.lua deleted or renamed") else print("Running") file.close("init.lua") dofile("YOUR_SCRIPT.lua") end end signalPin = 3 gpio.mode(signalPin, gpio.OUTPUT) gpio.write(signalPin, gpio.LOW) print("Connecting to WiFi access point...") wifi.setmode(wifi.STATION) wifi.sta.config("YOUR_SSID", "YOUR_PASSWORD") -- wifi.sta.connect() not necessary because config() uses auto-connect=true by default tmr.alarm(1, 1000, 1, function() if wifi.sta.getip() == nil then print("Waiting for IP address...") else tmr.stop(1) print("WiFi connection established, IP address: " .. wifi.sta.getip()) print("You have 10 seconds to abort") print("Waiting...") tmr.alarm(0, 10000, 0, startup) end end)