NTP-synced work shift buzzer

Post Reply
User avatar
JamesCooper
Posts: 164
Joined: Tue Jan 11, 2011 11:46 pm

NTP-synced work shift buzzer

Post by JamesCooper » Sun Apr 14, 2013 5:22 pm

This project an idea that we came up with Friday and had built by mid-day Saturday using nothing but spare parts and tools in our shop.

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
This script is using the "gpio" command-line utility included with the WiringPi library. Although we don't need the rest of the library (as nice as it is), the command line tool makes it much easier and clearer to control the GPIO pins. The above script uses GPIO-18 (numbered as GPIO-1 in WiringPi: see here for the mapping between pins and numbers), tells it to act as an output (so it will drive a voltage, rather than just sensing it), sets it "high" (binary 1 or 3.3V), waits for 3 seconds, then sets it to "low" (binary 0 or 0V).

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:
gpio-relay.png
Schematic for an open-collector amplifier to drive a relay using a GPIO pin on the Raspberry Pi
gpio-relay.png (12.23 KiB) Viewed 6264 times
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.

User avatar
erikk
Posts: 12
Joined: Sun Aug 12, 2012 11:42 am

Re: NTP-synced work shift buzzer

Post by erikk » Wed Apr 17, 2013 9:41 pm

Something to check: Will the Rpi and/or network time know that it is in Saskatchewan and not one of those places where time jumps around by an hour twice a year?

jullman
Posts: 1
Joined: Sun Apr 07, 2013 12:01 pm

Re: NTP-synced work shift buzzer

Post by jullman » Thu Apr 18, 2013 8:53 pm

That was an awesome explanation. Thanks!

User avatar
JamesCooper
Posts: 164
Joined: Tue Jan 11, 2011 11:46 pm

Re: NTP-synced work shift buzzer

Post by JamesCooper » Fri Apr 19, 2013 3:48 pm

Thanks jullman.

erikk, good question. It does know because I previously configured the timezone for the system. It takes that into account when synchronizing time. I believe the timezone is set through the text file "/etc/timezone", which contains the string "America/Regina". I'm not sure how Linux stores the current time internally, but it can easily convert between timezones and will use the system timezone to display times unless otherwise requested.

User avatar
Bun-Bun
Posts: 23
Joined: Mon Nov 12, 2012 2:21 pm

Re: NTP-synced work shift buzzer

Post by Bun-Bun » Mon Apr 29, 2013 9:19 am

Just saw this. Great write up JamesCooper.

I will take some pictures of our build as well as the completed unit installed. I am currently working on an enclosure that will keep everything internal and allow external connections, ie via panel mount Ethernet and USB connectors. The mains side will be done by a hired electrician.

User avatar
Bun-Bun
Posts: 23
Joined: Mon Nov 12, 2012 2:21 pm

Re: NTP-synced work shift buzzer

Post by Bun-Bun » Tue May 28, 2013 1:52 pm

Image

Image

Image

Image

Image

Post Reply