Search Code

How to Use a Seven-Segment LED Display with Arduino

How to Use a Seven-Segment LED Display with Arduino

Seven-segment LED displays are everywhere around us—from digital clocks and kitchen appliances to elevator floor indicators and scoreboards. They are one of the most intuitive ways to display numerical information, and learning to control them with an Arduino is a fundamental skill for any electronics enthusiast. This tutorial will guide you through building a simple yet versatile seven-segment display driver that can count from 0 to 9, and it's designed to work with both common anode and common cathode displays. You can use this project as the foundation for building a variety of practical devices, such as a digital counter for your workshop, a temperature readout for a weather station, a timer for a board game, or a simple scorekeeper for your next family game night.

How to use a seven-segment LED display with Arduino
How to use seven segment LED display with Arduino

Hardware Components

To follow along with this project, you will need the following components. The wiring is straightforward, and the component list is minimal, making this a perfect beginner-friendly build.

  • Arduino Uno (or any compatible board)
  • 7-segment LED display (common cathode or common anode)
  • 8x 1kΩ resistors (560Ω to 1kΩ works fine)
  • Jumper wires
  • Breadboard
    How to use a seven-segment LED display with Arduino
    How to use seven segment LED display with Arduino

 

Wiring Guide

The wiring for this project connects each segment of the display to a digital pin on the Arduino, with a current-limiting resistor in series for each segment. The key difference in wiring depends on whether you have a common anode or common cathode display. In a common anode display, all the anodes (positive legs) of the internal LEDs are connected together and should be connected to the 5V supply. In a common cathode display, all the cathodes (negative legs) are connected together and should be connected to ground. The code automatically handles the logic inversion needed for your specific display type, so you only need to ensure the physical wiring is correct.

Arduino code to update the I2C address on the VL53L0X
Arduino code to update I2C address on VL53L0X — Arduino code to update the I2C address on the VL53L0X

As demonstrated in the video, connect the display's segment pins (A through G, and the decimal point) to Arduino pins 2 through 9 in order. Each segment pin should have a 1kΩ resistor in series. The common pin (anode or cathode, depending on your display) should be connected to 5V or GND respectively. It's important to use individual resistors for each segment rather than a single shared resistor. This ensures consistent brightness across all segments, regardless of how many are lit at once. With a single resistor, the current would be divided unevenly, causing some numbers to appear dimmer than others.

Code Explanation

The code for this project is written from scratch and doesn't require any external libraries. It's designed to be simple to configure and adapt to your specific display. The main user-configurable parts are at the top of the file.

#define type 1 // 0 for common cathode and 1 for common anode

const int displayPins[7] = {2,3,4,5,6,7,8}; // define pins
const int dicimalPoint = 9;// decimal point

The first line is the most critical setting. You must set the type variable to 1 if you are using a common anode display and 0 if you are using a common cathode display. This single value tells the code whether to invert the logic signals, ensuring the correct segments light up.

The next two lines define the Arduino pins connected to the display. The displayPins array holds the pin numbers for segments A through G, and dicimalPoint defines the pin for the decimal point. If you wish to use different pins, you can modify these values, but it's recommended to keep them in sequential order for simplicity.

The code includes a custom function, writeDigit(int digit), which is the primary way to display a number. You can call this function from your own code to show any digit from 0 to 9. For example, to display the number 5, you would simply call writeDigit(5);. The function handles the complex task of looking up the correct segment pattern and setting the pins accordingly. Another helper function, convertHighLow(int v), handles the logic conversion between common anode and common cathode displays, so you don't have to worry about the internal logic.

Live Project Demonstration

Once the wiring is complete and the code is uploaded, the Arduino will begin counting from 0 to 9, displaying each digit for one second on the seven-segment display. After reaching 9, the decimal point will light up for three seconds before the count restarts. As shown in the video, the use of individual resistors ensures that the brightness of the digits remains consistent, whether displaying a single segment (like the number 1) or all seven segments (like the number 8). This is a significant improvement over using a single resistor, which would cause noticeable variations in brightness.

Chapters

  • [00:00] Introduction to the Seven-Segment Display
  • [00:22] Understanding Seven-Segment Displays
  • [01:09] Common Anode vs. Common Cathode Wiring
  • [03:25] Step-by-Step Wiring with Resistors
  • [06:07] Demonstration and Brightness Considerations
  • [08:26] Code Explanation and Configuration
  • [14:25] Setup and Main Loop Functions
  • [16:33] Conclusion and Applications

