Using a Seven Segment Display Library for Arduino

by Donut Studio in Circuits > Arduino

1442 Views, 4 Favorites, 0 Comments

Using a Seven Segment Display Library for Arduino

cover_text.png

Hey everybody,

in this instructable I'm going to show you how you can control a seven segment display directly with your arduino.

For that purpose, I've created a library that makes using a seven segment display super easy.

It makes use of the principle of multiplexing and is driven by bytes, but more on that later.

Supplies

supplies.jpg

First of all, you will need a few parts:

  • seven segment display (you can use various digit sizes, common anode or cathode)
  • pinout for your seven segment display and arduino
  • jumper wires to connect the pins
  • (220ohm resistors)
  • arduino IDE


I'm using:

  • arduino pro mini
  • F3461BH display (common anode, four digits)

Install the Library

1github.png
3library.png
2ziplibrary.png

You can find my library on github: https://github.com/Donut-Studio/Arduino-Seven-Segment-Controller

Once you've downloaded the .zip-file you can extract into the 'Documents/Arduino/libraries/' directory or use 'Sketch/Include Library/Add .ZIP Library...' from the IDE.

To make sure everything is set up correctly, restart your IDE.

Connect the Display

sevensegmentdisplay.png
connection1.jpg
connection2.jpg

Now back to the breadboard!

You can connect the pins however you like, but to make use of the brightness manipulation you should connect gnd/vcc to a pwm pin (analogwrite pins).

If you like, you can add 220ohm resistors. In my case however, it worked great without them.

Keep in mind that with to much current you can destroy your display. It can be better to use them.


Connections (arduino pro mini, common anode):

display -> arduino

--- segments ---

A -> 2

B -> 4

C -> 10

D -> 7

E -> 11

F -> 8

G -> 12

DP -> 13

--- vcc/gnd ---

D1 -> 9

D2 -> 6

D3 -> 5

D4 -> 3

Coding Time

4examples.png

Open up one of the examples inside your IDE: 'File/Examples/DonutStudioSevenSegment/...'


Before we can upload anything we have to adjust the sketch a bit though.

