In this tutorial we will see how to create a Timer start, UP/Down setting using push button and buzzer for Timer end with the Arduino board and the TM1637 display
Components required
Arduino Nano - 1 no
TM1637 module - 1 no
Push Button switch - 3 no
5V/3.3V Power module - 1 no
Buzzer - 1 no
Circuit diagram
TM1637
TM1637 seven segment modules is a ready made multiplexed seven segment display with 4 digits. The driver IC is TM1637; It has 4pin control there are GND, VCC, DIO, CLK. only two signal lines can make MCU control four Digit 7-segments LED can be used to display decimal, letters and so on. The 7segment LED Display has common anode type.
TM1637 4-Digit 7-Segment Display Specifications
Operating voltage 3.3 – 5 V
Current draw-80 mA
Operating temperature-10 – 80 °C
Interfacing TM1637 with the arduino:
So the first thing you want to do is connect the clock pin to any pin on the Arduino.
CLK clock pin of the TM1637 with the digital pin 3 of the arduno.
DIO pin of the TM1637 with digital pin 2 of the arduino.
VCC of the TM1637 with the 3.3V of the Power module.
Gnd of the TM1637 with the ground of the Power module.
Installing Library
7segment: you need to Download and install the TM1637 library.
Follow the next steps to install those libraries.
In your Arduino IDE, to install the libraries go to Sketch > Include Library > Add .ZIP library… and select the library you’ve just downloaded.
After installing the required libraries, copy the following code to your Arduino IDE.
Refer Counter project for testing of TM1637 module.
Arduino Code
#include <TM1637Display.h>
// Define the connections pins for display
#define CLK 2
#define DIO 3
// Define other pin connections
#define UP_BUTTON 8
#define DOWN_BUTTON 9
#define START_BUTTON 4
#define BUZZER 5
int duration; // Duration in seconds
// Create display object of type TM1637Display:
TM1637Display display = TM1637Display(CLK, DIO);
// Set the individual segments for the word displays:
const uint8_t seg_end[] = {
SEG_A | SEG_D | SEG_E | SEG_F | SEG_G, // E
SEG_C | SEG_E | SEG_G, // n
SEG_B | SEG_C | SEG_D | SEG_E | SEG_G, // d
0x00 // All off
};
const uint8_t seg_go[] = {
SEG_A | SEG_C | SEG_D | SEG_F | SEG_G, // s
SEG_D | SEG_E | SEG_F | SEG_G, // t
0x00, // All off
0x00 // All off
};
void setup() {
pinMode(UP_BUTTON, INPUT_PULLUP);
pinMode(DOWN_BUTTON, INPUT_PULLUP);
pinMode(START_BUTTON, INPUT_PULLUP);
pinMode(BUZZER, OUTPUT);
duration = 10; // Default to 10 seconds
display.setBrightness(5); // 0 to 7 change if required
ShowTime(duration);
}
void loop() {
// Function will checks for time change buttons and only return
// when start button pressed
WaitForStart();
// Start the duration timer
TimeDuration();
}
void WaitForStart(){
// Check for button presses every 0.15 seconds
while (digitalRead(START_BUTTON) == HIGH){
// Check if up or down has been pressed
//if time > 60 then increment by 10 seconds
if (digitalRead(UP_BUTTON) == LOW){
if (duration < 60){
duration++;
}
else{
duration += 10;
}
ShowTime(duration);
}
if (digitalRead(DOWN_BUTTON) == LOW){
if (duration > 60){
duration -= 10;
}
else{
duration--;
}
ShowTime(duration);
}
delay(150);
}
// Start button has been pressed
tone(BUZZER, 1500, 100);
display.clear();
display.setSegments(seg_go);
}
void TimeDuration(){
// While loop will continue until time up
unsigned long startTime = millis();
unsigned long timer = 1000ul * duration;
// Repeatedly check if time is up
while ((millis() - startTime) <= timer){
// Calculate time elapsed in seconds
int elapsed = int((millis() - startTime)/1000);
// Only start to display countdown after 3 seconds
if ((millis() - startTime) > 3000){
ShowTime(duration - elapsed);
}
}
// Time up
tone(BUZZER, 500, 5000);
display.clear();
display.setSegments(seg_end);
// Wait 5 seconds and reset display
delay(5000);
// Show duration for next player
ShowTime(duration);
}
void ShowTime(int value){
static int lastTime;
// Update the display if time has changed
if (lastTime != value) {
lastTime = value;
int iMinutes = value / 60;
int iSeconds = value - (iMinutes * 60);
// Show on 4 digit display
uint16_t number = iMinutes * 100 + iSeconds;
display.showNumberDecEx(number, 0b01000000, true, 4, 0);
}
}
Demo
This is really a nice project, however one thing that is required is a reset switch. Can you revise the code to use the START_BUTTON If pressed, cycle through states:
Start/Resume (state = 1)
Pause (state = 2)
Reset (state = 0)
Thank-you