In this tutorial, we’ll explore how to create a 2-digit odd up-down counter using an Arduino UNO, a seven-segment display, and a BCD to seven-segment decoder. This project will deepen your understanding of controlling displays with Arduino and showcase the use of basic programming concepts.
Components Required
For this project, you will need the following components:
- Arduino UNO Board
- Seven-Segment Display
- BCD to Seven-Segment Decoder
Source Code
Below is the source code that implements a 2-digit odd up-down counter:
int segment_pin[] = {6, 7, 8, 9, 10, 11, 12, 13}; // Pins connected to the 7-segment display
int a, b, count = 1, convert = 0; // Counter and conversion variables
int bit_value;
bool upDirection = true; // Direction of counting
void setup() {
for (a = 6; a <= 13; a++) {
pinMode(a, OUTPUT); // Set pins as outputs
}
}
void loop() {
if (upDirection) {
odd_up_counting(); // Count up if the direction is true
} else {
odd_down_counting(); // Count down if the direction is false
}
}
void odd_up_counting() {
display_data(); // Display current data
count += 2; // Increment counter by 2
if (count >= 100) { // Check if count exceeds 99
count = 99; // Set count to 99 if exceeded
upDirection = false; // Change direction to down
}
convert = bintobcd(count); // Convert decimal count to BCD
delay(300); // Delay for readability
}
void odd_down_counting() {
display_data(); // Display current data
count -= 2; // Decrement counter by 2
if (count < 1) { // Check if count goes below 1
count = 1; // Set count to 1 if below
upDirection = true; // Change direction to up
}
convert = bintobcd(count); // Convert decimal count to BCD
delay(300); // Delay for readability
}
void display_data() {
b = 7; // Start with the most significant bit
for (a = 0; a <= 7; a++) {
bit_value = bitRead(convert, b); // Read the bit value
digitalWrite(segment_pin[a], bit_value); // Write to the segment
b--; // Move to the next bit
}
}
int bintobcd(int recv) {
int q, r;
q = recv / 10; // Divide by 10 to get the tens place
r = recv % 10; // Get the ones place
q = q << 4; // Shift tens place to the left by 4
q = q | r; // Combine tens and ones place
return q; // Return BCD value
}
Code Explanation
- Pin Definition: The
segment_pin[]
array specifies the pins connected to the 7-segment display.
- Counting Logic: The
count
variable begins at 1 and increments or decrements by 2 to ensure only odd numbers are displayed.
- Direction Control: The
upDirection
boolean determines whether to count up or down. If the count reaches 99, the direction switches to down; if it goes below 1, it switches back to up.
- Displaying Data: The
display_data()
function uses thebitRead
command to read bits from the BCD value and displays them on the 7-segment display.
Results (Circuit Diagram)
To better understand the connections and overall setup, here is the circuit diagram for this project. The diagram illustrates how the Arduino UNO, seven-segment display, and BCD to seven-segment decoder are connected.

Conclusion
In this project, we successfully constructed a 2-digit odd up-down counter. Using the Arduino UNO and the BCD to seven-segment decoder, we effectively displayed odd counts visually. This project demonstrates the practical applications of digital logic and programming in electronics.
Feel free to modify the code or experiment with different counting sequences! If you have any questions or comments, please leave them below!
Leave a Reply