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!
Description of the Arduino 7-Segment Display Code
This Arduino code controls a 7-segment display to show numbers from 0 to 9 based on a button press. The segmentPins array defines the pins connected to each segment of the display, while the buttonPins variable is set to an analog pin that reads the button state. The numbers array contains the byte representations of digits 0 through 9, which are used to activate the appropriate segments of the display.
In the setup()
function, the segment pins are configured as outputs, and the button pin is set as an input. The loop()
function continuously checks if the button is pressed. When the button is pressed, it calls the displayNumber()
function to show the current counter value on the 7-segment display. The counter increments with each button press, looping back to 0 after reaching 9, creating a simple counting mechanism.
This project is perfect for beginners interested in learning about Arduino programming, 7-segment display interfacing, and button input handling. It demonstrates fundamental concepts in electronics and coding, making it an excellent hands-on project for aspiring hobbyists.
SCHEMATIC:

CODE:
int segmentPins[] = {2, 3, 4, 5, 6, 7, 8};
int buttonPins = A0;
byte numbers[10] = {
0x3F, // 0
0x06, // 1
0x5B, // 2
0x4F, // 3
0x66, // 4
0x6D, // 5
0x7D, // 6
0x07, // 7
0x7F, // 8
0x6F // 9
};
int counter = 0;
void setup() {
for (int i = 0; i < 7; i++) {
pinMode(segmentPins[i], OUTPUT);
}
pinMode(buttonPins, INPUT);
}
void loop() {
if(digitalRead(buttonPins) == HIGH){
displayNumber(counter);
counter = (counter + 1) % 10;
delay(1000);
}
}
void displayNumber(int num) {
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], numbers[num] & (1 << i));
}
}
0 Comments