In this article we’ll look at how to program an ESP32 from the Arduino IDE in C or C++. At the time or writing Espressive Systems (who make the ESP32) have not integrated this board fully into the Arduino board manager so it is a more manual process, don’t be put off though as it is extremely straight forward. Just bare in mind that if you are reading this article several months or more after it was written, then there may be another way of doing this. I will try to update this article if that happens but will not always spot right away when this future change happens (if it does).
Intro
The ESP32 is a very powerful MCU for the price (around two cups of quality coffee at time of writing). It is however about twice as expensive as it’s younger brother the ESP8266 and perhaps about 3 times as expensive as an Arduino Nano. So you would choose the other MCU’s over this one if they met all of your requirements. I won’t do a full comparison of the chips here or go through the specs of the ESP32, that will be in another article.
Why use the Arduino IDE?
This IDE is not the easiest to use in my opinion compared to other IDE’s I’ve used over the years. It lacks quite a few essential features for larger projects. At the time of its creation the Atmel processors used in the Arduino were so memory restricted that really large projects were not even really possible. But with newer MCU’s appearing on the scene that have massive resources available large projects are a reality. However many people have had their intro to embedded processors using the Arduino and it’s IDE suite and it is very familiar. In addition it is this very lack of features which gives it a nice simplistic and uncluttered interface (IMO).
Which ESP32?
For hobby projects and for general development work you need a development board as the ESP32 is a tiny surface mount chip. Just go onto your preferred online shop of choice such as ebay, BangGood, AliExpress etc. and search for “ESP32 Development Board”. Several variations will pop up. You are looking for a nice big general board that exposes most if not all of the ESP32 connections to pins on the board. Generally I buy the NodeMCU versions of the Expressive Systems boards. Here’s the one I’m currently working with;
However I do regret my choice, look closely and you will see there is no silk screen markings for what each pin is on the top-side of the board. This makes connecting wires to the right pin when connecting up a right pain and prone to error. The board itself is fine, it’s just this issue. So I’ve ordered another board which is roughly similar but has markings on the top, I will add a picture here when it arrives.
Step 1 USB Driver
We need to be able to talk to the board and for that we need a USB driver. The board I’m using has a CP2102 chip on it that handles the USB serial communications. The driver below is for any CP210x series chips. Here’s the link;
https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers
From this link choose and install the correct driver for you computer system. This chip appears to be the most common amongst the cheap generic development boards available. The only reason I knew it was this chip was by reading the code off the chip that was closest to the micro USB. Other suppliers may use a different chip set, perhaps using the FTDI chip. You will need to research your board if the above driver does not work. Once installed plug in your board and windows ( or whatever OS your using) should do a search and set up the drivers to use your board.
Step 2 The software for the Arduino IDE.
With the driver installed the next step is to install the required files into the Arduino set-up. First go the the link below, click “clone or download” and save as “zip” format to your hard drive.
https://github.com/espressif/arduino-esp32
Step 3 Making a home for the ESP Files
Locate your Arduino folder, if your not sure then launch your Arduino IDE and click
“File->Preferences”
At the top you will see an entry titled “Sketchbook location”, this is the location you need to open on your hard disk. On windows you can copy this file-path and paste it into a filer window to open the actual location.
You should now be in your Arduino main folder with other folders in their such as “libraries” and folder names for any sketches you have created. Create the following folder path within this location
“hardware/espressif/esp32”
That is to say create a folder called “hardware” within your Arduino folder, then open this new “hardware” folder and create another within this called “espressif” and then within the new “espressif” folder create one called esp32.
Open up the zip folder you just downloaded and you should see one folder called “arduino-esp32-master”, open up this and you should then see some folders such as “cores” and “tools”. Select everything and copy them to your “hardware/espressif/esp32” folder.
Step 4 “Getting” and installing the additional files
We’re not yet done downloading files, but to do this we need to run some software from the tools folder mentioned just above. So open up the tools folder within the “hardware/espressif/esp32” folder and run the “get.exe” program. This will open a command window and it will spend a little time getting additional required files. When it has completed you should see some additional folders including this one “xtensa-esp32-elf”.
Step 5 Setting up the IDE to work with your board
Plug in your development board and launch the Arduino IDE. Select the correct COM port and then go to “Tools->Board” and scroll down the list and you should see many supported boards for the ESP32. Which one you choose depends on what development board you have. For myself I have a NodeMCU ESP-32S and I chose the “NodeMCU 32-S” board. Once you’ve selected your board you should try a simple LED blink test. Different development boards connect the LED to different pins, for my board it is pin 2, but some boards also use pin 5, unless you have the same board as mine you should do a search on the internet for your board and find out which pin connects to the on-board LED. Either way here’s the code that works for my board;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
int ledPin = 2; // or perhaps 5 or 9, check your board on internet if doesn't work void setup() { pinMode(ledPin, OUTPUT); Serial.begin(115200); } void loop() { Serial.println("Hello, world!"); digitalWrite(ledPin, HIGH); delay(2000); digitalWrite(ledPin, LOW); delay(2000); } |
Obviously you could just wire up an LED to the pin of your choice if you wish. Take care with resistor values as the ESP32 chip can not supply a lot of current, aim for around 10mA. An that’s it, you should now have a working system. Check out some of the other articles that show you how to use this board with other components.