How to Create a Magic 8-Ball Using a Raspberry Pi and a Sense HAT - Raspberry Pi Projects, Tutorials, Learning DIY Electronics - Makergenix

Breaking

 


How to Create a Magic 8-Ball Using a Raspberry Pi and a Sense HAT

 The Raspberry Pi Foundation launched the Sense HAT add-on in 2015, and it is now one of the best Raspberry Pi HATs since it has a full science platform as well as an 8x8 RGB LED matrix for some fun in your Raspberry Pi projects. The Sense HAT is jam-packed with temperature, humidity, acceleration, direction, and air pressure sensors. In addition, we have a fantastic RGB LED matrix and a joystick that we can use in our designs.

We used Python to power the Sense HAT, but we can also use Scratch and Node-RED. Sense HAT was created in conjunction with the AstroPi mission, which saw two Raspberry Pi B+ boards sent to the International Space Station. These two Pi's had their own Sense HAT boards, official Pi cameras, and a specially made aluminium case to keep them safe and cool in space. Projects written by children all over the world were run on those two Raspberry Pi's, and that project is still active today, and you can participate by visiting their website at https://astro-pi.org/.

This tutorial will introduce the board, show text on the LED matrix, and teach you how to interpret accelerometer data for a classic game of chance.

Also Read: 50 Raspberry Pi Hacks & Tips You Should Know

You will need the following materials for this project:

If you've never used a Raspberry Pi before, check out our posts on how to set it up for the first time or how to do a headless Raspberry Pi install, which doesn't require a keyboard, mouse, or computer.

How to Attach the Sense HAT to a Raspberry Pi

It's easy to connect a Sense HAT to a Raspberry Pi. Attach the Sense HAT to all of the GPIO pins when the Raspberry Pi is turned off, making sure that the Sense HAT perfectly overlaps the Raspberry Pi.

To firmly install the SenseHAT, use the included stand offs. Now attach your keyboard, mouse, HDMI, micro SD card, and finally turn on the Raspberry Pi to boot it up to the desktop. We don't need to update any applications or Python packages because we're using the most recent version of the Raspberry Pi OS.

Using Sense HAT to Build a Hello World Program
Writing a Hello World Program with Sense HAT
Image Credit: Tom's Hardware

“Hello World” is the first project of any new piece of technology or applications. It demonstrates that our equipment is operational and that all is in place for us to proceed. The first project is a quick scrolling message that you code in your favourite Python editor, such as Thonny, IDLE, Mu, or a text editor. Make a new file called text scroll.py. Remember to save regularly!

1. Load the SenseHat class from the sense hat package, then construct an entity called "sense" to make the module easier to use.
from sense_hat import SenseHat
sense = SenseHat()
2. Make two artefacts to hold the RGB values for red and white. There are tuples, which are data storage systems that can be generated and discarded but never modified. These tuples store the RGB colour values for a specific colour in the format expected by Sense().
red = (255, 0, 0)
white = (255, 255, 255)
3. Make a variable to hold a message that will be shown on the screen.
message = "Hello World"
4. Implement an error handler and a loop. The handler will attempt to execute the code indented inside, and the loop will run project code indefinitely.
try:
    while True:
5. Use the show message feature inside the loop to print the message to the screen. In this case, it will scroll "Hello World," with the text colour set to red via the tuple. The background colour is white, which is also regulated by the tuple. The scroll speed is set to 0.1, which is slow enough to allow for reading. Color is spelled differently in UK English.
sense.show_message(message, text_colour=red, back_colour=white, scroll_speed=0.1)
6. Make an exception, in this case a KeyboardInterrupt, outside of the loop. As the consumer presses CTRL + C, the code is terminated and the LED matrix of the Sense HAT is cleared.
except KeyboardInterrupt:
    sense.clear()
This is how the code for this project should look.
from sense_hat import SenseHat
sense = SenseHat()

red = (255, 0, 0)
white = (255, 255, 255)
message = "Hello World"
try:
    while True:
      sense.show_message(message, text_colour=red, back_colour=white, scroll_speed=0.1)
