One can think that internet know everything about everything. Yesterday I've learned the hard way that is does not. It took me better part of evening to find a working way to flash firmware, any firmware, to cheap ESP-01 ESP8266 WiFi modules. There are many tutorials, most of them were just wrong in my case. If I had Windows PC, that might have been simpler. But I don't.

ESP8266 ESP-01 Version 2

So, if you want to flash ESP8266 on any PC (Windows, Mac, Linux) here is what you should do:

Requirements

  • We will use esptool.py Python script, so make sure you have Python 2.7 or newer
  • ESP-01, as well as most generic ESP8266 modules does not have USB-to-Serial converters, so we will need one. CP2102 for example. Very important note: ESP8266 is 3.3V and is not 5V tolerant. So, you will either need 3.3V USB-to-Serial (on data lines, not only power) or logic levels converter between serial and WiFi module
  • ESP8266 is 3.3V device and requires quite a lot of power. More that 200mA. Voltage regulator inside usb to serial will not do. You can try powering it from 2 AA batteries, but using external voltage regulator might be a better option. LM7833 are cheap and provide more than enough current. Note about LM7833: it has 2V dropdown voltage, so to provide stable 3.3V it required more than 5.3V on input. USB power might not be enough
  • 1kOhm resistor
  • some cables

Flasher device

ESP8266 ESP-01 Pinout

To flash ESP8266 it has to enter flashing mode first. It has to be done by pulling GPIO0 to the ground and then powering it up. And it has to stay pulled down all the time. Some tutorials suggest that GPIO0 has to be pulled down only during startup, or some 2 button device can be made, but all those tries failed in my case. So, to enter flashing mode you will have to:

  • power down
  • connect GPIO0 to GND
  • power up
  • flash device over serial port
  • power down
  • disconnect GPIO0 from GND

I have build small device on universal PCB that has voltage regulator, jumper to put ESP8266 into flashing mode and both GPIOs on separate connectors. Can be used to flash and as development board.

ESP8266 programming device

Software

When hardware part is done, time to setup software. If you have Python and GIT, we can start:

  1. Clone esptool.py by typing [email protected]:themadinventor/esptool.git
  2. cd esptool
  3. python setup.py install
  4. confirm USB-to-serial com port. In my case (Mac OS X) it was /dev/tty.usbserial-A50285BI
  5. Get new firmware files

Flashing

  1. Disconnect power
  2. Switch FLASH_SW to pull GPIO0 to the ground
  3. Connect USB to serial converter to PC and ESP8266. Do not connect power from it, only TX, RX and GND
  4. Apply power to voltage regulator. Since LM7833 has 2V min. voltage drop, 6V is recommended. I was able to use 5V (3V on ESP8266) but had some problem with stability
  5. Run esptool.py by python esptool.py -p /dev/tty.usbserial-A50285BI write_flash <ADDR1> <FILE1> <ADDR2> <FILE2> ...
    1. Replace /dev/tty.usbserial-A50285BI with yours COM port. Windows users will use COM3, COM4 and similar, Mac and Linux users will use /dev/tty.usbserial- or similar
    2. Since ESP8266 firmware very often splitted into several files, each of them has to be placed under separate memory address. For example 0x00000 0x00000.bin 0x40000 0x40000.bin. Always refer to firmware documentation to know which files should be flashed where
  6. Flashing should begin immediately
  7. When finished, power down ESP8266 and switch FLASH_SW so GPIO0 is not pulled to GND anymore

Troubleshooting

Everything can go wrong, here are some common problems I've encountered

  1. esptool.pl "Failed to connect to ESP8266". There are following possibilities:
    1. ESP8266 is not in flashing mode: check again if GPIO0 is connected to ground, cycle power
    2. USB to serial converter is not working or not connected properly. RX and TX lines should be crossed, so converter's TX connects to ESP8266 RX and converter's RX connects to ESP8266 TX. Drivers for converter installed and configured? Proper COM port chosen?
    3. In some cases it might be required to manually select flashing speed. Try with -b 9600 and -b 115200
  2. "Invalid head of packet". This can happen if power quality is not good enough: voltage regulator was unable to satisfy momentary higher demand and voltage drop occurred. There are two solutions:
    1. Provide better power. Perhaps voltage regulator's input voltage is too low?
    2. Modify esptool to lower data packet size. To do this:
      1. Open espytool.py with text editor
      2. Replace ESP_RAM_BLOCK = 0x1800 with ``ESP_RAM_BLOCK = 0x180
      3. Replace ESP_FLASH_BLOCK = 0x400 with ESP_FLASH_BLOCK = 0x40
      4. Save and try flashing again