MicroPython + ESP32 + Eurorack: Part 1 :: setting up!

Update Part 2 is out: MicroPython ESP32 Synth Module: Part 2 :: Receiving Gates

Hello and welcome, in this series we will learn how to build eurorack modules using Python (MicroPython) and ESP32 boards (PyBoard).

In this article we will learn how to:

  • Flash the ESP32 chip with MicroPython
  • Connect through a serial USB connection and execute some python code on the board
  • Uploading files to the board’s filesystem
  • Load a program into the board to flash the built in LED on board

Basically this is me, documenting the process of learning to use micropython to build my own module, so take everything here with a grain of salt, as it is a result of my own research, please also feel free to comment and say your opinion, correct, or suggest projects for the next parts of the series.

so let’s get started!

About the board

I’m using the ESP32 WROOM-32 that you can find easily googling it

https://www.mouser.de/ProductDetail/Espressif-Systems/ESP32-WROOM-32

You will find the datasheet here:

https://joy-it.net/files/files/Produkte/SBC-NodeMCU-ESP32/SBC-NodeMCU-ESP32-Manual-20200320.pdf

Alternatively you can use this board:

https://www.espressif.com/sites/default/files/documentation/esp32-wroom-32_datasheet_en.pdf

Model: NodeMCU ESP32

Specifications:

  • Tensilica LX6 Dual-Core
  • clock frequency: 240Mhz
  • 448 KBytes ROM for booting and core functions.
  • 512 KBytes on-chip SRAM.
  • 4MB of RAM (there are 16MB variants)
  • WiFi: 802.11 b/g/n/d/e/i/k/r (802.11n up to 150 Mbps)
  • Wi-Fi mode Station/softAP/SoftAP+station/P2P
  • Bluetooth: Classic / LE
  • Interfaces: UART(Serial) / I2C / SPI / DAC / ADC
  • Operating Volatage: 3,3V (operable via 5V-microUSB)

Board Pinout

Installation

I’m using mac, but if you are running linux or windows please follow this guide:

https://docs.micropython.org/en/latest/esp32/tutorial/intro.html

Get the firmware up and running

To get the firmware we are going to head over to the micropython website and copy the link to the latest stable firmware available

So let’s head there:

Next we right-click and copy the link location, at the time of writing this is the latest stable one:

GENERIC : esp32-idf3–20191220-v1.12.bin

# get firmware
wget https://micropython.org/resources/firmware/esp32-idf3-20191220-v1.12.bin

Next we need to find out the port number for the ESP32 board, so we make sure the board is disconnected and we run the following:

ls /dev/ | grep usb

here we list all what is in /dev/ that contains the word usb, we should see something like

crw-rw-rw- 1 root wheel 18, 5 Jul 8 02:49 cu.usbmodem56748201

Now we connect the board and run the same command again and look for the new device that have been listed

ls /dev/ | grep usb

we should see something like:

crw-rw-rw-   1 root  wheel      18,   5 Jul  8 02:49 cu.usbmodem56748201
crw-rw-rw- 1 root wheel 18, 3 Jul 8 13:26 cu.usbserial-0001
crw-rw-rw- 1 root wheel 18, 4 Jul 8 02:49 tty.usbmodem56748201
crw-rw-rw- 1 root wheel 18, 2 Jul 8 01:47 tty.usbserial-0001

Now we know that /dev/cu.usbserial-0001 is our port in question, we need to note that because we are going to need it in the next steps

Flashing the MicroPython Firmware

Now I have my ESP32 on port /dev/cu.usbserial-0001, and then deploy the new firmware, but first we need to install the esptool

pip3 install esptool

erase the current firmware:

# erase flash
esptool.py — port /dev/cu.usbserial-0001 erase_flash

# flash with micropython

esptool.py  — chip esp32 — port /dev/cu.usbserial-0001 — baud 460800 write_flash -z 0x1000 esp32-idf3–20191220-v1.12.bin  — verify

Please change the bold fields with the respected, port and firmware file and then press enter, you should see something like

