Activity 3: Count Down With 4 Digit 7 Segment Objectives
Activity 3: Count Down With 4 Digit 7 Segment Objectives
Activity 3: Count Down With 4 Digit 7 Segment Objectives
Hardware
• Arduino Uno R3
• Arduino Mega
Software
• MS Word
• Fritzing
• Arduino IDE
• Platform.io IDE
Procedure
Activity 3: Part 1 Counting Down with Seven Segment and Buzzer
f For buzzer, connect the positive terminal of the passive buzzer to the digital
pin 13.
g Connect negative pin of the buzzer to the GND pin of the Arduino
a To test the Counting Down Sketch, you have to copy and paste the sketch
below, under source code section of this document.
b Make sure to connect the TYPE B Header to the Arduino UNO and connect
the USB of the cord to your Laptop.
c With Platform.io, after pasting the code, just in the lower left of the screen,
there are the compile, upload and serial monitor
d Compile the sketch by clicking the check button
e Upload the sketch to Arduino UNO by clicking the arrow button besides the
compile button
f Once uploaded, the 4 Digit 7 Segment will display 10 and will automatically
count down
g Once it reached to 0000, the next display will be 1234 together with long
buzzer sound.
h Note: If you want to restart from 10, just click the restart button of Arduino.
Activity 3: Part 2 Counting Down with Seven Segment, Buzzer and Relay
a For setting the 4 Digit 7 Segment, follow the steps from a to e in the procedure
of “Activity 3: Part 1 Counting Down with Seven Segment and Buzzer”
b In setting up the relay, get a 5V source from Arduino Uno. Just connect the
negative terminal (3rd terminal) of the relay module to the GND (ground) pin.
While connect the positive terminal (2nd terminal)to the 5v pin of the Arduino
c Connect the signal pin (1st terminal) to pin 13 of the Arduino
d Connect the negative terminal of the buzzer to NO (Normally Open) terminal of
the relay driver
e Connect the Common terminal, the middle terminal with screw, to the GND pin
of the Arduino.
f Finally, connect the positive terminal of the buzzer to the pin 0 of the Arduino.
Activity 3 Part 2: Setup the Arduino IDE/Platform.io for Uploading of Sketch
a. Do the same step from the procedure “Activity 3 Part 1: Setup the
Arduino IDE/Platform.io for Uploading of Sketch” starting from a – e.
Refer to Activity 3.2 Source Code for the sketch.
b. Note: remove the connected positive buzzer terminal to pin 0 before
uploading. It will always prompt an error every time you will upload.
Connect it back right after successfully uploaded
c. Once uploaded, it will display a 10
d. After a second, the timer will start to countdown, you will hear a tick sound
coming from relay together with lighting up of indicator light and a sound
coming from passive buzzer
e. Right after 0 it will display 1234 and a long sound coming from buzzer
Activity 3: Part 3 Counting Down with Seven Segment, Buzzer and 4 Push
Buttons
e Connect the push button4 to digital pin 6, push button3 to digital pin 7, push
button2 to digital pin 8, and push button1 to digital pin 9
f Connect the pin A to digital pin 10, B to digital pin 11, D to
digital pin 13, E to digital pin 15, F to digital pin 16, and G to digital pin 17.
g For buzzer, connect the positive terminal of the passive buzzer to the analog
pin 0.
h Connect negative pin of the buzzer and other terminal of the push button(upper
left terminal) to the GND pin of the Arduino.
Activity 3 Part 3: Setup the Arduino IDE/Platform.io for Uploading of Sketch
a. Do the same step from the procedure “Activity 3 Part 1: Setup the
Arduino IDE/Platform.io for Uploading of Sketch” starting from a – e.
Refer to Activity 3.3 Source Code for the sketch.
b. Once uploaded, it will display 60 as default number
Application
In this activity we are able to use the 4 digit 7 segment, buzzer, relay and
buttons. The combination of this circuit was able to produce a countdown timer from
0 – 9999. In addition, we can change the starting number by adding 2 button for
increasing and decrementing, and another 2 buttons for starting and resetting the
counting.
Aside from that, we are also able to utilize the SevSeg library for displaying numbers
easily. There are variety of 7 segments. Some has the capacity to display 0 – 9,
letters from A to Z, and other symbols.
In the process, I was also able to understand the difference between passive and
active buzzer. Active buzzer can produce sound by directly connecting it to DC
voltage, on the other hand, Passive buzzer can’t produce sound by connecting it to
DC voltages. Thus, we can use digitalWrite(pin, HIGH); and tone(pin, frequency);
for active buzzer. While tone(pin, frequency); is only used. On the other hand, active
buzzer may have advantages, it fall short when producing a sound. It can’t produce
a smooth sound. Thus it is best to use passive buzzer when you will use a varying
frequency.
Despite of having very low voltage Arduino, it can switch on and off higher voltages
that reaches 110 – 220 V and 10A. It was made possible to relay drivers. But be
careful when playing with this voltages since it is very risky and can destroy your
Arduino. An interesting fact, relay module I’m using has a buzzer.
In conclusion, combining the following materials, we can display numbers and create
a sound out of buzzer. We can apply it as shot clock for basketball, an alarm clock,
time stomp for timing an action, timer for racing, and even a digital clock. We can
also use it even just for displaying values. We can also use big speakers/sirens to
be powered with this small but very powerful relay. Thus, Arduino don’t fail us and
still amaze us through its capability.
Source Code
Activity 3.1 Source Code
void loop() {
if ( startNumber >= endNumber) {
void setup() {
for (long i = 0; i <= displayTime; i++) {
pinMode (buzzerPin, OUTPUT); // outputting a
sevseg.setNumber (startNumber, 0);
signal
sevseg.refreshDisplay();
pinMode (relayPin, OUTPUT); // outputting a
}
signal
startNumber--;
digitalWrite(relayPin, HIGH);
byte numDigits = 4; // Using this data, it
tone(buzzerPin, buzzerFrequency,
will tell how many 7 segments will be used
buzzerDuration);
byte digitPins[] = {2, 3, 4, 5}; // Using
}
this data, it tells the available pins that
else {
we should put our D1 - D4
tone(buzzerPin, buzzerFrequency,
buzzerDuration);
byte segmentPins[] = {6, 7, 8, 9, 10, 11,
sevseg.setNumber (1234, 0);
12}; // This data will tell us the pins that
sevseg.refreshDisplay();
our 7 segment pins should be put
}
}
Activity 3.3 Source Code
break; break;
case 3: case 8:
break; break;
case 4: case 9:
break; break;
break; break;
case 6: }
digitalWrite(segA, SEGMENT_ON); }
digitalWrite(segB, SEGMENT_OFF);
digitalWrite(segC, SEGMENT_ON);
digitalWrite(segD, SEGMENT_ON);
digitalWrite(segE, SEGMENT_ON);
digitalWrite(segF, SEGMENT_ON);
digitalWrite(segG, SEGMENT_ON);
break;
void SwitchDigit(int digit) { bool Countdown(int n, int del) {
for (int i = 0; i < 4; i++) { for (int q = n; q > 0; q--) {
if (i == digit) { PrintNumber(q, del);
digitalWrite(digit_pin[i], DIGIT_ON); if (digitalRead(button2) == LOW) {
} else { return false;
digitalWrite(digit_pin[i], DIGIT_OFF); }
} }
} PrintNumber(0, 0);
} playTone(1519, 1000);
struct struct_digits IntToDigits(int n) { return true;
struct struct_digits dig; }
int zeros = 0; bool Countdown(int n, int del) {
int d; for (int q = n; q > 0; q--) {
for (int i = 0; i < 4; i++) { PrintNumber(q, del);
d = n / pow(10, 3 - i); if (digitalRead(button2) == LOW) {
zeros += d; return false;
n = n - d * pow(10, 3 - i); }
if (zeros != 0 || i == 3) { }
dig.digit[i] = d; PrintNumber(0, 0);
} else { playTone(1519, 1000);
dig.digit[i] = 10; return true;
} }
} void reset() {
return dig;
} int m, zeros, d, pressed3 = 0, pressed4 = 0;
m = countdown_time;
dig = IntToDigits(n);
while (digitalRead(button1) == HIGH) {
for (int i = 0; i <= time / 20; i++) { for (int j = 0; j < 4; j++) {
return; lightNumber(dig.digit[j]);
} delay(5);
} countdown_time -= 1 ;
} }
} dig = IntToDigits(countdown_time);
}
pressed3 += 1;
else if (digitalRead(button4) == LOW) {
if (pressed4 == 0 || pressed4 > 30) {
if (countdown_time < 9999) {
countdown_time += 1 ;
}
dig = IntToDigits(countdown_time);
}
pressed4 += 1;
}
if (digitalRead(button3) == HIGH) {
pressed3 = 0;
}
if (digitalRead(button4) == HIGH) {
pressed4 = 0;
}
}
}
void loop() {
reset();
while (!Countdown(countdown_time, 962)) {
reset();
}
while (digitalRead(button2) == 1) {};
}