Magic 8 Ball (Magic 2 Ball) With Voice, Round Color LCD Screen and Motion Sensing

by BitstoArt in Circuits > Raspberry Pi

1441 Views, 10 Favorites, 0 Comments

Magic 8 Ball (Magic 2 Ball) With Voice, Round Color LCD Screen and Motion Sensing

IMG_6618.JPG

In the long tradition of fortune telling Magic 8 Balls I created a self-contained Magic 2 Ball running a Raspberry Pi Zero 2. When you shake the ball and turn it over, the answer tile is displayed on a round screen and the answer is spoken out loud. The 110mm diameter ball is 3D printed in three parts. The round color LCD screen has a 240 x 240 pixels resolution. The sound is high quality PCM and the ball’s orientation is read by a circuit board. The python programs and resources are maintained through a WIFI connection and additional answers can be added.

I had a full spool of dark blue 3D filament, so instead of an eight ball, mine is a Magic 2 Ball to match the color of the two pool ball. This project requires a steady hand and good vision for soldering the connections.

I am grateful to all of the people who have documented their previous Raspberry Pi projects on the web and will try to give credit when appropriate. I could not have made this without you.


Supplies

IMG_6609.JPG

Below are the parts I used. Others will work but may require wiring or program changes. 

Computer Raspberry PI Zero 2 W

Raspberry Pi Zeros are available from various dealers. I used a Raspberry Pi Zero 2 W and soldered on the header. First I tried an original Pi Zero W, but the response was noticeably slower.


Memory Card SAMSUNG (MB-ME32GA/AM) 32GB 95MB/s (U1) microSDHC EVO Select

Link      https://www.amazon.com/dp/B06XWN9Q99?psc=1&ref=ppx_yo2_dt_b_product_details

Any decent memory card with at least 32GB will do. 


Audio Amplifier AITRIP 3 PCS MAX98357 Audio Power Amplifier Module I2S Class D

Link      https://www.amazon.com/AITRIP-Amplifier-Filterless-Amplifiers-Raspberry/dp/B0912CWB7Z/ref=sr_1_1

This board provides fantastic PCM sound at a decent volume.


Speaker WMYCONGCONG 4 PCS 1.5" 4ohm 3W Audio Speaker

Link      https://www.amazon.com/dp/B07DVDW8PV?psc=1&ref=ppx_yo2_dt_b_product_details

Or use any tiny 4 ohm 3W speaker. 


Display 1.28" Round LCD Display Module for Arduino/Raspberry Pi/Jetson Nano/STM32 240×240 Resolution 65K RGB Colors GC9A01 Driver SPI Interface

Link https://www.amazon.com/dp/B09FZGYC7L?psc=1&ref=ppx_yo2_dt_b_product_details

Other displays will probably work, including the one in the referenced link in the LCD section. Most are shipped from China.


Accelerometer               Adafruit LIS3DH Triple-Axis Accelerometer

Link https://www.adafruit.com/product/2809

This board is for motion sensing.


Jumpers Premium Breadboard Jumper Wires 22AWG

Link https://www.amazon.com/dp/B08QFNH11Z?psc=1&ref=ppx_yo2_dt_b_product_details


Jumpers Haitronic 120pcs 20cm Length Jumper Wire/Dupont Cable Multicolored (10 Color) M to F, M to M, F to F Each

Link https://www.amazon.com/dp/B01LZF1ZSZ?psc=1&ref=ppx_yo2_dt_b_product_details

Any similar jumpers should work. Heavy gauge 22 AWG is easier to strip and solder later.

 

Optional Parts

These parts are used to create a power extension.

USB Connector 20 Pcs Micro USB Male Type B 5-Pin Jack Port Solder Connector

Link https://www.amazon.com/Micro-Male-5-Pin-Solder-Connector/dp/B07G5ZY7MH/ref=sr_1_1


USB Connector HiLetgo 10pcs Micro USB to DIP Adapter 5pin Female Connector B Type PCB Converter pinboard

Link https://www.amazon.com/gp/product/B07W844N43/ref=ppx_yo_dt_b_asin_title_o04_s00?ie=UTF8&psc=1



