How can I use a 4 digit 7 segment display with arduino and 74HC595?

Thread Starter

Jayasanka Bandara

Joined Mar 4, 2022
17
So as on the title I wanna use a 4 digit 7 segment display with 74HC595. The hardware side is OK. But the software is the problem.

I'm using Arduino Nano and I wanna to make a solar charge controller with Bluetooth(HC-05), That 7 segment display(which is common cathode), Attiny13 based remote and receiver(Or I will use a generic remote for Arduino), 2 P channel mosfets(IRF9540) with NPN driving transistor(2N3904), 100k and 22k voltage divider for solar input and battery input, buck converter for power the microcontroller and others, LM35 temp sensor and a lot. Here is a schematic with a scheme PDF below.

And also if there is problems with it; just say me.
Schematic_Arduino Based Solar Charge Controller_2022-03-06.jpgAnyway; You know what; I'm new to this. And I only know some of them.
The problem is that I don't have a lot of time to figure out how to do this because I'm still 14 or 14.5 and I have a lot of exams.

Don't worry about grammar because I'm also not English.

I also don't wanna to throw this away because It cost me 30$ and I accidentally broke one that was in home. My mom said it's okay. But my father will eat me if he noticed that. (And if I made one I can say him it was a junk and I made one better than it).

So if the 7 segment thing doesn't work well how can I use a LCD with I2C module?

Here is a bit of code I wrote(based on another code; I will put them below) but not sure any line is correct.

I sent this because I'm using this pins.

Code:
    // A0 - Voltage divider to measure solar panel voltage   (Solar)
    // A1 - Voltage divider to measure battery voltage       (Battery)
    // A2 - LM 35 Temperature Sensor IC              
    // A3 - 7 Segment Digit 3                                (analogWrite funtion)
    // A4 - 7 Segment Digit 4                                (analogWrite funtion)            
    // A5 - Bluetooth En/Dis                                 (Bluetooth ON/OFF)
    // A6 - Push Button 1                                    (Options)
    // A7 - Push Button 2                                    (Change (ON/OFF))
     
    // D0-  Bluetooth TX          
    // D1-  Bluetooth RX          
   
    // D2 - PWM Output to control MOSFET Q1                  (Charging MOSFET)
    // D3 - Control Load MOSFET Q2                           (Output MOSFET)
   
    // D4 - 74HC595 DataPin
   
    // D5 - Battery Red LED      
    // D6 - Battery Green LED
   
    // D7 - 74HC595 LatchPin
    // D8 - 74HC595 ClockPin
   
    // D9 - Load Green Led      
   
    // D10- Solar Green LED
           
    // D11- IR remote reciever  
   
    // D12- 7 segment digit1
    // D13- 7 segment digit2

void setup() {
  #include <Wire.h>
  #define SOL_ADC A0    // Solar panel side voltage divider is connected to pin A0
  #define BAT_ADC A1    // Battery side voltage divider is connected to pin A1
  #define TEMP_ADC A2   // LM 35 Temperature is connected to pin A3
  #define DIG3 A3
  #define DIG4 A4
  #define BL_CON A5
  #define OPT A6
  #define CHG A7
  #define AVG_NUM 10    // number of iterations of the adc routine to average the adc readings
  #define BAT_MIN 09.5  // minimum battery voltage for 12V system
  #define BAT_MAX 15.0  // maximum battery voltage for 12V system
  #define BULK_CH_SP 14.4 // bulk charge set point for sealed lead acid battery // flooded type set it to 14.6V
  #define FLOAT_CH_SP 13.6  //float charge set point for lead acid battery
  #define LVD 10.5          //Low voltage disconnect setting for a 12V system
  #define PWM_PIN 2         // pin-3 is used to control the charging MOSFET //the default frequency is 490.20Hz
  #define LOAD_PIN 3       // pin-2 is used to control the load
  #define DISP_DS 4
  #define BAT_RED_LED 5
  #define BAT_GREEN_LED 6
  #define DISP_STCP 7
  #define DISP_SHCP 8
  #define LOAD_GREEN_LED 9
  #define SOL_GREEN_LED 10
  #define IR_IN 11
  #define DIG1 12
  #define DIG2 13
}

