The ESP32 is a very inexpensive versatile chip designed for efficient Wi-Fi and Bluetooth including BLE. It has a powerful 240MHz dual core microcontroller with 520K of SRAM. It is designed for mobile devices so it has ultra-low power consumption. The ESP32 chip has 32 GPIO pins with support for I2C, I2S, SPI and UART. In addition it has multiple analog to digital channels and digital to analog, hardware accelerated encryption, pulse width modulation, capacitance touch interfaces and a lot more.
One of the main reasons I like the ESP32 and the ESP8266 is that you can program them in Python. More specifically, MicroPython, which is a very efficient streamlined version of the Python 3 programming language that is optimized to run on microcontrollers, such as the ESP32.
The Raspberry Pi is great for many projects, but it is a full-fledged computer with a Linux operating system. For simple repetitive task such as monitoring sensors and controlling relays it’s often easier, cheaper, more reliable and more efficient to use a microcontroller such as an Arduino which boots up instantly and just runs your program. Arduinos such as the Uno are usually programmed using a set of the C language using the Arduino IDE, which can also be used to program the ESP32. Instead of C, this tutorial focuses on MicroPython, which works great when integrating with a Raspberry Pi because you can use Python on both devices for all the code.
To get started the MicroPython firmware needs to be uploaded to the ESP32. This can be done with a simple micro-USB cable which provides a 2 way serial interface between the Pi and the ESP32. It also provides 5 volts to power the ESP32.
Before uploading the firmware, please make sure the Pi is up-to-date. From a terminal, type the following:
I recommend you use the latest version of Raspbian to ensure you have all the necessary software.
A utility called ESPTool is used to upload the MicroPython firmware to the ESP32. It is installed using pip3 to target Python3:
Next the USB port needs to be determined using dmesg | grep ttyUSB. The results should show a CP210x UART to USB bridge attached to a USB port. Below the port is ttyUSB0.
Some boards need to be put in programming mode using the push buttons on the board before you can use the ESPtool. However, I’ve found that it’s not necessary on the ESP32 (at least not on mine.) The ESPTool flash_id command can be used to ensure everything is working. Specify the port with double dash port followed by /dev/ and the port name returned by the dmesg command above.
The query returns basic info such as the chip type (ESP32) and flash size (4 MB). Before uploading the firmware it is recommended to erase the ESP32, which can also be done with the ESPTool:
Once the firmware is downloaded it can be uploaded with the ESPTool using the write_flash command. The hex value indicates the starting memory address and it is followed by the path to the downloaded MicroPython ESP32 firmware file.
UPDATE: the starting address now needs to be 0x1000 instead of zero.
MicroPython boards have a Read-Eval-Print Loop (REPL) which is a simple interactive programming environment. It’s similar to the Python Idle shell. On the ESP32, the REPL is accessed using a serial connection. Other boards like the ESP8266, which is the predecessor to the ESP32, also have a Web REPL which allows you to manage and program them over the web. Currently, this feature has not yet been implemented on the ESP32 along with many other features, but new stuff is being added daily so there might be more options in the future. For now we’ll stick to serial. The same USB cable that was used to upload the ESP32 firmware can also be used to access the REPL. Any serial program such as PuTTY or Screen should work with the REPL. However, these programs won’t let you manage the ESP32 file system which can be used to store your programs. Actually you can access some file commands by writing Python code, but this is cumbersome and still does not provide for file transfers between the ESP32 and the Pi. Ideally you want a program that provides a REPL terminal and also can perform file management. I’ve tried several programs and currently my favorite is rshell. It can be installed using pip:
This simple program will run on the Raspberry Pi and allow you to access the REPL terminal on the ESP32. It also provides file management to transfer and manipulate files on both the Pi and the ESP32. To start rshell, type rshell and specify 30 for the buffer size and your USB port:
The first prompt can be used to execute file commands. For example, type boards to get a list of connected boards. The first line returned should start with the board ID which for my ESP32 is pyboard.
The boot.py file is automatically run at startup and contains low level code to set up the board and finish booting. You typically don’t want to edit it. However, you can add a file called main.py if you need your own code to run at start up after the boot.py.
Type repl to open the MicroPython programing environment. The terminal will now accept Python code. For example, print hello world, outputs hello world.
Next, let’s upload a MicroPython program to control a NeoPixel LED on the ESP32. A NeoPixel is an RGB led with a built in chip (usually WS2812B or SK6812) to control color and brightness. It can be controlled using a single data line. They are often sold in strips with multiple LED’s, but for this demo a single 8mm LED is used. These single 8mm LED’s can also be daisy-chained using a single data line.
It is very easy to connect a NeoPixel to the ESP32. A 5 volt pin provides power. A 1N4004 diode is used to drop the voltage from 5 volts to 4.3 volts which allows the NeoPixel to read the ESP32’s 3.3V output. The ESP32 3.3V data output needs to be at least 70% of the NeoPixel’s supply voltage. At 5V it’s outside this range at 66% but at 4.3V it’s good to go at 76%. A ground from the ESP32 is connected to the ground on the NeoPixel. GPIO13 is connected to the data in. Please note that a single NeoPixel can use up to 60mA at full brightness. Therefore, if you want to run a strip of LED’s, please use an external power source to avoid damaging the board and it would be preferable to use a level shifter on the data line instead of the diode approach.
Here’s the MicroPython code to control the NeoPixel. The Pin library is very similar to the Raspberry Pi GPIO library. It lets you reference and control the ESP32 GPIO pins. The NeoPixel library drives NeoPixel LED’s and strips. Since MicroPython is a subset of Python, it doesn’t have all the standard libraries. For example, an easy way to cycle RGB LED colors is to vary the hue between 0 and 1. This can be done with the hsv_to_rgb method which is part of the Python colorsys library. It’s not included with MicroPython, but I can just add the function I need to the code. HSV is a common cylindrical-coordinate representation of points in an RGB color model. The hsv_to_rgb function simply converts an HSV value (hue, saturation and brightness) to an RGB value (red, green, blue).
A NeoPixel is instantiated, on GPIO pin 13. One indicates the number of LED’s. Spectrum is a list to represent 2048 colors. The first range is zero to 2047 and the 2nd is 2047 to zero. A try statement to catch errors wraps the main while loop which is infinite. A for loop cycles through the color spectrum range (0 to 2047 and back to 0). Hue is set to a value from 0 to 1 (divided into 2048 steps.) NP refers to the first NeoPixel LED. In this case, there is only one. If we were using a strip we could reference other LED’s using other numbers. The hsv_to_rgb function is passed the hue, 1 for saturation and brightness is set to 15%. The np.write method sets the NeoPixel LED to the previously specified color. The loop sleeps for ten milliseconds and continues. Except is used to gracefully exit the program on ctrl-C. Finally ensures the LED is turned off on exit by setting red, green and blue to zeros.
Back in rshell type ctrl-X if you are still in the REPL to exit back to the main rshell terminal. Navigate to the folder where the program is saved. You can use cd and ls just like in a regular terminal. Then use cp to copy the program file to the ESP32 which is specified with /pyboard:
Type repl to return the REPL and type import rgb to run the rgb.py program.
Here’s a pic of the breadboard with an ESP32 and a NeoPixel.
Part 2 of this tutorial demonstrates MQTT protocol.
Adafruit NeoPixel UberGuide
Sipping Power with NeoPixels
MicroPython Forum a very helpful community to answer MicroPython related questions.
MicroPython-lib a great source for additional MicroPython libraries.
NeoPixel RGB LED Python Code from Video – Released 7/16/2017