esptool.py v2.8
Serial port /dev/cu.usbserial-0001
Connecting........_____....._____....._____....._____....._____.
Chip is ESP32D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: 24:6f:28:09:ef:0c
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Auto-detected Flash size: 4MB
Compressed 1247280 bytes to 787794...
Wrote 1247280 bytes (787794 compressed) at 0x00001000 in 19.3 seconds (effective 516.0 kbit/s)...
Hash of data verified.
Leaving...
Verifying just-written flash...
(This option is deprecated, flash contents are now always read back after flashing.)
Verifying 0x130830 (1247280) bytes @ 0x00001000 in flash against esp32-idf3-20191220-v1.12.bin...
-- verify OK (digest matched)
Hard resetting via RTS pin...

Connecting to the board over serial USB

Next we will install picocom which is a program used to connect to the board using a serial connection over usb.

We are going to use homebrew to install picocom, if you don’t have homebrew installed, please checkout the installation guide.

brew update && brew install picocom

So first let’s connect to python prompt (REPL)

picocom /dev/cu.usbserial-0001 -b115200

here we just run picocom with our port number the specified serial baudrate 115200

we should see something like

picocom v3.1port is : /dev/cu.usbserial-0001
flowcontrol : none
baudrate is : 115200
parity is : none
databits are : 8
stopbits are : 1
escape is : C-a
local echo is : no
noinit is : no
noreset is : no
hangup is : no
nolock is : no
send_cmd is : sz -vv
receive_cmd is : rz -vv -E
imap is :
omap is :
emap is : crcrlf,delbs,
logfile is : none
initstring : none
exit_after is : not set
exit is : no
Type [C-a] [C-h] to see available commands
Terminal ready
T1C�,k�E���0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:4844
load:0x40078000,len:9656
load:0x40080400,len:6216
entry 0x400806f0
I (457) cpu_start: Pro cpu up.
I (457) cpu_start: Application information:
I (457) cpu_start: Compile time: Dec 20 2019 07:50:41
I (461) cpu_start: ELF file SHA256: 0000000000000000…
I (467) cpu_start: ESP-IDF: v3.3
I (471) cpu_start: Starting app cpu, entry point is 0x40083600
I (0) cpu_start: App cpu up.
I (482) heap_init: Initializing. RAM available for dynamic allocation:
I (489) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (495) heap_init: At 3FFBA488 len 00025B78 (150 KiB): DRAM
I (501) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (507) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (514) heap_init: At 40092D6C len 0000D294 (52 KiB): IRAM
I (520) cpu_start: Pro cpu start user code
I (203) cpu_start: Chip Revision: 1
W (203) cpu_start: Chip revision is higher than the one configured in menuconfig. Suggest to upgrade it.
I (206) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
MicroPython v1.12 on 2019–12–20; ESP32 module with ESP32
Type “help()” for more information.
>>>

You are probably familiar with this command prompt, we can just execute any python code at this point, running on the board.

Blinking an LED

First we will use the python prompt to blink the built in LED, for my board it is pin no. 2, for other boards you can try pin no. 13 or google your board led pin number, if you are using another board

Now we’re ready to blink the LED, for this we use the machine’s Pin class to control a GPIO pin

from machine import Pinp = Pin(2, Pin.OUT)
p.value(1)
p.value(0)

Here we set the pin no. 2 to pinout using the built in constant, and then we set the value on/off of that pin number.

You should see the LED on the board turning on and off.

now we are done, so let’sexit picocom using the following key combinations
ctrl+A
ctrl+x

>>>
Terminating…
Thanks for using picocom

Uploading python programs to the ESP32 board

Now that used the prompt, we can move ahead and start uploading programs to the board

the upload script will load boot.py, then whatever we define in the includes
for this example we will upload also main.py

upload.py
- uploads the currently defined files to the ESP32 board’s filesystem

boot.py
- bootstraps any modules we need to bootstrap, for instance wifi or bluetooth

main.py
- our program to be loaded by the board

so first we create upload.py, use your favourite editor, I’m just going to use vim, but you can use nano, sublime text…etc

vim upload.py

Setting up the upload script

open upload.py

change the port variable to whatever port on your machine, like the following

port = ‘/dev/cu.usbserial-0001’

now that we have our upload.py, we can move ahead and create boot.py

And here you can see the list of includes, that means that all these files will be copied to our board and we can use them later through the serial connection or import them through our main module.

Loading scripts on boot of the board

We’ll just put some debug strings on the 2 remaining files, just to get started, later we can use them to actually do things