void loop() {
  // put your main code here, to run repeatedly:

}
Some codes I found while searching is also below.

ARDUINO SOLAR CHARGE CONTROLLER ( Version 2.0)

Measure any DC voltage using Arduino

Getting Started with HC-05 Bluetooth Module & Arduino

Use an IR Remote Transmitter and Receiver with Arduino

7 Segment LED Displays 102 -- Using a Shift Register

Create a Bluetooth App and control the Arduino - YouTube

Thank You.
 

Attachments

Thread Starter

Jayasanka Bandara

Joined Mar 4, 2022
17
hi JB,
Welcome to AAC.
Looks an interesting project.
Have you written a Test Sketch for the LCD and shift register.?
E
Thanks for the Welcome; But I still don't have the LCD. So if wanted; I will buy one but they also expensive!

I have a test code but it only shows hex values on every digit but i want to use other possible digits and different letters with my desired data(like temp, bluetooth on/off, load on/off/automatic, delayed start, stop and so on!)

Here is the test code I have;

Code:
/*  SevenSegmentLEDdisplay102a.ino
 *   2017-02-20
 *   Mel Lester Jr.
 *  Simple example of using Shift Register with a
 *  Single Digit Seven Segment LED Display
*/
// Globals
const int dataPin = 4;  // blue wire to 74HC595 pin 14
const int latchPin = 7; // green to 74HC595 pin 12
const int clockPin = 8; // yellow to 74HC595 pin 11

/* uncomment one of the following lines that describes your display
 *  and comment out the line that does not describe your display */
const char common = 'c';    // common cathode
//const char common = 'a';    // common anode

bool decPt = true;  // decimal point display flag
 