Obsolete Parts

I originally prototyped a rechargeable battery system. It couldn’t provide enough power. I probably didn’t have enough room in the 110mm diameter ball anyway. If you can add a battery, you will have it all.

 Obsolete Charger HiLetgo 3pcs TP4056 Type-c USB 5V 1A 18650 Lithium Battery Charger Module Charging Board with Dual Protection Functions

Link https://www.amazon.com/dp/B07PKND8KG?psc=1&ref=ppx_yo2_dt_b_product_details


Obsolete Voltage Regulator Frienda 10 Pieces MT3608 DC-DC Boost Converter 2A Adjustable Step Up Module Voltage Regulator Board Input Voltage 2V-24V to 5V-28V Output Voltage

Link https://www.amazon.com/dp/B089Y7NDCR?ref=ppx_yo2_dt_b_product_details&th=1


Obsolete Battery IMREN 3.7v Rechargeable Battery 3000mAh

Link https://www.amazon.com/IMREN-Rechargeable-Battery-Flashlight-Headlamp/dp/B09L5M1WN6/ref=sr_1_1

Or any 3.7v battery that has a similar wattage and size.

Install Raspbian on Pi Zero

IMG_6623.JPG

First install the Raspberry Pi Linux operating system on your Pi Zero. I followed the following link for a headless install.

https://www.tomshardware.com/reviews/raspberry-pi-headless-setup-how-to,6028.html

Remove the SD card from your other computer and insert it into your Zero. Turn the Zero on. 

Connect to the Zero using your favorite SSH software. I use Putty. If you have trouble finding your Zero you can check your local router to get the wireless IP. 

Sign in to your new system with the default user and password.

User pi

Password raspberry

 

Change your password. If you have or will have more than one Pi, change the host name from "raspberry" to a new value in two files. Then reboot.

passwdsudo nano /etc/hostnamesudo nano /etc/hostssudo reboot


Restart your putty session. Sign in as pi with your new password.

Update your system. This will take a while!

sudo apt updatesudo apt upgrade -ysudo apt autoremove -y

  

GPIO Pins

IMG_6593.JPG

This is an overview of ALL of the connections. I will expand on this in the later steps. Don’t do anything yet! 


1 LCD screen                2 Amplifier

3 Accelerometer            4

5 Accelerometer            6 LCD screen

7                                    8

9 Amplifier                    10

11                                  12 Amplifier

13 LCD screen              14

15                                  16

17 Accelerometer         18 LCD screen  

19 LCD screen               20

21                                   22 LCD screen  

23 LCD screen               24 LCD screen

25 Accelerometer          26        

27                                   28

29                                   30

31 Accelerometer          32

33                                   34

35 Amplifier                    36

37                                  38

39                                  40 Amplifier

Install LCD

IMG_6579.JPG

I’m indebted to the following website for the information to install the LCD.

https://www.industrialshields.com/blog/raspberry-pi-for-industry-26/post/1-28-lcd-display-module-raspberry-tutorial-318


You need 8 wires to connect the LCD module. I had a couple of module pins named differently than in the article. Of course you can use any 3V3 pin and any GND pin on the Zero.

 ZERO HEADER                             MY MODULE LABELS     ORIGINAL ARTICLE MODULE LABELS

Pin 1 3V3                                        3V3                                  VCC

Pin6 GND                                       GND                                GND

Pin 19 GPIO 10 MOSI SPI             SDA                                 DIN

Pin 23 GPIO 11 SCLK SPI             SCL                                  CLK

Pin 22 GPIO 25                              DC                                    DC

Pin 13 GPIO 27                             RST                                  RST

Pin 18 GPIO 24                             BLK                                  BL

Pin 24 GPIO 7 CEO SPI                 CS                                    CS

 

Enable SPI for the LCD.

sudo raspi-config

Interface Options > SPI > Yes > Enter

 Install the required libraries.

wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.68.tar.gztar zxvf bcm2835-1.68.tar.gzcd bcm2835-1.68/


Configure and compile.

sudo ./configuresudo makesudo make checksudo make install


Install wiring libraries.

