Making ESP8266 Programming Easier

Updated ESP-0109 Apr 2015 – code fix: A little something here from my guest writer and friend Aidan who’s been working with me on the latest home control successes to follow after this blog!

If you’ve successfully written and uploaded a piece of C code for the ESP8266 in any of its’ guises such as the ESP-01, ESP-03 or ESP-12 modules, then you must also be familiar with the process of holding GPIO0 low then applying a reset to get it into upload mode… it’s a PAIN IN THE REAR!!

If you’ve attempted anything more than a simple flashing light program for the ESP8266, then you have probably become frustrated or bored to tears with this process. This goes especially for anyone who is used to programming with the Arduino IDE, where programming the board is a as simple as pressing the upload sketch button. Well, weep no more, salvation is at hand in the shape of another Atmel processor, the TINY85. This chip is a few tens of pennies from Mouser, RS, Farnell or Ebay and we have used it to handle the programming pins for you.

The process is very simple. When you attempt to upload your complied C program using the Espressif SDK in Windows, or I guess Linux (not yet tried), then the DTR line on most FTDI USB to serial units will be pulsed low then high. This is normally used to reset an Arduino which goes into sketch upload mode for a second or two after reset, although you may not be aware of this once you’ve uploaded a sketch.

In our case, the DTR line is used to reset the TINY85, not the ESP module. When the DTR line goes high again, the TINY85 starts up and monitors the serial line coming from the FTDI. If you have started the program upload process using the Espressif SDKs Upload Tool, then it will be attempting to negotiate a connection with the ESP8266 bootloader and so there will be serial data being sent out.

As soon as the TINY85 sees this attempt to negotiate, it drops the GPIO0 line to zero and issues a reset to the ESP8266. Of course, as soon as the ESP8266 comes out of reset, it sees attempts by the ESPTOOL to communicate and the programming process starts.

The TINY85 continues to monitor the serial data stream and holds the rest line high and the GPIO0 line low until the data stream stops. At this point, GPIO0 is brought high and another reset is issued to the ESP8266. This then triggers the normal start-up process in the ESP8266 and your newly uploaded program runs.

Just to keep things tidy, we then turn the reset control and GPIO0 lines into inputs so that you can do whatever you want with them until the next power up or reset events occur.

The code for the TINY85 is shown below and the diagram of the ESP-DEV3 board is here. So, you can see how the lines are connected across between the TINY85 and the ESP8266 if you want to breadboard this. However, the ESP-DEV3 board has all of this already in place, and we’ve put a few other useful items on as well:-

  • Choice of a 10A relay or an optically isolated mains switch with switch terminals
  • DS18B20 or DHT22 temperature/humidity detector
  • LED Indicator
  • Footprints for three of the most popular mains to 5V power supplies to drop into the PCB – we regulate to 3.3V after this as these supplies can be a little bit noisy electrically.
  • A PCB button which we use for triggering a web setup process. If you hold the button down for about 10 seconds after power up, the ESP module generates a WiFi access point (AP) which you can then connect to using a phone, tablet or WIFI-enabled device capable  of viewing a web page. Using this you can also toggle the relay on and off, read the temperature, set the LED etc. We’ll be developing this theme more in the coming months. A really neat addition is the ability to setup a serial, WS2812 LED output.
  • All of the GPIO lines broken out to a 0.1” connector.

MUCH more on that in the NEXT blog entry. We have been mounting the ESP-12 module onto pins and putting the matching 2mm header sockets onto the ESP-DEV3 PCB so that you can just plug a module in and program it. Then it can be removed and added to another project.

If you want to program the TINY85 chip and you don’t have a programmer, then there are plenty of instructions out there on how to do it using an Arduino board.

So, no excuses then…

#define  F_CPU 1000000

#define RST_ESP 3
#define ESP_PROG 1

void setup()
digitalWrite(RST_ESP, 1); // Clear reset }

void loop()
unsigned long st = millis();

digitalWrite(ESP_PROG, 0); //  program mode digitalWrite(RST_ESP, 0); // Reset ESP module delay(200); digitalWrite(RST_ESP, 1); // Clear reset

//digitalWrite(ESP_PROG, 1); // program mode

st = millis();

