A Simple Raspberry Pi Tiny2040 Dice Project

by SeanCarr451 in Circuits > Microcontrollers

547 Views, 0 Favorites, 0 Comments

A Simple Raspberry Pi Tiny2040 Dice Project

IMG_20210504_091140-3.jpg

I've been playing around lately with the many Dice programs available here, but most used the Arduino family of microcontrollers. With the recent release of the Raspberry Pi Pico and Pimoroni's Tiny2040 development board, I decided to make one using that, but it required a change in programming from the Arduino Sketches I am familiar with. I found it a great way to improve my Micropython skills. This is an easy project great for beginners, of which I certainly am!

Supplies

Raspberry Pi RP2040

6 5mm LEDs (color optional)

6 220Ω Resistors

1 Touch Switch (Momentary)

Wires

Breadboard

Make Your Circuit

Single Die Breadboard RC2040 Tiny_bb.jpg

Attach the RP2040 board to a breadboard. If you use a half-size breadboard, it's probably best to insert the RP2040 into the topmost row (Rows 1 through 8).

Attach a momentary switch to the breadboard. Only one side of the switch is needed for connections.

Attach 6 LEDs with the Cathode (negative) leg inserted into the GROUND rail of the breadboard and the Anode (positive) leg in the connection ports. I found leaving a space of one breadboard hole between each LED worked fine without too much crowding.

Insert each of the 6 220Ω resistors into the same port as the LED anode connections and across the divider gap into the other side.

Connect a wire from each resistor to one of the pins marked 1 through 6 on the RP2040. The LEDs can be connected to the pins through the resistors to any of the pins, but it will be more visually appealing if you do them in order so that the lights flash in spectacular sequence.

Connect one leg of the switch to the Power rail of the breadboard and the other to the RP2040's pin marked A0.

Use a wire to connect the RP2040's pin 0 (5v) to the power rail of the breadboard and another wire to connect pin 1 (-) to the ground rail of the breadboard.

And now our circuit is complete.

Program Your Dice

Load up your editor of choice and run code. (I used Thonny.)

Enjoy Your Dice

IMG_20210504_091140.jpg

The program waits for the user to press the button and when they do, it turns on the LEDs from Led#1 through #6 and then turns them off from #6 through #1. It then generates a random number of 1 through 6 and then turns on the number of LEDs of that number. It waits 2 seconds and then turns on the remaining LEDs through #6 and then off #6 through #1 and waits for the user to press the button again.

I am not completely satisfied with the code as I know there are (or at least should be in Micropython) more efficient ways of cycling through the LEDs using For or While loops as I've done in my Arduino sketches. I'll continue my quest to find it, but if anyone knows a better way, feel free to comment below. I decided to publish this for all of the beginners out there because I spend hours looking for things like this and figured someone out there might find it useful. If you do, please let me know!

Cheers!

Improve Your Code

Programming is a never ending quest to improve our code and as I said above, I wasn't completely satisfied with my original code. So after a few more hours of work, I have come up with this and I'm much much happier with it.

Cheers!