| A 4 digit 7 segment LED display. |
|
I found a website that solves all the wiring and coding issues;
A library, written by Dean Reading.
I downloaded the zip file, copied it to /usr/share/arduino/libraries/ and unzipped it (includes the .cpp, the .h and an example
sketch). (The directory is correct for my Linux Mint 16 system).
Given this library, the example code is simplicity itself.
/*Written by Dean Reading, 2012. deanreading@hotmail.com
This example is a centi-second counter to demonstrate the
use of my SevSeg library.
*/
#include "SevSeg.h"
//Create an instance of the object.
SevSeg sevseg;
//Create global variables
unsigned long timer;
int CentSec=0;
void setup() {
//I am using a common anode display, with the digit pins connected
//from 2-5 and the segment pins connected from 6-13
sevseg.Begin(1,2,3,4,5,6,7,8,9,10,11,12,13);
//Set the desired brightness (0 to 100);
sevseg.Brightness(90);
timer=millis();
}
void loop() {
//Produce an output on the display
sevseg.PrintOutput();
//Check if 10ms has elapsed
unsigned long mils=millis();
if (mils-timer>=10) {
timer=mils;
CentSec++;
if (CentSec==10000) { // Reset to 0 after counting for 100 seconds.
CentSec=0;
}
//Update the number to be displayed, with a decimal
//place in the correct position.
sevseg.NewNum(CentSec,(byte) 2);
}
}
|
I used the wiring listed on the webpage, which when interpreted gives us :
Pin 1 is the LED top row, left pin Pin 7 is the LED bottom row, left pin. LED pin - Arduino pin --------- ------------------- 1 - 1K2 resistor to 2 2 - 6 3 - 11 4 - 1K2 resistor to 3 5 - 1K2 resistor to 4 6 - 7 7 - 10 8 - 9 9 - 13 10 - 8 11 - 12 12 - 1K2 resistor to 5
However, wired up and the sketch uploaded, all I got was swift random flickering from the display.
| Wired up, all I got was flickering. |
|
I was pretty sure it was wired as described, after several tries, so clearly my display had different pin inputs.
I looked at the code, the first obvious thing to change was the common cathode / anode option.
In the code, I replaced the line
sevseg.Begin(1,2,3,4,5,6,7,8,9,10,11,12,13);
with
sevseg.Begin(0,2,3,4,5,6,7,8,9,10,11,12,13);
And it worked perfectly. So my display has a common anode instead of cathode. No problem.
| Wired up the same, with the code changed to use a common anode - and it works beautifully. |
|
The next obvious point is that this method uses way too many arduino pins to use in any standalone project, which might
also include a number of other sensors or motors so that you run out of arduino pins to use. One solution would be to
use a separate arduino to drive this display, and pass the data through the serial port.
A nice solution that I found is to use shift registers. I have been using these quite a lot recently on my robot car.
A separate Arduino Pro Mini can be bought for around £4 on EBay UK.
For a look at using shift registers, see
this chap's webpage.