22 thoughts on “How to code a 4 Digit 7 segment display using Arduino?

      1. Thank you for your help anyway. I have looked at the pinout diagram of my display and rearranged the cables accordingly so that the numbers are displayed correctly.
        One more question – how do I get a number with 2 digits or more (e.g., 16) to show on the display without having to alternate between each digit?

      2. Thank you for your help anyway. I have looked at the pinout diagram of my display and rearranged the cables accordingly so that the numbers are displayed correctly.
        One more question – how do I get a number with 2 digits or more (e.g., 16) to show on the display without having to alternate between each digit?

        1. I got around the issue by implementing a very small delay, such as this:
          void loop() {
          digit4();
          one();
          delay(4);
          digit3();
          six();
          delay(4);
          }
          It’s a bit of a drawback that if you try to display all four different numbers, the LEDs get dimmer, but with only two digits you shouldn’t notice too much of a difference. Hope this helps 🙂

    1. I wanted to do the same thing and wrote the following, but I had some problems with the source code. I guess it’s because I’m using a different 7-segment display, but I had to change all the HIGHs to LOWs and LOWs to HIGHs, so if the original code works with your display, you would probably need to change that back again. Also, the E-Segment – the one that’s connected to the first pin – didn’t turn off at all, so I plugged it into the 13th pin. Nevertheless, now it works : )
      #include

      int pinA = 11;
      int pinB = 7;
      int pinC = 4;
      int pinD = 2;
      int pinE = 13; // I changed this pin from 1 to 13, since my first pin was always on HIGH – no idea why
      int pinF = 10;
      int pinG = 5;
      int pinDP = 3;
      int D1 = 12;
      int D2 = 9;
      int D3 = 8;
      int D4 = 6;

      // Variables
      int minCount;
      int tenMinCount;
      int hourCount;
      int tenHourCount;
      int timer;

      void setup()
      {
      Serial.begin(9600);
      pinMode(pinA, OUTPUT);
      pinMode(pinB, OUTPUT);
      pinMode(pinC, OUTPUT);
      pinMode(pinD, OUTPUT);
      pinMode(pinE, OUTPUT);
      pinMode(pinF, OUTPUT);
      pinMode(pinG, OUTPUT);
      pinMode(pinDP, OUTPUT);
      pinMode(D1, OUTPUT);
      pinMode(D2, OUTPUT);
      pinMode(D3, OUTPUT);
      pinMode(D4, OUTPUT);
      minCount = 0; // Here you can input where the minute-segment should start (0-9)
      tenMinCount = 0; // Here you can input where the ten-minute-segment should start (0-5)
      hourCount = 0; // Here you can input where the hour-segment should start (0-9 if tenHourCount (249*60)) // I tested the clock by displaying seconds instead of minutes, so if you want to test it too, just delete the *60
      {
      ClockCounter();
      timer = 0;
      }

      }

      void ClockCounter()
      {

      minCount++;
      if (minCount > 9) {
      minCount = 0;
      tenMinCount++;
      if (tenMinCount > 5)
      {
      tenMinCount = 0;
      hourCount++;
      if (hourCount > 9 && tenHourCount != 2)
      {
      hourCount = 0;
      tenHourCount++;
      }
      else if (hourCount > 3 && tenHourCount == 2)
      {
      hourCount = 0;
      tenHourCount = 0;

      }
      }
      }
      Serial.println(minCount);
      Serial.println(tenMinCount);
      Serial.println(hourCount);
      Serial.println(tenHourCount);
      }

      void minDisplay()
      {
      digit4();
      if (minCount == 0)
      {
      zero();
      }
      else if (minCount == 1)
      {
      one();
      }
      else if (minCount == 2)
      {
      two();
      }
      else if (minCount == 3)
      {
      three();
      }
      else if (minCount == 4)
      {
      four();
      }
      else if (minCount == 5)
      {
      five();
      }
      else if (minCount == 6)
      {
      six();
      }
      else if (minCount == 7)
      {
      seven();
      }
      else if (minCount == 8)
      {
      eight();
      }
      else if (minCount == 9)
      {
      nine();
      }
      }

      void tenMinDisplay()
      {
      digit3();
      if (tenMinCount == 0)
      {
      zero();
      }
      else if (tenMinCount == 1)
      {
      one();
      }
      else if (tenMinCount == 2)
      {
      two();
      }
      else if (tenMinCount == 3)
      {
      three();
      }
      else if (tenMinCount == 4)
      {
      four();
      }
      else if (tenMinCount == 5)
      {
      five();
      }
      }

      void hourDisplay()
      {
      digit2();
      digitalWrite(pinDP, HIGH);
      if (hourCount == 0)
      {
      zero();
      }
      else if (hourCount == 1)
      {
      one();
      }
      else if (hourCount == 2)
      {
      two();
      }
      else if (hourCount == 3)
      {
      three();
      }
      else if (hourCount == 4)
      {
      four();
      }
      else if (hourCount == 5)
      {
      five();
      }
      else if (hourCount == 6)
      {
      six();
      }
      else if (hourCount == 7)
      {
      seven();
      }
      else if (hourCount == 8)
      {
      eight();
      }
      else if (hourCount == 9)
      {
      nine();
      }
      }

      void tenHourDisplay()
      {
      digit1();
      digitalWrite(pinDP, LOW);
      if (tenHourCount == 0)
      {
      zero();
      }
      else if (tenHourCount == 1)
      {
      one();
      }
      else if (tenHourCount == 2)
      {
      two();
      }
      }

      //functions representing numbers 0-9
      void zero() {
      digitalWrite(pinA, HIGH);
      digitalWrite(pinB, HIGH);
      digitalWrite(pinC, HIGH);
      digitalWrite(pinD, HIGH);
      digitalWrite(pinE, HIGH);
      digitalWrite(pinF, HIGH);
      digitalWrite(pinG, LOW);
      }

      void one() {
      digitalWrite(pinA, LOW);
      digitalWrite(pinB, HIGH);
      digitalWrite(pinC, HIGH);
      digitalWrite(pinD, LOW);
      digitalWrite(pinE, LOW);
      digitalWrite(pinF, LOW);
      digitalWrite(pinG, LOW);
      }

      void two() {
      digitalWrite(pinA, HIGH);
      digitalWrite(pinB, HIGH);
      digitalWrite(pinC, LOW);
      digitalWrite(pinD, HIGH);
      digitalWrite(pinE, HIGH);
      digitalWrite(pinF, LOW);
      digitalWrite(pinG, HIGH);
      }

      void three() {
      digitalWrite(pinA, HIGH);
      digitalWrite(pinB, HIGH);
      digitalWrite(pinC, HIGH);
      digitalWrite(pinD, HIGH);
      digitalWrite(pinE, LOW);
      digitalWrite(pinF, LOW);
      digitalWrite(pinG, HIGH);
      }

      void four() {
      digitalWrite(pinA, LOW);
      digitalWrite(pinB, HIGH);
      digitalWrite(pinC, HIGH);
      digitalWrite(pinD, LOW);
      digitalWrite(pinE, LOW);
      digitalWrite(pinF, HIGH);
      digitalWrite(pinG, HIGH);
      }

      void five() {
      digitalWrite(pinA, HIGH);
      digitalWrite(pinB, LOW);
      digitalWrite(pinC, HIGH);
      digitalWrite(pinD, HIGH);
      digitalWrite(pinE, LOW);
      digitalWrite(pinF, HIGH);
      digitalWrite(pinG, HIGH);
      }

      void six() {
      digitalWrite(pinA, HIGH);
      digitalWrite(pinB, LOW);
      digitalWrite(pinC, HIGH);
      digitalWrite(pinD, HIGH);
      digitalWrite(pinE, HIGH);
      digitalWrite(pinF, HIGH);
      digitalWrite(pinG, HIGH);
      }

      void seven() {
      digitalWrite(pinA, HIGH);
      digitalWrite(pinB, HIGH);
      digitalWrite(pinC, HIGH);
      digitalWrite(pinD, LOW);
      digitalWrite(pinE, LOW);
      digitalWrite(pinF, LOW);
      digitalWrite(pinG, LOW);
      }

      void eight() {
      digitalWrite(pinA, HIGH);
      digitalWrite(pinB, HIGH);
      digitalWrite(pinC, HIGH);
      digitalWrite(pinD, HIGH);
      digitalWrite(pinE, HIGH);
      digitalWrite(pinF, HIGH);
      digitalWrite(pinG, HIGH);
      }

      void nine() {
      digitalWrite(pinA, HIGH);
      digitalWrite(pinB, HIGH);
      digitalWrite(pinC, HIGH);
      digitalWrite(pinD, LOW);
      digitalWrite(pinE, LOW);
      digitalWrite(pinF, HIGH);
      digitalWrite(pinG, HIGH);
      }

      void digit1() {
      digitalWrite(D1, LOW);
      digitalWrite(D2, HIGH);
      digitalWrite(D3, HIGH);
      digitalWrite(D4, HIGH);
      }

      void digit2() {
      digitalWrite(D1, HIGH);
      digitalWrite(D2, LOW);
      digitalWrite(D3, HIGH);
      digitalWrite(D4, HIGH);
      }

      void digit3() {
      digitalWrite(D1, HIGH);
      digitalWrite(D2, HIGH);
      digitalWrite(D3, LOW);
      digitalWrite(D4, HIGH);
      }

      void digit4() {
      digitalWrite(D1, HIGH);
      digitalWrite(D2, HIGH);
      digitalWrite(D3, HIGH);
      digitalWrite(D4, LOW);
      }

  1. I need help even I installed the SevSeg.h library and added to the libraries of arduino it still says.

    Arduino: 1.8.19 (Windows Store 1.8.57.0) (Windows 10), Board: “Arduino Uno”

    try_arduino:1:10: fatal error: SevSeg.h: No such file or directory

    #include

    ^~~~~~~~~~

    compilation terminated.

    exit status 1

    SevSeg.h: No such file or directory

Leave a Reply

Your email address will not be published. Required fields are marked *