In this tutorial, I will explain how to connect a 7-segment display with 4 digits to an Arduino. Before I start explaining the connection details, I just want to point out a few basic things about this setup. This setup consumes almost all of the digital pins on boards like the Arduino Uno or Leonardo. Most displays have 12 breakout pins that connect either directly to the Arduino or through a resistor. Another thing to mention here is that these displays do not require any connection of Ground, 5V, or 3.3V.
Step1: Materials Required:
Material | Quantity |
330Ω resistors | 4 |
Male-to-male jumper wires | 12 |
Arduino board | 1 |
Breadboard | 1 |
Step 2: Breadboard Layout
For your context, here is how the pins are used:
8 of the 12 control the segments. There are seven to form digits, and one is for the decimal point.
4 control the individual digits on the display.
Important: Any pin with a resistor is one of the digit control pins. The rest are segment pins.
The following schematic (in Fritzing) shows how the display connects to the Arduino. Notice where the resistors go. Connecting a resistor to the wrong pin could mean either that digit or segment won’t work—ever.
Step 3: Installing the Library
The library linked below is from the Arduino website to make controlling the display easy. It’s hosted on GitHub. If you’ve never used GitHub before, listen up; if you’re familiar, skip this paragraph. When you get to the site, look on the right side of the page, scroll until you see “Download Zip,” then click it and check your downloads folder.
Now, you’ll import the library into the Arduino libraries folder. If you don’t know how to do it, just skip this part. If you do, get on with the tutorial. Otherwise, here’s what you’ll do:
- Open Finder and go to the Documents folder.
- Open the Arduino folder inside it.
- Open the libraries folder.
- Drag and drop the downloaded library into the libraries folder.
Step 4: The Code
Here is the code you need to see something:
#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 everything’s wired up properly and you have uploaded the code correctly, you should see pi (3.141) on your display.
If not, read on for some troubleshooting steps. If it works, go to Step 7 for advanced ideas.
Step 6: Troubleshooting
Here are two problems you might encounter with your display, and how to fix them:
- Your display reads 8888.
- This is fixable! Just do the following:
- Find that
sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins);
- Change
COMMON_CATHODE
toCOMMON_ANODE
. - Upload the code again.
- Find that
- This is fixable! Just do the following:
- Your display reads 3.41, .141, 3.1, or 3.14 (missing digits).
- This might or might not be fixable.
- Double check all connections; nothing should be loose.
- Check your schematic; you probably did something wrong.
- Change
3.141
to8888
in the code and upload it. If one of the 8s is missing:- Check the back of the display for black marks, which means a digit got burned out (not fixable).
- If there are no black marks, follow the schematic and switch the wires then reset the code.
- This might or might not be fixable.
Step 7: Moving On
You can even use a tri-state 8-pin shift register to control LEDs with fewer Arduino pins. You connect the segment pins to the shift register instead of directly to the Arduino. I haven’t tested this, but it could be an interesting option for those of you out there who like experimenting.