Introduction: HackerBoxes 0008: Clockwork
[cue theme music]
Clockwork: This month, HackerBox Hackers are exploring time, clocks, and timekeeping. There is no time like the present. This Instructable contains information for working with HackerBoxes #0008.
HackerBoxes is the monthly subscription box service for electronics hobbyists, makers, and hackers. Even if you are not a HackerBoxes subscriber, you can always join in the fun using your own materials and equipment. Of course, if you would like to receive a box like this right to your mailbox each month, please SUBSCRIBE HERE and join the HackerBoxes movement. Hack the Planet!
Step 1: HackerBoxes #0008: Box Contents
- HackerBox #0008 Collectible Reference Card
- Digital Clock Kit with Acrylic Case
- Ublox GY-NEO6MV2 GPS Module
- Ceramic GPS Antenna 25mm x 25mm
- Arduino Pro Mini 328P - 5V/16MHz
- PL2303 USB to Serial Module
- DS1302 Real-Time Clock Module
- ESP8266 ESP-01 Serial Wi-Fi Module
- TM1637 LED Clock Display Module
- MAX7219 LED 8x8 Matrix Module
- 40 DuPont Jumper Wires (F/F 10cm)
- Overclock Computer Decal
Step 2: What Is Time?
This is a simple question with no simple answer. Wikipedia offers, "Time is the indefinite continued progression of existence and events that occur in apparently irreversible succession from the past through the present to the future. Time is a component quantity of various measurements used to sequence events, to compare the duration of events or the intervals between them, and to quantify rates of change of quantities in material reality or in the conscious experience. Time is often referred to as the fourth dimension, along with the three spatial dimensions."
Like we said... no simple answer. Fortunately, we all sort of agree that we know what it is when we see it. We can generally employ phrases like, "I'll be there in an hour," without suffering any sort of philosophical crisis. That said, if you have the curiosity to invest in the scientific and philosophical aspects of our question, consider the magnificent video entitled The Illusion of Time from NOVA.
In the simplest, practical terms, time may be understood by simply showing how we measure it. For example, watching the movement of the hands on a clock gives a feeling for what a second is, or a minute, or an hour. Watching the sun, moon, and seasons gives us a feeling for longer units of time.
Step 3: How Is Time Measured?
In this brief interview, the well known physicist, Neil deGrasse Tyson, is asked what is time? He explains that, without really needing to know what it is, we can keep track of time. He suggests, "If you have something that repeats predictably, then you have created a timekeeping mechanism."
In nature, systems that repeat predictably are oscillators and timekeeping mechanisms are clocks based on those oscillators. Our earliest "clocks" were astronomical observations using orbits as their predictable oscillators. Later time keeping technologies used pendulums and springs as their predictable oscillators. Modern clocks generally use oscillation within a crystal lattice (e.g. a quartz wristwatch) or electrons hopping between orbits around an atomic nucleus (e.g. an atomic clock).
The official international standard for the length of one second is 9,192,631,770 cycles of the radiation needed for a cesium atom to vibrate between two energy states. For more information, have a look at the NIST Timekeeping FAQ.
Step 4: Electronic Oscilators
Planetary orbits, pendulums, springs, and atomic clocks can serve as a basis for time keeping because they have known repeating frequencies of oscillation. The frequency at which a system naturally oscillates as the system's resonant frequency. This concept is useful when considering a tuning fork or a guitar string as additional examples of mechanical oscillators.
A circuit combining a capacitor and an inductor (an LC Circuit) also has a resonant frequency given by the values of the capacitor (C) and of the inductor (L). This circuit (often referred to as a "tank circuit") is the basis of many electronic oscillators along with various astable multivibrators, other relaxation oscillators, and crystal oscillators.
Here are a couple of nice videos that survey basic electronic oscillators:
Looking back on previous HackerBoxes, Box #0004 (Step 4) explored the astable mode of the 555 timer chip functioning as an oscillator. Box #0001 leveraged a ring oscillator to create a seemingly non-synchronized flashing LED effect. And of course, experiments for every HackerBox have relied upon crystal oscillators to clock microcontrollers and other digital circuitry.
Step 5: Crystal Oscillators
Pretty much all digital circuits use crystal clock oscillators. This video from EngineeringGuy illustrates just how amazing these components are. Would you believe that there are actually SEVEN crystal oscillators in this HackerBox? No lie! Can you identify them all?
Step 6: Set Up the Arduino Pro Mini
The Arduino Pro Mini is a microcontroller board based on the ATmega328. It was originally designed by SparkFun Electronics. While less than one square inch in area, the Pro Mini features 14 digital input/output pins, six analog inputs, a crystal oscillator, a voltage regulator, and a reset button.
Because the Arduino Pro Mini was intended for integration into other systems, it arrives without pre-mounted headers, allowing the use of various types of connectors or direct soldering of wires. For our purposes here, we will be interfacing the Pro Mini using female DuPont jumper wires, so you should start by carefully soldering in the provided header pins.
WARNING: If you are new to soldering, really take your time with this. Go very, very slowly and take great care. There are a lot of great guides and videos online about soldering. Here is one example. You might want to find an old junk PCB for some practice (un)soldering. If you feel that you need additional assistance, consider joining a local makers group or hacker space in your area. Also, amateur radio clubs are always excellent sources of electronics experience.
TIP: You might want to start by only soldering the six pin header on the short edge of the Pro Mini board. These pins will be enough to let you power up the board and perform the verification process below and in the next step where we observe operation of, and then modify, the "blink" sketch. Once that basic level of operation is confirmed, you can go back and very carefully install the two longer headers.
Note that the Pro Mini does not have an on-board USB port. Instead, the six pin header (on the short side) provide a serial interface and power inputs that can be connected to a USB module. The USB module then provides power and communication to the board form a USB port on a PC. We'll be using a USB to Serial module based on the PL2303 chip (datasheet).
Wiring the Pro Mini to the PL2303 is fairly simple using four female-to-female DuPont jumpers. There are two power pins (3.3V and 5V) on the PL2303. We will only be using the 5V pin, since we are working with a 5V Pro Mini. Also note that the TX on one board goes to the RX to the other and vice versa. Take a moment to consider why.
- PL2303 <----> PRO MINI
- 5V <----> VCC
- GND <----> GND
- TX <----> RX
- RX <----> TX
Unless your PC has been used with a PL2303 USB/Serial chip before, you will most likely need to install a device driver. The best source is straight from Prolific (manufacturer of the PL2303 chip itself). Their site provides Linux, OSX, and Windows drivers.
Once you have the two boards wired together and the driver installed, plug the PL2303 module into a USB connector on your PC. The power LED on the PL2303 should light up. Then, some pre-loaded code on the Pro Mini will start running. That code blinks an LED on the Pro Mini.
Step 7: Loading Code Onto the Arduino Pro Mini
If you haven't used an Arduino with your PC before, you will need to install the Arduino Software onto your computer. This software is often referred to as the Arduino IDE (short for integrated development environment).
Once the IDE is installed, make sure the PL2303 (wired to the Pro Mini) is plugged into your PC and launch the IDE.
Next, tell the IDE which board we are using:
tools->board: “Arduino Pro or Pro Mini”
And also which port:
tools->port: “usbserial” (or something indicating the PL2303 "port")
Finally, let's open some source code:
This is actually the code that was preloaded onto the Pro Mini and should be running right now to blink the LED. Accordingly, if we load this code as it stands nothing will happen. Instead, let's modify the code a little bit. Looking closely, you can see that the program turns the LED on, waits 1000 milliseconds (one second), turns the LED off, waits another second, and then does it all again - forever.
Modify the code: change both "delay(1000)" statements to "delay(100)"
This mod will cause the LED to blink ten times faster, right?
Let's load it to board and check.
- Press and Hold the reset button on the Pro Mini
- Click the UPLOAD button (the arrow icon) just above your modified code
- Watch below the code for the status info: "compiling" and then "uploading"
- As soon as it switches to "uploading" release the reset button on the Pro Mini
- The IDE should now say "Uploading Complete" and your LED should be blinking faster
If so, congratulations! You have just hacked your first code. If not, try again, sometimes the timing of the reset release is a little off. The blue transmit LED on the PL2303 module should light up steady for a second or two while the upload occurs. When you don't see this (followed by an error on the IDE), the reset release is generally to blame.
Can you change the code again to cause the LED to blink fast twice and then wait a couple of seconds before repeating? Give it a try! How about some other patterns?
As a bit of an aside, but in keeping with our time theme, here is an explanation of why you generally do not want to use the delay() function aside from really simple situations like what we've done here.
Step 8: Keeping Time With Software
So we've seen the delay() function, but how does the Pro Mini know what a millisecond is when we tell it to delay a given number of milliseconds. Well... It has a crystal oscillator of course. The tiny, surface-mount, 16MHz crystal oscillator is connected between the PB6/XTAL1 pin and the PB7/XTAL2 pin of the ATmega328 chip as highlighted here. Note that "xtal" is a common abbreviation for the word crystal.
The Arduino Time Library can use the internal clocking (from the crystal oscillator) to provide timekeeping functionality. The library allows a sketch to get the time and date as: second, minute, hour, day, month and year.
The Time Library includes an example sketch called TimeSerial. Open this code up. Also, open the serial monitor and set its baud rate to 9600. Finally, program TimeSerial to the Pro Mini and let it rip. You can set the time in the serial monitor by typing the letter T followed by ten digits. These digits are the number of seconds since January 1, 1970. For example, you can set the time to noon on January 1, 2013 by entering "T1357041600"
The number of seconds since January 1, 1970 is known as Unix Time or Epoch Time. Here is a website for converting human readable date/time into epoch time. You can use this with the serial monitor "T" command to set the time to the actual time if you like.
This approach may seem to work great, but there is one big shortcoming to keeping time with software. The timekeeping stops when the software stops. For example, when we turn the power off. When we power back up, we will have to set the time again. In practice, this isn't very useful.
Step 9: Interfacing to Real-Time Clock (RTC) Hardware
A real-time clock (RTC) is a computer clock, generally in the form of an integrated circuit, operable to keep track of the current time. RTCs usually have an alternate source of power, so that they can continue to keep time while the primary source of power is off or unavailable. This alternate source of power may be a coin/button cell or a supercapacitor. The alternate power source can also supply power to battery backed RAM. In older personal computer systems, this battery backed RAM was often used to store BIOS configuration settings. When the battery failed, the user would generally have to boot the computer into the BIOS to reset the date/time and possibly settings such as the hard disk parameters or boot order.
A common example RTC integrated circuit is the DS1302 (datasheet). It's a trickle-charge timekeeping chip containing a real-time clock/calendar and 31 bytes of static RAM. It uses very little power and can be maintained by a small coin cell. The DS1302 can communicate with a microprocessor via a simple serial interface.
The DS1302 module has five pins. Power, Ground, Clock, Data, and Reset. The Reset pin is also sometimes referred to as CE (chip enable). As shown here, the five pins can just be wired straight through to pins 5-9 of the Pro Mini. Hold on? Why would we tie the VCC (power) and Ground pins onto I/O pins? Good catch. That is generally a bad idea since I/O pins cannot source/sink very much current. In this instance, we can allow it since the DS1302 uses very little power. We just have to be sure to configure those I/O pins as outputs and then drive the one associated with the RTC's VCC to high (5V) and the one assoicated with the RTC's GND to low (0V). There are proper VCC and GND pins available on the Pro Mini, but we're going to use them for something else in the next step.
Download and install the DS1302 Library.
The example code for this library also uses the streaming library, so install that as well.
Bring up the example called SetSerial that comes with the DS1302RTC library. Before compiling it, we need to set the five pin numbers in the code to what we have them wired up to. In our example, we have the I/Os wired to the RTC modules like so for pins 5-9:
// Set pins: CE, IO, CLK
DS1302RTC RTC(9, 8, 7);
// Optional connection for RTC module
#define DS1302_GND_PIN 6
#define DS1302_VCC_PIN 5
Those 5 pins numbers are the only things that need to be changed in the example code.
Before loading this code, open up the serial monitor and set the baud rate to 115,200.
Load the code (remember the reset button) and let the code run.
The RTC can be set from the serial monitor by entering the time in this format: year, month, day, hour, minute, second. For example:
"2016,6,15,8,30,0" for June 15, 2016 at 8:30.
You can use this interface to easily set the RTC time to your actual current time.
The example program will then also read the time from the RTC and show it on the serial monitor.
You can cut the power (unplug the USB port) and then notice that when you boot the Pro Mini back up (you may need to restart serial monitor) it is reading the correct time from the RTC. Success!
Step 10: Seven Segment LED Clock Display
A seven-segment display is an electronic display device for presenting decimal numerals. Seven-segment displays are widely used in digital clocks, electronic meters, basic calculators, and other electronic devices that display numerical information.
The TM1637 is a chip for driving seven-segment displays. There are several modules using this chip to form a four digit numerical display module.
We can install a TM1637 Library into the Arduino IDE.
Next, wire the four pins from the modules to the Pro Mini as shown in the photo:
- TM1637 <----> Arduino Pro Mini
- CLK <----> 3
- DIO <----> 2
- VCC <----> VCC
- GND <----> GND
There is a cool example that comes with the TM1637 library called "Basic" it displays some messages and numbers onto the LED segments.
When you load up the "Basic" example, just change these two lines to match the correct pins:
const byte PIN_CLK = 3; // define CLK pin
const byte PIN_DIO = 2; // define DIO pin
Then upload and give it a go.
Once the TM1637 display is working, let's use it to display the time from the RTC. The program called RTCtoLCD shown here is a nice simple example. You've just made a basic digital clock!
Step 11: 8x8 LED Matrix for Binary Clock or Seconds Counter
The 8x8 LED Matrix Module is a small PCB supporting an 8x8 LED matrix wired to a serial interface MAX7219 LED driver chip (datasheet). The module has pins on each side so that multiple modules can be easily wired in a chain. This might be useful for passing through the 5V and ground signals to another module given the limited number of power pins on Pro Mini.
The 8x8 LED Matrix Module can be used to display the time in binary instead of using the seven-segment display. Take a look at these examples for some inspiration:
Also, you could use the 8x8 LED Matrix Module along with the seven-segment display to display a seconds counter. Since there are 64 LEDs in the matrix, we can fill in one per second until the matrix is nearly full and then begin again for the next minute. Can you write code to make the matrix fill in interesting patterns? Random, Raindrops, Middle-Out, Sand in an Hourglass, Spiral, etc?
Step 12: SatNav - It’s About Time
Satellite navigation (SatNav) uses a system of satellites to determine positioning information. A SatNav system with global coverage is referred to as a global navigation satellite system (GNSS). The United States operates the NAVSTAR Global Positioning System (GPS) and Russia operates GLONASS. China is in the process of expanding its regional BeiDou Navigation Satellite System into the Compass GNSS. The European Union operates the Galileo GNSS scheduled to be fully operational by 2020.
SatNav systems allow small electronic receivers to determine their location (longitude, latitude, and altitude/elevation) to high precision (within a few meters) using time signals transmitted along a line of sight by radio from satellites. The signals also allow the electronic receiver to calculate the current local time to high precision.
GPS satellites continuously transmit their current time and position. A GPS receiver monitors multiple satellites and solves equations to determine the precise position of the receiver and its deviation from true time. At a minimum, four satellites must be in view of the receiver for it to compute four unknown quantities (three position coordinates and clock deviation from satellite time).
FYI, you can download a print resolution version of the GPS poster shown here or even request a printed copy for a classroom.
Step 13: Receiving GPS Time
The GY-NEO6MV2 GPS receiver has a ublox NEO-6M module on board.
We need to solder to the interface pads on the receiver. Use some header pins, or just solder stripped DuPont wires directly if you don't have any header pins. Now, wire the GPS receiver to the Arduino Pro Mini using three connections.
- GPS Module <----> Arduino Pro Mini
- TX <----> PIN 2
- VCC <----> VCC
- GND <----> GND
- RX <----> DO NOT CONNECT
Download the TinyGPS Library for the Arduino IDE.
Load up the "TimeGPS" example from the Arduino Time Library.
Change the SoftwareSerial Pins to 2 and 3 like so:
SoftwareSerial SerialGPS = SoftwareSerial(2, 3); //RX on 2
Also, bump up the GPS serial rate from 4800 baud to 9600 like so:
Open the serial monitor. Set it to 9600 baud. Upload the code (remember the reset button).
The first time you operate the GPS receiver, it will take a few minutes to detect and synchronize with enough birds. Once it does, the green LED on the receiver will start blinking. After that, you will see GPS time stamps streaming in on the serial monitor.
Step 14: Operating Voltage and the Ublox NEO-6M
The ublox NEO-6M is a 3.3V device, but the Pro Mini is 5V.
As you can see from the GPS receiver schematic, there is an on-board 3.3V regulator, so VCC into the module can be either 3.3V or 5V and the regulator will fix it.
The TX signal from the GPS receiver is only 3.3V, but that is still enough to trigger an input on the Pro Mini.
However, the TX signal from the Pro Mini will be 5V and the RX pin into the GPS receiver expects to see a signal that is 3.3V max. That is why we did not connect that signal in the previous step. It also wasn't needed, so that was totally fine. However, if you do want to hook up the serial signal transmitting from the microcontroller and receiving into the GPS receiver, please take caution. In practice, leaving the signal at 5V seems to work just fine, and you would probably be safe just hooking the line up and not worrying about it. That said, best practices would dictate using a voltage divider as a level shifter on the TX line coming out of the Pro Mini before inputting into the RX line of the GPS receiver. The example voltage divider shows how it works.
The actual resistor values do not matter, only their relative values. The smaller (top) resistor should be 2K-20K with the larger (bottom) resistor being very close to twice that value.
Step 15: Receiving GPS Signals on a Computer
Wire the three GPS receiver pins (VCC, GND, TX) directly to the PL2303 pins (5V, GND, RX).
Plug the PL2303 into a PC and GPS data will come streaming in at 9600 baud in NMEA format as specified by the National Marine Electronics Association (NMEA).
If you are running Windows, you can download u-center, which is an GUI interface for ublox GPS modules. It can record, visualize, and analyze GPS data.
Step 16: Network Time Protocol and the ESP8266
Network Time Protocol (NTP) is a networking protocol for clock synchronization between computer systems over packet-switched, variable-latency data networks. In operation since before 1985, NTP is one of the oldest Internet protocols in current use.
Of course, to get the time using NTP, you need to be connected to a network. What follows is an advanced exercise leveraging the the ESP-01 module. The ESP-01 is based on the ESP8266 chip, which is a low-cost Wi-Fi chip with full TCP/IP stack and microcontroller core.
The ESP-01 is a 3.3V board and will not work at all with 5V. As such, so you have to take great care if you want to use it with with the 5V Pro Mini. For example, you can pull the 3.3V supply from the PL2303 and use a voltage divider to level shift the RX input into the ESP8266 from 5V to 3.3V.
Better yet, it turns out that the ESP8266 actually has a processor core right inside it that is possible to program directly from the Arduino IDE with a little hackory:
- Interface from to the ESP8266 using the PL2303
- Use the 3.3V supply and a voltage divider as shown in the schematic
- Modify and configure the Arduino IDE according to this guide
The Example program "TimeNTP_ESP8266WiFi" from the Arduino Time Library shows an example of how to pull time via NTP over an ESP8266 Wi-Fi connection.
Step 17: Radio-Controlled Clocks
A radio-controlled clock (RCC) is a clock that is automatically synchronized by a time code transmitted by a radio transmitter connected to a time standard such as an atomic clock. Such a clock may be synchronized to the time sent by a single transmitter, such as many national or regional time transmitters. This technology may be used to automatically set clocks or for any purpose where accurate time is needed.
WWVB is a time signal radio station near Fort Collins, Colorado and is operated by the National Institute of Standards and Technology (NIST). Most radio-controlled clocks in North America use WWVB's transmissions to set the correct time. The 70 kW ERP signal transmitted from WWVB is a continuous 60 kHz carrier wave, the frequency of which is derived from a set of atomic clocks located at the transmitter site, yielding a frequency uncertainty of less than 1 part in 1012. A one-bit-per-second time code, which is based on the IRIG "H" time code format and derived from the same set of atomic clocks, is then modulated onto the carrier wave using pulse width modulation and amplitude-shift keying. What is now WWVB began as radio station KK2XEI in July 1956.
Step 18: Assemble a Digital Clock With Acrylic Housing
This video shows a slightly different PCB, but it is generally the same kit and can be followed more or less to assemble the kit. An little instruction sheet is included with the clock kit, but it is a somewhat difficult to understand. Hopefully this is a little clearer:
1. Components are soldered onto the back of the board (where the resistor, capacitor, and microcontroller silkscreens are printed). Start by soldering the lowest profile components such as the crystal, resistors, and capacitors first. Next, solder the battery holder.
2. Solder the socket for the MCU chip and the socket for the RTC chip. The semicircular pin-one-indicator on the PCB silkscreen needs to line up with the one on each if the sockets. Next, solder the switches, buzzer, and power socket.
3. Using diagonal cutters, cut-off the excess wire leads from the opposite side of the PCB. Make these as short as possible since the LED display will be seated on that side of the PCB and you want to avoid shoring any lines together.
4. Solder the LED display last. It is placed on the front of the PCB opposite form the other components.
5. Insert the two integrated circuit chips being careful to note the direction of each with respect the semicircular pin-one-indicator on the sockets.
To set the time and/or the alarm, there are two buttons: SET and ADJUST. Use the SET button to select which value is being adjusted and then the ADJUST button to change the value.
A 3V backup battery can be installed to prevent having to reset the time whenever the external power is removed. The battery holder should be able to grip a CR1220 3V lithium button cell.
Step 19: Anything Worth Doing...
Anything worth doing is worth overdoing!
This impressive EEVblog details how to design a digital clock from 4000 series CMOS chips. Note that the time is based on the 50Hz AC oscillations of the power mains. Would this clock work in the United States?
This amazing piece of work is fabricated from 777 transistors, 1233 resistors, 136 LEDs, and half of a kilogram of solder.
In recent news, overclockers push Intel's crazy 10-core Broadwell-E chip to 5.7GHz. Liquid nitrogen is used to keep the chip cool while it runs at almost double standard speed.
Stop the Insanity!
Step 20: Hack the Planet
We hope you appreciate this little reminder to never take time for granted. If you enjoyed this Instrucable and would like to have a box like this delivered right to your mailbox each month, join us by SUBSCRIBING HERE.
Please share your success in the comments below and/or on the HackerBoxes Facebook page. Certainly let us know if you have any questions or need some help with anything. Thank you for being part of the HackerBoxes adventure. Please keep your suggestions and feedback coming. HackerBoxes are YOUR boxes. Let's make something great!
5 People Made This Project!
We have a be nice policy.
Please be positive and constructive.