Introduction: QuizzPi, a Raspberry Pi Trivia Game With Python
QuizzPi is an arcade type trivia game. QuizzPi was born becasuse I had the need to give my daughter an entertainment. She is 7 years old and she already demands the use of new technologies, the idea was to create something that was both fun and educational.
Requirements of the project:
- Easy to use
- Easy to upgrade the set of questions and answers
- High scores table
Multiple items are configurables. You can create your own database of questions/answers or you can use one of the multiple servers which offers a database, where you decide the type of questions, difficult, ... I created for my daughter a database of first grade questions, but we can change configuration to get trivia questions from an oline server. This instructable version get the questions from a collaborative questions and anwers, the Open Trivia Database. I have choose general knowledge questions but you can choose film, sports or religion questions.
I apologize for my poor English. I'm Spanish but I have preferred to create this instructable in English so it can reach to more people.
Step 1: You Need Some Materials and Tools...
You have most of the materiasl at home for sure...
- Raspberry Pi 2/3 with a 8Gb or more SD card (35$)
- Raspberry Pi 3.5inch LCD with case (11$). See (this is optional, you can use any display you have, you only need to modify the QuizzPi case for it)
- Batteries. You can use a power bank or you can use a diy 18650 battery pack (5$). In my case I used one Aukey 5000mah power bank (12$). See
- Four arcade buttons. I have these at home (3$, 12 buttons for 10$). See
- One on/off switch (0.25$). See
- Two USB connector (0.50$)
- One USB/micro USB cable
- One cheap speaker (optional) (0.25$). See
- One PAM8403 amplifier (optional) (0.80$). See
- Some wires to link buttons, usb connector and on/off switch to GPIO pins(1$?)
- Four neodymium magnets or you can use whatever thing to get the back door closed. See
Plywood. I used 5mm plywood but you can use any you have at home (2$)
You need some tools too...
- Soldering Utilities
- Screw drivers
- Hotglue gun
- 3D printer (optional)
Step 2: Let's Design a Cool QuizzPi Case...
First thing we need is to know how many space we need to include hardware (Raspberry + screen + buttons + battery + wires). Taking into account the parts described in the previous step I made a 3D design of the case using the free online 3D designer Tinkercad (See).If you want to print the 3D model of the case you can download it from the attached file QuizzPi case.stl.
At that moment I had to decide whether to print the model on my 3D printer or make it out of plywood. I thought that being for my daughter I could spend a little more time making it out of plywood. So I get the plans from the 3D model and cut the wood.
Next step is hot-glueing the parts...
Step 3: Making the Plywood Case...
Once I had cut all plywood parts of the plans next step is hot-glueing them. In some joins I put reinforcements to make the box stronger. In the bezel I draw with the pencil the outline of the LCD screen, and cut it out. I make four holes for the screws. If you use a different screen you must modify this thing.
Back door closes with two magnets glued to the door and two magnets glue to the case, so I avoid the use of hinges.
The QuizzPi case is finished...
Step 4: Wiring GPIO Pins, Mounting Raspberry+LCD...
We need to connect the buttons to the Raspberry Pi so it can know what button are pushed. Buttons are connected to GPIO pins. We need four IN GPIO pins plus one Gnd GPIO pin. I used GPIO pin 31 (GPIO6), 33 (GPIO13), 35 (GPIO19), 37 (GPIO26) and 39 (Gnd) for red, yellow, green and blue buttons respectively. This screen has SPI interface. This means that it is connected to GPIO bank, so firstly I put wires ended in square in buttons pins and then I connected the LCD screen.
Next step was to mount the Raspberry+LCD+case onto the bezel. I used the Raspberry Pi case's own screws.
The QuizzPi is mounted...
Step 5: Battery, Buttons, Switch and USB Connector...
I used Tinkercad again to design a clip to hold the power bank to the case. I printed the clip with my 3D printer and join to the case with hot glue pistol. I attach the .stl file of the model below. If don't have access to a 3D printer you can join the power bank with velcro tape.
Next step is to connect the on/off switch between the power bank and the Raspberry Pi. I took an USB cable and I cut only the power wire, then I soldered one wire end to one pin of the on/off switch and the other wire end to the other pin of the on/off switch. So when the switch in off power doesn't go through the swtich and Raspberry is off and when the switch in on power goes through the swtich and Raspberry is on.
When power bank discharges is a problem to take it out the case to charge it, so I didn't want to take out it everytime to charge. The problem was the power bank charge port is USC type C, and I only had the cable of the power bank. So I soldered two female USB connectors and put in the case. The connector inside to connect the power bank cable, and the outside connector to connect a USB power cable.
To place the buttons and wiring them was easy. The buttons have 2 pins, one pin of every button connects to a common cable that goes to a ground GPIO pin, and the other pin of the button connects to its respective GPIO pin of the raspberry. The wiring diagram is shown in the diagram attached.
The sound system is based in PAM8403, a cheap sound amplifier. It is necessary because the sound get from the jack of Raspberry Pi isn't too powerful to make a speaker works. You need to power it with 5v or more. In the images attached you can see how to wire the amplifier and the speaker. We need a male 3.5mm jack to connect to the Rapberry's audio/video jack. i soldered 3 wires to the jack: audio, video and ground. The diagram attached shows what pin is everyone. Then I soldered a wire to the On/Off switch for take power from, and another wire to the ground wire from the power bank cable. Then I connect the wires from the speaker to the outputs of the amplifier. The easy part is to allocate the sound system inthe case. You can see in the images.
QuizzPi is mounted, in the last photo we can still see pencil marks on the wood, but it's software time... let's go to create a tiny quiz engine...
Step 6: Rasbian Image and Programming the Quiz Engine...
We press the power button. Nothing happens. Oh my God! It doesn't work! What may be wrong? Any cable without connection? Don't worry, we need to install an image on the SD card...
The quiz engine is writed in Python 3, so we need a Raspbian image with the Python compiler installed. At this point we have to take into account the type of screen we have installed. My screen is SPI interface, and seller gives a Raspbian image with all the screen drivers installed. This version of Raspbian has Python engine too. You can download this image in this link.
If you have an HDMI or RCA screen you can download the last Raspbian image avaliable in raspberrypi.org site. Attention: you need Desktop Rasbian version, so avoid to download Lite Raspbian image. This images contains Python compiler.
To install the image onto the SD card we can use any aplication. I use Win32diskimager. Then we insert the SD card into the Raspberry and push on/off switch. It works! Now we need to create a Python program with the quizz engine.
Next step is to download the files of the quiz engine. I have attached source code in the file QuizzPi.zip. Download the source and create a folder in the Raspberry Pi:
# mkdir /home/pi/QuizzPi
and unzip the file downloaded into this folder.
QuizzPi.zip contains this files:
- QuizzPi.py - it contains the code
- PNG image files - it contains graphical interface, you can change them to personalize
- MP3 sound files - you can change to personalize
- losmejores.txt - it contains High Scores table, you can delete it to initialize the table
I will not give an exhaustive explanation of how I have programmed the engine code in Python because it's to hard for people without programming knowledge. They only need to save the files in the Raspberry. People with programming knowldge will be able to modify it without any explanation ;)
Questions and answers are from a Question&Answers collaborative database called Open Trivia Database, you can find here. You can change the category and difficult of the questions changing a line in the file QuizzPi.py:
#Cargo desde URL
url = 'https://opentdb.com/api.php?amount=10&type=multiple'
changing with the url obtained from the api configurator https://opentdb.com/api_config.php
I designed the game screens in Photoshop, you can modify them. You only must to preservate some white areas in the question screen, in the results screen and the high scores screens because the game engine writes on them.
You can personalize the sounds of the game too. You only must to change the .mp3 files by others you want, you must to preserve the same names. You can get a lot of sounds from online databases, I get mines from Free Sound Effects.
Now you must to modify your Raspbian to automatically run QuizzPi. You can read this article where explain how to do it: https://jackbarber.co.uk/blog/2017-03-02-automatically-run-a-python-script-at-boot-in-raspbian
We have finished!!!
Step 7: Demonstration...
To finish this instructable you can see this video. You can see it's easy to use and the difficult of the questions depends of you...
Edit: video updated now with sound!
Runner Up in the