void setup() {
  // initialize I/O pins
  pinMode(dataPin, OUTPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(A5, OUTPUT);
}

void loop() {
  decPt = !decPt; // display decimal point every other pass through loop

  // generate characters to display for hexidecimal numbers 0 to F
  for (int i = 0; i <= 15; i++) {
    byte bits = myfnNumToBits(i) ;
    if (decPt) {
      bits = bits | B00000001;  // add decimal point if needed
    }
    myfnUpdateDisplay(bits);    // display alphanumeric digit
    delay(500);                 // pause for 1/2 second
  }
}

void myfnUpdateDisplay(byte eightBits) {
  if (common == 'a') {                  // using a common anonde display?
    eightBits = eightBits ^ B11111111;  // then flip all bits using XOR
  }
  digitalWrite(latchPin, LOW);  // prepare shift register for data
  shiftOut(dataPin, clockPin, LSBFIRST, eightBits); // send data
  digitalWrite(latchPin, HIGH); // update display
}

byte myfnNumToBits(int someNumber) {
  switch (someNumber) {
    case 0:
      return B11111100;
      break;
    case 1:
      return B01100000;
      break;
    case 2:
      return B11011010;
      break;
    case 3:
      return B11110010;
      break;
    case 4:
      return B01100110;
      break;
    case 5:
      return B10110110;
      break;
    case 6:
      return B10111110;
      break;
    case 7:
      return B11100000;
      break;
    case 8:
      return B11111110;
      break;
    case 9:
      return B11110110;
      break;
    case 10:
      return B11101110; // Hexidecimal A
      break;
    case 11:
      return B00111110; // Hexidecimal B
      break;
    case 12:
      return B10011100; // Hexidecimal C or use for Centigrade
      break;
    case 13:
      return B01111010; // Hexidecimal D
      break;
    case 14:
      return B10011110; // Hexidecimal E
      break;
    case 15:
      return B10001110; // Hexidecimal F or use for Fahrenheit
      break; 
    default:
      return B10010010; // Error condition, displays three vertical bars
      break;   
  }
}
 

Thread Starter

Jayasanka Bandara

Joined Mar 4, 2022
17
hi,
With a 7 Segment LCD, you have 0 to 9 a few ASCII characters A, H, E, L, P, etc and some very basic other symbols.

E
Edited:
View attachment 262177
Thanks. But do you have any idea to program it? Because my father will come soon! And also I'm gonna go to town after 2 days from today!(So if wanted I can buy the LCD along with I2C)

I wanna to make this salad edible because without the code, the salad isn't useful right?
Thanks.
 

MrSalts

Joined Apr 2, 2020
2,767
If you are buying a 4-segment display that communicates with the Arduino by I2C, then it is very easy to connect and your 74HC595 will not be needed in most cases.
Please post a link to the display you plan to use. Adafruit.com has a library for Arduino driving a 4-digit, 7-segment display on an I2C carrier board (look for "7-segment feather") on their website. I cannot insure that each segment on your display will be connected exactly the same way as the Adafruit,com version but a solution is possible if the display is labeled I2C and you are buying from a reliable vendor.

If you are buying a bare display, I am sure we can help you figure it out too. While you are at the electronics shop, buy
- 8 pieces 470 ohm resistors to limit current to the display segments (buy 330 ohm instead of you are using white LEDs).
- buy your shift register chip 74HC595 if you have
- (do not connect D12, D13, A3 and A4 directly to the display to drive each digit! You should add PNP transistors there to handle the higher current of all seven segments to make a letter 8 (like 2N3906 - you'll need one for each digit( 4)).
 
Last edited:

Thread Starter

Jayasanka Bandara

Joined Mar 4, 2022
17
Is this schoolwork?
No but it's like a homework because as on the start; I broke the solar charge controller accidentally. But we have a constant power cuts so before it broke; I said my father that we can use this at night. But if he found that I broke it; ou know what will happen!(It was a chinese thing that was sitting inside my cupboard for 3 years. But I remembered it to my father again)
 

MrSalts

Joined Apr 2, 2020
2,767
If you are buying a bare display, I am sure we can help you figure it out too. While you are at the electronics shop, buy
- 8 pieces 470 ohm resistors to limit current to the display segments (buy 330 ohm instead of you are using white LEDs).
- buy your shift register chip 74HC595 if you have
- (do not connect D12, D13, A3 and A4 directly to the display to drive each digit! You should add PNP transistors there to handle the higher current of all seven segments to make a letter 8 (like 2N3906 - you'll need one for each digit( 4)).
 

Thread Starter

Jayasanka Bandara

Joined Mar 4, 2022
17
If you are buying a 4-segment display that communicates with the Arduino by I2C, then it is very easy to connect and your 74HC595 will not be needed in most cases.
Please post a link to the display you plan to use. Adafruit.com has a library for Arduino driving a 4-digit, 7-segment display on an I2C carrier board (look for "7-segment feather") on their website. I cannot insure that each segment on your display will be connected exactly the same way as the Adafruit,com version but a solution is possible if the display is labeled I2C and you are buying from a reliable vendor.
The problem is it's not a 1 piece. I couldn't find 4 digit one so I made one using 4 single ones(they are called 5161AS)
5161as.jpgAnd I made 4 digit one like this7Segments_displays.png
 

dl324

Joined Mar 30, 2015
16,846
So anyway can you at least say like can I code them to work with this 4 digit display?
Shouldn't be any problem.

Are you constrained by the number of outputs the Nano has? If not, I'd drop the shift register and just use 7 outputs to drive the segments. The Nano can sink/source more current than an HC795 and save time by setting all of the segments in 1 or 2 statements instead of using a loop to shift them into the shift register.
 

Thread Starter

Jayasanka Bandara

Joined Mar 4, 2022
17
Shouldn't be any problem.

Are you constrained by the number of outputs the Nano has? If not, I'd drop the shift register and just use 7 outputs to drive the segments. The Nano can sink/source more current than an HC795 and save time by setting all of the segments in 1 or 2 statements instead of using a loop to shift them into the shift register.
Actually I don't have that much pins(Because I want that every option mentioned!). So can I use an I2C module instead of 74HC595? Because The LCD is way expensive than 7 segment displays so I felt not good using LCD one!
 

Thread Starter

Jayasanka Bandara

Joined Mar 4, 2022
17
For homework, we can only provide hints. If this isn't actually homework, we're not bound by that rule.
You mean because I had selected the 'Homework Help' topic! So you can't(I think)

But it's fake! I had selected 'Programming and Languages'. Then it moved from 'Programming and Languages' to 'Homework Help' automatically!
 
Top