Go Down

Topic: Problem interfacing 74hc595 and 4 digits 7 segment display (Read 174 times) previous topic - next topic

cristian10001

Hello gentlemen.
I tried to reproduce this project:

https://circuitdigest.com/microcontroller-projects/arduino-7-segment-display-clock# 

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):

Code: [Select]

#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.

spycatcher2k

Thank god I'm NOT a gentleman! Now I don't have to help.
Mrs Drew
-=-
http://www.uk-pcb.co.uk - UK PCB Fab Company
I will design & code for you, but I will also charge you (PM me)
If you don't like my answers, realize : I'm not being cheeky, I'm Just trying to prompt you to use your own brain/google etc.

meltDown


spycatcher2k

You are correct, but being excluded because of my gender! now that is just taking the p@#s
Mrs Drew
-=-
http://www.uk-pcb.co.uk - UK PCB Fab Company
I will design & code for you, but I will also charge you (PM me)
If you don't like my answers, realize : I'm not being cheeky, I'm Just trying to prompt you to use your own brain/google etc.

larryd

Quote
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 :) .




No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

cristian10001

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

spycatcher2k

If you had not been sexist, I would of provided the solution (a very small change to your code).
Mrs Drew
-=-
http://www.uk-pcb.co.uk - UK PCB Fab Company
I will design & code for you, but I will also charge you (PM me)
If you don't like my answers, realize : I'm not being cheeky, I'm Just trying to prompt you to use your own brain/google etc.

TomGeorge

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... :)
Everything runs on smoke, let the smoke out, it stops running....

spycatcher2k

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


Code: [Select]

#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);

}
Mrs Drew
-=-
http://www.uk-pcb.co.uk - UK PCB Fab Company
I will design & code for you, but I will also charge you (PM me)
If you don't like my answers, realize : I'm not being cheeky, I'm Just trying to prompt you to use your own brain/google etc.

larryd

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


Code: [Select]

#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?





No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

cristian10001

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:



Code: [Select]
//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.

TomGeorge

Thanks tom for the link but it seems to contain some viruses.
Okay, try this one to get your display working before anything else;
https://circuits4you.com/2016/12/14/segment-display-interfacing-arduino/

Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

johnwasser

Four switch statements are a poor substitute for an array:
Code: [Select]
// 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:
Code: [Select]
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:
Code: [Select]
   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
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

cristian10001

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.

Go Up