Back to Step by Step Course by Robojax

Lesson 21: Using Seven Segment Display | Arduino Step by Step Course

Part 2: LCD, LED and OLED Screens

In this lecture we learn how to use seven segment display direclty with Arduino.

 *  Robojax Arduino Step By Step Course
 * Lesson 21  Seven Segment Display
 * Display number on seven segment display with Arduino

   Please watch video instruction here
 This code is available at
with over 100 lectures Free On  YouTube Watch it here
Get the code for the course: 
 * Written by Ahmad Shamshiri for Robojax 
 * 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 download from
    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
    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 <>.

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

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 wites digit to display 
 * @param digit (integer)
 * @return no return value
 * Written by Ahmad Shamshiri on Friday Dec 07, 2018 for
void writeDigit(int digit) {
  // Robojax Seven Segment Display for Arduino
  Serial.print(" ");

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

      digitalWrite(displayPins[i], convertHighLow(displayLEDs[digit][i]));// write the HGIH 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 if 0 (zero)
 * @param v (integer)
 * @return the convertedValue
 * Written by Ahmad Shamshiri ( on Friday Dec 08, 2018 for
int convertHighLow(int v)
  int convertedValue;
    if(type ==0)
      convertedValue = 1 - v;// if common anode, change the HIGH to LOW and LOW to HIGH
      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  
  for(int d=0; d<8; d++)
    pinMode(displayPins[d], OUTPUT);// set pin as output
   pinMode(dicimalPoint,OUTPUT);// define a pin for decimal pint
   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  
 for (int i=0; i<=9 ; i++) {
  digitalWrite(dicimalPoint,LOW);// turn decimal point ON
 // writeDigit(4);
  // delay(3000);

}// loop ends here


The least I expect from you is to give the video a thumbs up and subscribe to my channel. I appreciate it. I have spent hundreds of hours making these lectures and writing code. You don't lose anything by subscribing to my channel. Your subscription is a stamp of approval for my videos, helping more people find them and, in turn, helping me. Thank you!

If you found this tutorial helpful, please support me so I can continue creating content like this. support me via PayPal

**** AFFILIATE PROGRAM **** We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to and affiliated sites.

Right Side