Inside the PowerSwitch Tail, there are a few electronic
components that help you control high-voltage devices
like a lamp or blender by using a low-voltage signal such
as the one from the Raspberry Pi. The “click” you hear
from the PowerSwitch Tail when it’s turned on or off is
the relay, the core component of the circuit inside. A
relay acts like a switch for the high-voltage device that
can be turned on or off depending on whether the low-
voltage control signal from the Raspberry Pi is on or off.
Scheduling Commands with cron
So now you’ve packaged up a few different commands into two-
simple commands that can turn a pin on or off. And with the lamp
connected to the Raspberry Pi through the PowerSwitch Tail, you
can turn the lamp on or off with a single command. Now you can
use cron to schedule the light to turn on and off at different times
of the day. cron is Linux’s job scheduler. With it, you can set com-
mands to execute on specific times and dates, or you can have jobs
run on a particular period (for example, once an hour). You’re going
to schedule two jobs: one of them will turn the light on at 8:00 p.m.,
and the other will turn the light off at 2:00 a.m.
As with other time-dependent programs, you’ll want to
make sure you’ve got the correct date and time set up
on your Raspberry Pi, as described in “Setting the Date
and Time” on page 47.
To add these jobs, you’ll have to edit the cron table (a list of com-
mands that Linux executes at specified times):
root@raspberrypi:/home/pi/# crontab -e
This will launch a text editor to change root’s cron table (to change
to the root user, type sudo su). At the top of the file, you’ll see some
information about how to modify the cron table. Use your arrow
Basic Input and Output 105
GSW_RASPI_4ED_FIN.indd 105GSW_RASPI_4ED_FIN.indd 105 10/28/21 10:54 AM10/28/21 10:54 AM