# Simple 7 Segment LED Clock

6,125

41

3

## Introduction: Simple 7 Segment LED Clock

LED clock doesn't make this instructable interesting, also I'm not intent to make a clock, but I found it simple ever without too much code to complete a clock, so I want to share it to all. In fact I'm experimenting the 7 segment LED display with 74HC595 driver, originally I used the bread broad to build the circuit, but due to contact problem, I decided to solder it permanently, in fact this cost me most of the work.

### Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

## Step 1: 7 Segment LED Display

The circuitry can be found online, also in the Arduino home, I post no right on the circuit, I just done the pcb. The 4 digit 7 segment LED is common anode type, there are only 5 pins to connect to the UNO broad, two clock pins, one data pin, and two power pins.

## Step 2: The Code

The simplest of this clock is because I used DS1302 RTC as the source, I used the code but not written the code, I post no right, the start of code included the DS1302 header file, initialized pin no. and character map, initialized RTC object.

#include <DS1302.h>
int pinLatch = 8; // Latch pin (LCHCLK - pin 12 both u2 and u3).

int pinClock = 9; // Clock pin (SFTCLK - pin 11 both u2 and u3).

int pinData = 10; // Data pin (SD1 - pin 14 - u2 only - u3 SD1 pin 14 goes to u2 SD0 pin 9).

int kCePin = 5; // Chip Enable

int kIoPin = 6; // Input/Output

int kSclkPin = 7; // Serial Clock

unsigned char characterMap[] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0X80, 0X90}; // Map the led segements into the letters '0'-'9'.

unsigned char characterBuffer[] = {0xF1, 0xF2, 0xF4, 0xF8};

DS1302 rtc(kCePin, kIoPin, kSclkPin);

## Step 3: The Code Setup()

The below setup() code included the set clock routine, when set the clock, do not write protect, enable set clock, input the hour and minute, you may not need the year, month and date, upload the file. After upload, write protect it, and disable set clock, upload again.

void setup (){
// Define the pins as output;

for (int i=5;i<=10;i++)

pinMode (i,OUTPUT);

// Set Clock

//rtc.writeProtect(false); // uncomment this when set clock

rtc.writeProtect(true); // comment this when set clock

rtc.halt(false); // Make a new time object to set the date and time.

// March 3, 2016 at 23:51:50. Time t(2016, 3, 3, 23, 51, 50);

// Set the time and date on the chip.

//rtc.time(t); // uncomment this when set clock

}

## Step 4: The Code Loop()

In the loop() code, hour and minute fetched from RTC and passed to the display routine as a 4 digit integer, each digit disassembled into individual segment position and passed to the shiftout function.

void loop(){

Time t = rtc.time();

ledNumberWrite(t.hr*100+t.min);

}

void ledNumberWrite(int timeDisplay){

ledSegmentWrite(0, timeDisplay / 1000);

ledSegmentWrite(1, timeDisplay % 1000 / 100);

ledSegmentWrite(2, timeDisplay % 100 / 10);

ledSegmentWrite(3, timeDisplay % 10);

}

void ledSegmentWrite(byte segmentNo, byte digitValue){

digitalWrite(pinLatch, LOW);

if (segmentNo == 1) //display decimal point on 2nd digit

shiftOut(pinData, pinClock, MSBFIRST, characterMap[digitValue]&0x7F);

else

shiftOut(pinData, pinClock, MSBFIRST, characterMap[digitValue]);

shiftOut(pinData, pinClock, MSBFIRST, characterBuffer[segmentNo] );

digitalWrite(pinLatch, HIGH);

}

## Step 5: Assemble the Clock

As an experiment, I don't need to assemble it, but I have a empty CD tube in hand, so I want it can be tidy look, one thing I must remind, the DS1302 VCC better to connect the 3.3V vout from the UNO board, otherwise it will mad with crazy date time.

5 222
31 8.9K
759 45K

## 3 Discussions

"circuitry can be found online"..funny

let's stop deceiving ourselves