Arduino based coundown timer projects. |
Arduino 7 segment display counter.
Hello viewers! In this blog you will find out the counter timer project which is base on arduino uno and 7 segment display and control the counter with help of rotary encoder.
This project is based on a countdown timer. You need to set the time by rotating the rotary encoder clockwise and then you need to press the button on the encoder.
Component requirements:
1. Arduino Uno - Buy2. Rotary encoder. - Buy
3. 7 segment display(Anode). - Buy
4. Resistor (220om) x 4
6. Jumper Wires.
There are two type of 7 segment display.
1. Anode base.
2. Cathode base.
You can do this project either anode base or cathode base. The step-by-step instructions are provided below.Pinout Diagram:
Pinout of anode and cathode base 7 segment display |
Circuit is based on an anode 7 segment display. For a cathode display the circuit would be different. The circuit for a cathode display can be found below.
Circuit Diagram based on anode display:
The circuit diagram for anode display. |
Pin Connection:
1. Firstly, make a connection between the Arduino Uno and the 7 segment display.
- The Arduino pin 13 should be connected to the 7 segment A pin.
- The Arduino pin 12 should be connected to the 7 segment B pin.
- The Arduino pin 11 should be connected to the 7 segment C pin.
- The Arduino pin 10 should be connected to the 7 segment D pin.
- The Arduino pin 9 should be connected to the 7 segment E pin.
- The Arduino pin 8 should be connected to the 7 segment F pin.
- The Arduino pin 7 should be connected to the 7 segment G pin.
2. The 7 segment COM pin is protected with a resistor, and the resistor's end is attached to the Arduino 5v pin. In order to protect the 7 segment LEDs, a 220 ohm resistor is used as a current limiter.
3. To connect the Arduino Uno and the rotary encoder, we have to make the following connections:
- Connect the encoder CLK pin to pin 2 of the Arduino.
- Connect the encoder DT pin to pin 3 of the Arduino.
- Connect the encoder SW pin to pin 4 of the Arduino.
- Connect the encoder VCC pin to 5v pin of the Arduino.
- Connect the encoder GND pin to GND pin of the Arduino.
Now the circuit is completed. Lets jump to the coding part.
Circuit Diagram based on cathode display:
Circuit diagram of cathode display. |
Pin Connection:
This is a cathode base 7 segment display.
It is the same circuit as the anode display circuit, except that we need to add a 220 ohm resistor between the Arduino pins and the 7 segment display pins and also Connect the COM pin of 7 segment to arduino GND pin.
Coding:
1. Connect the Arduino uno to the PC using a USB cable for uploading the code.
2. Open the Arduino IDE and copy the code from below and paste it on Arduino IDE.
Note: In the code you need to change ledCathode to true if your display uses a cathode base 7 segment display.
// C++ code
#define A 13
#define B 12
#define C 11
#define D 10
#define E 9
#define F 8
#define G 7
#define CLK 2
#define DT 3
#define SW 4
boolean ledCathode = false; //if you using anode 7 segment display, Make it true
int currentStateCLK, lastStateCLK, start;
unsigned long lastButtonPress = 0;
unsigned int counter = 0;
int countDelay = 1000; // Countdown delay
byte numeral[11] = {
B0000001,//0
B1001111,//1
B0010010,//2
B0000110,//3
B1001100,//4
B0100100,//5
B0100000,//6
B0001111,//7
B0000000,//8
B0001100,//9
B0111000 //F
};
const int segmentPins[7] = {G, F, E, D, C, B, A};
void setup()
{
Serial.begin(9600);
for (int i = 0; i < 7; i++)
{
pinMode(segmentPins[i], OUTPUT);
}
pinMode(CLK, INPUT);
pinMode(DT, INPUT);
pinMode(SW, INPUT_PULLUP);
lastStateCLK = digitalRead(CLK);
counter = 0;
}
void loop()
{
currentStateCLK = digitalRead(CLK);
if (currentStateCLK != lastStateCLK && currentStateCLK == 1) {
if (digitalRead(DT) != currentStateCLK) {
counter --;
} else {
counter ++;
}
}
lastStateCLK = currentStateCLK;
Serial.println(counter);
if (counter > 0 && counter <= 10) {
showDigit(counter);
} else if (counter == 11) {
counter = 0;
showDigit(counter);
} else {
counter = 0;
showDigit(counter);
}
int btnState = digitalRead(SW);
if (btnState == LOW) {
if (millis() - lastButtonPress > 50) {
Serial.println("Button pressed!");
for (start = counter; start >= 0; start -- ) {
showDigit(start);
delay(countDelay);
}
counter = 0;
showDigit(counter);
}
lastButtonPress = millis();
}
delay(1);
}
void showDigit( int number)
{
boolean isBitSet;
for (int segment = 0; segment < 7; segment++)
{
isBitSet = bitRead(numeral[number], segment);
if (ledCathode == true) {
isBitSet = ! isBitSet;
}
digitalWrite( segmentPins[segment], isBitSet);
}
}
Once the code has been successfully uploaded, turn the rotary encoder clockwise to set the time from 0-9, by pressing the top of the rotary encoder to begin counting down.
Watch This Tutorial:
Feel free to comment down if you have any problems and also tell me what next tutorial you would like to see?