Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Activity 3: Count Down With 4 Digit 7 Segment Objectives

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

TECHNOLOGICAL UNIVERSITY OF THE PHILIPPINES

Ayala Boulevard, Ermita, Manila


CIT - ESET DEPARTMENT
CPET 11L-M - Microprocessor and Microcontroller Systems, Lab
1ST Semester, SY 2021 – 2022
Name: Joshua Zion L. Olaño Instructor: Sir. Tristan Mopas
Section: BET-CpET 3E NS Date: November 18, 2021

Activity 3: Count Down with 4 Digit 7 Segment


Objectives
1. To be able to program and display a Counting Down Timer.
2. Utilize the relay driver to power 220 Voltage switch control
3. Use buzzer to send signal that time ends.
4. To be able to set, start and reset the timer.
Equipment and Materials
Materials
• Breadboard
• 4 Resistor (1K OHMS)
• 4 Digit 7 Segment
• Buzzer
• 4 Push Button
• Jumper Wires
• Relay Module

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

Setup of Seven Segment and Buzzer

Fritzing Diagram of Countdown Timer

a Prepare all the materials


b Put the 4 Digit 7 Segment and Buzzer to the Breadboard.
c Connect the resistor to the pins D1 D2 D3 D4 of 4 Digit 7 Segment

d With connected resistors to Digital Pins, Connect the D4 to digital pin 2, D3 to


Digital pin 3, D2 to Digital pin 4, D1 to Digital pin 5
e Connect the pin A to digital pin 6, B to digital pin 7, D to
digital pin 9, E to digital pin 10, F to digital pin 11, and G to digital pin 12.

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

Activity 3 Part 1: Setup the Arduino IDE/Platform.io for Uploading of sketch

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

Setup of 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

Setup of Seven Segment, Buzzer and 4 Push Buttons

a Prepare all the materials


b Put the 4 Digit 7 Segment, Buzzer, and 4 push buttons to the Breadboard.
c Connect the resistor to the pins D1 D2 D3 D4 of 4 Digit 7 Segment
d With connected resistors to Digital Pins, Connect the D4 to digital pin 2, D3 to
Digital pin 3, D2 to Digital pin 4, D1 to Digital pin 5

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

c. Hit the first button to start

d. Hit the second button to restart


e. Hit the third button or hold to decrease the number

f. Hit the fourth button or hold to increase the 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

// import libraries bool disableDecPoint = true; // Disable DP


#include <Arduino.h> segment
#include "SevSeg.h"
bool resistorOnSegments = false; // The
resistor segment is false
SevSeg sevseg; // Calling the SevSeg library byte hardwareConfig = COMMON_CATHODE; // The
used 4 digit 7 segment is common cathode
variant
float displayTimeSecs = 1; // Duration of
bool updateWithDelays = false;
every number to display in sec bool leadingZeroes = true; // The numbers in
float displayTime = (displayTimeSecs * 5000); the highest place is always 0
// Conversion of time
bool disableDecPoint = true; // Disable DP
segment
long buzzerFrequency = 800; // Frequency of sevseg.begin ( hardwareConfig,
the buzzer numDigits,
float buzzerDuration = (displayTimeSecs *
digitPins,
100);
segmentPins,
resistorOnSegments,
long startNumber = 10; // Starting Number updateWithDelays,
long endNumber = 0; // Ending Number
leadingZeroes,
disableDecPoint
const int buzzerPin = 13; );
void setup() { sevseg.setBrightness(90);
pinMode (buzzerPin, OUTPUT); // outputting a
}
signal
void loop() {
if ( startNumber >= endNumber) {
byte numDigits = 4; // Using this data, it for (long i = 0; i <= displayTime; i++) {
will tell how many 7 segments will be used
sevseg.setNumber (startNumber, 0);
byte digitPins[] = {2, 3, 4, 5}; // Using
sevseg.refreshDisplay();
this data, it tells the available pins that }
we should put our D1 - D4 startNumber--;
byte segmentPins[] = {6, 7, 8, 9, 10, 11,
tone(buzzerPin, buzzerFrequency,
12}; // This data will tell us the pins that
buzzerDuration);
our 7 segment pins should be put }
bool resistorOnSegments = false; // The else {
resistor segment is false
tone(buzzerPin, buzzerFrequency,
byte hardwareConfig = COMMON_CATHODE; // The
buzzerDuration);
used 4 digit 7 segment is common cathode sevseg.setNumber (1234, 0);
variant sevseg.refreshDisplay();
bool updateWithDelays = false;
}
bool leadingZeroes = true; // The numbers in
}
the highest place is always 0
Activity 3.2 Source Code

