Search Code

How to Use a Seven-Segment LED Display with Arduino

How to Use a Seven-Segment LED Display with Arduino

Arduino code to display digits on a common anode and common cathode LED display. The code has been explained in the video.

The main circuit is shown in the video tutorial.

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
183-Arduino code to update the I2C address on a VL53L0X
Language: C++
/*
 * Display number on a seven-segment display with Arduino
 * 
 * Written by Ahmad Shamshiri for Robojax.com 
 * on December 07, 2018 at 22:34 in Ajax, Ontario, Canada

Watch the video instruction for this code: https://youtu.be/f3GqRMQ8jYs
Get this code and other Arduino codes from Robojax.com
 * 
  * 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
  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
 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📁

No files available.