wget https://project-downloads.drogon.net/wiringpi-latest.debsudo dpkg -i wiringpi-latest.deb

 

Install the unzip program, download the demo program and extract.

sudo apt-get install p7zip-full -ysudo wget https://www.waveshare.net/w/upload/a/a8/LCD_Module_RPI_code.7z 7z x LCD_Module_RPI_code.7z -O./LCD_Module_code


Test it.

cd ~/bcm2835-1.68/LCD_Module_code/LCD_Module_RPI_code/RaspberryPi/python/example sudo python3 1inch28_LCD_test.py


If everything worked you will see a clock face on the screen. Copy the lib directory to your new program directory for ease of access. 

mkdir ~/magic8 cd ~/bcm2835-1.68/LCD_Module_code/LCD_Module_RPI_code/RaspberryPi/python   cp -r lib ~/magic8cd ~/magic8sudo reboot

Install Amplifier

IMG_6612.JPG

For this I’m indebted to the following website.

http://www.lucadentella.it/en/2017/04/26/raspberry-pi-zero-audio-output-via-i2s/

 

You need 5 wires to connect the MAX98357 amplifier to the Raspberry Pi Zero: Of course you can use any 5V pin and any GND pin on the Zero.

 

ZERO HEADER                             MODULE           

Pin 2 5V                                          Vin                      

Pin 9 GND                                      GND                  

Pin 12 GPIO 18                             BCLK                  

Pin 35 GPIO 19                             LRC                                                  

Pin 40 GPIO 21                             DIN                    

 

Change the following file.

sudo nano /boot/config.txt

Comment out one line.

# dtparam=audio=on

 Below this add the following two lines.

dtoverlay=hifiberry-dacdtoverlay=i2s-mmap 


Add a new file.

sudo nano /etc/asound.conf

 Add the following lines to the file.

pcm.hifiberry {type hw card 0} pcm.!default {type plugslave.pcm "dmixer"} pcm.dmixer {type dmixipc_key 1024slave {pcm "hifiberry"channels 2}}

Reboot.

Sign back in.


Important A little conversation about the GPIO and hardware drivers. Behind the scene, drivers can reset the GPIO from its original configuration. The driver for the LCD module changes the pins you need for the amplifier. But you can change them back and the LCD module is still happy. You can run these commands at the prompt and I have added them to my program.

Display the current GPIO status, set four GPIOs to their original state and display the new status.

raspi-gpio getraspi-gpio set 18 a0raspi-gpio set 19 a0raspi-gpio set 20 a0raspi-gpio set 21 a0raspi-gpio get

 

I installed mplayer to play my audio files. You can try other ones.

sudo apt install mplayer

Install Accelerometer

IMG_6573.JPG

The accelerometer can be read to determine the Magic 2 Ball's relative position and if the ball has been shaken.

 

First turn on 12C.

sudo raspi-config

Interface options > 12C > Yes

 

You will need 5 wires to connect the accelerometer to the Zero. Of course you can use any 3V3 pin and any GND pin on the Zero.

 

ZERO HEADER                             MODULE           

Pin 17 3.3V                                    Vin                     

Pin 6 GND                                      GND                  

Pin 5 GPIO 3 SCL                          SCL                     

Pin 3 GPIO 2 SDA                          SDA                   

Pin 31 GPIO 6                                 INT                     

 

Install required Adafruit software.

sudo pip3 install adafruit-circuitpython-lis3dhsudo pip3 install adafruit-circuitpython-busdevice 


If you haven’t already, create a new program directory. Create a new file.

mkdir ~/magic8sudo nano 3d.py

 

Add the following lines.

import timeimport boardimport digitalioimport adafruit_lis3dh i2c = board.I2C()int1 = digitalio.DigitalInOut(board.D6) # Set this to the correct pin for the >lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, int1=int1) i = 1print("Move me") while i < 6:   x, y, z = lis3dh.acceleration   print("#" + str(i), x, y, z)   time.sleep(3)   i = i + 1 print("Shake me") while True:   if lis3dh.shake(shake_threshold=10):       print("Shaken!")

 

