Using a 4 Digit & 7 Segment Display, With Arduino

207,941

81

80

Introduction: Using a 4 Digit & 7 Segment Display, With Arduino

In this tutorial I will be showing you how to use a 7 segment display with 4 digits using arduino. Some basic things I would like to point out is that this takes up almost all of the digital pins on the arduino uno, leonardo, the boards with 13 digital pins. Most displays have 12 breakout pins that connect either directly to the arduino, or through a resistor. Also, I would like to point out that these displays have no need for Ground, 5V, or 3.3V to be connected. So, let's get started...

Step 1: Materials

Using a display of this kind requires almost no materials.

- 4 x 330Ω resistors

- 12 x Male to male jumper wires

- 1 x Arduino

- 1 x breadboard of any size (they don't need the power rails)

No tools are required in order to make a prototype of this design. However, a soldering iron and a pcb board would be needed for a more permanent use.

Step 2: Breadboard Layout

Just to give you some context i will explain the usage of each pin. 8 out of the 12 pins on the display are used for the 8 segments. There are 7 segments used to form any digit while one controls the decimal point. The other 4 out of the 12 pins control each of the 4 digits on the display. Any pin that has a resistor on it is one of the 4 digit pins, otherwise they are the segment pins.

The above design I created from fritzing in order to show the connection between the display and the arduino. Note the placement of where the resistors are because if you connect the resistor to the wrong pin on the display, either that digit will not work or that segment will not work, FOREVER...

***IMPORTANT NOTE***

When setting up the circuit switch the yellow and the purple wires (I messed up my circuit diagram).

Step 3: Installing the Library

The library linked below is from the arduino website for easily controlling a display. The link goes to github. If you have never been on github listen carefully, if you have read the next paragraph. When you get onto the website look at the right side of the window and look down until you see, "Download Zip". Click that button and watch it appear in your downloads.

Now you have to load the previously installed library into your libraries folder. If you now how to do this then skip to the next step, otherwise keep on reading. First go into finder and locate the "documents" folder, and open it. Then, locate the "arduino" folder, and open it. Then, locate the libraries folder, and open it. Finally drag and drop the installed library into the just opened libraries folder.

IMPORTANT. If your arduino application was opened and running while you where putting the library into the libraries folder, the arduino won't recognize the library. You just have to quit the application and open it again.

https://github.com/DeanIsMe/SevSeg

Step 4: The Code

The code for actually displaying something is listed below.

#include "SevSeg.h"
SevSeg sevseg; //Initiate a seven segment controller object


void setup() {
    byte numDigits = 4;  
    byte digitPins[] = {2, 3, 4, 5};
    byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13};
    bool resistorsOnSegments = 0; 
    // variable above indicates that 4 resistors were placed on the digit pins.
    // set variable to 1 if you want to use 8 resistors on the segment pins.
    sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins, resistorsOnSegments);
    sevseg.setBrightness(90);
}
void loop() {
    sevseg.setNumber(3141, 3);
    sevseg.refreshDisplay(); // Must run repeatedly
   

Step 5: The Result

If you connected the display correctly, uploaded the code correctly, and copied the coded just right, then your display should be displaying pi (only 3.141).

If not then, read step six for whatever is happening wrong.

If you did get it right, then I suggest reading going further in step seven.

Step 6: Troubleshooting

Here i have listed two possible things that could have happend to your display that could have messed it up. Sadly only one of the two is fixable.

- Your display is displaying 8888

Do not worry this is the problem that is fixable, just follow these steps

1. Locate in the code " sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins); "

2. Change "COMMON CATHODE" to "COMMON ANODE"

3. Upload the code again

- Your display is displaying 3. 41 or .141 or 3.1 1 or 3.14 without the other 1

Sadly, this problem can either be fixable or not :.(... (thats my crying face)

1. Check your connections and make sure everything IS plugged in and not hanging out

2. Check the schematic again, because you most likely plugged it in wrong

3. Locate the line in void loop where it says to display (3.141, 3) and change 3.141 to 8888 and see if one of the 8 are missing

4. If so one of the wires might have not had a resistor when it should have had one meaning that one of the digits had burned out. You can check this by looking at the back of the display. If you see any black near one of the pins you did overpower one of the digit places. Sadly this IS NOT fixable and you would want to buy another display.

5. If you do not see a black mark anywhere than you switched up wires that can't get overpowered so check the schematic and plug then in correctly and change the code back from 8888 to 3.141.

Step 7: Going Further

One method of using a few pins on the arduino to control multiple LEDs that would otherwise require alot of pin is a tri-state 8 pin shift-register. So instead of directly connecting the 8 segment pins to arduino you can plug them into the shift register and figure out how to do that.

It is just an idea that I came up with but I do not know how to do or if it is even possible with the arduino and the display's library, but hey, food for thought for the curious.

Have fun with your display and i hope you liked this instructable because it is the first that i have actually finished out of the many i have in my drafts.

26 People Made This Project!

Recommendations

  • Pi Day Speed Challenge

    Pi Day Speed Challenge
  • Trash to Treasure Contest

    Trash to Treasure Contest
  • Microcontroller Contest

    Microcontroller Contest

80 Comments

0
ElectroBing
ElectroBing

Question 8 weeks ago

I am getting this error
no matching function for call to 'SevSeg::SevSeg()'

0
TheArduinoGuy
TheArduinoGuy

8 months ago

Do NOT follow this tutorial. There should be one resistor per LED in one segment (as you will be multiplexing) so 8 resistors in total. The way this guy has done it is totally wrong.

0
chickeneater
chickeneater

Reply 8 months ago

Convince me why a resistor per segment is better than a resistor per digit and I will edit. The instructions so far work and don’t burn out any LEDs so perhaps your solution will save some power?? I think both solutions accomplish the same thing because the internal wiring in the segment (a multiplexing setup) has all of the connections between connected LEDs in series so it shouldn’t matter where you put the resistors, as long as every LED has one in either of its respective series’ (the series where all of the X-segments are connected or the series where all of the segments in the Y-digit are connected). I opted for putting resistor on the digit pins because it would require less resistors (I.e less $, less work, more organized breadboard). The only evidence I’m getting to put them on the segments is “multiplexing” and “this guy has done it totally wrong.” Expand and let’s see if we could get to the bottom of which one is better...

0
leef_me
leef_me

Reply 8 months ago


Actually, you are both wrong; about different things.

TheArduinoGuy:
The library, takes into account whether the use will put resistors in
the segment or digits; based on a paramter called 'resistorsOnSegments'
As is, the library turns on a single segment but all affected digits.
depending on the refresh rate, the display would look terrible.

chickeneater:
The sketch you published is missing 'resistorsOnSegments' parameter, and others.

sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins,
resistorsOnSegments, updateWithDelays, leadingZeros, disableDecPoint);

The code happens to work, because the compiler /defaults/ that parameter to 0.
This POOR practice, and in some compilers this would appear as an ERROR/WARNING.

TheArduinoGuy & chickeneater:

If the library had defaulted to 'resistorsOnSegments =1' then resistors should
have beem used. They are necessary to limit (and balance) the current to each
digit segment.

0
chickeneater
chickeneater

Reply 8 months ago

Hey nice catch! I just went through the library and in the definition for the begin() function I see the parameter "bool resOnSegmentsIn=0" you were talking about.

I'm glad you found this, and further along in the library it is clear that this parameter affects how the display updates its value and if assigned a value inconsistent with the electrical schematic used, then some LEDs would be powered unequally and some would perhaps completely burn out. However, I'm not sure how this would throw an Error if the parameter has been assigned to a default parameter of 0 though? The whole point of default parameters is that the compiler won't throw an Error if a parameter is not assigned a value when the function is called.

In any case, I will update the code and add a comment to indicate to users that if they want to deviate from the schematic and use 8 resistors on the 8 segment pins they would have to change the code.

Thanks you both for starting and finishing this inquiry.

0
leef_me
leef_me

Reply 8 months ago

> I see the parameter "bool resOnSegmentsIn=0" you were talking about.

I missed that in the *.h file, so I didn't realize it had a default.

0
leef_me
leef_me

Reply 8 months ago


Actually, you are both wrong; about different things.

TheArduinoGuy:
The library, takes into account whether the use will put resistors in
the segment or digits; based on a paramter called 'resistorsOnSegments'
As is, the library turns on a single segment but all affected digits.
depending on the refresh rate, the display would look terrible.

chickeneater:
The sketch you published is missing 'resistorsOnSegments' parameter, and others.

sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins,
resistorsOnSegments, updateWithDelays, leadingZeros, disableDecPoint);

