Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino UNO 4-Digit Seven Segment Display Counter

Image of Arduino UNO 4-Digit Seven Segment Display Counter

Circuit Documentation

Summary

This circuit consists of an Arduino UNO microcontroller connected to a 4-digit seven-segment display. The Arduino UNO is programmed to display a counter on the seven-segment display, which increments every 100 milliseconds. The SevSeg library is used to control the display.

Component List

Arduino UNO

  • Description: A microcontroller board based on the ATmega328P.
  • Pins:
    • UNUSED
    • IOREF
    • Reset
    • 3.3V
    • 5V
    • GND
    • Vin
    • A0
    • A1
    • A2
    • A3
    • A4
    • A5
    • SCL
    • SDA
    • AREF
    • D13
    • D12
    • D11
    • D10
    • D9
    • D8
    • D7
    • D6
    • D5
    • D4
    • D3
    • D2
    • D1
    • D0

4 Digit Seven Segment Display

  • Description: A display module that can show numbers and some letters using seven segments per digit.
  • Pins:
    • DIG1
    • A
    • F
    • DIG2
    • DIG3
    • B
    • E
    • D
    • DP
    • C
    • G
    • DIG4

Wiring Details

Arduino UNO to 4 Digit Seven Segment Display

  • D13 (Arduino UNO) to DP (4 Digit Seven Segment Display)
  • D12 (Arduino UNO) to G (4 Digit Seven Segment Display)
  • D11 (Arduino UNO) to F (4 Digit Seven Segment Display)
  • D10 (Arduino UNO) to E (4 Digit Seven Segment Display)
  • D9 (Arduino UNO) to D (4 Digit Seven Segment Display)
  • D8 (Arduino UNO) to C (4 Digit Seven Segment Display)
  • D7 (Arduino UNO) to B (4 Digit Seven Segment Display)
  • D6 (Arduino UNO) to A (4 Digit Seven Segment Display)
  • D5 (Arduino UNO) to DIG4 (4 Digit Seven Segment Display)
  • D4 (Arduino UNO) to DIG3 (4 Digit Seven Segment Display)
  • D3 (Arduino UNO) to DIG2 (4 Digit Seven Segment Display)
  • D2 (Arduino UNO) to DIG1 (4 Digit Seven Segment Display)

Code Documentation

Main Sketch (sketch.ino)

/* SevSeg Counter Example

   Copyright 2017 Dean Reading

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
   http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.


   This example demonstrates a very simple use of the SevSeg library with a 4
   digit display. It displays a counter that counts up, showing deci-seconds.
 */

#include "SevSeg.h"
SevSeg sevseg; //Instantiate a seven segment controller object

void setup() {
  byte numDigits = 4;
  byte digitPins[] = {2, 3, 4, 5};
  byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13};
  bool resistorsOnSegments = false; // 'false' means resistors are on digit pins
  byte hardwareConfig = COMMON_ANODE; // See README.md for options
  bool updateWithDelays = false; // Default 'false' is Recommended
  bool leadingZeros = false; // Use 'true' if you'd like to keep the leading zeros
  bool disableDecPoint = false; // Use 'true' if your decimal point doesn't exist or isn't connected

  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments,
  updateWithDelays, leadingZeros, disableDecPoint);
  sevseg.setBrightness(90);
}

void loop() {
  static unsigned long timer = millis();
  static int deciSeconds = 0;

  if (millis() - timer >= 100) {
    timer += 100;
    deciSeconds++; // 100 milliSeconds is equal to 1 deciSecond

    if (deciSeconds == 10000) { // Reset to 0 after counting for 1000 seconds.
      deciSeconds=0;
    }
    sevseg.setNumber(deciSeconds, 1);
  }

  sevseg.refreshDisplay(); // Must run repeatedly
}

/// END ///

SevSeg Library Header (SevSeg.h)

/* SevSeg Library
 *
 * Copyright 2020 Dean Reading
 *
 * This library allows an Arduino to easily display numbers and letters on a
 * 7-segment display without a separate 7-segment display controller.
 *
 * See the included readme for instructions.
 * https://github.com/DeanIsMe/SevSeg
 */

#ifndef MAXNUMDIGITS
#define MAXNUMDIGITS 8 // Can be increased, but the max number is 2^31
#endif

#ifndef SevSeg_h
#define SevSeg_h

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

// Use defines to link the hardware configurations to the correct numbers
#define COMMON_CATHODE 0
#define COMMON_ANODE 1
#define N_TRANSISTORS 2
#define P_TRANSISTORS 3
#define NP_COMMON_CATHODE 1
#define NP_COMMON_ANODE 0


class SevSeg
{
public:
  SevSeg();

  void refreshDisplay();
  void begin(uint8_t hardwareConfig, uint8_t numDigitsIn, const uint8_t digitPinsIn[],
          const uint8_t segmentPinsIn[], bool resOnSegmentsIn=0,
          bool updateWithDelaysIn=0, bool leadingZerosIn=0,
          bool disableDecPoint=0);
  void setBrightness(int16_t brightnessIn); // A number from 0..100

  void setNumber(int32_t numToShow, int8_t decPlaces=-1, bool hex=0);
  void setNumberF(float numToShow, int8_t decPlaces=-1, bool hex=0);

  void setSegments(const uint8_t segs[]);
  void getSegments(uint8_t segs[]);
  void setSegmentsDigit(const uint8_t digitNum, const uint8_t segs);
  void setChars(const char str[]);
  void blank(void);

  uint8_t getNumDigits() { return numDigits; }

  void setNewNum(int32_t numToShow, int8_t decPlaces, bool hex=0);
  void findDigits(int32_t numToShow, int8_t decPlaces, bool hex, uint8_t digits[]);
  void setDigitCodes(const uint8_t nums[], int8_t decPlaces);
  void segmentOn(uint8_t segmentNum);
  void segmentOff(uint8_t segmentNum);
  void digitOn(uint8_t digitNum);
  void digitOff(uint8_t digitNum);

  uint8_t digitOnVal,digitOffVal,segmentOnVal,segmentOffVal;
  bool resOnSegments, updateWithDelays, leadingZeros;
  uint8_t digitPins[MAXNUMDIGITS];
  uint8_t segmentPins[8];
  uint8_t numDigits;
  uint8_t numSegments;
  uint8_t prevUpdateIdx; // The previously updated segment or digit
  uint8_t digitCodes[MAXNUMDIGITS]; // The active setting of each segment of each digit
  uint32_t prevUpdateTime; // The time (millis()) when the display was last updated
  uint16_t ledOnTime; // The time (us) to wait with LEDs on
  uint16_t waitOffTime; // The time (us) to wait with LEDs off
  bool waitOffActive; // Whether  the program is waiting with LEDs off
};

#endif //SevSeg_h
/// END ///

SevSeg Library Implementation (SevSeg.cpp)

/* SevSeg Library
 *
 * Copyright 2020 Dean Reading
 *
 * This library allows an Arduino to easily display numbers and letters on a
 * 7-segment display without a separate 7-segment display controller.
 *
 * See the included readme for instructions.
 * https://github.com/DeanIsMe/SevSeg
 */

#include "SevSeg.h"

#define BLANK_IDX 36 // Must match with 'digitCodeMap'
#define DASH_IDX 37
#define PERIOD_IDX 38
#define ASTERISK_IDX 39
#define UNDERSCORE_IDX 40

static const int32_t powersOf10[] = {