In this tutorial, we’ll learn how to construct a 7-segment circuit that functions as an even-odd counter using an Arduino UNO and a BCD to seven-segment decoder. This project is a great way to understand how to use basic components to display numerical values visually.
Understanding Commands
Before we dive into the project, let’s review some important concepts that will help you understand the code and circuit connections better.
a) bitRead Command
The bitRead
command reads a single bit of a number from an 8-bit data and returns the value as either 0 or 1.
Syntax:
bitRead(x, n)
Where:
- x = the number from which to read.
- n = the bit position to read, starting at 0 for the least-significant (rightmost) bit.
b) Arrays
An array is a collection of variables that can be accessed using an index number. Arrays are often manipulated inside for
loops, where the loop counter serves as the index for each array element.
c) If Command
The if
statement checks for a specific condition and executes the following statement or set of statements if the condition is ‘true’.
Syntax:
if (condition)
{
// code to execute
}
d) Binary Coded Decimal (BCD)
Binary-Coded Decimal (BCD) is a system for representing decimal numbers, assigning a four-digit binary code to each digit from 0 to 9. In simpler terms, BCD converts decimal numbers into their binary equivalents.
Generate a 2-Digit Even Up-Down Counter
Components Required
For this project, you will need the following components:
- Arduino UNO Board
- 7-Segment Display
- BCD to Seven Segment Decoder
Source Code
Below is the source code that implements a 2-digit even 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 = 0, convert = 0; // Counter and conversion variables
int bit_value;
void setup() {
for (a = 6; a <= 13; a++) {
pinMode(a, OUTPUT); // Set pins as outputs
}
}
void loop() {
if (count % 2 == 0) {
display_data(); // Display data only for even counts
}
count += 2; // Increment counter by 2
if (count >= 100) { // Reset the counter after reaching 99
count = 0;
convert = 0;
}
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 defines the pins connected to the 7-segment display. - Count Control: The
count
variable controls the counting logic, incrementing by 2 to ensure only even numbers are counted. - Displaying Data: The
display_data()
function utilizes thebitRead
command to read bits from the BCD value and display them on the 7-segment display.
Results (Circuit Diagram)
To visualize the connections and the overall setup, here is the circuit diagram for this project. The diagram illustrates how the Arduino UNO, 7-segment display, and BCD to seven-segment decoder are connected.

Conclusion
In this project, you learned how to successfully construct a 7-segment circuit to generate an even up-down counter. By utilizing the Arduino UNO and the BCD to seven-segment decoder, you can display the count visually. This project serves as a fundamental example of how digital logic can be applied in practical 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