Raspberry Pi Clock

This project will combine the old and the new to make an electro mechanical clock which can obtain the correct time from the internet.
The clock will maintain the correct time via the Raspberry PI. The Pi obtains the time via a wireless dongle.
The I/O of the PI is protected by using a “Slice of PI” I/O board. The board communicates with the PI over the I2C interface.
The main parts are:
| PART | SOURCE |
| Raspberry Pi. | RS Components |
| Slice of PI I/O board | Maplin Electronics |
| Solenoid driver board | Individual Parts from Ebay |
| Stepper motor driver board | ebay |
| Old mechanical analogue clock mechanism. | ebay |
| Stepper motor to control the clock mechanism. | ebay |
| Westminster chimes taken from an old clock | ebay |
| Solenoids to strike the chimes. | ebay |
Block Diagram

Slice of Pi I/O Board

Slice of PI mounted on the Raspberry PI.

The Slice of Pi communicates with the Raspberry PI over the I2C interface. This enables two banks of 8 bits to be controlled as inputs or outputs.
Stepper Motor / Hand controller
A stepper motor drives the clock mechanism. The Slice of PI is only capable of supplying up to 25mA on each output. Outputs B0 to B3 are connected to a driver board based around the ULN2003 Darlington array. This enables a current of up to 500mA to be supplied .
Stepper Motor connection to clock mechanism

Chimes
Solenoids are used to strike the four chimes. When struck in the correct order the Westminster chimes will be heard.

Outputs a4 to a7 of the slice of pi are connected to the inputs of a ULN2003 Darlington array.

The corresponding outputs of the Darlington array are connected to the solenoid as per the circuit diagram above. The ULN2003 has reverse bias diodes built in to protect against back EMF from the solenoids
The solenoids were rated at 5V, but at 5V it was found that the plunger failed to strike the chimes with sufficient force. The minimum voltage required was 6.5V. When energised, each solenoid draw approx 150mA which is well within the 500mA limit of the ULN2003A.
Adjustment switches.
Four switches are connected to the GPIO pins of the Raspberry PI.
The table below shows the connections.
| Switch Number | GPIO Connection |
| S1 | G24 |
| S2 | G23 |
| S3 | G18 |
| S4 | G22 |


Power Consumption
A 9v PSU is regulated to 5V for the PI and 6.5V for the solenoids.
The following values were measured using a Fluke 87 Multimeter:
| Raspberry PI - During boot up | 460mA |
| Raspberry PI - When idling | 430mA |
| During rapid hand movement: | 500mA |
| With single solenoid energised: | 760mA |
| With all solenoid energised: | 1700mA |
The 9V supply needs to be able to supply 1700mA or 2A to be on the safe side.
Enabling The I2C Interface On The Raspberry Pi
Step 1 – Enable i2c using raspi-config utility
From the command line type :
sudo raspi-config
This will launch the raspi-config utility.

Now complete the following steps :
§ Select “8 Advanced Options”
§ Select “A7 I2C”
§ Select “Yes”
The screen will ask if you want the interface to be enabled :
§ Select “Yes”
§ Select “Ok”
The screen will ask if you want the module to be loaded by default :
§ Select “Yes”
The screen will state the module will be loaded by default :
§ Select “Ok”
§ Select “Finish” to return to the command line
When you next reboot the I2C module will be loaded.
Step 2 – Manually Edit Module File
Next we need to edit the modules file using :
sudo nano /etc/modules
and add the following two lines :
i2c-bcm2708
i2c-dev
Use CTRL-X, then Y, then RETURN to save the file and exit.
Step 3 – Install Utilities
To help debugging and allow the i2c interface to be used within Python we can install “python-smbus” and “i2c-tools” :
sudo apt-get update
sudo apt-get install -y python-smbus i2c-tools
Step 4 – Shutdown
Shutdown your Pi using :
sudo halt
Wait ten seconds, disconnect the power to your Pi and you are now ready to connect your I2C hardware.
Checking If I2C Is Enabled (Optional)
When you power up or reboot your Pi you can check the i2c module is running by using the following command :
lsmod | grep i2c_
That will list all the modules starting with “i2c_”. If it lists “i2c_bcm2708″ then the module is running correctly.
Testing Hardware (Optional)
Once you’ve connected your hardware double check the wiring. Make sure 3.3V is going to the correct pins and you’ve got not short circuits. Power up the Pi and wait for it to boot.
If you’ve got a Model A, B Rev 2 or B+ Pi then type the following command :
sudo i2cdetect -y 1
If you’ve got an original Model B Rev 1 Pi then type the following command :
sudo i2cdetect -y 0
Why the difference? Between the Rev 1 and Rev 2 versions of the Pi they changed the signals that went to Pin 3 and Pin 5 on the GPIO header. This changed the device number that needs to be used with I2C from 0 to 1.
I used a Pi 2 Model B with a sensor connected and my output looked like this :
pi@raspberrypi ~ $ sudo i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: 20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
This shows that I’ve got one device connected and its address is 0x20 (32 in decimal).
Python Scripts
Running Python scripts simultaneously
Sudo python script1.py & sudo python script2.py etc.
The software for the clock uses three Python scripts that run simultaneously.
PI-Clock, PI-Chimes and PI-Adjust
PI-Clock.
This script controls the hand movement, moving that hands forward one step approximately every 14.2 seconds.
Over a 12 hour period there are 3036 stepper motor pulses.
12 hours in seconds is 12 * 60 * 60 = 43200 seconds.
Therefore the time between each stepper pulse should be 43200 / 3036 = 14.229 Seconds.
PI-Chime
This controls the chiming function and plays the Westminster Chimes

The time is obtained via a web connection.
PI-Adjust
This script monitors the GPIO pins of the Raspberry PI and depending on which button is pressed will allow adjustment of the clock hands and will also enable the Pi to be shutdown.
Useful Information Download (ZIP)















