Problem interfacing 74hc595 and 4 digits 7 segment display

Hello gentlemen.
I tried to reproduce this project:

I followed the same diagram, obviously I had some problems. I didn't find a solution, so I only extracted part of the program(this is for common anode):

#define latchPin 5                       
#define clockPin 6
#define dataPin 4

int thousands;     
int hundreds;
int tens;
int unit;

void setup () 
{
  
    pinMode(9,OUTPUT);//digit 4
    pinMode(10,OUTPUT);//digit 1
    pinMode(11,OUTPUT);//digit 2
    pinMode(12,OUTPUT);//digit 3
    pinMode(latchPin, OUTPUT);
    pinMode(clockPin, OUTPUT);
    pinMode(dataPin, OUTPUT);

}
 
void loop () 
{
    int number = 1234;// may be formed of one digit or two digits    
    int thousands = number/1000%10; 
    int hundreds = number/100%10;  
    int tens = number/10%10;       
    int unit = number%10;           
    int t= unit;
    int u= tens;
    int v= hundreds;
    int w= thousands;

//Converting the individual digits into corresponding number for passing it through the shift register so LEDs are turned ON or OFF in seven segment
switch (t)
{
  case 0:
  unit = 64;
  break;
  case 1:
  unit = 121;
  break;
  case 2:
  unit = 36;
  break;
  case 3:
  unit = 48;
  break;
  case 4:
  unit = 25;
  break;
  case 5:
  unit = 18;
  break;
  case 6:
  unit = 2;
  case 7:
  unit = 120;
  break;
  case 8:
  unit = 00;
  break;
  case 9:
  unit = 16;
  break;  
  }

switch (u)
{
  case 0:
  tens = 64;
  break;
  case 1:
  tens = 121;
  break;
  case 2:
  tens = 36;
  break;
  case 3:
  tens = 48;
  break;
  case 4:
  tens = 25;
  break;
  case 5:
  tens = 18;
  break;
  case 6:
  tens = 2;
  case 7:
  tens = 120;
  break;
  case 8:
  tens = 00;
  break;
  case 9:
  tens = 16;
  break;  
  }
 
  switch (v)
  {
  case 0:
  hundreds = 64;
  break;
  case 1:
  hundreds = 121;
  break;
  case 2:
  hundreds = 36;
  break;
  case 3:
  hundreds = 48;
  break;
  case 4:
  hundreds = 25;
  break;
  case 5:
  hundreds = 18;
  break;
  case 6:
  hundreds = 2;
  case 7:
  hundreds = 120;
  break;
  case 8:
  hundreds = 00;
  break;
  case 9:
  hundreds = 16;
  break;  
  }
  
  switch (w)
  {
  case 0:
  thousands = 64;
  break;
  case 1:
  thousands = 121;
  break;
  case 2:
  thousands = 36;
  break;
  case 3:
  thousands = 48;
  break;
  case 4:
  thousands = 25;
  break;
  case 5:
  thousands = 18;
  break;
  case 6:
  thousands = 2;
  case 7:
  thousands = 120;
  break;
  case 8:
  thousands = 00;
  break;
  case 9:
  thousands = 16;
  break;  
  }

    digitalWrite(9, LOW);
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST,thousands);  
    digitalWrite(latchPin, HIGH);  
    digitalWrite(9, HIGH);         
    delay(5);                      // delay for multiplexing 

    digitalWrite(10, LOW);
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST,hundreds );    
    digitalWrite(latchPin, HIGH);
    digitalWrite(10, HIGH);
    delay(5);                                 
  
    digitalWrite(11, LOW);
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST,tens);   
    digitalWrite(latchPin, HIGH);
    digitalWrite(11, HIGH);
    delay(5);
    
    digitalWrite(12, LOW);
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST,unit);   
    digitalWrite(latchPin, HIGH);
    digitalWrite(12, HIGH);
    delay(5);
    
}

I don`t understand how to display a number on the 7 segment display digit by digit. This number represents the value measured with a dht11 or another sensor. For example number = 32, I want to display 3 on digit 1 and 2 on digit 2, but it lights up like an 8. How to correctly represent a number on this display? Or, maybe you have some suggestions about the clock, using common anode display.

(deleted)

spycatcher2k:
Now I don't have to help.

Nobody has to help in the first place...

(deleted)

Thank god I'm NOT a gentleman! Now I don't have to help.

‘You’, the stronger sex, always put us guys down, we cannot live with you, ‘we’ cannot live without you :slight_smile: .

I guess no one has any suggestions. Maybe I shouldn`t address to certain category of people.

