Robotics and Hardware#
So far we’ve dealt exclusively in software. Whole new ballparks of fascinating capabilities can be reached by looking to hardware as well. Hardware can interact physically with us and our environment. And what a time to be alive in the hardware realm! Of course, to begin interacting with the physical world, it’s best to start with some user-friendly hardware. The exploration of hardware in this chapter builds on the skills introduced in Programming, since some coding is required. Fortunately, completely novice programming experience is more than enough to begin the journey. In fact, hardware projects often provide a thrilling motivation for expanding one’s programming skills.
Smartphones have an astounding number of hardware sensors and input devices on them, but unfortunately there’s no system that I’m aware of that provides entry-level access to the data, other than the Unity3D system discussed in Making games. Thus, in order to try out some hardware-related superpowers, it may be necessary to buy some (relatively inexpensive) hardware systems. This chapter is therefore simply explanatory.
Warning
Electricity is dangerous and can kill you and/or damage property. Taking an electronics course is a good way to get started with the kinds of projects discussed in this chapter if you are inspired to do so.
The era of cheap, user-friendly microcontrollers#
Since the early 2000s, the market has been flooded with circuit boards containing a low-powered CPU, some memory, digital and analog input/output ports (for interacting with the external world), and network capabilities. These are basically mini-computers that you can program with your normal computer and then disconnect to put in a robot, your garden, an art installation, and so on.
One such product is the Arduino, an open-source
microcontroller (the hardware design is open as well as the software) that really started
the cheap-microcontroller revolution. To use, you attach various sensors, buttons, lights,
or other circuits to it and then write a small program (called a sketch) on the
computer. You transfer the program via the USB port to the Arduino, where it gets stored
on the board and begins executing. These sketches are generally written in the C
programming language, which is lower-level than Python (C
is more powerful and faster
but harder to read and write). Getting started, many users keep it attached to the
computer’s USB port as a power source and/or to have it interact with the computer while
executing. For robots and other things away from the computer, a battery can be used for
power while the embedded program runs.
Large collections of add-on boards (called shields) have been built and are on the market that add additional features to Arduinos.
The Raspberry Pi is another extremely popular single-board computer. It has a much more powerful CPU than most Arduinos, an Ethernet port, a Wi-Fi card, an HDMI video port, an audio output port, and a port for an optional camera. It can run many of the same programs you use on your daily PC, like web browsers and word processors. It’s used for computer science education and has an absolutely huge hobbyist following. A Pi can even be set up to play collections of old video games (SNES anyone?). People run their entire home automation systems and self-hosted cloud drives with them (more on that later). Raspberry Pis are certainly more complex systems than Arduinos, but the extra power makes them practical for running higher-level programming languages (like Python), and the built-in peripherals make it more flexible.
Some basic peripherals#
You’ll need some hardware components to fully embrace the hardware superpowers. Here’s a list of some useful peripherals.
LEDs — Little lights that you can turn on and off as indicators in a game, on a control panel, or for thousands of other uses.
Resistors — fundamental electronic components that resist the flow of electricity. Often needed to keep too much electricity from flowing (e.g. often used in series with LEDs). Get a variety pack.
Prototype board — a board that you can plug circuit components into to make connections, but that’s easy to tear down as well. Used for experimenting and learning. Often when a design is finished, it’s transferred from a proto-board to a custom circuit board with solder.
Relay board — A series of electromagnetic switches that can be flipped on or off by small voltages (i.e. from a microcontroller). With relays, you can use a microcontroller to switch on and off something large like a heater or a large light. As always, electricity is dangerous and can kill you. Do not hook a relay up to some appliance without appropriate training and safety precautions. You can often hear relays clicking on and off, and you may recognize the sound from your turn signal relays in your car clicking on and off.
PIR motion sensor — Passive Infrared motion sensors are those white plastic motions sensors you see all over. They are cheap and sensitive. You can build things that respond to motion with them, like a security system or a Halloween product that yells when people walk by.
Sonar — Sonar uses sound reflections to measure distances. You can mount sonar peripherals on your projects to see how far away they are from a wall, or to increase audio pitch as something gets closer, or whatever.
Photoresistor — Responds to light. Can be used to build a flashlight-tag game, respond to darkness, or turn off a light when the sun comes up.
Transistors — Fundamental components similar to relays but with no moving parts and a continuum of responses, rather than just on and off. These are the basis of digital technology behind all of computer technology, but can also be useful for sending more current than a microcontroller can safely provide to a bright LED or a series of LEDs, among nearly infinite other things. You will need some for various examples during your training.
Patch cables — You’ll want a bunch of short multi-colored cables to connect your hardware pieces together. These are available with double-male, double-female, and mixed end connectors, and all three styles come in handy.
Power supplies — While a lot of fun, small stuff can run on USB power, you’ll likely want more power supplies for certain projects. For example, the LED light strips generally require 12V power (not the 5V that comes out of a USB port) and a lot of amps. If you want to control LED light strips you’ll need a power supply and a relay board. These power supplies are exactly what you plug your laptop and phone in with, but they have different voltages and amp ratings which can be chosen based on your needs.
An interactive art installation#
I’d like to share an example of what kinds of things become possible with the superpowers we are discussing. A Sweden-based artist named Mary Coble happened upon a project I had done using a Raspberry Pi to blink a laser in Morse code to communicate with light. She had used Morse code in previous projects and wanted to set a new one up. Patrons would type protest chants into a form on their phones, and the installation would beam them out in Morse code across the town. I agreed to help out, and so began a rewarding and exciting foray into the world of the art technician.
The project wasn’t simple. Mary had to do all the artistic design and physical implementation. I provided a shopping list (including a Raspberry Pi, LED lights, and relays), instructions on how to set up the Pi and wire the lights, and remote software support. Once Mary had the Pi on her network, I was able to remotely connect and add the software I had written to receive messages from a web page and switch the relays on and off accordingly.
In the end, the system had the following schematic:
It went well, and Mary did an even larger version of the system in her SHOUT FIRE exhibit at the Röda Sten Konsthall, in Göteborg, Sweden.
Note
For remote installations of any kind, a reverse SSH tunnel can be useful. Wherever your controller is plugged into a network, it will seek out and connect to your server. Once it does, you can connect to the remote device through the tunnel. If you want to change the behavior of the art installation once it’s installed, this can be a good option for doing so.
Working with this artist from across the Atlantic, whom I had never met in person, in such an engaging and cool project was really just wonderful. It felt like the internet really was bridging gaps in the way people had dreamed of in the early days. What an experience! [1]
Even cheaper microcontrollers#
One of the more astounding pieces of hardware is the Espressif 8266, or ESP8266. This is a very tiny board with a processor and onboard Wi-Fi capabilities. In bulk, it only costs $4! It’s now been succeeded by the ESP32 which has Bluetooth as well. You can find a single unit of either in a nice package online for about $10. These are super lower power and can be put in about anything you can imagine. These little guys can be the whole brains and communication system behind whatever Wi-Fi capable little gadget you can dream up if you were interested in going into the business of building and selling products. Since they’re so cheap, it’s no problem to dedicate them to seemingly trivial applications. For example, you can use them:
To add an internet-controlled thermostat to an old boiler/furnace. It’s basically a $4 Nest system
To add a wireless opening sensor to a cabinet (which could be used to set off an alarm or otherwise keep a child or friend away from something)
As a doorbell extender that senses when the doorbell goes off and plays a recording of the doorbell on the stereo upstairs
As the brains behind a star-tracking camera mount to keep the camera pointed at the same star for many minutes while the world turns
That last example deserves more explanation…
A star-tracking camera mount for astrophotography#
The Earth rotates 360° every 24 hours, making it difficult to take a long-exposure photograph of stars with the shutter open more than a few seconds without getting streaks. A simple solution is to put the camera on a mount that rotates the same exact speed but in the opposite direction, canceling out the rotation and keeping the stars deadlocked with the lens. You can buy a camera mount that does this, or you could make one yourself using your digital superpowers. One simple mechanism is to put two boards together with a hinge and have a motor slowly turn a screw to push the boards apart. This is called a barn-door mechanism, and looks schematically like this:
If you make the distance L
equal to 29 cm, a motor that turns at 1 RPM will give you
very close to the right speed. In that case, this project is fairly simple and no fancy
software is needed. But that rate will only work for short exposures. If you want very
long exposures, the motor has to speed up as the screw is inserted more. Mathematically,
just note that the long arm of the triangle in the figure gets longer as the screw slides
along the bottom of the upper board to see why.
If you use a stepper motor controlled by a microcontroller (like the ESP8266), you can precisely control the speed with which the motor turns, and you can even speed it up as time goes on to do the tangent correction (as its called) in software!! This is extremely cool and works very well.
Note
By the way, the Sun is a star like any other, and this project can be adapted with larger motors to track it with a solar-panel, increasing the efficiency of your solar installation. The software is the same!
Detailed pictures, source code, and construction steps for doing this exact project are available under an open-source license at partofthething/startracker
Controlling hardware directly from your laptop#
If you’d like to interface with some hardware but you don’t want or need to bother with microcontrollers, you can just use your laptop directly for many of these activities. You do need a bridge to the hardware world. When I first read about Bunny Hwang’s “heirloom laptop” that he added General Purpose Input/Output (GPIO) ports to, I was really jealous and thought that was a great idea. That would allow you to control relays, read signals, blink lights, read data from various sensors, and all sorts of things people do with microcontrollers right on a laptop. It turns out that you can get a USB device that adds GPIO ports to any computer quite easily.
The Adafruit FT232H breakout is one such device. Most of the demos you may find online about doing such things with an Arduino or Raspberry Pi can also probably be done with a board like this and software on your computer.
For example, if you have a simple digital level and you’d like to read the data in live to support some experiment or other project, this breakout board is perfect. An example usage would be to precisely measure and calibrate the rate of change of the angle in a star-tracker like the one just discussed.
This chapter briefly introduced you to a variety of ways to bridge the gap between computers and the physical world. I hope these concepts help bring you an “Ah-ha!” moment regarding something you’ve been dreaming of starting. Many devices on the shelves of stores can be built using combinations of the components you read about here. What new product can you dream up?
Footnotes