HC4LED library

Overview

The HC4LED is an inexpensive and easy to use 4-digit 7-segment display module, useful for microcontroller projects that need to display small numbers or short bits of text. It's also useful as a debugging device in situations where a serial monitor is not available or practical.

The module has some drawbacks compared to the popular LCD-style displays. It only has four available characters/digits, and these are made from 7-segment LED-type displays, which seriously limit its ability to render certain letters and cases. Because you program the device using a serial "shift in" with a fairly slow (2ms) clocking algorithm, display updates can look flickery and lethargic. Finally, there is the concern that these devices seem to be remaindered and may not be readily available in quantity when you are ready to mass produce that killer product. The supplier linked above, for example, seens to sell only used ones that have been "pulled from working systems".

These caveats aside, the devices are very cheap (~$4 USD) and easy to program using the library described here. Most importantly, each consumes only 2 (or possibly 3) pins from your precious Arduino collection.

You can buy these modules here.

Wiring

Wiring the HC4LED is easy. Select two available digital output pins from your Arduino and attach them to the data (1) and clock (2) inputs on the module. The module's blank (4) pin could be connected to Gnd, or assigned a third pin on the Arduino. The latter approach provides a cleaner look and feel, but consumes a pin. Lastly, hook the module's Gnd (5) and +5V (6) inputs appropriately.

The Library

You can download the library here. To install it, simply unzip the archive into the Arduino hardware libraries folder, in Windows, usually something like C:\Program Files\arduino-0011\hardware\libraries\. Restart the Arduino software. The library provides functions to display text using the Roman alphabet, numbers, and a couple of punctuations marks (- and '). Now's a good time to mention that there are several letters that look rotten because they just don't work well on 7-segment displays: 'k', 'm', 'v', 'w', 'x', and 'z'. Avoid them!

Usage

To use the library, first #include the appropriate header:

#include <HC4LED.h>

Assuming the module's "clock" and "data" lines are attached to Arduino digital output pins 4 and 5, and that the "blank" line is on pin 7, you might create an instance of the module object like this:

HC4LED display(4, 5, 7, true);

The true in the above example indicates that an upside-down orientation is desired. In this context, "upside-down" simply means that the numbers and letters are drawn in the 7-segment display with the non-functional period LED in the upper left, instead of, as conventionally used, in the lower right. There is only one practical difference between the "upside-down" and "right-side-up" orientations, but it is an important one. Since the module's update mechanism scrolls from the left side of the display to the right in the "normal" orientation, if you are going to use the library's scrolling feature to display left-to-right text, you will probably always want to use the "upside-down" orientation and mount your display accordingly. This will allow text to scroll smoothly and readably from the right side to the left.

There are just a few methods in this library:

  • display_text(char *text, unsigned delay_time = 0) - display a 4-character string with optional wait
  • display_number(unsigned short num, unsigned delay_time = 0) - display an unsigned short number from 0-9999 with optional wait
  • scroll_text(char *text, unsigned delay_time = 0) - scroll an arbitrarily long string with optional wait. The scrolling interval -- the amount of time between characters -- defaults to 250 ms, but may be set using the set_scroll_interval method.
  • set_scroll_interval(unsigned ms) - change the delay interval between characters of a scrolling message (default: 250).
  • set_upside_down(bool upside_down) - set display orientation. If you intend to use scroll_text(), it's recommended to set this to true either here or in the constructor.
  • clear() - clear the display

Sample Code

Here is a complete program that uses the HC4LED module to display some simple messages and then repeatedly displays the number of seconds that have elapsed since the computer was started.

#include <HC4LED.h>

#define DISP_DATA_PIN 4
#define DISP_CLOCK_PIN 5
#define DISP_BLANK_PIN 7

HC4LED disp(DISP_CLOCK_PIN, DISP_DATA_PIN, DISP_BLANK_PIN, true);

void setup()
{
  // Why do you think I don't say "hello world" here? :)
  disp.scroll_text("hello happy place    ", 1000);
  disp.scroll_text("0123456789-'    ", 2000);
  disp.display_text("hi", 1000);
  disp.set_upside_down(false);
  disp.display_text("hi", 1000);
  disp.set_upside_down(true);
}

void loop()
{
  // display a count of seconds since the computer started
  disp.display_number((unsigned short)(millis() / 1000));
  delay(1000);
}

Projects using the HC4LED

The Reaction Time Tester uses this library.

Contact

For further information or to make comments, please write mhcom <at> sundial <dot> com.

Share