Advertisement
For one of my hobby projects, I needed to add a small countdown timer at an affordable cost. Fortunately, a DigiSpark Attiny85 development board was lying around on my workbench. The next component needed was a four-digit LED display. Because it was an important part of the design, I went on eBay and started looking for something when I found a four-digit red LED display module with an integrated driver chip labelled TM1637. The TM1637 display module has only four pins — VCC, GND, DIO, and CLK — and uses an I2C-like “odd” protocol for communication. Yes, the TM1637 four-digit LED display module v1.0 seemed good enough for my job!
Let’s get started by taking a closer look at the TM1637 display module. According to the datasheet (if I understood the machine translation of the Chinese datasheet correctly), microprocessor data realizes the communication with TM1637 by means of two-wire bus interface; however, this communication method is not equal to I2C bus protocol because there is no slave address. This is the interface interpretation:
Advertisement
“When data is input, the DIO signal should not change for a high-level CLK signal and should change for a low-level CLK signal. When the CLK signal level is high and the DIO signal level changes from high to low, data input starts. When the CLK signal level is low and the DIO signal level changes from low to high, data input ends. TM1637 data transfer carries with answering signal ACK. For a right data transfer, an answering signal ACK is generated inside the chip to lower the DIO pin at the failing edge of the eighth clock. DIO interface wire is released at the end of the ninth clock.”
Advertisement
Here is the hardware setup of my matchbox-sized digital timer project:
The code is written to display the countdown timer and induce an alarm function after a set time of about 60 seconds. The pulsing output available through D2 of Digispark (after the set time) can be used to trigger an external alarm/siren circuitry. For initial testing of the alarm function, you can use a pre-wired active-buzzer module (not a passive-type) like the one shown below. Connect the module’s VCC to 5 V, GND to GND, and I/O to D2 of the Digispark Development Board. That’s all!
Now for the code:
/* Matchbox-Sized Digital Timer
* DigiSpark Attiny85 linked with TM1637 Display Module
* By T.K.Hareendran
*/
#include "TM1637.h"
// Frankie Chu’s Library for TM1637 Display
// Download this library from the GitHub link shown below
// Put it inside the “arduino/libraries” folder
// https://github.com/reeedstudio/libraries/tree/master/DigitalTube //{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
//0~9,A,b,C,d,E,F // Functional for display
// Wires for display
#define CLK 0
#define DIO 1
#define Pulse 2 // Alarm Trigger Output
int timer_val=1; // Countdown value in minutes
int timer_seconds=00; // Display seconds
// Variables used to store individual numbers
int firstnum=0;
int secondnum=0;
int thirdnum=0;
int fournum=0;
TM1637 tm1637(CLK,DIO); // Create instance of the Display
void setup(){
pinMode(Pulse, OUTPUT);
digitalWrite(Pulse, LOW);
tm1637.init(); // Display Reset
tm1637.set(BRIGHT_TYPICAL); // Brightness Level
// BRIGHT_DARKEST = 0,BRIGHTEST = 7 BRIGHT_TYPICAL = 2;
tm1637.point(POINT_ON); // Centre "colons" ON
delay(1000); // Delay of 1 second
}
void loop() {
// Check if timer is elapsed
while (timer_val == 0 && timer_seconds == 0) {
tm1637.clearDisplay(); // Clear display
tm1637.display(0,0);
tm1637.display(1,0);
tm1637.display(2,0);
tm1637.display(3,0);
digitalWrite(Pulse, HIGH); // Alarm Trigger ON
delay(1000); // Wait
tm1637.clearDisplay();
digitalWrite(Pulse, LOW); // Alarm Trigger OFF
delay(500); // Wait
}
// Breakdown minutes and seconds in individual numbers
if (timer_val > 9) {
firstnum = timer_val/10%10;
secondnum = timer_val%10;
}
else {
secondnum = timer_val;
}
if (timer_seconds > 9) {
thirdnum = timer_seconds/10%10;
fournum = timer_seconds%10;
}
else {
thirdnum = 0;
fournum = timer_seconds;
}
// Show countdown on 4 digit 7 segment display
tm1637.clearDisplay(); // Clear display
if (timer_val > 9) {
tm1637.display(0,firstnum);
}
if (timer_val > 0) {
tm1637.display(1,secondnum);
}
if (timer_seconds > 9 || timer_val > 0) {
tm1637.display(2,thirdnum);
}
tm1637.display(3,fournum);
// Decrease seconds
timer_seconds=timer_seconds-1;
delay(1000); // Delay of 1 second
// Decrease timer
if (timer_seconds == -1) {
timer_val=timer_val-1;
timer_seconds=59;
}
}
// END








3 comments on “Matchbox-Sized Digital Timer”