Change the pins in gnd and the constructor (if you used the same wiring, you can copy those lines; if you don't want to use the decimal pin, just put in a zero: 'SegmentController(2,4,10,7,11,8,12,0, gnd, 4);'):

// define the ground pins 
// d1, d2, d3, d4
int gnd[] = { 3, 5, 6, 9 };
// create an object of the SegmentController class and define the segment, ground pins and display length.
// SegmentController(int a, int b, int c, int d, int e, int f, int g, int dp, int gnd[], int length);
SegmentController disp = SegmentController(2, 4, 10, 7, 11, 8, 12, 13, gnd, 4);


In setup() make some changes to the initialize method of the display to fit your setup:

// initialize the display
// void initialize(bool _commonAnode, unsigned long _refreshTime, byte _brightness)
disp.initialize(true, 2, 175);

If you see flickering, you can try and use a better power supply for your board or a decreased refresh time (1, 0.5, etc.).

Now upload the sketch to your board and watch the display light up.


If you want to use more that eight digits, you have to change '#define MAXDIGITS 8' in the .h library file to your preferred number

Further Steps

cover_empty.png

Now you can try out the other examples and take a look at the results.


In the following, I will show you some of the most important methods for the display. The rest can be found at the github repository.

All of the code snippets will be in setup():

#include "DonutStudioSevenSegment.h"

int gnd[] = { d1, d2, d3, d4 };
SegmentController disp = SegmentController(a, b, c, d, e, f, g, dp, gound, displaylength);

void setup()
{
  disp.initialize(true, 2, 175);

/* --- NEW CODE HERE ---
...
*/ --- NEW CODE HERE ---
}

void loop()
{
  disp.refresh();
}

Display Strings and Text

text.jpg

What makes this library special is the fact that it can display strings and let them run through the display.

--- main methods ---

To display a fixed string call this method (void setString(String text, int shift = 0);):

disp.setString("Hey, how are you?");

If your display can't show everything, you can move the text by adjusting the 'shift' variable.


For a text which automatically moves through the screen, call this method (void setText(String text);):

disp.setText("Hey, how are you?");

Not setting restart to 'true' will start the new text at the position of the last text.


--- setting methods ---

Unknown characters will have a own symbol which can be changed by calling this method:

byte x = B01000000; // an explanation for bytes can be found below
disp.setUnidentifiedCharByte(x);
byte y = getUnidentifiedCharByte();


Maybe the speed of the text is to fast. With this method you can adjust the time it takes to make the next step:

disp.setTextUpdate(500); // it will take 500 milliseconds to move the text by one digit
int x = getTextUpdate();

Display Numbers

int.jpg
float.jpg

There are currently two methods for displaying numbers. The method for displaying a float is currently having a weird behaviour, but you can bypass it by converting your number into a string and displaying it.

disp.setInt(123); // will display ' 123'
disp.setInt(123, true); // will display '0123'
disp.setFloat(12.3f); // will display '12.29'

Display Bytes

byte.jpg

Since one digit of a seven segment display has eight leds, which can be turned on or off, you can use a byte (8 bit) to indicate the state of all segments. Each bit represents one segment of the digit, beginning with the decimal point and going backwards till you are at the 'A' segment. If you are using a common cathode display, 0 means off and 1 means on. At the other type the byte is inversed (0=on, 1=off).

byte x = B.GFEDCBA;
byte x = B00000000; // all segments disabled (at common cathode) - all segmets active (at common anode)


Inversing can be done in two ways:

byte x = disp.inverseByte(B10101010);
byte x = ~B10101010;


--- main method ---

Displaying a byte can be done by calling this method and hand over an byte array with the length of the display:

byte x[] = { B10101010, B10101010, B10101010, B10101010 };
disp.setByte(x);


--- presaved bytes ---

In the SegmentController class you can call those methods and receive a byte (which is automatically inversed if necessary):

byte x = disp.getNumber(0); // returns the byte for a number from 0 to 9
byte x = disp.getCharacter(char character); // returns a byte representing the character (if not known, it returns the unidentified byte)
byte x = disp.getMinus(); // returns the minus byte: B01000000 or B10111111
byte x = disp.getDot(); // returns the dot byte: B10000000 or B01111111

Byte Manipulation

You can also play around with those awesome bytes. The library inclues a few methods for (de-)activating segments:

byte x = disp.addByte(B01001000, B10101001); // adds to bytes together: B11011001
byte x = disp.subtractByte(B11011001, B10101001); // subtracts to bytes: B01001000
byte x = disp.setByteSegment(B01001000, 0, true/false); // (de-)activate the 'a' segment
byte x = disp.setByteSegment(B01001000, 7, true/false); // (de-)activate the 'dp/dot' segment


If you want to manipulate the current displayed byte you can call this method:

disp.setDigitSegment(0, 2, true); // activate the 'c' segment at the first digit

Effects

I've also added some effects to play around with. The first one is blinking:

disp.setBlinking(3, true); // enable blinking at d2
disp.setBlinkingAll(false); // disable blinking at all digits
disp.setBlinkInterval(250); // set the blink interval of the display (a smaller number means faster blinking)


You can disable/enable unwanted digits by calling those methods:

disp.setDigitState(1, false); // disable the second digit
disp.setDigitStateAll(true); // enable all digits


To reset all effects call this method:

disp.resetEffects(); // disables blinking and enables all digits


And now finally, I've got the method for changing the brightness of the display. It goes from 0=off to 255=full brightness

disp.setBrightness(100);

Thanks

Thanks for trying out my library and have fun creating projects with it. If you got some feedback, I'd like to hear it!

Cheers, Donut Studio!