The code happens to work, because the compiler /defaults/ that parameter to 0.
This POOR practice, and in some compilers this would appear as an ERROR/WARNING.

TheArduinoGuy & chickeneater:

If the library had defaulted to 'resistorsOnSegments =1' then resistors should
have beem used. They are necessary to limit (and balance) the current to each
digit segment.

1
wcab24
wcab24

10 months ago on Step 3

You should get a shift register chip to reduce the amount digital pins that you use.

1
Gray7
Gray7

1 year ago

Thanks! It works well. I had the 11 and 12 wires switched the wrong way (I accidentally overlooked your warning ;) ) but after switching the two numbers in the code, it works.

0
KingCobraSlayer
KingCobraSlayer

Question 2 years ago on Step 2

What if i want to make it into a clock? i have an RTC module

0
Gray7
Gray7

2 years ago

Thanks! It Works!!!!!!!

0
DavidJ479
DavidJ479

Question 2 years ago on Step 7

HI, I am mega new to this but have tried adapting the code for my 3digit display. I have got it working bar 1 issue! Digit 1 always displays segment a. any ideas also is there any way I can edit the library???? really interested in its layout and learning to make adaptions. cheers

0
JaronF2
JaronF2

Question 2 years ago on Introduction

(Sorry for the long post)

Hi. I know this is 3 years after but, does anyone have a fix for this?

