Oh, the magic of hardware
Not long ago, I showed you how to make the game Snake on JavaScript and Canvas. This is kind of cheating for a number of reasons:
While using JavaScript and Canvas was challenging and fun and everything, there is a way to make our game even cooler. Let’s build a hardware-based Snake!
At the core of the system, we’ll have Arduino. It’s a system that helps you build DIY robots, sensors, smart home solutions, and even some slow web servers, should you so desire. Arduino itself is a small board with components soldered onto it:
At the center of the board, there’s a chip that you can program in any way you like. To program it, you attach the Arduino to your computer and run special software that puts your code onto the chip.
See those black sockets on both sides of the board? That’s where you attach wires to carry electricity. The chip that you programmed can read the values of that electricity and act on them.
Before I go into Snake, here’s a simple home project you can do with Arduino to make sense of how the system works.
On one side, you connect the Arduino to two wires that lead to a humidity sensor. The sensor will output a voltage between 0.1 volt and 5 volts, corresponding to how humid the air is. Let’s say the main wire from the sensor is connected to Socket 1 on the Arduino.
On the other side of the Arduino, connect an LED light that can be either on or off. To fire it up, you need to send it 5 volts of current. Let’s say the LED is connected to Socket 2.
For the chip, you write some code that has the following meaning:
To the Arduino, it’s just a matter of taking a reading, doing some logic, and sending power to someplace on its board. The Arduino is unaware of what is attached to it. The Arduino simply routes electricity.
But to me, the user, this is a humidity system that alarms me once the air gets too dry. Currently, my system warns me with a light, but should I choose to, I could attach a buzzer instead. I could even attach an electric humidifier and have it turn on to remediate the dry air.
Arduino can work as an alarm system, a timer, a notification system, and more. But for us, it’s going to be a game.
For this project, we’re going to need:
An Arduino board. You can get the classic Arduino or an Arduino Nano. The mini version comes either pins, sockets, or soldering holes. If you want to avoid soldering, make sure you buy a model with either pins or sockets—like this:
A breadboard. This is a plastic board with metal leads underneath. It allows you to connect wires without soldering. Each column of the breadboard is a single lead. If you want to connect two wires, stick them into the same column.
Breadboards are a temporary arrangement. So, should you decide to make this game permanent, you’ll need to solder some components together. But for now, a breadboard will be good enough.
An LED matrix, 8 * 8. This matrix has two parts: an arrangement of LEDs that fire up on demand, and a controller board that communicates which LEDs to light up and when. The matrix will communicate with our Arduino board through five pins, and we’ll use the breadboard to connect the matrix to the Arduino.
A joystick. We’ll use this to control the snake.
A 10K rotary potentiometer. This device receives electrical current and makes it weaker by some amount. The more you turn the potentiometer’s handle, the weaker the current becomes. This means that if we know how much current we sent over to the potentiometer, and how much current we got back in return, then we can figure out how much the potentiometer was turned.
Some wires. Get a bunch of wires off Amazon or AliExpress. Make sure you get some wires with pins, some wires with sockets, and some wires with both pins and sockets. Wires are a dime a dozen, so just get a handful.
If this is your first Arduino project, let’s go over some basics and safety guidelines first:
Now with basics out of the way, it’s time to connect your components like so:
You’ll notice that:
If you’re having trouble connecting everything in this way, check out this video:
Now you need to connect your Arduino to your computer via the USB cable that came with the Arduino. The Arduino will start up and probably start blinking with an onboard LED, but nothing beyond that. At this point, it’s time to upload the software that will turn your hardware into a playable game.
First, go to Arduino’s website, download the Arduino IDE onto your computer, and launch it. (IDE stands for Integrated Development Environment.) Arduino IDE makes it easy for you to write code and upload it to your Arduino board.
Next, copy and paste the code for Snake from this developer’s page into your Arduino IDE, and press ‘Upload to Arduino’ (an arrow at the top left corner). The software will compile the code and send it over to the Arduino chip.
I won’t bug you with how the code works for now. We just want to get the firmware into the hardware so we can run our game. By the way, firmware means the kind of software that controls a device’s hardware.
Once the firmware sits inside the Arduino, the Arduino will start executing that code. The snake will run.
Turn the potentiometer to increase or decrease the snake’s speed. Move the joystick to control how the snake moves. The food will blink.
And done! Happy snakin’!
If you enjoyed learning about hardware, firmware, and making games, you may be interested in exploring Practicum. We offer online education and mentoring to help you build essential tech skills and amp up your career.