// import libraries bool resistorOnSegments = false; // The


#include <Arduino.h> resistor segment is false
#include "SevSeg.h" byte hardwareConfig = COMMON_CATHODE; // The
used 4 digit 7 segment is common cathode
variant
SevSeg sevseg; // Calling the SevSeg library bool updateWithDelays = false;
bool leadingZeroes = true; // The numbers in
the highest place is always 0
float displayTimeSecs = 1; // Duration of
bool disableDecPoint = true; // Disable DP
every number to display in sec
segment
float displayTime = (displayTimeSecs * 5000);
// Conversion of time sevseg.begin ( hardwareConfig,
numDigits,
long buzzerFrequency = 800; // Frequency of
digitPins,
the buzzer segmentPins,
float buzzerDuration = (displayTimeSecs * resistorOnSegments,
100);
updateWithDelays,
leadingZeroes,
long startNumber = 10; // Starting Number disableDecPoint
long endNumber = 0; // Ending Number );
sevseg.setBrightness(90);
const int relayPin = 13;
}
const int buzzerPin = 0;

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

#include <Arduino.h> pinMode(speakerPin, OUTPUT);


#include <math.h>
pinMode(button1, INPUT_PULLUP);
int digit_pin[] = {2, 3, 4, 5}; // PWM pinMode(button2, INPUT_PULLUP);
Display digit pins from left to right pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
int speakerPin = A0; }
void playTone(int tone, int duration) {
#define DIGIT_ON LOW for (long k = 0; k < duration * 1000L; k +=
#define DIGIT_OFF HIGH tone * 2) {
digitalWrite(speakerPin, HIGH);
int segA = 10; delayMicroseconds(tone);
int segB = 11; digitalWrite(speakerPin, LOW);
int segC = 12; delayMicroseconds(tone);
int segD = 13; }
int segE = 15; }
int segF = 16; void lightNumber(int numberToDisplay) {
int segG = 17;
int button1 = 9; #define SEGMENT_ON HIGH
int button2 = 8; #define SEGMENT_OFF LOW
int button3 = 7;
int button4 = 6; switch (numberToDisplay){

int countdown_time = 60; case 0:


digitalWrite(segA, SEGMENT_ON);
struct struct_digits { digitalWrite(segB, SEGMENT_ON);
int digit[4]; digitalWrite(segC, SEGMENT_ON);
}; digitalWrite(segD, SEGMENT_ON);
void setup() { digitalWrite(segE, SEGMENT_ON);
pinMode(segA, OUTPUT); digitalWrite(segF, SEGMENT_ON);
pinMode(segB, OUTPUT); digitalWrite(segG, SEGMENT_OFF);
pinMode(segC, OUTPUT); break;
pinMode(segD, OUTPUT);
pinMode(segE, OUTPUT); case 1:
pinMode(segF, OUTPUT); digitalWrite(segA, SEGMENT_OFF);
pinMode(segG, OUTPUT); digitalWrite(segB, SEGMENT_ON);
digitalWrite(segC, SEGMENT_ON);
for (int i = 0; i < 4; i++) { digitalWrite(segD, SEGMENT_OFF);
pinMode(digit_pin[i], OUTPUT); digitalWrite(segE, SEGMENT_OFF);
} digitalWrite(segF, SEGMENT_OFF);
digitalWrite(segG, SEGMENT_OFF);
break;
case 2: case 7:

digitalWrite(segA, SEGMENT_ON); digitalWrite(segA, SEGMENT_ON);

digitalWrite(segB, SEGMENT_ON); digitalWrite(segB, SEGMENT_ON);

digitalWrite(segC, SEGMENT_OFF); digitalWrite(segC, SEGMENT_ON);

digitalWrite(segD, SEGMENT_ON); digitalWrite(segD, SEGMENT_OFF);

digitalWrite(segE, SEGMENT_ON); digitalWrite(segE, SEGMENT_OFF);

digitalWrite(segF, SEGMENT_OFF); digitalWrite(segF, SEGMENT_OFF);

digitalWrite(segG, SEGMENT_ON); digitalWrite(segG, SEGMENT_OFF);

break; break;

case 3: case 8:

digitalWrite(segA, SEGMENT_ON); digitalWrite(segA, SEGMENT_ON);

digitalWrite(segB, SEGMENT_ON); digitalWrite(segB, SEGMENT_ON);

digitalWrite(segC, SEGMENT_ON); digitalWrite(segC, SEGMENT_ON);

digitalWrite(segD, SEGMENT_ON); digitalWrite(segD, SEGMENT_ON);

digitalWrite(segE, SEGMENT_OFF); digitalWrite(segE, SEGMENT_ON);

digitalWrite(segF, SEGMENT_OFF); digitalWrite(segF, SEGMENT_ON);

digitalWrite(segG, SEGMENT_ON); digitalWrite(segG, SEGMENT_ON);

break; break;

case 4: case 9:

digitalWrite(segA, SEGMENT_OFF); digitalWrite(segA, SEGMENT_ON);

digitalWrite(segB, SEGMENT_ON); digitalWrite(segB, SEGMENT_ON);

digitalWrite(segC, SEGMENT_ON); digitalWrite(segC, SEGMENT_ON);

digitalWrite(segD, SEGMENT_OFF); digitalWrite(segD, SEGMENT_ON);

digitalWrite(segE, SEGMENT_OFF); digitalWrite(segE, SEGMENT_OFF);

digitalWrite(segF, SEGMENT_ON); digitalWrite(segF, SEGMENT_ON);

digitalWrite(segG, SEGMENT_ON); digitalWrite(segG, SEGMENT_ON);

break; break;

case 5: case 10:

digitalWrite(segA, SEGMENT_ON); digitalWrite(segA, SEGMENT_OFF);

digitalWrite(segB, SEGMENT_OFF); digitalWrite(segB, SEGMENT_OFF);

digitalWrite(segC, SEGMENT_ON); digitalWrite(segC, SEGMENT_OFF);

digitalWrite(segD, SEGMENT_ON); digitalWrite(segD, SEGMENT_OFF);

digitalWrite(segE, SEGMENT_OFF); digitalWrite(segE, SEGMENT_OFF);

digitalWrite(segF, SEGMENT_ON); digitalWrite(segF, SEGMENT_OFF);

digitalWrite(segG, SEGMENT_ON); digitalWrite(segG, SEGMENT_OFF);

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;

void PrintNumber(int n, int time) { struct struct_digits dig;

struct struct_digits dig;


dig = IntToDigits(countdown_time);

dig = IntToDigits(n);
while (digitalRead(button1) == HIGH) {

for (int i = 0; i <= time / 20; i++) { for (int j = 0; j < 4; j++) {

if (digitalRead(button2) == LOW) { SwitchDigit(j);

return; lightNumber(dig.digit[j]);

} delay(5);

for (int j = 0; j < 4; j++) { }

SwitchDigit(j); if (digitalRead(button3) == LOW) {

lightNumber(dig.digit[j]); if (pressed3 == 0 || pressed3 > 30) {

delay(5); if (countdown_time > 0) {

} 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) {};
}

You might also like