Digital Electronics

Digital Electronics Class #

In my Junior year of high school, I took a Digital Electronics class. We learned about boolean algebra, logic gates, basic circuitry, and microprocessors. The class culminated in a month-long project of our choice. I didn’t want to spend time figuring out how to create a complex digital circuit for the class, so instead I proposed a project where I wouldn’t have to create a complex circuit (If you’re reading this, sorry Mr. Schmidt…). It was approved, so I got to work.

For my project, I created a videogame where the player controls a character on the screen by tilting the controller. I used a Raspberry Pi and a senseHAT sensor for this project. I used the Inertial Measurement Unit sensor in the senseHAT to determine the angle that the controller was being tilted at.

Here is a video demonstration of the game.

Project Proposal #

Basic Concept

My project will use the Raspberry Pi 4 in conjunction with the senseHAT sensor to create a game that can be controlled by the user through the use of the gyroscope embedded in the senseHAT. What is the Raspberry Pi?

From the Raspberry Pi website:

“The Raspberry Pi is a low cost, credit-card sized computer that plugs into a computer monitor or TV, and uses a standard keyboard and mouse. It is a capable little device that enables people of all ages to explore computing, and to learn how to program in languages like Scratch and Python. It’s capable of doing everything you’d expect a desktop computer to do, from browsing the internet and playing high-definition video, to making spreadsheets, word-processing, and playing games.”

In this project, I will use Python on the Raspberry Pi to create a game. I have the Raspberry Pi 4B.

What is the senseHAT?

The Raspberry Pi has 40 pin GPIO (General Purpose Input Output) Headers which, as the name suggests, has a variety of purposes. These ports can be used with a breadboard for a number of applications. Boards called HATs are designed for the Raspberry Pi to fit on top of the GPIO ports. For this project, I will be using the senseHAT, a HAT for the RPi which includes an IMU device, weather sensors, and an 8x8 LED Display. For this project, I will be using the IMU device to get information about the board’s physical position in addition to the LED Display which will be used for improving the UX of my game through visual notifications of events in the game. Below is a picture showing the Raspberry Pi and senseHAT put together.

Picture of Raspberry Pi 4 with senseHAT mounted on top, attached at the 40 GPIO Pins

Getting User Input

As mentioned previously, the senseHAT includes an IMU. An IMU, standing for Inertial Measurement Unit, enables the calculation of the relative position of the sensor to a previous position. Included in an IMU is a gyroscope and accelerometer.

A gyroscope is a device used to measure the tilt of an object in a particular direction. The three planes of measurement for a gyroscope are pitch, roll, and yaw.

Picture of airplane, demonstrating pitch, roll, and yaw

The senseHAT has a python module that facilitates the acquisition of this data. I’ve written a program to output this data to the terminal.

"""
Hello World program for Raspberry Pi 4 senseHAT
Outputs values from IMU in the console

Written by Philip Roberts 12/12/20
"""
from sense_hat import SenseHat
import time
sense = SenseHat() #Create senseHAT object
sense.set_imu_config(False, True, False) #Disables all functions of IMU but the gyroscope
try:
    while True:
        orientation = sense.get_orientation() #Gets data from senseHAT, data is returned as a dictionary
        print("Pitch: {pitch}, Roll: {roll}, Yaw: {yaw}".format(**orientation))
        time.sleep(0.5)
except KeyboardInterrupt:
    print("Interrupted by user")

Here’s a video of my Hello World for the senseHAT. I recorded this video from my desktop using ssh to connect to my pi. I printed out the program, then ran it. The text may be easier to read if the video is opened in Youtube and/or fullscreen. The measurements are outputted in degrees, which means that the data is already in a form that is easy to use.

The Game

The game I plan on creating will allow the user to control a ball, with the gyroscope data from the user, on a two-dimensional plane. The goal of the game will be to navigate the ball to the end of the course without touching any of the obstacles.

Here’s an example of the type of game I plan on creating.

I plan on adding three objects to the game.

  • The ball controlled by the character
  • The objects for the ball to avoid
  • The goal for the ball to reach

If I have more time to work on the game, I will add more features and add complexity to it, such as a physical button that controls some aspect of the game, possibly as a game mechanic to manipulate obstacles.

Materials For Project

  • Raspberry Pi 4
  • senseHAT for Raspberry Pi
  • Associated peripherals for Raspberry Pi (Monitor, Keyboard, Mouse)

I already have the materials necessary to complete this project, so purchasing additional materials will not be necessary.