(deleted)

Hi,
Welcome to the forum.

Can you tell us your electronics, programming, arduino, hardware experience?

This may help, by removing all the clock programming; Look at the last half of the presentation;

http://osoyoo.com/2017/08/08/arduino-lesson-4-digit-7-segment-led-display/

Have you googled

74hc595 and 4 digits 7 segment display arduino

Thanks.. Tom... :slight_smile:

(deleted)

spycatcher2k:
Here is a solution, can you explain what I have done.

#define latchPin 5

#define clockPin 6
#define dataPin 4

int thousands;
int hundreds;
int tens;
int unit;

void setup ()
{

pinMode(9, OUTPUT); //digit 4
  pinMode(10, OUTPUT); //digit 1
  pinMode(11, OUTPUT); //digit 2
  pinMode(12, OUTPUT); //digit 3
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

}

void loop ()
{
  display(7);
}

void display(int number)
{
  int thousands = number / 1000 % 10;
  int hundreds = number / 100 % 10;
  int tens = number / 10 % 10;
  int unit = number % 10;
  int t = unit;
  int u = tens;
  int v = hundreds;
  int w = thousands;

//Converting the individual digits into corresponding number for passing it through the shift register so LEDs are turned ON or OFF in seven segment
  switch (t)
  {
    case 0:
      unit = 64;
      break;
    case 1:
      unit = 121;
      break;
    case 2:
      unit = 36;
      break;
    case 3:
      unit = 48;
      break;
    case 4:
      unit = 25;
      break;
    case 5:
      unit = 18;
      break;
    case 6:
      unit = 2;
    case 7:
      unit = 120;
      break;
    case 8:
      unit = 00;
      break;
    case 9:
      unit = 16;
      break;
  }

switch (u)
  {
    case 0:
      tens = 64;
      break;
    case 1:
      tens = 121;
      break;
    case 2:
      tens = 36;
      break;
    case 3:
      tens = 48;
      break;
    case 4:
      tens = 25;
      break;
    case 5:
      tens = 18;
      break;
    case 6:
      tens = 2;
    case 7:
      tens = 120;
      break;
    case 8:
      tens = 00;
      break;
    case 9:
      tens = 16;
      break;
  }

switch (v)
  {
    case 0:
      hundreds = 64;
      break;
    case 1:
      hundreds = 121;
      break;
    case 2:
      hundreds = 36;
      break;
    case 3:
      hundreds = 48;
      break;
    case 4:
      hundreds = 25;
      break;
    case 5:
      hundreds = 18;
      break;
    case 6:
      hundreds = 2;
    case 7:
      hundreds = 120;
      break;
    case 8:
      hundreds = 00;
      break;
    case 9:
      hundreds = 16;
      break;
  }

switch (w)
  {
    case 0:
      thousands = 64;
      break;
    case 1:
      thousands = 121;
      break;
    case 2:
      thousands = 36;
      break;
    case 3:
      thousands = 48;
      break;
    case 4:
      thousands = 25;
      break;
    case 5:
      thousands = 18;
      break;
    case 6:
      thousands = 2;
    case 7:
      thousands = 120;
      break;
    case 8:
      thousands = 00;
      break;
    case 9:
      thousands = 16;
      break;
  }

digitalWrite(9, LOW);
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, thousands);
  digitalWrite(latchPin, HIGH);
  digitalWrite(9, HIGH);
  delay(5);                      // delay for multiplexing

digitalWrite(10, LOW);
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, hundreds );
  digitalWrite(latchPin, HIGH);
  digitalWrite(10, HIGH);
  delay(5);

digitalWrite(11, LOW);
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, tens);
  digitalWrite(latchPin, HIGH);
  digitalWrite(11, HIGH);
  delay(5);

digitalWrite(12, LOW);
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, unit);
  digitalWrite(latchPin, HIGH);
  digitalWrite(12, HIGH);
  delay(5);

}

