If you are not familiar with ESP8266 microcontroller, I really feel you have been missing out! These things are incredible: they are cheap, powerful and best of all have built-in WiFi!
ESP8266 started their journey out as a WiFi add on board for more traditional Arduino boards but shortly after, the community realized the power of them and added support to be able to program directly with the Arduino IDE.
These days if you are looking to use an ESP8266 for your project I would recommend using a development board such as the Wemos D1 Mini* ($2.50 delivered!!) or Adafruit Feather Huzzah. These boards have all the usable pins broken out, are easily programmable via their micro USB connector and have a built in 3.3V regulator.
But what if you want to use an ESP8266 in a project with a custom PCB? The easiest way is to use an ESP12 module, and in this tutorial I'll show you a couple of simple ways of programming them.
Check Out the Video!Here is a video with all the info contained in the tutorial if you like to check it out.On my channel I do a lot of work with ESP8266s so my channel in general maybe worth checking out if that sort of stuff interests you!
Pre-Step: Setup for Programming the ESP8266If you have never used the ESP8266 or Arduino before, we will need to a little bit of software setup. I have a dedicated video for this. It's only 5 minutes long and goes through everything you need to setup.
Before you move to the next section you should be able to upload a simple sketch to an ESP8266 (such as the blink example)
Method 1: "Frog" Pin ProgrammerThis is probably the easiest way to program an ESP12 module. You basically just pop the module into the board and then it behaves exactly like one of the development boards mentioned earlier. I selected the Wemos D1 Mini board from the boards drop down when programming.There downsides to this method are:
- The programmer is a little bit expensive if you are only making a few boards.
- Boards can only be programmed this way before they are soldered to your PCB
The one I bought is from Aliexpress, but I believe the original designer is a user on Tindie called Fred. They were sold out at the time so I ended up going with the Aliexpress one, but I'll link to both.
Method 2: Using Basically Any USB to Serial ConverterFor the next method we are going to manually put the ESP module into programming mode and then use basically any USB to serial converter to program the ESP8266, even this PL2303 one* that costs 50c delivered works!
Basic Operation:
Before we get started on programming we first need to take a look at the what external components the ESP8266 needs to run. For normal operation an ESP-12 module needs the following
- EN, RST and GPIO 0 needs to be pulled high using a 10K resistor
- GPIO 15 needs to be pulled to Ground using a 10K resistor
- 3.3V power supply capable of about 250mA of current (A cap between VCC and GND is recommended)
With the above configuration the ESP8266 when powered on will be in Normal operating mode, so it will run whatever sketches you have programmed to it. You can buy breakout boards for ESP-12 modules* that are really useful for testing out the different setups. The modules have the resistors setup for the EN and GPIO 15 pin, so you will still need to add the pull-up resistor for RST and GPIO 0,
Enabling Programming mode:
To get it into programming mode, GPIO 0 needs to be pulled low when the ESP is starting up. The easiest way to do this is to add buttons to GPIO 0 and the RST pin that connect to ground when pressed. Then to enable flash mode, you simply
- Hold down the GPIO 0 button
- Press the RST button
- Then let go of both buttons
You don't need to perform this sequence at any particular time during the upload process or anything, once the ESP is in programming mode it will stay there til the next reset, so just perform the steps any time before uploading.
Programming with a USB to serial adapter:
Enabling program mode is only half the battle, now we need to actually program the module. Most USB to serial adapters can not provide enough current to the ESP8266 so it's recommended that you power the ESP8266 using an external 3.3V source.
To wire up the programmer you need to connect the following pins (also shown in an image above):
- Connect TX of the programmer to RX of the ESP8266 (Not a typo, the connections are reversed)
- Connect RX of the programmer to TX of the ESP8266
- Connect Ground of the programmer to Ground of the ESP8266
To upload your sketch, do the following steps:
- Select the port number of your Serial adapter (Tools->Port)
- Enable programming mode on your ESP8266 as described above
- Click the upload button. (If it fails double check your wiring and try resting your board into programming mode again)
- Click the reset button when the upload has finished
Here are the board settings I used when uploading using this method:
- Board: Generic ESP8266 Module
- Flash Mode: DIO
- Flash Size: 4M (3M Spiffs)
- Reset Method: ck
- Flash Frequency: 40MHz
- Upload Speed: 115200
The last thing to note is the LED_BUILIN definition for a generic ESP8266 module points to pin 1, but the built in LED of a ESP12 module is connected to pin 2 (It's active low too just as an FYI). So if you are testing out a blink sketch you will need to use the number 2 rather than LED_BUILTIN
* = Affiliate Link
Method 3: Auto Resetting Programmer Using a NodeMCUIf you used any ESP development boards you will have noticed that basically all of them do not require you to manually enable programming mode using the buttons, so how do they do that?
Most USB to serial chips have additional pins that output signals at different stages of the upload process and with the use of some external circuitry it's possible to trigger the low of the GPIO 0 and the resets required. RuiMoteiro has an tutorial on this topic using an FTDI board and ESP8266.
But I think there is a simpler approach to this, and what's even better is there is a good chance you already have everything you need to do it! You can use an ESP8266 development board such as a NodeMCU as the programmer*.
Whats the advantage of using a NodeMCU?
The main advantages of using the NodeMCU as a programmer are the following:
- You can power the ESP8266 directly from 3V pins of the NodeMCU
- Will handle the auto-resetting/enabling of programming mode
- Costs about $2.50 delivered (and can be used as regular dev board when not programming others!)
You can use this same approach with any dev board the exposes the chip enable pin of the ESP8266.
How does it work?
I got the idea of this from a YouTuber called Mika Kurkela, in his video he was using the NodeMCU to program an ESP-01 module, but we can use the same idea for the ESP12 module.
Basically we are going to disable the ESP8266 on the NodeMCU by forcing the EN pin low, this will prevent the ESP8266 module on the NodeMCU from booting. We are then going to going to connect all the relevant pins of the NodeMCU board to our external ESP8266.
Wiring it up
To wire it up, you will need the standard ESP8266 wiring as shown in the previous step and then to add the following connections (also shown in the image above):
- Connect 3V of the NodeMCU to VCC of the ESP8266
- Connect GND to GND
- Connect TX of the NodeMCU to TX of the ESP8266 (This is different than the previous step)
- Connect RX of the NodeMCU to RX of the ESP8266
- Connect D3 of the NodeMCU to GPIO 0 of the ESP8266 (D3 is GPIO 0 of the ESP8266)
- Connect RST of the NodeMCU to RST of the ESP8266
- Connect EN of the NodeMCU to GND
Uploading a sketch
Once you have the ESP8266 wired up, you need to do the following:
- Select the port number of the NodeMCU (Tools->Port)
- Select the Board type "NodeMCU 1.0 (ESP12-E Module)"
- Click upload
And that's it! It will automatically enable programming mode and will automatically reset when finished uploading so it will start executing the sketch.
Using this in your board designs
In order to use this method in your board designs, you simply need to break out the following pins:
- Ground
- GPIO 0
- VCC
- TX
- RX
- RST
And when you want to program your boards, wire it up as above.
Hopefully you find this tutorial useful, if you have any questions please feel free to ask!
* = Affiliate Links
Comments