In this tutorial we will see how to create a adjustable timer using rotary encoder and buzzer for Timer end with the Arduino board and the TM1637 display.
Components required
Arduino Nano - 1 no
TM1637 module - 1 no
Toggle switch - 1 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 libraries
#include <Arduino.h>
#include <TM1637Display.h>
// Module connection pins (Digital Pins)
#define CLK 2
#define DIO 3
TM1637Display display(CLK, DIO);
byte D1=0;// seconds s
byte D2=0;// Second x 10
byte D3=0;// Second x 100
byte D4=0;// Second x 1000
int counter=0;
int n=0;
#define Toggle 5 //Toggle Switch D5
#define Buzzer 9 // Buzzer
#define RotSW 11 // Rotary Encoder
#define RotdtB 12 //Rotary Encoder "B"
#define RotclkA 10 //Rotary Encoder "A"
void setup()
{
pinMode(Toggle, INPUT_PULLUP);//Toggle Switch
pinMode(RotclkA, INPUT_PULLUP);//Rotary Encoder CLK
pinMode(RotSW, INPUT_PULLUP);// Rotary Encoder SW
pinMode(RotdtB, INPUT_PULLUP);//Rotary Encoder DT
pinMode(Buzzer,OUTPUT );//Buzzer
display.setBrightness(7);
display.showNumberDec(0, true);
delay(1000);
display.clear();
delay(1000);
display.showNumberDec(0, true);
digitalWrite((Buzzer), HIGH);
delay(1000);
digitalWrite((Buzzer), LOW);
}
void loop()
{
// set timer
// set s.
do{
D1= SCounter();
//display.clear();
display.showNumberDec(D1); //Display the n value;
delay(250);
}while(digitalRead(RotSW)==HIGH);//Push to Rotary
delay(250);
counter=0;
// set Second x 10
display.showNumberDec(D1,true,2,2);//Display D1
do{
D2= SCounter();
n=D2*10+D1;
//display.clear();
display.showNumberDec(n , true,2,2); //Display the n value;
delay(250);
}while(digitalRead(RotSW)==HIGH);//Push to Rotary
delay(250);
counter=0;
// set Second x 100
do{
D3= SCounter();
n=D3*100+D2*10+D1;
//display.clear();
display.showNumberDec(n , true, 3, 1); //Display the n value;
delay(250);
}while(digitalRead(RotSW)==HIGH);//Push to Rotary
delay(250);
counter=0;
// set Second x 1000
do{
D4= SCounter();
n=D4*1000+D3*100+D2*10+D1;
//display.clear();
display.showNumberDec(n , true, 4,0); //Display the n value;
delay(250);
}while(digitalRead(RotSW)==HIGH);//Push to Rotary
delay(250);
counter=0;
//Out of set routine
n=D4*1000+D3*100+D2*10+D1;//compute "n"
if(n!=0){
do{//Repeat set value
n=D4*1000+D3*100+D2*10+D1;//compute "n"
//display set "n"
display.showNumberDec(n);
while(0==digitalRead(RotSW));//Push Rotary to start
while(1==digitalRead(RotSW));
delay(50);
do{// decrease time
//display current "n"
display.showNumberDec(n);
while(millis() % 1000 != 0);//time base, 1second
n=n-1;
}while(n!=0);
display.showNumberDec(n);//display n=0
digitalWrite(Buzzer,HIGH);// start beep
delay(1000);
digitalWrite(Buzzer,LOW); // stop beep
}while(digitalRead(Toggle)==LOW);//"Toggle" to GND
D1=0;
D2=0;
D3=0;
D4=0;
}
}
byte SCounter(){ // Rotary encoder routine
while(digitalRead(RotSW) &digitalRead(RotdtB) & digitalRead(RotclkA)==1);
if(digitalRead(RotSW)==0){
return counter;
}
switch(digitalRead(RotclkA)) {
case HIGH:
while(digitalRead(RotclkA)==0);
while(digitalRead(RotdtB)==0);
counter ++;
counter=counter%10;
break;
case LOW:
while(digitalRead(RotdtB)==0);
while(digitalRead(RotclkA)==0);
counter --;
if(counter==-1){
counter=9;//left begin with 9
}
counter=abs(counter)%10;
break;
}
return counter;
}
Demo:
Comments