Sep 172017

Every now and again, I get the bug to build something. Lately, I’ve been following MicroPython and the microcontrollers that it supports. The new hotness is the Expressif ESP32 chip. These are available from a number of different sources, many supplying a breakout board. Prices are all over the place from 20+ to 8+ depending on where you shop and how patient you are.

I went with the dev board from Expressif. I got a pair of them for about 15 each from Amazon. I like the trade off of delivery time, supplier and cost. You can see and order it here: 2 PACK Espressif ESP32 ESP32-DEVKITC inc ESP-WROOM-32 soldered dils CE FCC Rev 1 Silicon

$  -p /dev/ttyUSB0 flash_id v2.1
Detecting chip type... ESP32
Chip is ESP32D0WDQ6 (revision 1)
Uploading stub...
Running stub...
Stub running...
Manufacturer: c8
Device: 4016
Detected flash size: 4MB
Hard resetting...

With just a bit of searching, you’ll find that you need the latest Micropython for ESP32 and the

pip install esptool

. Then after you connect your Board to your computer, you can load up the MicroPython firmware. --chip esp32 --port /dev/ttyUSB0 write_flash -z 0x1000 images/esp32-20170916-v1.9.2-272-g0d183d7f.bin

Now in the world of microcontrollers, blinking an LED is the “Hello World” program. However, the boards I purchased only had an LED that lit if the board was receiving power. No other LEDs on the board connected to a GPIO pin like some other breakout boards. It does have 2 switches, one of which, Switch 1(SW1) is connected to the GPIO0 pin.
ESP32 In the image, SW1 is the button on the top right, labeled boot.

So I write some code to figure out the initial state of GPIO and then toggle the button a couple times.

""" - look at initial state of GPIO0 and then record it toggling"""
from machine import Pin

def main():
    # setup
    sw1p0 = Pin(0, Pin.IN)  # switch sw1 connected to logical Pin0
    state_changes = 0       # loop control
    prior_value = sw1p0.value() # sw1p0 previous state, initially unknown
    print("sw1p0 initial value is %s" % prior_value) # report initial state

    # main loop
    while state_changes < 4:    # press, release, press, release
        new_value = sw1p0.value()   # cache value, as inputs can change
        if new_value != prior_value:    # has state changed?
            print('sw1p0 was %s is now %s' % (prior_value, new_value))
            prior_value = new_value # update prior_value for next loop
            state_changes += 1

if __name__ == '__main__':

I did sort some of this out using the serial REPL, but for this post, I wrote up a script to demonstrate my findings.

Using the adafruit ampy tool, we’ll run the code.

pip install adafruit-ampy

Note: you will need to press sw1 twice before you see anything after the ampy cmd.

$ ampy -p /dev/ttyUSB0 run 
sw1p0 initial value is 1
sw1p0 was 1 is now 0
sw1p0 was 0 is now 1
sw1p0 was 1 is now 0
sw1p0 was 0 is now 1

As you can see from the results, the initial state of GPIO0 was high(or 1). When sw1 is pressed/closed it goes low(0) and goes back high(1) when it is released/open. If you look at the board schematic, in the Switch Button section, you’ll see that when sw1 is closed, it shorts out GPIO0 to ground. This would indicate that you were pulling it low from a high state. So our observations match the schematic.

If you look at the schematic, you will see a capacitor from R3 to Ground that is used to debounce the switch. You should assume that all mechanical switches bounce and that bouncing needs to be dealt with in either the circuit or code. Life is much easier if you debounce the circuit with hardware.


  1. Success! While we don’t have an onboard LED to blink, we can do something with the board without extraneous components, a Hello World app.
  2. The app is very naive since it uses polling to monitor state changes and spins in a tight loop most of the time. Often the reason for using a microprocessor has a power element to it. Sitting and spinning would be counter to a goal of low power usage.
  3. We covered a lot of ground in this article, skipping or very lightly going over how to load MicroPython and the other tools I used. There are lots of very good resources for them on the interwebs.
  4. If you liked this article, and you want to get an ESP32 board, you can use the Amazon affiliate link above as an expression of your support.

In an upcoming article, I’ll rework the example to be more energy conscious by using an interrupt to signal the state change.

May the Zen of Python be with you!

 Posted by at 10:53 am

Sorry, the comment form is closed at this time.