Run the test program.

sudo python3 3d.py

Power

I created a power connector using a pair of USB connectors as shown in the following link. Do not switch polarity!

https://withrow.io/simpsons-tv-build-guide

(Fun project by the way!)


Now that I don't use a battery, I would probably just run a permanent cable through a hole in the ball to the Zero!

Tiles and Waves

TILE033.jpg
TILE034.jpg
TILE035.jpg
TILE036.jpg
TILE037.jpg
TILE038.jpg
TILE039.jpg
TILE040.jpg
AskaQuestion.jpg
TILE001.jpg
TILE002.jpg
TILE003.jpg
TILE004.jpg
TILE005.jpg
TILE006.jpg
TILE007.jpg
TILE008.jpg
TILE009.jpg
TILE010.jpg
TILE011.jpg
TILE012.jpg
TILE013.jpg
TILE014.jpg
TILE015.jpg
TILE016.jpg
TILE017.jpg
TILE018.jpg
TILE019.jpg
TILE020.jpg
TILE021.jpg
TILE022.jpg
TILE023.jpg
TILE024.jpg
TILE025.jpg
TILE026.jpg
TILE027.jpg
TILE028.jpg
TILE029.jpg
TILE030.jpg
TILE031.jpg
TILE032.jpg

Create the folders to hold the tiles and the wave files.

cd ~/magic8mkdir tilesmkdir waves


Use FileZilla or another program to transfer your tile files and wave files to each directory. You can use my tiles and waves or create your own. Tiles must be 240 pixels by 240 pixels. Of course, you can always add additional tiles and matching waves. (Be sure to change the random statement in the program to your new total.)

Magic 8 Program

First a note about the program. Yes, it is very simplistic. But sometimes the ability to understand and easily change the program is more important than speed. Feel free to make your own changes!

Copy the "magic8wav.py" python program available below to your magic8 subdirectory.

You can now run and edit the program.

cd ~/magic8sudo python3 magic8wav.pysudo nano magic8wav.py

Downloads

Auto Start

First disable any traces of a previous start and create the following file.

sudo systemctl disable magic8.servicesudo nano /lib/systemd/system/magic8.service

 

Add the following lines.

[Unit]Description=Start Magic 2 BallAfter=network.target [Service]WorkingDirectory=/home/pi/magic8ExecStart=/usr/bin/python3 /home/pi/magic8/magic8wav.py > /home/pi/magic8/magic8wav.log 2>&1Restart=always [Install]WantedBy=multi-user.target

 

Restart the daemon system, enable the service and reboot.

sudo systemctl daemon-reloadsudo systemctl enable magic8.servicesudo reboot


If you are having problems, you can check and erase the logs.

cat /home/pi/magic8/magic8wav.logsudo rm /home/pi/magic8/magic8wav.log

3D Printing the Ball

IMG_6590.JPG

The ball was designed using the free Tinkercad website. For complex items, I find it handy to create them using multiple steps. Create an item, export all of it as a STL file, import this as a whole into a new project, make changes. Repeat as needed (just like your shampoo). You can even import an item into Tinkercad and convert it to be used as a "hole". This is how I added (subtracted) the speaker slots. But Tinkercad cannot design a smooth ball.

I originally designed the ball to accommodate the battery system. After I eliminated the battery I cut out all of the shelves in the number ball half. Besides I needed all of the available room to cram everything in!

I printed the parts on a $300 3D printer. The printing isn't as precise as a more expensive model but it did the job. Slowly!

Assemble the Ball

Important Before assembling the ball I removed all of the jumper pin covers, slid on shrink tube and soldered all of the connections. The ball will be shaken (not stirred) every time!

Insert the ring into one half before crossing between the two halves.

Your hot glue gun is your friend! I used dabs of hot glue to anchor the components inside the ball. Dried glue can be pried up later if you need to make changes.

The 3D printed ring fits snugly at the intersection of the two halves holding them together. I still needed a couple of dabs of glue to keep the halves in place.

Mission Accomplished

Congratulations! Hopefully you are ready to have fun with your Magic 2 (8) Ball! Mine has been slightly spooky!