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.
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, numOfDigits, ccDPins, segDPins, softCurrentLimitResistor, updateWithDelays, allowLeadingZeros, disableDecPoint);
arg1= Display type :
Value = COMMON_CATHODE or COMMON_ANODE.
When COMMOM_CATHODE is selected, the program automatically consults digit-vs-ccCode LUT Table of the library to collect the ccCodes for the digits to be shown on 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 = allowLeadingZeros : 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 65535;
arg2 = after how many digits (counting from right), the decimal point is to be placed
Value: 1 //23.5 is 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. Temperature is updated in every 2 sec.
(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 changing values.