Arduino: 1.8.4 (Windows 10), Board: "Arduino/Genuino Uno"

C:\Users\(myName)\AppData\Local\Temp\cc6Op89S.ltrans0.ltrans.o: In function `setup':

C:\Users\(myName)\Documents\Arduino\Seven_Segment_four_digit_displayer/Seven_Segment_four_digit_displayer.ino:9: undefined reference to `SevSeg::begin(unsigned char, unsigned char, unsigned char*, unsigned char*, bool, bool, bool)'

C:\Users\(myName)\Documents\Arduino\Seven_Segment_four_digit_displayer/Seven_Segment_four_digit_displayer.ino:10: undefined reference to `SevSeg::setBrightness(int)'

C:\Users\(myName)\AppData\Local\Temp\cc6Op89S.ltrans0.ltrans.o: In function `loop':

C:\Users\(myName)\Documents\Arduino\Seven_Segment_four_digit_displayer/Seven_Segment_four_digit_displayer.ino:14: undefined reference to `SevSeg::setNumber(int, char, bool)'

C:\Users\(myName)\Documents\Arduino\Seven_Segment_four_digit_displayer/Seven_Segment_four_digit_displayer.ino:15: undefined reference to `SevSeg::refreshDisplay()'

C:\Users\(myName)\AppData\Local\Temp\cc6Op89S.ltrans0.ltrans.o: In function `_GLOBAL__sub_I_sevseg':

C:\Users\(myName)\Documents\Arduino\Seven_Segment_four_digit_displayer/Seven_Segment_four_digit_displayer.ino:3: undefined reference to `SevSeg::SevSeg()'

collect2.exe: error: ld returned 1 exit status

exit status 1

Fehler beim Kompilieren für das Board Arduino/Genuino Uno. (Error Whilst compiling for the Arduino/Genuino Uno)

I even tried this for the library error(I had a library error):

(I know the file name's weird but I had to give it a name for the checking and uploading).

Unbenannt.png
0
YOSHIKOC
YOSHIKOC

Question 2 years ago

to make a regressive counter from the number shown on the display, how could it be?

0
HernanL8
HernanL8

Question 2 years ago

I'm trying to make it with a Wemos D1 board, but got no results so far, 3 segments light up, no matter what number should I write to display, do you know if I have to have another precaution for this board? Thanks for the walkthrough

9a08bfb7-669d-4618-a301-ec7f8bf98b46.jpg
0
jebidiahkerman828
jebidiahkerman828

Question 2 years ago

I found something interesting about the module. If you attach a motor to a switch (VCC --> button--> motor --> GND) so that the motor spins when you press the button, whenever you push the button the seven segment resets. It really isn't a bug, but what might be causing this?

0
johanmoberg
johanmoberg

Answer 2 years ago

Probably your motor inducing back-EMF and the display module/Arduino is shorted and resets.Whenever something resets its usually a bad sign :)

Try adding a diode in parallel with the motor and the problem should be solved.

0
jgyprime
jgyprime

Question 2 years ago

you mentioned you are using 4x330ohm (one for each digit out of 4) resistors and risk burning some of the led's on the display (segments or digits).
why not use 12 resistors half the size of the one you mentioned (e.g. 160 or 180 ohm): one for each digit (4 resistors), and one for each segment (8 resistors)?

0
unicycling madness
unicycling madness

2 years ago

I found that with my display I either had to change the cpp file's mapping on all the g and f pins, or change the segment pins layout (swap 11 and 12, which correspond to the g and f segments) or the easiest way, just swap the wires. I would recommend you change the picture of the wiring setup, because its the easiest and quickest way to get everyone doing it right.