Friday, 22 November 2013

Knock Clock with DS1307 continued!

Last post we designed a circuit to make a knock clock....Here is the circuit construction process:

1.  Obtain copper clad circuit board and print out bottom layer reversed on 120gm glossy paper using a laser printer.

2.  Using a clothes Iron to transfer bottom layer design to copper clad board.

3.  Once the design has been transferred soak off the excess paper with water to leave only the required copper.  Then etch the PCB using the preferred etchant - I used Ferric Chloride.

4.  After twenty minutes or so the copper should be etched away - monitor the process closely

5.  Once the etching is complete clean off the black ink using something abrasive - I used steel wool.  

6.  Finally drill out the holes using suitable drill sizes.  I used 0.8mm for every hole and then increased the FET, regulator and screw terminal holes to 1mm.

7.  Next populate the PCB with the components.  Make sure the IC socket (if you are using one) is orientated correctly along with the FET and the regulator and diode.  All of the other components are reversible.

Once that is complete we are onto the next stage.  I have to admit at this point I applied 12Vdc to the input to see if the red LED would light - it shows all of the power supply section is working - it was after I removed the LED and connected it with the correct polarity.  I always seem to get something wrong!

Next I flashed the arduino bootloader onto the ATMEL 328p chip using a dedicated ATMEL chip programmer.  There are loads of instructions available on how to achieve this:

How to flash a bootloader onto an ATMEL 328p

Once that was complete I loaded blink onto the target IC and connected an LED between MOSI and GND.  The LED flashed on and off - SUCCESS!

After that moment of awesomeness I loaded the code that I have developed earlier to achieve the knock clock functionality required.  I have to admit I had already done this as I breadboarded the circuit before populating the PCB...

Before doing all of this I populated the DS1307 adafruit RTC kit following the instructions on their website. I also then set the time on device using the serial terminal.  The instructions for doing this are:

Adafruit DS1307-real-time-clock-breakout-board-kit

The final Knock Clock code was developed from several libraries and some fairly standard programming.

Here is the code:

 * Langster's Knock Clock Code
 * Forked from the TimeRTC Library example
 * The Knock Sensor Example from the Arduino Library
 * and some simple code to drive the solenoid from  
 * the FET - 22-11-2013

#include <Time.h>       // Include time.h library
#include <Wire.h>       // Include Wire.h
#include <DS1307RTC.h>  // Include DS1307.h Library

byte val = 0;           // Variable to store a byte value

int solenoidPin = 9;    // FET solenoid Gate is connected to Digital pin 9
int knockSensor = A0;   // Piezo sensor attached to analogue zero            
int THRESHOLD = 10;     // integer variable that sets knock sensitivity
int hoursLoop = 0;      // integer variable for hours loop
int minutesLoop = 0;    // integer variable for minutes loop

void setup()  {
  Serial.begin(9600);         // start serial terminal at 9600 baud for debugging
  while (!Serial) ;           // wait until Arduino Serial Monitor opens
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet) 
     Serial.println("Unable to sync with the RTC");
     Serial.println("RTC has set the system time");

  pinMode(solenoidPin, OUTPUT); //Set solenoid pin as a digital output    

void loop()
    val = analogRead(knockSensor);     // check for a knock!
    if (val >= THRESHOLD) 
      Serial.println("Knock!");        // knock detected!
      if (timeStatus() == timeSet) 
        digitalClockDisplay();         // display the time on the serial terminal
        actuateSolenoid();             // Drive the FET to actuate the solenoid
        Serial.println("The time has not been set.  Please run the Time");
        Serial.println("TimeRTCSet example, or DS1307RTC SetTime example.");

