7-segment Display Unit driven by SevSeg.h Library

1. The Library named SevSeg.h contains ready-made codes, routines, functions, and LUT tables written by somebody else, and it can be used to drive multi-digit 7-segment Display Unit. Now, the user is not required to execute pinMode(), digitalWrite(), bitRead(), delay(), for() and other commands in the sketch. The Library must be down loaded from this path: https://github.com/DeanIsMe/SevSeg and then it has to be included in the sketch and IDE. In this tutorial, we will use SevSeg.h Library to show 2, and 5 on the display unit of Fig-1.

7seg2
Figure-1:

2. The Library contains the following functions and declarations to drive multiplexed 7-segment display unit.
(1) sevSeg.begin(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);

sevSeg.begin(displayType, numDigits, ccDPins, segDPins, softCurrentLimitResistor, updateWithDelays, leadingZeros, disableDecPoint);

arg1= Display type : Value = COMMON_CATHODE or COMMON_ANODE.
When COMMOM_CATHODE is selected, the program automatically consults digit-vs-ccCode LUT Table (Fig-1.7) to collect the ccCodes for the digits of display unit.

arg2 = Number of digits to be shown: Value = 1 to 8

arg3 = an array that contains DPins with which the cc-pins of the digits are to be connected

arg4 = an array that contains DPins with which the segments-pins of the digits are to be connected

arg5 = SoftcurrentLimitResistor (Software controlled): Value = true or false
true means software controlled current limit resistor;
false means that the current limiting resistors are installed manually.

arg6 = updateWithDelays : true or false (choose false; why? No idea.)

arg7 = leadingZeros : Value = true or false;
true means keep the leading zeroes like: 05

arg8 = disableDecPoint : Value = true or false;
false means that we can show decimal point at any position of the display unit. The position is determined by sevseg.SetNumber() function.

===> sevseg.begin(COMMON_CATHODE, 2, ccDPins, segDPins, false, false, false, true);

(2) sevenSeg.SetNumber(arg1, arg2, arg3);

===> sevenSeg.SetNumber(numberToShow, placeOfDecimalPoint, base);

arg1 = integer value to be displayed
Declaration:

byte valueToShow = 25;   //range: 0 to 255;
unsigned int valueToShow = 0 to 35535;

arg2 = after how many digits (counting from right), the decimal point is to be placed
Value: 1 //23.5 an example of 3-digit display with decimal point before 1-digit from RHS

arg3 = base in which the “number of arg1” is to be shown; Value = HIGH or LOW
HIGH means that the hexadecimal digits (0 – 9, A- F) will appear on display unit.
LOW means that decimal digits (0 – 9) will appear on the display unit.

(3) sevSeg.refreshDisplay(); //performs the action of display refreshing
As the digits of a multiplexed display unit share common segment lines, we must show the digits one after another with some delay in-between; otherwise, the display unit will be frozened to a single digit. This process is known as refreshing. The sevSeg.refreshDisplay() function performs this refreshing job. The C++ codes of this function are within the given Library.

3. Sketch to show 2 and 5 on Display Unit of Fig-1 using SevSeg.h Library (tested)

#include<SevSeg.h>
SevSeg sevSeg;           //create object sevSeg

byte numberToShow = 25;

void setup()
{
	byte segDPins[] = {8, 9, 10, 11, 12, 13, 6, 7};  //DPin8 = seg-a, …, DPin-7 = seg-p
	byte ccDPins[] = {A0, A1};					//DPin-A0 = cc0, DPin-A1 = cc1
	sevSeg.begin(COMMON_CATHODE, 2, ccDPins, segDPins, false, false, false, true);
	sevSeg.setNumber(numberToShow, 0, LOW);
}

void loop()
{
	sevSeg.refreshDisplay();
}

4. Building LM35 Sensor based 3-digit Thermometer using SevSeg.h Library
(1) Build the following circuit of Fig-2 on breadboard.


Figure-2:

(2) Upload the following sketch (tested on UNO)

#include <SevSeg.h>
SevSeg sevSeg; 

void setup()
{
  Serial.begin(9600);
  byte ccDPins[] = {A0, A1, A2, A3}; //A0 = cc0-pin, A1 = cc1-pin, ...
  byte segDPins[] = {8, 9, 10, 11, 12, 13, 6, 7}; //8 = seg-a, 9 = seg-b ...
  sevSeg.begin(COMMON_CATHODE, 4, ccDPins, segDPins, false, false, false, false);
  analogReference(INTERNAL); //1.1V Vref for ADC
}

void loop()
{
  unsigned long prMillis = millis();
  while (millis() - prMillis < 2000) //wait for 2-second
  {
    sevSeg.refreshDisplay();  //keep refreshing display until 2-sec has elapsed
  }
  float rawTemp = 100 * ( 1.1 / 1023) * analogRead(A4);// 31.25xxxx...
  rawTemp = rawTemp*100.00; //myTemp = 3125.xxxx.......
  unsigned int myTemp = (unsigned int)rawTemp; //myTemp = 3125(.(xxxx...) takes onlyinteger part
  sevSeg.setNumber(myTemp, 2, LOW); //shows: 31.2(5), deciimal point before 2-digit from right
}

(3) Check that display unit shows the room temperature with 1-digit precision.
(4) Rub the sensor by fingers and observe that display unit shows chnaging values.

1 Like

Good tutorial, well explained step-by-step :+1:

A few things can be fixed or added:

  1. A link is missing, where to download it. It is here: https://github.com/DeanIsMe/SevSeg.
  2. It should not be downloaded, but installed with the Library Manage in the Arduino IDE, "SevSeg by Dean Reading".
  3. The second sketch does not compile, and the numbers after the dot are zero.
  4. An temperature update every 2 seconds ? Why not 10 times per second ?
  5. Did you know that Wokwi can simulate a 7-segment display. Both Common Cathode and Common Anode types are supported with an option: https://docs.wokwi.com/parts/wokwi-7segment. You can select an example and click start, that's all.
  6. So here is your tutorial in Wokwi: https://wokwi.com/arduino/projects/303626927555478080 :wink: I had to use a potentiometer for A4. Sign up at Wokwi to be able to store your own project.

[EDIT] Changed bulleted list into numbered list.

If you would serialize your comments like 1., 2. ..., it would be convenient for me to make the replies. However, I have numbered them as 1. for the top comment and so on.

1. I have added the link in my original post.
2. I like for myself to add the .zip file in the Arduino IDE.
3. Probably, I pasted the codes from text file. Now, I have tested again and pasted from code file.
4. It depends on the user. I have chosen 2-sec as an example to demonstrate the multi-tasking capbility of millis() function.

5. I usually do not do any simulation; rather, I make the hardware directly and test it. The production house needs simulation before hand to check circuit reliablity, robustness etc.

6. I will try.

Thanks a lot for going through my tutorial and offering appreciation along with constructive criticisms.

Building hardware is a necessary part to learn, but Wokwi is a lot of fun. It has no analog simulation yet, but it can do a lot. When someone shows a sketch for a ledstrip, then Wokwi can show what it looks like. That helps to understand it. When someone makes a simulation of a sketch for Pi (not by me), then I can immediately see the result.

1 Like