Components Required:
- Arduino Uno
- 7-Segment Common Cathode Display
- Push Button Switch
- 330 Ohm Resistors (7 pieces)
- Breadboard and Jumper Wires
- Power Supply (9V Battery or Arduino power adaptor)
Understanding the 7-Segment Display
The 7-segment display is a simple device made up of eight Light Emitting Diodes (LEDs), which include seven segments to form digits and one segment for the decimal point. The segments light up in different combinations to represent numbers from 0 to 9.
In this project, we are using a common cathode 7-segment display, where the cathode pins (pins 3 and 8) are connected to the ground (GND). Each segment of the display corresponds to an LED, and by turning on specific segments, we can display any digit.
Circuit Schematic
To build the circuit, follow these steps:
1. Connect the Arduino to the 7-Segment Display:
- The Arduino I/O pins 2, 3, 4, 5, 6, 7, and 8 should be connected to the corresponding pins of the display. These pins control the individual segments of the 7-segment display.
- Use the following pin connections:
- Arduino Pin 2 → Display Pin 7 (Segment A)
- Arduino Pin 3 → Display Pin 6 (Segment B)
- Arduino Pin 4 → Display Pin 4 (Segment C)
- Arduino Pin 5 → Display Pin 2 (Segment D)
- Arduino Pin 6 → Display Pin 1 (Segment E)
- Arduino Pin 7 → Display Pin 9 (Segment F)
- Arduino Pin 8 → Display Pin 10 (Segment G)
2. Push Button Connection:
- Connect the push button switch to Arduino pin 9. The switch will be used to increment the counter each time it is pressed.
3. Resistor Setup:
- To protect the segments of the display, place 330-ohm resistors between the Arduino pins and each of the segments (pins 1, 2, 4, 6, 7, 9, and 10).
- Connect the common cathode pins (pins 3 and 8) directly to the ground.
4. Power the Circuit:
- The circuit can be powered using a 9V battery connected to the Arduino, or by using a USB cable or a power adaptor.
Code Explanation
The following Arduino sketch controls the 7-segment display and increments the counter each time the push button is pressed.
// Array to hold the binary representation of each digit (0-9)
byte numbers[10] = {
B11111100, B01100000, B11011010, B11110010, B01100110,
B10110110, B10111110, B11100000, B11111110, B11100110
};
void setup() {
// Set pins 2 to 8 as outputs for the 7-segment display
for(int i = 2; i <= 8; i++) {
pinMode(i, OUTPUT);
}
// Set pin 9 as input for the push button
pinMode(9, INPUT);
}
int counter = 0; // Initialize the counter variable
bool go_by_switch = true; // Use push button to increment the counter
int last_input_value = LOW; // Store the previous state of the button
void loop() {
if(go_by_switch) {
// Read the state of the push button
int switch_input_value = digitalRead(9);
// Increment the counter when the button is pressed
if(last_input_value == LOW && switch_input_value == HIGH) {
counter = (counter + 1) % 10; // Loop counter between 0 and 9
}
last_input_value = switch_input_value;
} else {
// If no button is used, increment automatically after a delay
delay(500);
counter = (counter + 1) % 10;
}
// Display the current number on the 7-segment display
writeNumber(counter);
}
// Function to write a number to the 7-segment display
void writeNumber(int number) {
if(number < 0 || number > 9) return; // Ensure the number is between 0 and 9
byte mask = numbers[number]; // Get the bitmask for the current number
byte currentPinMask = B10000000; // Start with the highest bit
// Loop through each segment pin and turn it on or off
for(int i = 2; i <= 8; i++) {
if(mask & currentPinMask) digitalWrite(i, HIGH); // Turn on the segment
else digitalWrite(i, LOW); // Turn off the segment
currentPinMask = currentPinMask >> 1; // Shift to the next bit
}
}
How the Code Works:
1. Setup Function:
- This function initializes pins 2 to 8 as outputs (for the 7-segment display) and pin 9 as an input (for the push button).
2. Loop Function:
- If the push button is pressed, the counter increments by one. It wraps around to 0 when it reaches 9.
- The current digit is displayed using the
writeNumber()
function.
3. writeNumber Function:
- This function takes the current number and translates it into the corresponding segments on the display by setting the appropriate pins high or low.
Final Thoughts
Building a 7-segment LED display counter with Arduino is a great project for beginners to understand how to interface displays with microcontrollers. The setup is simple, and by following the circuit diagram and code, you can have a working counter that displays digits from 0 to 9. This project can also be expanded by adding more features, such as counting beyond 9 or displaying numbers at timed intervals without a button.
With a few components and some basic wiring, you’ll have a functional and interactive digital counter!