Seven Segment LED Displays

Some Notation To Avoid Confusion

In this article I have used the following names:

The individual LED bars and dots on a four-digit 7-segment display are addressed as follows:

  1. module 1 - address 0 - first 7-segment module plus its dot
  2. module 2 - address 1 - second 7-segment module plus its dot
  3. colon - address 2 - both dots are either on or off
  4. module 3 - address 3 - third 7-segment module plus its dot
  5. module 4 - address 4 - fourth 7-segment module plus its dot

It's a bit odd that a 7-segment module with a decimal point is still called 7-segment even though it has eight addressable LEDs, but I'll still refer to them as 7-segment.

Addressing a 7-segment module

Each 7-segment module plus its dot are controlled by sending the address of the module and an 8-bit decimal number that represents the desired "on" positions. Each LED segment is assigned a value that is a power of 2 so that a single number, which is the sum of all the "on" values, can be used to create any segment pattern of the 256 that are possible. Using this method you can create you own characters or symbols and display them using writeDigitRaw().

There's a page here that shows all the possible combinations.

Practical Example

Example customised display

For example, you cannot display 3.6°C in a single print statement. Instead, you need to compose each character on its own by summing the values of the "on" segments and then sending that number to the appropriate module using a separate call to writeDigitRaw() for each as module shown here.

writeDigitRaw(0,1+2+4+8+64+128);     // First module, value 3 plus the decimal point
writeDigitRaw(1,1+32+16+8+4+64);     // Second module, value 6
writeDigitRaw(3,1+2+64+32);          // Fourth module, degree symbol
writeDigitRaw(4,1+32+16+8);          // Fifth module, value C

You might think that you can do this

char string[4];
int  digit1,digit2,digit3,digit4;


but there is no function that accepts a string either as a variable or a literal string.

To make coding easier, you could #define some symbols as shown below and then put those in the function call, e.g. writeDigitRaw(1,TOP+TOPRIGHT+MIDDLE+TOPLEFT);.

#define TOP         1
#define MIDDLE     64
#define BOTTOM      8
#define TOPRIGHT    2
#define TOPLEFT    32
#define BOTTOMLEFT 16
#define DECIMAL   128

Or just define frequently used characters

#define DEGREE     99

For standard digits using writeDigitRaw() you could set up an array like this:

int digits[10]={63,  // 0
                6,   // 1
                91,  // 2
                79,  // 3
                102, // 4
                109, // 5
                125, // 6
                7,   // 7
                127, // 8
                111};// 9

The Fritzing diagram below shows the connections to the ESP8266. Adafruit 7-segment LED and connection to ESP8266

Do this with an Arduino.