Images

How to use a seven-segment LED display with Arduino
How to use seven segment LED display with Arduino
Arduino code to update the I2C address on the VL53L0X
Arduino code to update I2C address on VL53L0X
How to use a seven-segment LED display with Arduino
How to use seven segment LED display with Arduino
How to use a seven-segment LED display with Arduino
How to use seven segment LED display with Arduino
74HC595 LED Display: Seven-Segment Explained
74HC595 LED Display: Seven Segment explained
558-Lesson 21: Using a Seven-Segment Display | Arduino Step-by-Step Course
Language: C++
/*
 *  Robojax Arduino Step-by-Step Course
 * Lesson 21: Seven-Segment Display
 * Display number on seven-segment display with Arduino

   Please watch video instruction here: https://youtu.be/pxUjEsJQW2M
   or watch this https://youtu.be/f3GqRMQ8jYs

 * Written by Ahmad Shamshiri for Robojax (Robojax.com)
 * on December 07, 2018 at 22:34 in Ajax, Ontario, Canada

 * 
  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.
 * This code has been downloaded from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


#define type 1 // 0 for common cathode and 1 for common anode

const int displayPins[7] = {2,3,4,5,6,7,8}; // define pins
const int dicimalPoint = 9;// decimal point

// array for seven-segment LED values. Do not change. 
//This is used for both common anode and common cathode
byte displayLEDs[10][7] = { 
        { 0,0,0,0,0,0,1 },  // = 0
        { 1,0,0,1,1,1,1 },  // = 1
        { 0,0,1,0,0,1,0 },  // = 2
        { 0,0,0,0,1,1,0 },  // = 3
        { 1,0,0,1,1,0,0 },  // = 4
        { 0,1,0,0,1,0,0 },  // = 5
        { 0,1,0,0,0,0,0 },  // = 6
        { 0,0,0,1,1,1,1 },  // = 7
        { 0,0,0,0,0,0,0 },  // = 8
        { 0,0,0,0,1,0,0 }   // = 9     
        }; 
/*
 * 
 * @brief Writes digit to display 
 * @param digit (integer)
 * @return no return value
 * Written by Ahmad Shamshiri on Friday, December 7, 2018 for Robojax.com
 */        
void writeDigit(int digit) {
  // Robojax Seven-Segment Display for Arduino
  Serial.print(digit);
  Serial.print(" ");

   int digitValue;
  for (int i=0; i < 7; i++) 
  {

      digitalWrite(displayPins[i], convertHighLow(displayLEDs[digit][i]));// write the HIGH or LOW (depending on the type) to output pin
      Serial.print(convertHighLow(displayLEDs[digit][i]));// print the result to the Serial monitor

  }// for i
}//writeDigit ends here

/*
 * 
 * @brief Converts HIGH and LOW based on the "type" variable value
 * if type is 1, converts the "V" to LOW, or returns it as it is if type is 0 (zero)
 * @param v (integer)
 * @return the convertedValue
 * Written by Ahmad Shamshiri (Robojax.com) on Friday, December 8, 2018 for Robojax.com
 */  
int convertHighLow(int v)
{
  int convertedValue;
    if(type ==0)
    {
      convertedValue = 1 - v;// if common anode, change the HIGH to LOW and LOW to HIGH
    }else{
      convertedValue = v;// if common cathode, keep it the same
    } 

    return convertedValue;
}

void setup() {
  // Robojax Seven-Segment Display for Arduino
//Robojax Arduino Step-by-Step Course http://robojax.com/L/?id=338  
  for(int d=0; d<8; d++)
  {
    pinMode(displayPins[d], OUTPUT);// set pin as output
    
  }
   pinMode(dicimalPoint,OUTPUT);// define a pin for decimal point
   digitalWrite(dicimalPoint,HIGH);// turn decimal point OFF. Set to LOW if common Anode and HIGH for common cathode

  Serial.begin(9600);// initialize serial monitor with 9600 baud
}// setup ends here


void loop() {
  // Robojax Seven-Segment Display for Arduino
//Robojax Arduino Step-by-Step Course http://robojax.com/L/?id=338  
 for (int i=0; i<=9 ; i++) {
   writeDigit(i); 
   Serial.println("=====");
   delay(1000);
  }
  digitalWrite(dicimalPoint,LOW);// turn decimal point ON
  delay(3000);
 // writeDigit(4);
  // delay(3000);

}// loop ends here

Files📁

Other files