Count from 0 to 9 on a common cathode 7-segment display

 

 

code:

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

// Define the common cathode 7-segment display’s segments for digits 0-9
const byte digitSegments[10] = {
B00111111, // 0
B00000110, // 1                        osief.com
B01011011, // 2                            osief.com
B01001111, // 3                                osief.com
B01100110, // 4                                   osief.com
B01101101, // 5                                      osief.com
B01111101, // 6                                          osief.com
B00000111, // 7                                             osief.com                                                          
B01111111, // 8                                                 osief.com
B01101111 // 9                                                     osief.com
};

// ██████  ███████ ██ ███████ ███████     ██████  ██████  ███    ███ 
//██  ████ ██      ██ ██      ██         ██      ██    ██ ████  ████ 
//██ ██ ██ ███████ ██ █████   █████      ██      ██    ██ ██ ████ ██ 
//████  ██      ██ ██ ██      ██         ██      ██    ██ ██  ██  ██ 
// ██████  ███████ ██ ███████ ██      ██  ██████  ██████  ██      ██ 
                                                                   

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

void loop() {
// Count from 0 to 9
for (int count = 0; count <= 9; count++) {
displayDigit(count);
delay(1000); // Display each digit for 1 second
}
}

 

//  ▄▄▄▄▄▄▄▄▄   ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄       ▄▄ 
// ▐░░░░░░░░░▌ ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░▌     ▐░░▌
//▐░█░█▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀▀▀  ▀▀▀▀█░█▀▀▀▀ ▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀█░▌▐░▌░▌   ▐░▐░▌
//▐░▌▐░▌    ▐░▌▐░▌               ▐░▌     ▐░▌          ▐░▌          ▐░▌          ▐░▌       ▐░▌▐░▌▐░▌ ▐░▌▐░▌
//▐░▌ ▐░▌   ▐░▌▐░█▄▄▄▄▄▄▄▄▄      ▐░▌     ▐░█▄▄▄▄▄▄▄▄▄ ▐░█▄▄▄▄▄▄▄▄▄ ▐░▌          ▐░▌       ▐░▌▐░▌ ▐░▐░▌ ▐░▌
//▐░▌  ▐░▌  ▐░▌▐░░░░░░░░░░░▌     ▐░▌     ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░▌          ▐░▌       ▐░▌▐░▌  ▐░▌  ▐░▌
//▐░▌   ▐░▌ ▐░▌ ▀▀▀▀▀▀▀▀▀█░▌     ▐░▌     ▐░█▀▀▀▀▀▀▀▀▀ ▐░█▀▀▀▀▀▀▀▀▀ ▐░▌          ▐░▌       ▐░▌▐░▌   ▀   ▐░▌
//▐░▌    ▐░▌▐░▌          ▐░▌     ▐░▌     ▐░▌          ▐░▌          ▐░▌          ▐░▌       ▐░▌▐░▌       ▐░▌
//▐░█▄▄▄▄▄█░█░▌ ▄▄▄▄▄▄▄▄▄█░▌ ▄▄▄▄█░█▄▄▄▄ ▐░█▄▄▄▄▄▄▄▄▄ ▐░▌ ▄        ▐░█▄▄▄▄▄▄▄▄▄ ▐░█▄▄▄▄▄▄▄█░▌▐░▌       ▐░▌
// ▐░░░░░░░░░▌ ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░▌▐░▌       ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░▌       ▐░▌
//  ▀▀▀▀▀▀▀▀▀   ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀  ▀         ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀         ▀
 
 

void displayDigit(int digit) {
// Display the digit on the 7-segment display
if (digit >= 0 && digit <= 9) {
byte segments = digitSegments[digit];
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], bitRead(segments, i));
}
}
}

 

 

 

A common cathode and a common anode 7-segment display are two different types of displays used to show numeric and sometimes alphanumeric characters. The key difference between them lies in how the individual segments (LEDs) within the display are connected:

  1. Common Cathode 7-Segment Display:
    • In a common cathode display, all the cathodes (negative terminals) of the LED segments are connected together and shared as a common connection.
    • Each of the anodes (positive terminals) of the individual LED segments is connected to a separate pin or terminal.
    • To light up a specific segment, you apply a positive voltage (typically +5V or higher) to the respective anode pin while grounding (connecting to 0V) the common cathode pin.
    • When a segment is provided with a positive voltage and the common cathode is grounded, that segment will illuminate.
  2. Common Anode 7-Segment Display:
    • In a common anode display, all the anodes (positive terminals) of the LED segments are connected together and shared as a common connection.
    • Each of the cathodes (negative terminals) of the individual LED segments is connected to a separate pin or terminal.
    • To light up a specific segment, you ground the respective cathode pin while applying a positive voltage to the common anode pin.
    • When a segment is grounded on the cathode side and the common anode is provided with a positive voltage, that segment will illuminate.

In summary, the main distinction is the polarity of the common connection. In common cathode displays, the common connection is the cathode, and in common anode displays, the common connection is the anode. Depending on the type of display you are using, you’ll need to apply the appropriate voltage to either the anodes or cathodes to activate specific segments and display the desired numbers or characters.

 

 

0sief