8x8 LED Matrix Digital Dice
For the next project you will need the connected LED Matrix with the addition of buttons we will add to the circuit.
Only connect 5v to the VCC Pin on the led matrix!
|Connections Raspberry Pi||Connections Breadboard|
|GND + 10KOhm||Button 1|
|GND + 10KOhm||Button 2|
|GND + 10KOhm||Button 3|
|GND + 10KOhm||Button 4|
Please keep in mind that you always need to connect the pins, on the button, which are facing each other.
import RPi.GPIO as gpio import max7219.led as led from random import randint import time gpio.setmode(gpio.BCM) button = 14 gpio.setup(button,gpio.IN,pull_up_down=gpio.PUD_UP) matrix = led.matrix() def new_number(channel): matrix.letter(0, ord(str(randint(1,6)))) gpio.add_event_detect(button, gpio.RISING, callback=new_number) matrix.letter(0, ord("?")) while True: time.sleep(0.1)
When you execute the program for the first time you should see a question mark/?. After pressing the button, you need to press the button which is connected to GPIO Pin 14, and you should then see a number between 1 and 6 on the matrix. Like a normal dice ;)
Step for Step Code Walkthrough
As usual, in the first line we are importing all the libraries we need for the program.
In line 6-8 we program the logic of the button with which we will create a new number.
In line 9 we are creating the variable matrix and saving in it a new matrix object.
In line 11 and 12 we are creating our own function named new_number(). In line 14/15 you will see why we need to give this function a parameter. We are generating a new random number in line 12. This number will be between 1 and 6. To do this we are utilizing the
randint(1,6) function from the
random library we imported. We are passing these newly generated numbers on as a parameter to the
.letter() function to display it on the matrix.
In line 14 we are connecting the push button with a function. This event handling is done by using the
.gpio_event_detect() function. Please note that there is a line break in line 14/15 for printing reasons, you can ignore this line break in your python program. The first parameter in the function is the GPIO Pin, in our case, gpio14. The second parameter is the event for which we want to trigger the function. It is either
gpio.RISING for a new current or measured HIGH at the GPIO Pin or
gpio.FALLING for the exact opposite. The last parameter is the name of function
callback=name_of_function. When this function is called, it will get the number of the GPIO Pin as a parameter. We can use
.gpio_add_event() for different gpio pins but the same function. In conclusion: this line of code will call the new_number() function then the button which is connected to gpio14 is pressed.
The steps thus far represent the main part of the program. If we were to stop the program at line 15 and execute it, it would stop itself immediately because the function only gets called when the button is pressed. Similar to our previous project, we implement a while-loop in lines 19 and 20. This while-loop’s only purpose is to keep the program running and you can always close this program manually with
Lastly, in line 17 we are displaying the question mark with the
.letter() function so the matrix does not start empty.
And that is end of this little program. With just a few lines of code you have built yourself a digital dice with a cool interface to work with!