vim boot.py

print('RUN: boot.py')

vim main.py

print('RUN: main.py')

Uploading scripts to the board

we are all set to upload the files to the board

we just need to run the following:

python3 upload.py

output:

REPL-Ace Uploader - Copyright (c) 2017 Clayton Darwin
ClaytonDarwin.com - claytondarwin@gmail.com
Port: /dev/cu.usbserial-0001 Speed: 115200 Timeout: 0.1
System Root: /Users/omar/workspace/python/mpy/esp32_setup
Exclude: ['upload.py', 'archive', 'thumbs', 'video']
Include: ['led.py', 'boot.py', 'main.py']
STARTING UPLOAD...
,wp_drv:0x00
mode:DIO,�-"���2
load:0x3fff0018,len:4
load:0x3fff001c,len:4844
load:0x40078000,len:9656
load:0x40080400,len:6216
entry 0x400806f0
I (457) cpu_start: Pro cpu up.
I (457) cpu_start: Application information:
I (457) cpu_start: Compile time: Dec 20 2019 07:50:41
I (461) cpu_start: ELF file SHA256: 0000000000000000...
I (467) cpu_start: ESP-IDF: v3.3
I (471) cpu_start: Starting app cpu, entry point is 0x40083600
I (0) cpu_start: App cpu up.
I (482) heap_init: Initializing. RAM available for dynamic allocation:
I (489) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (495) heap_init: At 3FFBA488 len 00025B78 (150 KiB): DRAM
I (501) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (507) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (514) heap_init: At 40092D6C len 0000D294 (52 KiB): IRAM
I (520) cpu_start: Pro cpu start user code
I (203) cpu_start: Chip Revision: 1
W (203) cpu_start: Chip revision is higher than the one configured in menuconfig. Suggest to upgrade it.
I (207) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
RUN: boot.py
RUN: main.py

Running code on the board

So let’s first connect and load the led module manually, then we are going to upload the program to run on boot later.

picocom /dev/cu.usbserial-0001 -b115200>>> import led
>>> led.blink(2) # here use your pin number

now your LED should blink and you should see the following on your serial terminal:


pin 2 ON
pin 2 OFF
pin 2 ON
pin 2 OFF
pin 2 ON
pin 2 OFF
pin 2 ON
.....

Flashing a program to the board

Now in order to blink on boot, we will do exactly the same but write it on the main module (main.py)

so open up the main.py, and we are going to write the following

import ledled.blink(2) # here use your pin number

and then we run upload. again to upload the new files

python3 upload.py

Now your board shall start blinking after booting up.

That’s it for this tutorial…

To summarize:
- We have flashed the MicroPython firmware on the device
- We learned how to run python interactive shell into the device
- And how to upload and run scripts after boot of the device

Next on this series, we will learn how to create a simple LFO or a Low-pass Gate (LPG) using MicroPython and our ESP32/Pyboard

Hope this was useful for you,
Best

References:

ESP32 w-room 32 board Pin-out:
https://lastminuteengineers.com/esp32-arduino-ide-tutorial/

The ESP32 board I am using:
https://joy-it.net/en/products/SBC-NodeMCU-ESP32

A video explaining how to setup the ESP32:
https://www.youtube.com/watch?v=QopRAwUP5ds

ESP32 Data Sheet
https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf

Quick reference for the ESP32

https://docs.micropython.org/en/latest/esp32/quickref.html

--

--

--

A Series of Tutorials using MicroPython with ESP32 to design and build synthesizer modules specialized in eurorack format

Recommended from Medium

Emacs and PlantUML for UML Diagrams — Academic Tools

How to Remember That Door Code

Explaining Object Orientated Ruby Concepts: class methods, instance methods and self

Oracle BPM Interview Questions

Creating accessible styled checkboxes

Build the Laravel Application as Pro -for Beginner — Part 1

We were platinum sponsor of Techorama 2019!

How To Hire A Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Omar Shaban

Omar Shaban

Omar Shaban is a software engineer, socialpreneur, and sound artist with a passion for programming and open source. He began programming as a youngster in 2002.

More from Medium

IoT in railways with insights from a top expert

Enterprise in the 5G Era

Enterprise Vendor Audits and the Growth of SAM.

Using AWS Macie to Identify and Protect Sensitive Data at Scale