void digitalClockDisplay(){
  // digital clock display of the time

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  if(digits < 10)

void actuateSolenoid()
int convertHours = hour();  //convert from 24hour clock to 12 hour clock 
    if (convertHours > 12)
          convertHours = convertHours - 12;
  Serial.print("convertHours: ");
  Serial.println(" ");
  //drive FET to actuate solenoid for number of hours
  for (hoursLoop=1; hoursLoop <= convertHours; hoursLoop++)
       Serial.println(" ");
       digitalWrite(solenoidPin, HIGH);
       digitalWrite(solenoidPin, LOW);
    delay(1000);  //pause between hours knocks and minutes knock

  int convertMinutes = minute();         //convert minutes to tens of minutes
  convertMinutes = convertMinutes / 10;
  Serial.print("convertMinutes: ");
  Serial.println(" ");
  //drive FET to actuate solenoid for number of minutes
    for (minutesLoop=1; minutesLoop <= convertMinutes; minutesLoop++)
       Serial.println(" ");
       digitalWrite(solenoidPin, HIGH);
       digitalWrite(solenoidPin, LOW);


The code pretty simple and the comments should explain each section clearly.  Essentially the arduino constantly polls the piezo sensor for a knock.  If a knock is detected the time is read from the real time clock device (DS1307).  The Hour data is then converted from 24 Hour clock format to 12 Hour format and the Solenoid is actuated for the number of Hours.  There is a short delay and then the minutes data is converted to tens of minutes and again the solenoid is actuated for the number of minutes.  The program then loops back to the start polling the piezo sensor for another Knock!

I will post a video of the circuit working when I get a chance!  The next post will discuss making a laser cut box for the Knock Clock before final assembly!  That is it for now though - Enjoy Langster!

Sunday, 17 November 2013

Knock Clock Using DS1307 and an Arduino

Knock Clock!

A Knock Clock is a electro mechanical clock which tells the 'knocker' the time in audible 'knocks' to +/-10 minute accuracy.  My better half saw one of these recently and asked me to make her one.  This is my own implementation based on a circuit made by a member of the Manchester Hackspace - Paul Plowman!

The device itself is a basically a box with some electronics and a device for striking the side of the box to create the 'knock' sound.  In this case I am using an electromagnetic plunger known as a solenoid.  It is normally used to control valves or mechanical devices from an electronic controller.  An electro-mechanical relay is essentially made up of a solenoid and a switch.  More information about solenoids can be found via the internet and wikipedia!

Wikipedia's entry on Solenoids

The basic idea of the circuit is, as with most electronics:

  • Take an input (the user 'knocking on the enclosure). 
  • Process this input (tell the processor to output the current time). 
  • Provide some form of output (cause the solenoid to actuate the time in hours and tens of minutes).

In this case the input is going to be sense the user knocking on the enclosure using a piezo buzzer as a simple microphone.  Piezo crystals are a useful electrochemical crystalline structure which when force is exerted on the structure a high voltage low current signal is generated.  It is the main component used is electric stove gas lighters and certain types of cigarette lighter.  More wikipedia below:


Once the input has been detected it is necessary to process this input and then provide an output.  To this end I am going to use the ever popular Atmel 328P microcontroller with the arduino bootloader.  However I am not going to design a shield this time as I wanted to try to keep the cost of construction down.  Using an arduino main board for every project can get expensive - even using clones is becoming expensive!  So we have an input which we then need to compare to a known value - time!  How does the microcontroller know what the time is?  We could set the time on the microcontroller via the serial terminal each time the device is connected to a computer or we could use an ethernet controller and use the same technique; we could hard code the time from when the microcontroller was programmed and use that - best make sure the power is never removed!  Or we could use a real time clock...I have decided to use a real time clock as this is in my opinion the best method to use ensuring that the clock keeps reasonable time.

Real time clocks are special integrated circuits which communicate via the I2C protocol directly to the microcontroller and are battery backed up which means that they are constantly powered by a small battery and never lose the time (once it has been set) even if power to the main circuit is removed.  The accuracy over time is not always great (they lose a second each month) and the battery normally lasts about 5 years. There are several real time clock modules available to buy and rather than implement the circuit for myself I have bought one of these modules to save time.  The real time clock device used in the module is based on the ubiquitous DS1307 by Maxim Semiconductor.

DS1307 datasheet

The module I am using was provided by the excellent and very helpful Adafruit Industries:

DS1307 RTC Kit

Now that we have a method of receiving and processing the input it is necessary to drive the output - a solenoid. Solenoids are fairly common devices and can be treated in much the same was as a relay - although a power hungry relay!  It often takes a lot of current to drive a solenoid and the circuit will need to provide significant power (voltage and current) in order to make the solenoid actuate.  The best way to achieve this is to use a Field Effect Transistor.  I have discussed FETS in previous blog posts so I'm not going to go through this again - please read my previous posts.

We need an N-Channel metal oxide silicon field effect transistor ( N-MOSFET) with the following parameters:

Vds - 12V - I have decided to use 12V as the main voltage input
Vgs threshold - up to 5V - we need a logic level FET capable of being driven from the microcontroller
Ids - 1A or higher - we need a device capable of driving the Solenoid
T0220 Package - I like big through hole parts - they are easy to solder!   

There are probably over a hundred devices that meet these requirements.  If we do a search using Farnell Electronic's parametric search facility the following results are found:

We could use any of these N Channel-MOSFET devices and the VGS threshold voltage is the most important parameter as we need the device to work from a 5V digital signal from the microcontroller.  As we don't have any particular preference I am going to choose the least expensive device to keep costs down and the cheapest device is one I already own!

The device I have chosen is the IRF630 by ST Microelectronics - the datasheet is below:

IRF630 Datasheet

It meets all of the requirements and costs a very reasonable £0.56 from Farnell Electronics.

So....we now have all of the parameters for the Knock Clock decided we can go ahead and design the circuit.  I have decided to use a basic implementation of the arduino using an Atmel 328p.  The circuit will not have the serial to USB converter or a method of programming the microcontroller so we will have to provide a method of programming and talking to the microcontroller.  The rest of the circuit is standard boiler plate electronics - A 12V to 5V linear regulator, a piezo electric speaker (being used as a microphone) and a MOSFET driving a solenoid.  The centre header is for the real time clock module. The circuit is shown below:

I have included a six pin programming header to initially 'flash' the arduino bootloader onto the microcontroller and a header to allow communication with the microcontroller via a USB to serial cable.  The 12Vdc input will be provided via a standard dc barrel jack or via 5mm screw terminals.  The output to the solenoid is provided also via 5mm screw terminals.

Here is the PCB layout for the bottom layer:

Here is the top layer with the component identification:

The bill of materials for this project is as follows:

Part Value Device Description
C1 22pF Ceramic capacitor Capacitor
C2 22pF Ceramic capacitor Capacitor
C3 0.1uF Electrolytic Capacitor Capacitor Polarized
C4 10uF Electrolytic Capacitor Capacitor Polarized
C5 10uF Electrolytic Capacitor Capacitor Polarized
C6 100nF Ceramic capacitor Capacitor
C7 100nF Ceramic capacitor Capacitor
D1 1N4001 Axial rectifier Diode
IC1 LM7805 T0220 package  Voltage Regulator
J1 Programming Header 6x 0.1' pitch header pins 6 pin I2C programming header
J2 12Vdc power jack 3.5mm dc power jack Power Jack
JP1 6 pin Header 6x 0.1' pitch header pins Serial Communications Header
JP2 5 pin Header 5x 0.1' pitch header pins RTC Header
JP3 5mm Screw Terminal 5mm screw terminal phoenix connector 12Vdc input from screw terminals
JP4 5mm Screw terminal 5mm screw terminal phoenix connector Solenoid output from MOSFET
KK1 Heatsink T0220 Heatsink Heatsink for MOSFET
LED1 5mm Red LED 5mm Red LED Power LED
Q1 IRF630 MOSFET N-CHANNEL Common logic level MOSFET
R1 10k 1/4W axial resistor Resistor
R2 220R 1/4W axial resistor Resistor
R3 1k 1/4W axial resistor Resistor
R4 100k 1/4W axial resistor Resistor
R5 1M 1/4W axial resistor Resistor
S1 Microswitch momentary tactile switch Momentary Switch for reset
SP1 Piezo Buzzer Piezo Buzzer Piezo Buzzer used as a microphone
U1 ATMEGA328P ATMEGA328P - DIP package Microcontroller with arduino bootloader
Y1 16MHz 16MHz crystal (through hole) 16MHz Crystal for microcontroller

Just for fun I used an online gerber viewer to show what the printed circuit boards will look like if manufactured:

Here is the Bottom Layer
Here is the top layer:
Well that is about all for now.  In the next post I will show pictures of the actual construction and then finally I'll go through the programming of the firmware for the microcontroller.  Enjoy - Langster!