Arduino Snippets: LED Bargraph Display

An LED bargraph display comes in handy for displaying a tuning center point, monitoring a sensor above-and-below a setpoint, and suchlike.

Given the number of output bits, this requires a pair of ‘595 shift registers. There’s no particular need for speed, so the shiftOut() function will suffice. That means the register control bits don’t need the dedicated SPI pins and won’t soak up the precious PWM outputs required by, say, the RGB LED strip drivers.

LED Bargraph Display

LED Bargraph Display

It’s Yet Another Solderless Breadboard Hairball:

LED Bargraph Display - breadboard

LED Bargraph Display – breadboard

The upper LED bargraph is an HPSP-4836 RRYYGGYYRR block, the lower one is an all-green GBG1000. The bottom four LEDs aren’t connected; you could add another ‘595 shift register, but then you’d have four bits left over and you’d be forced to add more LEDs. Four bricks and five ‘595 chips would come out even, if you’re into that.

The LEDs run at about 4 mA, which would be enough for decoration in a dim room and seems about the maximum the poor little 74HC595 chips can supply. If you need more juice, you need actual LED drivers with dimming and all that sort of stuff.

You need not use LED bargraphs, of course, and discrete LEDs for the lower six bits make more sense. They’ll be good for mode indicators & suchlike.

The demo code loads & shifts out alternating bits, then repeatedly scans a single bar upward through the entire array. Note that the bar is just a bit position in the two bytes that get shifted out every time the array updates (which is every 100 ms); the array is not just shifting a single position to move the bar. Verily, the bar moves opposite to the register shift direction to demonstrate that.

The Arduino source code:

// LED Bar Light
// Ed Nisley - KE4ANU - November 2012

//----------
// Pin assignments
// These are *software* pins for shiftOut(), not the *hardware* SPI functions

const byte PIN_MOSI = 8;			// data to shift reg
const byte PIN_SCK  = 6;			// shift clock to shift reg
const byte PIN_RCK  = 7;			// latch clock

const byte PIN_SYNC = 13;			// scope sync

//----------
// Constants

const int UPDATEMS = 100;				// update LEDs only this many ms apart

#define TCCRxB 0x02						// Timer prescaler

//----------
// Globals

word LEDBits;

unsigned long MillisNow;
unsigned long MillisThen;

//-- Helper routine for printf()

int s_putc(char c, FILE *t) {
  Serial.write(c);
}

//-- Send bits to LED bar driver register

void SetBarBits(word Pattern) {

	shiftOut(PIN_MOSI,PIN_SCK,MSBFIRST,Pattern >> 8);
	shiftOut(PIN_MOSI,PIN_SCK,MSBFIRST,Pattern & 0x00ff);

	digitalWrite(PIN_RCK,HIGH);
	digitalWrite(PIN_RCK,LOW);

}

//------------------
// Set things up

void setup() {
	pinMode(PIN_SYNC,OUTPUT);
	digitalWrite(PIN_SYNC,LOW);	// show we arrived

//	TCCR1B = TCCRxB;					// set frequency for PWM 9 & 10
//	TCCR2B = TCCRxB;					// set frequency for PWM 3 & 11

	pinMode(PIN_MOSI,OUTPUT);
	digitalWrite(PIN_MOSI,LOW);

	pinMode(PIN_SCK,OUTPUT);
	digitalWrite(PIN_SCK,LOW);

	pinMode(PIN_RCK,OUTPUT);
	digitalWrite(PIN_RCK,LOW);

	Serial.begin(9600);
	fdevopen(&s_putc,0);				// set up serial output for printf()

	printf("LED Bar Light\r\nEd Nisley - KE4ZNU - November 2012\r\n");

	LEDBits = 0x5555;
	SetBarBits(LEDBits);
	delay(1000);

	MillisThen = millis();

}

//------------------
// Run the test loop

void loop() {

	MillisNow = millis();

	if ((MillisNow - MillisThen) > UPDATEMS) {

		digitalWrite(PIN_SYNC,HIGH);
		SetBarBits(LEDBits);
		digitalWrite(PIN_SYNC,LOW);

		LEDBits = LEDBits >> 1;
		if (!LEDBits) {
			LEDBits = 0x8000;
			printf("LEDBits reset\n");
		}

		MillisThen = MillisNow;
	}

}
About these ads

  1. #1 by YangYan Zheng on 19-February-2013 - 12:20

    HI there,

    I tried using the above code but changed the pin numbers as I am using arduino uno r3 with a sparkfun 30led bargraph breakout kit (https://www.sparkfun.com/products/10936?) but it doesn’t seem to work.

    the serial monitor keeps telling me “LEDBits reset”

    • #2 by Ed on 19-February-2013 - 13:25

      a sparkfun 30led bargraph breakout kit

      Remember to tie the serial clear pins high, otherwise nothing happens! In addition, their board has more LEDs than I’m using, so my code will light up just the first two displays.

      You should start from their demo code and libraries, then make sure your wiring matches their requirements; I didn’t use the SPI pins because I didn’t need hardware-assisted speed.

      It’ll be an eyecatching display when it works… good hunting!

  1. Arduino Snippets: DL1414 LED Character Display « The Smell of Molten Projects in the Morning
  2. Arduino Snippets: Temperature Measurement « The Smell of Molten Projects in the Morning
  3. Arduino Snippets: Bare-naked Passive IR Sensor « The Smell of Molten Projects in the Morning