The Plan

  1. Learn the PyGame Python Library
  2. Create visual assets for the game: The character, obstacles, and goal
  3. Create the game, but with keyboard controls (WASD)
  4. Write Python module to interpret the data from the gyroscope so that the degree measurements are translated into useful input for the game
  5. Connect the data from the senseHAT to the game
  6. Extra: Add any additional features (e.g. a button on a breadboard to control the game)

Plan for the next two weeks #

The Plan

  1. Set up a development environment on the Raspberry Pi - install vim, pip, and any other tools to facilitate development
  2. Install the Pygame python module on RPI
    • Possible Difficulties:
    • Python is the wrong version - It is necessary for Python to be the latest version (Python 3) to make use of several features
  3. Create a Hello World app in Pygame using a tutorial or the Pygame Documentation
    1. Use information from several tutorials to learn Pygame
    2. Tutorials:
    3. http://pygametutorials.wikidot.com/tutorials-basic
    4. Documentation:
    5. https://pypi.org/project/pygame/
    6. https://www.pygame.org/news
  4. Create player, enemy, and goal sprites in GIMP
  5. Control Player with WASD (Keyboard Controls)
  6. Translate SenseHAT input to directional information
  7. Control Player with SenseHAT Gyro

Project Update - Stage 1 #

After I had fixed the issue with the incorrect sensor input, I worked on the acceleration algorithm. Here are my notes and pseudocode for how the algorithm works.

In order to have velocity based on the angle of tilt, I needed to make every measurement on the same range. The rectangle in the top left is the senseHAT. In these notes, R represents the angle in degrees when the senseHAT is turned right (clockwise). L represents the angle in degrees when the senseHAT is turned left (counter-clockwise).

My notes on degree measurements from the IMU device.

Using the Breadboard - Stage 2 #

GPIO Port Issues

The senseHAT for the Raspberry Pi fits over all of the General Purpose Input Output (GPIO) ports on the board, but it does not use all of these ports. This means that these ports can be used for something else. To make use of these GPIO ports, I looked at the schematics for the SenseHAT to figure out which ports were reserved for the SenseHAT and which ports were free. I also found the pinout.xyz website, which is a list of all the GPIO ports used by HATs for the Raspberry Pi.

Note: You can find the pinout.xyz article for the SenseHAT at https://pinout.xyz/pinout/sense_hat.

The Raspberry Pi GPIO library has two modes for referencing the pins, GPIO.Board and GPIO.BCM. GPIO.Board refers to the physical pin numbers, one through forty, and GPIO.BCM refers to the Broadcom SOC channel number of each pin, which exists because the CPU of the Pi is made by Broadcom.

LED Wiring

The LEDs are wired from one of the ground pins on the Pi to GPIO pins. When the GPIO Pins are set to HIGH, the LEDs are turned on.

Button Wiring

A resistor connects the button to the 5V power pin on the Pi. The button is also connected to a GPIO port on the Pi, which is set to be an input. To understand how this works, I researched how pull down and pull up resistors work. This article from sparkfun helped me understand how these resistors work in switches.

A pull up resistor is connected to high voltage, and a pull down resistor is connected to ground. A resistor used next to a button makes it so that the input pin will be able to reliably tell the state of the circuit.

A basic circuit diagram displaying the operation of a pull down resistor

In this image, R1 is a pull-down resistor. When the button does not complete the circuit, the resistor guarantees that the input pin does not read a signal. If the resistor was not there, the input pin would read a floating signal. Also, when the circuit would be completed, the power would be shorted out when it would connect to ground.

Demonstration

This video explains my project update as well as giving a demonstration of the project.

My code

Here is my repository on GitLab: gitlab.com/philiprobertsProfessional/de-project

Before I wrote all of the code, I created hello world programs to make sure that the basic components of my game were going to work. These hello world programs demonstrate the basic functionality of features like the button, LEDs, and the gyroscope. These programs can be found in the utilities subdirectory of my repository. I also wrote a helper module to make interacting with the LEDs and the button easier. This module is named philbread and is located in the main directory of my repository. Lastly, the game python file itself is titled main.py. I have annotated my code so that each section’s purpose is clear.

The Final Product #

Features

  • Lights to alert the player of events in the game
  • The character is controlled by a handheld gyroscope
  • Game written in Python using Pygame
  • Avoid Enemies, get to the Goal
  • Button enables “pausing” the game

Using 3D design software I created a controller for the player to use when playing the game. Unfortunately, I did not have enough time to print it. Here’s two screenshots of my controller.

Screenshot of videogame controller in 3D design software Screenshot of videogame controller in 3D design software

Here is a demonstration of my final product. The breadboard with the button and LED lights is in the top right of the screen.