MULTITHREADING
Build a flashy dice roller with NeoPixels
Les Pounder is learning to multitask but we think he misunderstood the instructions.
The £4 Raspberry Pi Pico is powered by a dualcore ARM CPU, and in this tutorial we will learn how to run two threads of MicroPython code independent of each other. The code in core 0 will wait for a user to press a button, and when they do, the code will choose a random number to emulate a D20 die roll. If the user is unsure what to do, a simple instruction will scroll across a seven-segment display, a TM1637 connected to the Raspberry Pi Pico using I2C.
The second core will run a light show, designed to entice the user to try our project. The light show is powered by a stick of WS2812B RGB LEDs, commonly known by the Adafruit brand of NeoPixels. These RGB LEDs require perfect timing in order to display the correct colour, so dedicating an entire CPU core to them means our number generator can get on with making random numbers. Both of these cores are running concurrently, and they can talk to each other, but for this tutorial we have intentionally kept it simple.
Build the hardware
The build is split into three sections. The input, a push button, is connected to GPIO16 and GND. The first output, the TM1637 display, is connected to 3V, GND and two I2C pins (green GP27 and yellow GP26 wires), which provide the data connection from the Pico to the display. Lastly is an output for the NeoPixels. This uses any GND pin on the Pico, 3V, and GP15 is connected to Data In of the NeoPixels. Please refer to the highresolution diagram in the download for more details.