June 9th 2020
Chief Marketing Officer Practicum by Yandex, edtech expert, practicum.yandex.com
Oh, the magic of hardware
The core: Arduino
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.
Using Arduino: a quick example
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:
- Read the voltage from Socket 1 (the humidity sensor)
- If that voltage is above 2.5 volts, then do nothing for 30 seconds
- If that voltage is 2.5 volts or less, send 5 volts to Socket 2 (the LED light), do nothing for 60 seconds, and then stop sending power to Socket 2.
- Repeat until the heat death of the universe
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.
Collecting the gear
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.
Time for assembly
If this is your first Arduino project, let’s go over some basics and safety guidelines first:
- Arduino operates on tiny currents which won’t harm you even if you come in contact with them. However, once you start using external power supplies (like batteries), then improper wiring can cause damage—both to the Arduino and to people. Practice precaution.
- Disconnect the Arduino from any sort of power supply before you start assembly. While the Arduino is disconnected and there is no external power (like batteries), there is no current flowing, so nothing bad can happen.
- We connect the components with wires either directly or through the breadboard. The wires allow the current to flow between the Arduino and other devices.
- To connect anything through a breadboard, stick two pins into the same column (columns are numbered). Everything connected to a single column is interconnected.
- For current to flow, it needs a source and a drain. The current kind of flows from the source and into the drain. The drain can be called ‘Ground.’ If you connect a device to the source, but don’t provide the connection to ground, then the current won’t flow and the device won’t work.
- On some devices, you’ll see pins labeled VCC and GND. VCC is the source. GND is ground.
- If you connect the source and ground directly (without any devices or components in between), then the current will flow like crazy. This will make your wire heat up and eventually burn. This is called a ‘short circuit.’
- Never connect the source and the drain directly!
- Devices and components can share a common source and drain if there is enough power at the source.
- The term ‘VCC’ refers to the onboard power supply. Arduino has a 5-volt onboard regulator, which means that your VCC socket on the Arduino may be labeled 5V. It depends on which kind of Arduino you buy. What matters is that, for this project, VCC and 5V are the same.
Now with basics out of the way, it’s time to connect your components like so:
You’ll notice that:
- All the red wires are connected to 5V, or source. You can hook them all up to the red row on the breadboard and connect that row to Arduino’s 5V socket. That way, you’ll have a common source for all of your components.
- All the black wires are connected to ground. Connect all grounds to the blue line on the breadboard and hook up that blue line to Arduino’s ground. Now you have a common ground.
If you’re having trouble connecting everything in this way, check out this video:
Adding the firmware
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.
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’!