Bun-Bun mentioned in IRC that he was having trouble at work. As a machine shop, his employer has highly regimented work times, using a time clock to punch in and out and a buzzer that rings to signal the start and end of shifts and breaks. The problem was, the programmable relay they used to ring their 120V buzzer didn't keep time very well. People were complaining that the time wasn't accurate, but even just setting the time was quite an ordeal that required it be reprogrammed with a laptop. Between people being tardy on their shifts and complaining about the system, management had decided to invest money in an automated system to keep better track of time, but this system wouldn't fix the problem with the buzzer. So he wanted to build something simple that would synchronize its clock with the network using NTP (Network Time Protocol). This was the design he and I came up with.
Conceptually, it's a simple problem. The buzzer runs on 120V AC mains voltage -- the same as what is in your wall outlet. The buzzer doesn't require a lot of power, but being 120V, care has to be taken to ensure safety. As for turning it on and off, it's no more complicated than flipping a switch. A part called a "relay" handles this for us in a safe and convenient matter. It's basically a switch and an electromagnet. When you apply a small voltage to the coil of the electromagnetic, it pulls a lever that flips the switch from one side to other. Remove the voltage and the level falls back down, flipping the switch back to its natural position. This allows us to use a low voltage, low power signal to switch a much larger flow of power. It's also physically disconnected, making it very unlikely for the dangerous voltages on the high power side of the circuit to somehow get back into the low power side of the circuit.
The complicated part is turning on the switch at the appropriate time, but much of that complexity has already been taken care of. We decided to use a Raspberry Pi -- a $35 computer that runs a full Linux operating system. Not only is this as cheap or cheaper than a comparable Arduino, but software to communicate over a network and manage time is commonly available and easy to use. First, I installed Raspbian, a distribution of Linux based on Debian and designed to be used on the Raspberry Pi. I then used the "cron" program, which runs programs on a user-defined schedule, to trigger the buzzer at just the right time. To keep the time of the Raspberry Pi synchronized with the real time, I used the NTP service, as requested. NTP talks over the Internet to computers around the world, including many that use atomic clocks, and synchronizes our computer's clock with those clocks to within milliseconds. Linux comes with two programs for this: ntpd and ntpdate. By installing these and setting them to run when the system starts, the clock on the Raspberry Pi should always be on time.
I then wrote a simple script for "cron" to run when the time is right. This script uses one of the GPIO (General Purpose Input/Output) pins on the Raspberry Pi, turning the pin "on" for 3 seconds, then back "off". The script is as follows:
Code: Select all
#!/bin/bash gpio mode 1 out gpio write 1 1 sleep 3 gpio write 1 0
So now we have a computer, a program to keep the time synchronized, another one that runs a program at the appropriate time, and now a program that toggles the output pin on and off in response to all of this. Now there's only one small problem: the Raspberry Pi can't quite power the relay by itself!
Though digital electronics use very little power, the things they control often require much more power. Even small mechanical devices like our relay require considerably more current (the flow of electrical energy) than the chip on our little Raspberry Pi is prepared to deliver. So we need to build an amplifier. The amplifier works a lot like our relay does: it allows us to control a large flow of power using a little one. In this case, our amplifier will be a simple transistor in what is named an open-collector configuration. This allows us to use the 3.3V output pin, which can only drive up to about 16mA of current (see here), and drive the coil of the relay which requires 5V at about 100mA. Here's the circuit for it: Here's a high-level explanation of how this circuit operates. When the GPIO signal is low, the transistor is off and doesn't allow any current to flow through the coil of the relay. When the GPIO signal is high, it activates the transistor, allowing current to flow through the coil, causing the relay to flip its internal switch. The 220 ohm resistor controls just how much current is pulled from the GPIO pin, and as a result, how much current flows through the coil of the relay. This particular value will draw about 12mA from the GPIO pin. Assuming the gain of the transistor is at least 10 (though it's likely between 30-80), this will allow at least 120mA to flow through the coil, or as much of that as the coil can draw, which in our case is 100mA at 5V. The diode is there to prevent any damage to the transistor or the Raspberry Pi from the inductive kickback that occurs when we try to switch off the magnetic coil.
And that's it! The circuit was built and tested late Friday night using spare parts and tools that are available at the shop. Saturday afternoon I set up the software on the Raspberry Pi and made sure it worked.
Though the circuit works, there's still a bit of work to be done before it's ready to use in Bun-Bun's shop. It will need to be mounted to something secure to keep the circuit board from shorting out. It will also need to placed in some kind of enclosure to prevent somebody from accidentally touching the high voltage side and to protect it from dirt and other contaminants. It will also need to have a fuse or some other protection added to ensure any fault conditions on the high voltage side don't start a fire. And Bun-Bun will want to configure it so that he can easily connect to it over the network and make changes to the software as needed.
I forgot to take pictures of it at the time, but I'll see if Bun-Bun can snap a few photos to show how it turned out. There are also a few important points about the construction of the circuit that I'd like to mention, but they are much easier to illustrate with pictures.