list of products from Amazon (Not mine, i just link them!) Scroll Down For Article! Note: Elegoo boards are not Arduino, but they are compatible with Arduino IDE and are mutch more affordable!
This Arduino code implements a countdown timer with a 7-segment display driven by a 4511 IC, a buzzer for an alarm, and an optional start/reset button. Here's a brief explanation:
Components and Setup
Pins:
counterPins[]: Connected to the 4511 IC inputs (D, C, B, A) for driving the 7-segment display.buzzerPin: Controls a buzzer for an alarm.startButtonPin: Optional button for starting or resetting the countdown.
Setup:
- Configures
counterPinsas outputs to control the 7-segment display. - Configures the buzzer pin as output and turns it off initially.
- Configures the button pin as input with a pull-up resistor.
- Displays the initial countdown value (9) on the 7-segment display.
- Configures
Functionality
Button-Controlled Countdown:
- The
loop()waits for the button press (LOWsignal due to the pull-up resistor) to start the countdown. - A debounce delay of 200ms prevents false triggers.
- The
Countdown Logic:
- The
countdown()function decrements the counter from the specified start value (9 by default) to 0. - Each decrement updates the 7-segment display via the
updateCounter()function, which converts the counter value to binary and sets the corresponding pins on the 4511 IC. - A 1-second delay creates the countdown effect.
- The
Alarm Activation:
- When the countdown reaches 0, the
activateAlarm()function activates the buzzer, causing it to beep 5 times with 200ms on/off intervals.
- When the countdown reaches 0, the
Key Functions
updateCounter(int value): Converts the counter value to binary and sends the signal to the 4511 IC to update the 7-segment display.countdown(int startValue): Handles the countdown sequence and triggers the alarm when finished.activateAlarm(): Controls the buzzer to produce an audible alert when the countdown ends.
Schematic:
Code:
// Pin Definitions
const int counterPins[] = {7, 6, 5, 4}; // Pins connected to 4511 IC (D, C, B, A)
const int buzzerPin = 8; // Buzzer connected to pin 8
const int startButtonPin = 9; // Start/Reset button (optional)
// Timer Variables
int counter = 9; // Start value for the countdown
void setup() {
// Setup Counter Pins
for (int i = 0; i < 4; i++) {
pinMode(counterPins[i], OUTPUT);
}
// Setup Buzzer
pinMode(buzzerPin, OUTPUT);
digitalWrite(buzzerPin, LOW); // Buzzer off initially
// Setup Button
pinMode(startButtonPin, INPUT_PULLUP); // Button with pull-up resistor
// Display initial counter value
updateCounter(counter);
}
void loop() {
// Wait for button press to start the countdown (if button is used)
if (digitalRead(startButtonPin) == LOW) {
delay(200); // Debounce delay
countdown(counter); // Start the countdown
}
}
// Update the 4511 IC to display the counter value on the 7-segment
void updateCounter(int value) {
for (int i = 0; i < 4; i++) {
digitalWrite(counterPins[i], (value >> i) & 0x01);
}
}
// Countdown logic
void countdown(int startValue) {
counter = startValue;
while (counter >= 0) {
updateCounter(counter);
delay(1000); // Wait for 1 second
counter--;
}
// When countdown reaches 0, activate the buzzer
activateAlarm();
}
// Activate the alarm (buzzer)
void activateAlarm() {
for (int i = 0; i < 5; i++) { // Buzzer beeps 5 times
digitalWrite(buzzerPin, HIGH);
delay(200);
digitalWrite(buzzerPin, LOW);
delay(200);
}
}
Difference between Classical 7 SEG and 4511 BCD:
A classic 7-segment display is controlled directly by connecting its segments to microcontroller pins, requiring 7 or more pins to light up specific segments for each digit. In contrast, a display using a BCD 4511 IC simplifies control by requiring only 4 input pins to represent digits in binary (BCD), with the IC decoding this input and driving the display segments. This reduces wiring complexity and pin usage, making it more efficient!
Most important part of the code:
// Update the 4511 IC to display the counter value on the 7-segment
void updateCounter(int value) {
for (int i = 0; i < 4; i++) {
digitalWrite(counterPins[i], (value >> i) & 0x01);
}
}
The
updateCounter function translates a given number (0–9) into its binary equivalent and sends it to the 4511 IC to control the 7-segment display. For example, if the input value is 5, its binary equivalent is 0101. The function loops through the counterPins array, setting each pin to match the binary representation: pin 7 gets 1 (least significant bit), pin 6 gets 0, pin 5 gets 1, and pin 4 gets 0. Similarly, if value is 3, its binary representation is 0011, so pin 7 gets 1, pin 6 gets 1, pin 5 gets 0, and pin 4 gets 0. This way, the binary pattern is sent to the IC to display the correct digit on the 7-segment display.Download the project:
https://drive.google.com/file/d/13-bKJttsRTV5COfqrzBTCBb6XGiDa1jh/view?usp=drive_link
Summary
The code creates a simple countdown timer that starts on a button press, displays the countdown on a 7-segment display, and activates a buzzer alarm when the countdown completes.
0 Commentaires