@OP
“Maybe I shouldn`t address to certain category of people.”

See all you have to do is be nice to everyone and people will help you.

Who is that masked person?

By "certain category of people" I meant those who don`t spend their time solving this type of problems.
Thanks tom for the link but it seems to contain some viruses.
Thanks spycat for opinion, probably after that function display(7) I should get digit1-> 0, dig2-> 0, dig3-> 0, dig4-> 7. Not really, all I see is 0000, if I grow all those delay (5) to delay (500) I get 0000 after 500ms 7777 again 0000 and so on. If I change 7 to 0123 for example, I get 0000 after 500ms 1111 ...2222...3333, so I guess here is something tricky.
This is the entire code and circuit diagram:

//Four-Digit 7 Segments Multiplexing using Arduino: Display time in HH:MM


#include <Wire.h>    //Library for SPI communication
#include <DS3231.h>   //Library for RTC module 

#define latchPin 5                       
#define clockPin 6
#define dataPin 4
//#define dot 2

DS3231 RTC;         //Declare object RTC for class DS3231

int h;              //Variable declared for hour
int m;              //Variable declared for minute

int thousands;     
int hundreds;
int tens;
int unit;

bool h24;
bool PM;

void setup () 
{
    Wire.begin();    
    pinMode(9,OUTPUT);
    pinMode(10,OUTPUT);
    pinMode(11,OUTPUT);
    pinMode(12,OUTPUT);
    pinMode(latchPin, OUTPUT);
    pinMode(clockPin, OUTPUT);
    pinMode(dataPin, OUTPUT);
    //pinMode(dot,OUTPUT);
}
 
void loop () 
{
    //digitalWrite(dot,HIGH);
    int h= RTC.getHour(h24, PM);  //To get the Hour
    int m = RTC.getMinute();      //TO get the minute
    int number = h*100+m;         //Converts hour and minute in 4-digit
    int thousands = number/1000%10; //Getting thousands digit from the 4 digit
    int hundreds = number/100%10;  //Getting hundreds digit from 4 digit
    int tens = number/10%10;        //Getting tens digit from 4-digit
    int unit = number%10;           //Getting last digit from 4-digit
    int t= unit;
    int u= tens;
    int v= hundreds;
    int w= thousands;

//Converting the individual digits into corresponding number for passing it through the shift register so LEDs are turned ON or OFF in seven segment
switch (t)
{
  case 0:
  unit = 64;
  break;
  case 1:
  unit = 121;
  break;
  case 2:
  unit = 36;
  break;
  case 3:
  unit = 48;
  break;
  case 4:
  unit = 25;
  break;
  case 5:
  unit = 18;
  break;
  case 6:
  unit = 2;
  case 7:
  unit = 120;
  break;
  case 8:
  unit = 00;
  break;
  case 9:
  unit = 16;
  break;  
  }

switch (u)
{
  case 0:
  tens = 64;
  break;
  case 1:
  tens = 121;
  break;
  case 2:
  tens = 36;
  break;
  case 3:
  tens = 48;
  break;
  case 4:
  tens = 25;
  break;
  case 5:
  tens = 18;
  break;
  case 6:
  tens = 2;
  case 7:
  tens = 120;
  break;
  case 8:
  tens = 00;
  break;
  case 9:
  tens = 16;
  break;  
  }
 
  switch (v)
  {
  case 0:
  hundreds = 64;
  break;
  case 1:
  hundreds = 121;
  break;
  case 2:
  hundreds = 36;
  break;
  case 3:
  hundreds = 48;
  break;
  case 4:
  hundreds = 25;
  break;
  case 5:
  hundreds = 18;
  break;
  case 6:
  hundreds = 2;
  case 7:
  hundreds = 120;
  break;
  case 8:
  hundreds = 00;
  break;
  case 9:
  hundreds = 16;
  break;  
  }
  
  switch (w)
  {
  case 0:
  thousands = 64;
  break;
  case 1:
  thousands = 121;
  break;
  case 2:
  thousands = 36;
  break;
  case 3:
  thousands = 48;
  break;
  case 4:
  thousands = 25;
  break;
  case 5:
  thousands = 18;
  break;
  case 6:
  thousands = 2;
  case 7:
  thousands = 120;
  break;
  case 8:
  thousands = 00;
  break;
  case 9:
  thousands = 16;
  break;  
  }

    digitalWrite(9, LOW);
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST,thousands);  // The thousand digit is sent
    digitalWrite(latchPin, HIGH);  // Set latch pin HIGH to store the inputs 
    digitalWrite(9, HIGH);         // Turinig on that thousands digit
    delay(5);                      // delay for multiplexing 

    digitalWrite(10, LOW);
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST,hundreds );    // The hundered digit is sent
    digitalWrite(latchPin, HIGH);
    digitalWrite(10, HIGH);
    delay(5);                                 
  
    digitalWrite(11, LOW);
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST,tens);   // The tens digit is sent
    digitalWrite(latchPin, HIGH);
    digitalWrite(11, HIGH);
    delay(5);
    
    digitalWrite(12, LOW);
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST,unit);   // The last digit is sent
    digitalWrite(latchPin, HIGH);
    digitalWrite(12, HIGH);
    delay(5);
    
}