for (;;)
    if (!digitalRead(SERIAL_MONITOR)) // Got any serial comms?
        st = millis();
    if (millis() > (st + 3000L))
        break; // Comms finished so exit


digitalWrite(ESP_PROG, 1); // Set ESP program pin high – not program mode digitalWrite(RST_ESP, 0); // Reset ESP module delay(200); digitalWrite(RST_ESP, 1); // Clear reset delay(200); // Slight delay to allow ESP module to see GPIO high at power up

pinMode(ESP_PROG, INPUT); // Allow the ESP pin to be used for other things by making the TINY85 pin an input pinMode(RST_ESP, INPUT); // Allow the ESP pin to be used for other things by making the TINY85 pin an input for (;;); // loop forever

For updates don’t forget if you’re a Facebook fan to follow updates on Facebook


29 thoughts on “Making ESP8266 Programming Easier

  1. Even though this is all over the web (including this site), I’ll reiterate here…
    Test any serial/USB converter you choose to make sure it’s only putting out 3.3V (not 5) on the RX and TX pins or you might nuke your ESP8266. If that happens, you’ll probably spend a day trying to debug that.

    To do this, take your converter and plug in a live USB cable then measure the voltage on the TX and RX pins and they should both be around 3.3V.

  2. I have the ESP-201, Breadboard 3.3 Voltage Regulator and the Arduino Uno.
    I’m trying to solve the following errors when loading a sketch:

    espcomm_sync failed
    espcomm_open failed

    Applying power to the ESP-201 it is connected as follows:
    GIO0 >Grnd Arduino/ Grnd Voltage Regulator
    GIO15 > Grnd Arduino/ Grnd Voltage Regulator
    CHIP_EN > 3.3V Voltage Regulator
    Grnd > Grnd Arduino/ Grnd Voltage Regulator
    TX > RX Arduino
    RX > TX Arduino
    3.3V > 3.3V Voltage Regulator

    1. With this setup, you’re using the Arduino as and RS232-USB converter. Nowadays, those converters cost less than a dollar (try to find a real CP2102 based converter which might cost 1.50 USD). The FTDI based converters should also work but I’ve had one fail. Whenever I shop, I buy two of everything since they’re so cheap and will make debugging a hardware failure easier. With the dedicated USB/Serial converter you get to take the Arduino out of the picture and simplify your setup.

      You should be able to bring up the ESP board out of the box and actuate w/ AT commands on serial port. Note that most of them ship with an initial baud rate setting of 77,400 which is weird. A free serial terminal program called CoolTerm allows you to set any baud rate. There may be others but I know that one works.

      Now, if you want to code for it, I would start simple with the latest Arduino IDE which now allows you to select the ESP8266 directly from the droplist of platform choices and set the baud rate in code. This uses the AiThinker kernel. Once you’re comfortable with that, you’d want to start reading Mr. Scargill’s other posts which explain how to use the kernels from AiThinker and Espressif (the designers of the chip). These are steeper learning curves, but allow lower level access to interrupts and other features. But, if all you want to do is read and write I/O pins and send wifi packets, the Arduino IDE is more than sufficient.

      If any of the information above is not familiar to you, I urge you to do some research online. Everything’s out there. You just might be kissing some frogs. I happen to think this blog has the most accurate information outside of vendor websites.

      Hope this helps.

  3. Thanks Pete,
    I think so too and that’s what worries me. I have to dig deeper. I’m interested in the mesh kernel and the ability to run simple, user space programs to respond to sensors as needed. Maybe eventually GPS self location.

  4. If I use the ESP8266 plug in for the Arduino IDE, will it upload a completely new f/w image or is replace the user space code w/ my sketch?

    1. Ok, so I’m not following this (I understand the reset process)… DTR line connects to GPIO-0 – yup, got that, though you still surely have to disconnect that when you are done if GPIO0 is used as an output? And how does the reset fire – you don’t mention any connection between DTR and the reset? Sorry, not following?

      1. If you need to use gpio0, and disconnecting it after uploading is a pain, then connect DTR to the base of a PNP connected between GPIO0 and GND.
        The reset is triggered when esptool transmits a break. It discharges the RC circuit on the reset pin.

        1. Good idea Ralph. I’m trying to come up with a solution so that a 5v relay connected to GPIO0 doesn’t bounce up and down during all of this… Got some ideas – see latest blog entry…. always happy to hear other ideas (bearing in mind that most handy relays are 5v not 3v3 hence NPN transistor driven, Due to difference between 3v3 ESP and 5v relay, PNP not a good idea for THAT – I see what you are after in the case of the reset. With the terminal program active, GPIO0 would be held high permanently however.

          1. What I’d like to do is write a stage 2 bootloader that waits for flash upload, and if it times out, run the application code.
            That way gpio0&2 can stay hi at boot, and be brought low by the application to do things like control an attached relay.
            I still have to learn some more about the esp8266 before I get there.

  5. Hi Peter,

    I found a way to flash the esp, using the raspberry pi (no need for other devices)

    By installing wiring pi
    connect GPIO (0 and 2 and reset) esp8266 to GPIO raspberry pi (wPi 0 – 2 – 3)

    You can toggle the ports by executing e.g (gpio write 3 0 and gpio write 3 1, which resets the esp)
    gpio write 0 0 sets the esp in flashmode
    by using /usr/src/ –port /dev/ttyAMA0 write_flash 0×00000 firmware/0×00000.bin you can upload the binary

    I wrote a script to automate the process :

    Kind Regards,

    BTW there’s is a ESP-SDK for RPI

    1. Ok… I’m sure there’s a use for that.. over to others? Oh I guess you could do your dev on a Pi – hah !! That’s interesting..

  6. Peter, I do not have an ATTiny to hand, is it possible to use an Arduino as the programming interface rather than the ATTiny?



    1. Well, clearly a 328 (which is all an Arduino is really) would work – wrong size for our board but of course – the principle is very simple as the code demonstrates.

  7. I’m using cp2102 USB to TTL with Windows 8.1, connecting DTR to GPIO0 and RTS (not RST!) to REST in my ESP-12 and I’m flashing with the click of a button… no extra hardware, buttons, capacitors or whatever (sometimes I have to click a second time, but that’s probably just a missing pullup or capacitor somewhere).

    in “C:\Espressif\utils\” :

    def connect(self):
    # RTS = EXT_RSTB
    # DTR = GPIO0
    print ‘Entering bootloader…’

    1. @sfam, Arer you also doing TTL voltage level translation esp for TTL (Tx from CP2102 to Rx of ESP8266) ? I intend to use similar strategy with CH340G

    2. That’s really useful. So we have full control of both of those lines when flashing.. now, if only I know how to get that control when enabling the terminal in Eclipse because for reasons beyond me it is setting DTR low and I can’t seem to find any setup to turn that action off.

    3. I’m not really following that bit about RTS…. so I can see that it would be great to control DTR and hence GPIO0 – but do I take it you are using RTS to turn the ESP on and off (and hence reset) ???

      1. Yes, with both RTS and DTR lines connected to esp8266 (as in , and using the Eclipse devkit for windows, when I hit the FLASH target, the makefile calls esptool which takes care of everything:

        – RTS resets esp8266
        – DTR makes gpio0 LOW when booting
        – esp8266 enters flash mode and new firmware is uploaded
        – esp8266 reboots again in normal mode

        This is as easy as flashing an arduino… and combining this with Sming framework (which already has MQTT !!)… its really fast to develop and debug!

        1. Thats’ great. If you look I’ve just put a new post up on the subject. I have a problem as the Terminal add on for Eclipse seems to want to take the DTR line LOW – it flicks up and down then stays DOWN. We are using a little SMT ATTiny to handle this – and also stop any relay flicking if you happen to be using GPIO0 as a relay output. See latest blog item… I can’t seem to find any way to control the DTR in the terminal program.

  8. I’m impressed. Eagle has a “Mains_PSU_Cheap_Chinese” footprint nowadays. 🙂 🙂

    1. Erm, I don’t think it has – I think we probably added “Mains_PSU_Cheap_Chinese” – referring of course to the very cheap Chinese power supplies – and no they’re not very good but we used the 5v versions and then stuck a 3v3 linear regulator on the output – works a treat.

    1. Just spoke to Aidan – he wants to hang fire until we get the new boards back and tested. Don’t really want to be responsible for others having hassles. Keep looking in.

Comments are closed.