ESP8266 NodeMCU -Controling 4×7 Segment Multi-Digit Display 5641AH

In this tutorial, we will learn how to control a 4×7 Segment Display. To do so, we will use a 4-digit 7 segment display connected to a 74HC595 shift register to simply show whatever integer number from 0 to 9999 we send from the Serial Monitor of the Arduino IDE. If you want to display some numbers, this tutorial is the rigth one for you.

Parts Required

To build the global setup you must have the following parts:

Note: You do not need to have a NodeMCU board nor an ESP8266 to follow this tutorial. I am using this board because it is very famous and also because, I will post future tutorials using wifi communications. In order to use other boards, you just have to change the pin number.

4×7 Segment Display 5641AH

Figure 1

This kind of displays are similar to single 7-segment displays, but with some tricks. To display information such as the time or temperature, you can use this one instead of connecting multiple single digit displays side by side. If you want to know more about single 7-segment displays, check our tutorial on this topic.

In multi-digit displays, one segment pin (A, B, C, D, E, F, G, and DP) controls the same segments in all the digits, as you can see in figure 2. Multi-digit displays also have separate common pins for each digit. These are the digit pins. You can turn a digit on or off by switching the digit pin. The digit pins (D1 … D4) in our case, are common cathode. So to turn on a digit, pull the pin to GND.

Figure 2

Therefore, we are obligated to write to every digit at the same time. To control each digit individualy, we need to use the digit pin (D1… D4) to turn on and off the respective digit. For exemple, in figure 3 we can see that by pulling to GND the pin D4, we turn on the rigth digit. Therefore, to write a 4 digit number, like 1230, we have to follow some steps, starting from the rigth digit:

  • Turn off the digits D1, D2, and D3, by pulling them to Vcc;
  • Turn on the segments A,B,C,D,E,F, to write the number 0;
  • Wait some time;
Figure 3

Now, repeat the step above, but now for the digit D3 until you reach the digit D1.

Figure 4

This is an indication that we need to turn on and off and switch between digits very fast to be able to write something. For those who think that they will see the flickering effect provoced by switching the digits, do not worry because this operation occur very fast and your eye is not capable of detecting the transitions (Persistence of vision).

Shift Register 74HC595N

Figure 5

A shift register is a circuit you can use to control many outputs (8 here) at the same time while only using a few pins (3 here) of your board. It works on Serial IN Parallel OUT protocol. It receives data serially from the microcontroller and then sends out this data through parallel pins.

Thankfully Arduino provide a helper function specifically for shift registers called shiftOut(), which allows to simply shift the bits in one call.

Figure 6

As you can see in figure 6, pins marked as Q0-Q7 (15 and 1-7) are the output pins. Pin16 is connected to (VCC) 3.3V, whereas pins 8 and 13 are connected to ground and pin 10 to (VCC) 3.3V. Finally pins 14, 12 and 11 are data, latch and clock pins and are the ones used by NodeMCU to pass data to the shift register.

For further understanding about the logic and function tables of this circuit, you can visit the datasheet, and can also visit a older tutorial on this circuit.

Circuit with Shift Register

Figure 8

Now, let’s go to the fun part! To start, we can connect pins 16 (VCC) and 10 () of the shift register to the NodeMCU 3V3 pin and pins 8 (GND) and 13 () to its GND pin.

Then, connect the clock (SRCLK), latch (RCLK) and serial data (SER) pins of the shift register to the NodeMCU digital pins D5, D8 and D7 respectively.

All the parallel data output pins of the shift register (QA – QH) should be connected to the segments of the display (A – DP) by order.

Finally, connect the digit pins to the NodeMCU digital pins D1, D2, D3 and D4. Note that the digit pins need to be connected to current limiting resistors (220Ω), since they are the common terminals of the digits.

In the end, your circuit should be similar to the diagram represented in Figure 8.

Installing Timer Library

In order for us to apply the multiplexing method in our code, it is necessary to install the Timer.h library before we compile it. To do so, we first need to download the library by clicking in the following link:

https://github.com/JChristensen/Timer/archive/master.zip

Then, we need to install it into our Arduino library. Open the Arduino IDE and then select Sketch > Include Library > Add .ZIP Library… and search for the .zip file you have just downloaded, which should be in the Downloads folder (see Figure 9). Once installed, it will appear the following message in the Arduino IDE message area: “Library added to your libraries. Check “include library” menu”.

Figure 9

If you want to know more about this library, such as viewing some examples and functions, we suggest you consult this link:

http://www.doctormonk.com/2012/01/arduino-timer-library.html

Coding

As mentioned previously, with this code you can send any integer number from 0 – 9999 through the Arduino IDE serial monitor. To open the serial monitor, you can select Tools > Serial Monitor or simply click on the Serial Monitor button at the top right corner. Then type the number you want to see in the display in the small upper box and click on Send.

Regarding the code, the timer object is used to switch between digits. The segment pins are also turned ON and OFF for each number is stored in a byte array.

In the loop() function, the serial values are read, converted to integers and then to long data types. Then, the values in this long data are separated into single digits in the break_number() function.

Multiplexing is done by the timer class every() function and it calls the display_number() function once every millisecond. This uses the shiftOut() function to send signals to the shift register.

The latch pin is turned LOW before data is sent and turned HIGH afterwards. The function called cathode_high() is used to turn off the screen. Have Fun!

Video

To see a video of the setup working, you can visit our youtube channel and watch the video.

References

https://osoyoo.com/2017/08/08/arduino-lesson-4-digit-7-segment-led-display/#3.2

https://tttapa.github.io/ESP8266/Chap01%20-%20ESP8266.html

Please follow and like us:
Ricardo Carreira

Author: Ricardo Carreira

Portuguese Electrical Engineer interested in Automation, Energy, Power generation, Renewables and 3D printers . With a degree in Electrical and Computer Engineering in the field of Energy and Automation. Currently starting in Master’s degree in Electrical Engineering.

Leave a Reply

Your email address will not be published. Required fields are marked *