📖 Scenario: You have a seven-segment display connected to an Arduino board. Each segment is connected to a specific digital pin. You want to control the display to show numbers from 0 to 9 by turning on and off the correct segments.
🎯 Goal: Build a program that controls a seven-segment display by turning on the correct segments to show digits 0 through 9.
📋 What You'll Learn
Create an array called segmentPins with the exact digital pins connected to segments a to g: 2, 3, 4, 5, 6, 7, 8
Create a two-dimensional array called digits that holds the on/off pattern for each digit 0 to 9 for the seven segments
Write a function called displayDigit that takes a digit and lights up the correct segments
Use setup() to set all segment pins as outputs
Use loop() to display digits 0 to 9 with a 1-second delay between each
💡 Why This Matters
🌍 Real World
Seven-segment displays are common in clocks, counters, and simple numeric displays in appliances.
💼 Career
Understanding how to control hardware components like displays is essential for embedded systems and electronics programming jobs.
Progress0 / 4 steps
1
Set up segment pins array
Create an integer array called segmentPins with these exact values: 2, 3, 4, 5, 6, 7, 8 representing the Arduino pins connected to segments a to g.
Arduino
Hint
Use an integer array with the exact pin numbers in curly braces.
2
Create digit patterns array
Create a two-dimensional integer array called digits with 10 rows and 7 columns. Each row represents a digit 0 to 9. Each column is 1 or 0 to turn a segment on or off. Use this exact pattern: {1,1,1,1,1,1,0}, {0,1,1,0,0,0,0}, {1,1,0,1,1,0,1}, {1,1,1,1,0,0,1}, {0,1,1,0,0,1,1}, {1,0,1,1,0,1,1}, {1,0,1,1,1,1,1}, {1,1,1,0,0,0,0}, {1,1,1,1,1,1,1}, {1,1,1,1,0,1,1}
Arduino
Hint
Use a 2D array with 10 rows and 7 columns exactly as shown.
3
Write displayDigit function
Write a function called displayDigit that takes an integer digit. Use a for loop with variable i from 0 to 6 to set each segment pin HIGH if digits[digit][i] is 1, else LOW. Use digitalWrite(segmentPins[i], HIGH) or digitalWrite(segmentPins[i], LOW) accordingly.
Arduino
Hint
Use a for loop from 0 to 6 and digitalWrite to control each segment.
4
Complete setup and loop to show digits
Write the setup() function to set all pins in segmentPins as OUTPUT using a for loop with variable i. Then write the loop() function to display digits 0 to 9 in order using a for loop with variable digit. Call displayDigit(digit) inside the loop and add delay(1000) to wait 1 second between digits.
Arduino
Hint
Use loops in setup() and loop() to set pins and show digits with delay.
Practice
(1/5)
1. What is the main purpose of controlling pins connected to a seven-segment display in Arduino?
easy
A. To turn on or off specific segments to display digits
B. To read sensor values from the display
C. To power the Arduino board
D. To connect the display to the internet
Solution
Step 1: Understand seven-segment display function
Each segment lights up to form numbers by controlling its pin.
Step 2: Purpose of controlling pins
Setting pins HIGH or LOW turns segments on or off to show digits.
Final Answer:
To turn on or off specific segments to display digits -> Option A
Quick Check:
Control pins = display digits [OK]
Hint: Pins control segments to form numbers on display [OK]
Common Mistakes:
Thinking pins read data instead of controlling segments
Confusing display power with pin control
Assuming pins connect to internet
2. Which Arduino command correctly sets pin 7 as an output for controlling a seven-segment display?
easy
A. pinMode(7, INPUT);
B. digitalRead(7);
C. pinMode(7, OUTPUT);
D. digitalWrite(7, OUTPUT);
Solution
Step 1: Identify correct function to set pin mode
pinMode(pin, mode) sets pin as INPUT or OUTPUT.
Step 2: Choose correct mode for controlling segments
Segments need OUTPUT mode to send signals.
Final Answer:
pinMode(7, OUTPUT); -> Option C
Quick Check:
pinMode with OUTPUT sets pin to control segments [OK]
Hint: Use pinMode(pin, OUTPUT) to control segments [OK]
Common Mistakes:
Using INPUT instead of OUTPUT for control
Confusing digitalWrite with pinMode
Trying to read pin instead of setting mode
3. Given this Arduino code snippet controlling a seven-segment display segment connected to pin 8:
pinMode(8, OUTPUT);
digitalWrite(8, HIGH);
What will happen to the segment connected to pin 8?
Sending HIGH voltage turns the connected segment on.
Final Answer:
The segment will turn on -> Option B
Quick Check:
digitalWrite HIGH = segment ON [OK]
Hint: HIGH signal turns segment on, LOW turns it off [OK]
Common Mistakes:
Thinking HIGH turns segment off
Assuming blinking without delay code
Confusing pinMode with digitalWrite effects
4. This Arduino code is intended to display the digit '1' on a common cathode seven-segment display connected to pins 2 and 3 for segments B and C respectively:
Pins should be set to HIGH to turn on segments on a common cathode display -> Option A
Quick Check:
Common cathode ON = HIGH signal [OK]
Hint: Common cathode segments light with HIGH signal [OK]
Common Mistakes:
Setting pins LOW instead of HIGH for common cathode
Thinking pins 2 and 3 can't be outputs
Ignoring pinMode setup
5. You want to display digits 0 to 9 sequentially on a common anode seven-segment display using Arduino. Which approach correctly controls the segments to show each digit?
hard
A. Connect all segment pins to 5V and use delay to show digits
B. Set segment pins HIGH to turn on segments, LOW to turn off, and update pins for each digit
C. Use analogWrite to vary brightness of segments for each digit
D. Set segment pins LOW to turn on segments, HIGH to turn off, and update pins for each digit
Solution
Step 1: Understand common anode display logic
Common anode means segments turn on when pins are LOW (connected to ground).
Step 2: Control segments for digits 0-9
Set pins LOW for segments to light, HIGH to turn off, update pins per digit pattern.
Final Answer:
Set segment pins LOW to turn on segments, HIGH to turn off, and update pins for each digit -> Option D
Quick Check:
Common anode ON = LOW signal [OK]
Hint: Common anode segments light with LOW signal [OK]
Common Mistakes:
Using HIGH to turn on segments on common anode
Using analogWrite instead of digitalWrite for digits
Connecting all pins to 5V without control
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.