I`m trying to display the time, temperature and humidity on a 7 segment display common anode using 74hc595 for less wires. Of course I can find other projects on google, but i stopped here.

cristian10001:
Thanks tom for the link but it seems to contain some viruses.

Okay, try this one to get your display working before anything else;

Tom... :slight_smile:

Four switch statements are a poor substitute for an array:

// Up at the top:
const byte SevenSegmentDigits[10] = {64, 121, 36, 48, 25, 18, 2, 120, 0, 16};


// then your four big switch statements become:
unit = SevenSegmentDigits[t];
tens =  SevenSegmentDigits[u];
hundreds =  SevenSegmentDigits[v];
thousands = SevenSegmentDigits[w];

And you don't need the separate the digits first:

unit = SevenSegmentDigits[number % 10];
tens =  SevenSegmentDigits[(number/10) % 10];
hundreds =  SevenSegmentDigits[(number/100) % 10];
thousands = SevenSegmentDigits[(number/1000) % 10];

And instead of pre-calculating the bit patterns you can put those expressions right in the shiftOut() calls. For example:

    digitalWrite(9, LOW);
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, SevenSegmentDigits[(number/1000) % 10]);  // The thousand digit is sent
    digitalWrite(latchPin, HIGH);  // Set latch pin HIGH to store the inputs 
    digitalWrite(9, HIGH);         // Turinig on that thousands digit
    delay(5);                      // delay for multiplexing

Thanks john for the improvements, I managed to make the program to show me something concrete, in brief it counts the minutes, apparently starting from compilation. Probably, to display the current time and minutes correctly, I need to change something in the header files, but I changed the program for dht11 and it looks promising.

سلام
این مسئله به این شکل حل می شود
ترتیب کد ها را بصورت زیر تغییر دهید
8)

digitalWrite(0, HIGH);
 digitalWrite(latchPin, LOW);
 shiftOut(dataPin, clockPin, MSBFIRST, SevenSegmentDigits[(number / 1000) % 10]); // Getting thousand digit from 4 digit & sent it
 digitalWrite(latchPin, HIGH);  // Set latch pin HIGH to store the inputs
 delay(5);                        // delay for multiplexing  
 digitalWrite(0, LOW);         // Turinig on that thousands digit

hamid-smd:
سلام
این مسئله به این شکل حل می شود
ترتیب کد ها را بصورت زیر تغییر دهید
8)

digitalWrite(0, HIGH);

digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, SevenSegmentDigits[(number / 1000) % 10]); // Getting thousand digit from 4 digit & sent it
digitalWrite(latchPin, HIGH);  // Set latch pin HIGH to store the inputs
delay(5);                        // delay for multiplexing 
digitalWrite(0, LOW);        // Turinig on that thousands digit

Hi this is solved this way Change the order of the code as follows

I don't understand why using Pin 0 instead of Pin 9 helps anything or why the delay should be before Pin 0/9 goes HIGH. Could you explain?

سلام
بله اصلاح در ترتیب کد ها موجب عملکرد صحیح شد.
بعلاوه تغییر در 1 کردن پین در اول دستور بجای آخر دستور..
وگرنه تغییر شماره پین بخاطر این بود که من از ESP32 استفاده کردم

hamid-smd:
سلام
بله اصلاح در ترتیب کد ها موجب عملکرد صحیح شد.
بعلاوه تغییر در 1 کردن پین در اول دستور بجای آخر دستور..
وگرنه تغییر شماره پین بخاطر این بود که من از ESP32 استفاده کردم

[color=#222222]Translated;
Hello
Yes Corrected the code in the order it worked.
Plus change to 1 pin in the first command instead of the last command ..
Otherwise the PIN change was because I used ESP32

Tom.... :) [/color]