Home Julian Rogers Home New security light final program New timed security light

Under construction

New security light, software development and testing

Clock

The easiest way to adjust the time on the clock, initially or when the battery is replaced, is to use an Arduino. I use a prototype shield with a few extra headers soldered on to make a little bread board. The software needed is to be found here.

To test the system with the Trinket and the clock, I am using a bread board with an LED to simulate the relay and mains switching circuit.

The initial test program shines an LED when the number of seconds on the clock is odd. Count the number of flashes per minute (should be 30, obviously!) To check it’s working.

(You need Arduino IDE 1.6.x, Adafruit Trinket 8Mhz as the board and the Programmer as USBtinyISP - see here.)

The program below is a stripped down version of the one used above to set the clock.

//program to test Trinket with I2C connection to RTC.

#include "Wire.h"

#define DS3231_I2C_ADDRESS 0x68

byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;

byte result;

// Convert binary coded decimal to normal decimal numbers

byte bcdToDec(byte val)

{

  return( (val/16*10) + (val%16) );

}

void setup()

{

  Wire.begin();

  pinMode(4, OUTPUT);  

}

void readDS3231time(byte *second,

byte *minute,

byte *hour,

byte *dayOfWeek,

byte *dayOfMonth,

byte *month,

byte *year) {

  Wire.beginTransmission(DS3231_I2C_ADDRESS);

  Wire.write(0); // set DS3231 register pointer to 00h

  Wire.endTransmission();

  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);

  // request seven bytes of data from DS3231 starting from register 00h

  *second = bcdToDec(Wire.read() & 0x7f);

  *minute = bcdToDec(Wire.read());

  *hour = bcdToDec(Wire.read() & 0x3f);

  *dayOfWeek = bcdToDec(Wire.read());

  *dayOfMonth = bcdToDec(Wire.read());

  *month = bcdToDec(Wire.read());

  *year = bcdToDec(Wire.read());

}

void loop()

{

 readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,

  &year);

  result = second % 2;   //check if even number of seconds, if so shine LED

  if( result == 0){

    digitalWrite(4,HIGH);

  }

      else{

        digitalWrite(4,LOW);

      }

}

NB. You can’t power the trinket from the USB programming lead even for testing. I unplug the Trinket from the breadboard, plug in the USB. While the red LED is flashing (program mode) you can download the program. Otherwise, press the reset button to start up program mode (it lasts for about 10 seconds). When it has been programmed, I plug it back into the breadboard. Watch that you are not powering the PC’s USB with a separate power supply by having everything connected up at once, that might not be good (?)

The developed program and final testing is next…