except KeyboardInterrupt:
    sense.clear()
7. Save the code and execute it using your Python editor. Thonny and Mu each have a play/run toggle. IDLE is accessed via F5 or the Run menu. The phrase "Hello World" should now be scrolling through the LED matrix. When you're done, hit CTRL + C to clear the matrix.

Magic 8 Ball on Pi

The Magic 8 Ball is a well-known game. We ask a question aloud and then shake the 8-ball. A letter floats to a browsing portal in a matter of seconds, ready to be read. With the Sense HAT, we will create a new version that uses raw accelerometer data to decide when it has been shaken.

Create a new Python project in your preferred editor, name it 8ball.py, and remember to save frequently.

1. Import the SenseHat class from the sense hat package, followed by the random module's option function. Build an entity called "sense" to make it easier to use the module.
from sense_hat import SenseHat
from random import choice
sense = SenseHat()
2. Make two artefacts to hold the RGB values for red and white. There are tuples, which are data storage systems that can be generated and discarded but never modified. These tuples store the RGB colour values for a specific colour in the format expected by Sense().
red = (255, 0, 0)
white = (255, 255, 255)
3. Make a list called "answers" that contains five text strings that correspond to the answers to our excited player's questions. In other programming languages, lists are known as arrays. A list uses an index that begins at zero to store records. A list's first item is at position zero, and each corresponding item has its own numerical position. Lists can be generated, deleted, or modified.
answers = ["Not likely","Chances are slim","Maybe","Quite possibly","Certainly"]
4. Implement an error handler and a loop. The handler will attempt to execute the code indented inside, and the loop will run project code indefinitely.
try:
    while True:
5. Make an object called "acceleration" that will be used to retrieve raw accelerometer data from the Sense HAT's onboard accelerometer.
acceleration = sense.get_accelerometer_raw()
6. Make three variables, x, y, and z, to hold the raw accelerometer data for each dimension.
x = acceleration['x']
y = acceleration['y']
z = acceleration['z']
7. We may use abs() to change the variables x,y,z to store an absolute value that does not care whether it is positive or negative. The values contained in the variables x,y,z can be positive or negative, depending on how the Sense HAT is oriented. Our code would not need to consider negative numbers; all that is required is that the numbers exceed a certain threshold, which will cause the answers to appear.
x = abs(x)
y = abs(y)
z = abs(z)
8. Write a basic conditional test that tests the value contained in the three variables x,y,z and runs the indented line of code if all of the values are greater than 1.
        if x > 1 or y > 1 or z > 1:
9. Using the show message feature, use the preference function to pick a string at random from the list "answers." With red text on a white backdrop, this is scrolled around the LED matrix. The background colours are determined by the red and white tuples we previously made.
sense.show_message(choice(answers), text_colour=red, back_colour=white, scroll_speed=0.05)
10. When the Sense HAT has not been shaken, use an else state to clear the LED matrix.
else:
    sense.clear()
11. Throw an exception, in this case a KeyboardInterrupt. As the consumer presses CTRL + C, the code is terminated and the LED matrix of the Sense HAT is cleared.
except KeyboardInterrupt:
    sense.clear()
This is how the code for this project should look.
from sense_hat import SenseHat
from random import choice

sense = SenseHat()

red = (255, 0, 0)
white = (255,255,255)
answers = ["Not likely","Chances are slim","Maybe","Quite possibly","Certainly"]

try:
    while True:
        acceleration = sense.get_accelerometer_raw()
        x = acceleration['x']
        y = acceleration['y']
        z = acceleration['z']

        x = abs(x)
        y = abs(y)
        z = abs(z)

        if x > 1 or y > 1 or z > 1:
            sense.show_message(choice(answers), text_colour=red, back_colour=white, scroll_speed=0.05)
        else:
            sense.clear()
except KeyboardInterrupt:
    sense.clear()

 


Most Viewed Posts

Write For Us

Name

Email *

Message *

All Blogs