This vode is to introduct the 74HC595 to driver 8 LED lights and turn them ON from 0 to 8 one by one while keeping
each LED ON.
Projects in this video using 74HC595
Project 1: Basic Shifting LED lights
Project 2: Single Digit Seven Segment at 6:29 of video
Project 3: Two digits dispaly at 15:28 of this video
Project 4: Up to 8 Digits display is explaiend at 31:47
Project 5: Adjusting delay of each digits using potentiometer at 57:32
Project 6: Measuring DC voltage and dispalying on the LCD at 1:00:41
In Project 5: Adjusting delay of each digits using potentiometer
To learn better or to have fun, I have added a potentiometer (or variable resistor) to the project so we can adjust
the delay time between each digtis (multiplexing) so we can see how each didgits is turned on when for example
we want to display 876, we first see 8 then a delay, then 7 with a delay and then 6 with a delay which can be
adjust using the variable resistor.
Timing of chapters in the video
00:00 Start
2:19 Code-1 Walking light
4:02 code-1: demonstration
4:49 connecting seven segment to walking light code
6:19 Code-2 single digit display
8:13 code-2: wiring
12:20 code-2: code explained
14:35 code-2: demo
15:28 code-2: two digits wiring (main wiring)
24:25 code-3: two digits Seven Segment display using 74HC595 code
29:06 How to digits display works
31:47 Code-4: Up to 8 digits Seven Segment display using 74HC595
36:16 All digits code explained
50:43 All digits demo
55:38 two display in parallel
56:30 Common Anode/Common Cathode test
57:32 Code-5: Adjusting delay with potentiometer
1:00:42 Code-6: Pot voltage
1:03:36 driving display with transistor
1:04:01 wiring explained
1:05:29 demo: transistor driver
/*
This code is to driver two Digits Seven Segment display using two pices of 74HC595
Watch full video instruction on YouTube https://youtu.be/xhPXovgFhso
written and explained by Ahmad Shamshiri on https://youtube.com/@robojax
*/
/*
This is arduino sketch to use 74HC595 shift register to drive 2 to 8 LED Seven Segment Display
it can display integer like 38765 or floatping point number such as 345.7162
written by Ahmad Shamshiri
www.Robojax.com https://youTube.com/@robojax
writen on March 9 to March 16, 2024
*/
int CLOCK_SHCP_PIN11 = 12;//SHcp pin of 74HC595
int LATCH_STCP_PIN12 = 11;//STcp pin of 74HC595
int DATA_DS_PIN14 = 10;//ds pin of 74HC595
#define DIGITS 5
#define FLOAT_PRECISION 3
#define SHOW_ZERO false
const char type[]= "CC"; //CA for common anode, CC for common cathode
int myDigits[DIGITS], digitsLength;
long timeToRemember, decimalPosition=NULL;
bool debug , isFloatingNumber;
int debugDelay = 0;//set the debugging value in milliseonds like 2000. to turn OFF set it to zero
byte digits[] = {B00111111, //0
B00000110, //1
B01011011, //2
B01001111, //3
B01100110, //4
B01101101, //5
B01111101, //6
B00000111, //7
B01111111, //8
B01101111 //9
};
void showNumber(long, long);
int intLength(long);
void setup() {
Serial.begin(9600);
pinMode(CLOCK_SHCP_PIN11, OUTPUT);
pinMode(LATCH_STCP_PIN12, OUTPUT);
pinMode(DATA_DS_PIN14, OUTPUT);
digitalWrite(CLOCK_SHCP_PIN11, LOW);
digitalWrite(LATCH_STCP_PIN12, LOW);
digitalWrite(DATA_DS_PIN14, LOW);
if(debugDelay > 0)
{
debug= true;
}else{
debug= false;
}
}
void loop() {
showNumber(84321, 3000);
// showNumber(72, 3000);
// showNumberDecimal(1.654, 2000);
// showNumber(205, 3000);
// showNumberDecimal(90.548, 2000);
// for(int i=0; i < 131; i++)
// {
// showNumber(i, 100);
// }
// // //delay(2000);
// //delay(1);
}
/*
//code has been take from
https://forum.arduino.cc/t/digit-brightness-issue-7-segment-4-digit-display-2-x-74hc595/658032/16
and modified
* @brief shows number on the Seven Segment Display
* @param "num" is integer like 5, or 78 to display
* @param "t" is the delay time to display the digit.
* @return returns none
* Usage to show 18 for 2000 milliseconds: showNumber(18, 2000);
* Written by Ahmad Shamshiri on 08 Mar 2024
* in Ajax, Ontario, Canada
* www.Robojax.com
*/
void showNumber(long number, long t)
{
timeToRemember = millis();
isFloatingNumber = false;
digitsLength = intLength(number);//get the length of digits needed for leading zeros removal
int k=0;
for(int i=DIGITS-1; i >=0; i--)
{
int divisionValue = (int)(pow(10, k)+.5);
myDigits[i] = (number/ divisionValue) % 10;
k++;
}
// myDigits[4]=number % 10 ;//147 gets 7
// myDigits[3]=(number / 10) % 10 ;//147 gets 4
// myDigits[2]=(number / 100) % 10 ;//147 gets 1
// myDigits[1]=(number / 1000) % 10 ;//147 gets 4
// myDigits[0]=(number / 10000) % 10 ;//147 gets 4
//Serial.println(number);
while( millis() - timeToRemember < t)
// int k=0;
// while( 1)
{
// k++;
shiftDigits();
}
}
void shiftDigits()
{
int digitBits =B11111110;
int leadingZeros = DIGITS - digitsLength;
if(debug)
{
Serial.print("digitsLength:");
Serial.print(digitsLength);
Serial.print(" leadingZeros :");
Serial.println(leadingZeros);
}
for(int i=0; i< DIGITS+1; i++)
{
//Serial.print("Digit: ");Serial.println( myDigits[i]);
//delay(1000);
}
for(int i=0; i < DIGITS; i++)
{
switch (i)
{
case 0:
digitBits =B11111110;
break;
case 1:
digitBits =B11111101;
break;
case 2:
digitBits =B11111011;
break;
case 3:
digitBits =B11110111;
break;
case 4:
digitBits =B11101111;
break;
default:
break;
}
if(i == decimalPosition && isFloatingNumber)
{
digits[myDigits[i]] |= (1 << 7);//set 7th bit to 1 to display decimal
}
//digits[myDigits[i]] |= (1 << 7);//set 7th bit to 1 to display decimal
// Serial.print("myDigits[");
// Serial.print(i);
// Serial.print("] =");
// Serial.println(myDigits[i]);
//this loop below is to turn off the leading zeros if set the at the top of the code
int segmentsToDisplay =digits[ myDigits[i]] ;//remember the segments value for leading zero removal
if(!SHOW_ZERO)
{
for(int k=0; k < leadingZeros; k++)
{
if(i==k)
{
segmentsToDisplay =B00000000;//turn off all segments
break;
}
}
}
if(type =="CC")
{
segmentsToDisplay = ~segmentsToDisplay;//tottle all bits for common anode display type
}
if(debug)
{
Serial.print("i-:");
Serial.print(i);
Serial.print(" digitBits:");
Serial.print(digitBits, BIN);
Serial.print(" segmentsToDisplay:");
Serial.println(segmentsToDisplay, BIN);
Serial.print(" digits[ myDigits[");
Serial.print(i);
Serial.print("]]) = ");
Serial.println(digits[ myDigits[i]], BIN);
}
int potValue = analogRead(A0);
int delayValue = map(potValue, 0, 1024, 0, 2000);
if(delayValue > 20)
{
delay(delayValue);
}
// Serial.print(" delayValue:");
// Serial.println(delayValue);
digitalWrite(LATCH_STCP_PIN12, LOW);
shiftOut(DATA_DS_PIN14, CLOCK_SHCP_PIN11, MSBFIRST, digitBits);
shiftOut(DATA_DS_PIN14, CLOCK_SHCP_PIN11, MSBFIRST, segmentsToDisplay);
digitalWrite(LATCH_STCP_PIN12, HIGH);
delay(1);
if( i == decimalPosition && isFloatingNumber)
{
digits[myDigits[i]] ^= (1 << 7);//toggle the decimal bit to OFF
}
if(debug)
{
delay(debugDelay);
}
}
// int digitBits =B00100000;//keeps all digits ON with ditigtBits << 1 for [3] and << 2 for [2]
//digitBits =B00000001;
// Serial.print(number);
// Serial.print("digitBits:");
// Serial.print(digitBits, BIN);
// Serial.print(" for digit0: ");
// Serial.println(myDigits[0]);
}
void showNumberDecimal(float floatNumber, long t)
{
timeToRemember = millis();
isFloatingNumber = true;
// //if we have 2.8
// int d2 = num;//2.8 becomes 2
// int d1 = (int) (num *10);
// d1 = d1 % 10;// gets 8
// digits[d2] |= (1 << 7);//set 7th bit to 1 to display decimal
floatToInt(floatNumber);
if(debug)
{
Serial.print("Decimal position:");
Serial.println(decimalPosition);
}
while( millis() - timeToRemember < t)
{
shiftDigits();
}
//digits[d1] ^= (1 << 7);//toggle the decimal bit to OFF
}
void floatToInt(float floatNumber)
{
char charNumber[DIGITS+1];//create char array for one extra char for decimal place
dtostrf(floatNumber, DIGITS+1, FLOAT_PRECISION, charNumber);//
//now charNumber holds the float value as char array
String stringNumber =charNumber;//convert it to String to amke it easy to work with
if(debug)
{
Serial.print("===== ");Serial.print(floatNumber, FLOAT_PRECISION); Serial.println(" ===");
Serial.print("charNumber: ");
Serial.println(charNumber);
Serial.print("stringNumber: ");Serial.println(stringNumber);
}
////get the position of decimal place
for(int i=0; i< DIGITS+1; i++)
{
if(charNumber[i] =='.')
{
decimalPosition =i;//get the position of decimal point
break;
}
}
//this array is used to compare each char in the stringNumber variable
//so we assign integer for that value
char charDigits[]={'0','1','2','3','4','5','6','7','8','9'};
stringNumber.remove(decimalPosition, 1);//remove the decimal point from string
decimalPosition--;//after removing the decimal point, then we should subtract the position
if(debug)
{
Serial.print("stringNumber(. removed): ");Serial.println(stringNumber);
}
digitsLength =0;//need this to turn off leadzing zeros
for(int i=0; i< stringNumber.length(); i++)
{
//check each stringNumber.charAt(i) agains charDigits[n]
for(int n=0; n <=9; n++)
{
if(stringNumber.charAt(i) ==charDigits[n] )
{
myDigits[i]=n;//save the digit in the array
digitsLength++;
break;
}
}//inner loo end
}//for loop end
}//floatToInt end
/*
gets length of long integer
@param number is long integer to the its length
@return the lenght from 0 to 9
*/
int intLength(long number)
{
int Length = 0;
if(number > 99999999)Length = 9;
else if(number > 9999999)Length = 8;
else if(number > 999999)Length = 7;
else if(number > 99999)Length = 6;
else if(number > 9999)Length = 5;
else if(number > 999)Length = 4;
else if(number > 99)Length = 3;
else if(number > 9)Length = 2;
else if(number < 9 && number > 0)Length = 1;
else Length = 0;
return Length;
}//intLength ends