Arduino Seven Segment Display Counter Experiment

Arduino Seven Segment Display Counter Experiment

Introduction

In this experiment, you'll learn how to create a basic counter on a seven-segment display using an Arduino. A seven-segment display can show digits from 0 to 9 by lighting up specific segments.

Components Needed

Circuit Setup

  1. Place the seven-segment display on the Arduino breadboard shield.
  2. Identify the segment pins on the display. Refer to the datasheet if necessary, but here’s a common pin layout:
    • a: Pin 7
    • b: Pin 6
    • c: Pin 4
    • d: Pin 2
    • e: Pin 1
    • f: Pin 9
    • g: Pin 10
    • DP (decimal point): Pin 5 (optional)
    • Common Cathode: Pins 3 and 8 (connect to GND)
  3. Connect each segment pin to a and then to the Arduino pins as follows:
    • a: Arduino pin 2
    • b: Arduino pin 3
    • c: Arduino pin 4
    • d: Arduino pin 5
    • e: Arduino pin 6
    • f: Arduino pin 7
    • g: Arduino pin 8
  4. Connect the common cathode pins (3 and 8) to GND on the Arduino.

Code for Seven Segment Display Counter

Upload the following code to your Arduino to create a counter from 0 to 9 on the seven-segment display:


// Define segment pins
const int segmentPins[7] = {2, 3, 4, 5, 6, 7, 8};

// Number patterns for each digit (0-9) on a 7-segment display
const bool digitPatterns[10][7] = {
    {1, 1, 1, 1, 1, 1, 0},  // 0
    {0, 1, 1, 0, 0, 0, 0},  // 1
    {1, 1, 0, 1, 1, 0, 1},  // 2
    {1, 1, 1, 1, 0, 0, 1},  // 3
    {0, 1, 1, 0, 0, 1, 1},  // 4
    {1, 0, 1, 1, 0, 1, 1},  // 5
    {1, 0, 1, 1, 1, 1, 1},  // 6
    {1, 1, 1, 0, 0, 0, 0},  // 7
    {1, 1, 1, 1, 1, 1, 1},  // 8
    {1, 1, 1, 1, 0, 1, 1}   // 9
};

void setup() {
    // Set all segment pins as output
    for (int i = 0; i < 7; i++) {
        pinMode(segmentPins[i], OUTPUT);
    }
}

void loop() {
    // Loop through digits 0-9
    for (int digit = 0; digit < 10; digit++) {
        displayDigit(digit);
        delay(1000); // Wait 1 second before displaying the next digit
    }
}

// Function to display a digit on the 7-segment display
void displayDigit(int digit) {
    for (int i = 0; i < 7; i++) {
        digitalWrite(segmentPins[i], digitPatterns[digit][i]);
    }
}
            

Explanation

This code uses an array of binary patterns to control each segment of the display. Each pattern corresponds to a digit from 0 to 9, where each 1 turns a segment on and 0 turns it off.

The displayDigit() function iterates through the segments and sets each one based on the specified digit pattern. In the loop(), the digits 0-9 are displayed sequentially with a 1-second delay between each.

Troubleshooting