Categories
Electronics

Controlling 7 segment LED displays on Raspberry Pi with Node.js

As part of the preparation for my upcoming workshops (last two tickets left!) , I’ve been working on writing Node.js code to drive various bits of hardware that are included in the free kit that comes with the course. So I’ve made my first ever npm package! And it drives cute LED displays running on the MAX7219 chip.

In particular, there are two little LED displays that I really love. One is an 8 x 8 LED matrix, and the other is an 8 character 7 segment display. (The ‘7 segment’ refers to the arrangement of LEDs that makes up the number shape – think of the displays on the Delorean time-machine dashboard).

They’re both driven by the same chip, the MAX7219, which can drive 64 LEDs, so perfect for both of these displays, and you can actually daisy chain up to 8 of them together and run them from the same pins. There is already a Node.js library that can run these, and it works fine (MAX7219) – it’s geared towards the 7 segment display and it works with the native SPI device on the Raspberry Pi.

But it is possible to run a MAX7219 chip without using the SPI device. You can ‘bit-bang’ the GPIOs – this is a way of sending digital data out of pins manually by setting them to high or low for each bit. It’s probably more performant to use the native SPI device but this method is perfectly fast enough for most applications, can be connected to any pins, can have multiple outputs, and is simpler to set up (as you don’t have to enable the SPI device on the Raspberry Pi).

[UPDATE – I just found this post with more about how bit-banging the MAX7219 works]

I’ve also implemented a few other fun features :

  • Display a number on the 7 segment display, with a fixed number of decimal places and leading zeros
  • A full alphabet – send it any alpha-numeric character and the library will do its best to represent it (obviously m’s and w’s are a bit crap 🙂 )
  • Set a specific led at x and y coordinate of the 8 x 8 matrix – for fun graphics output

It’s based on the Arduino LedControl library so much of the API is similar, but it’s currently in a very early state so expect the API to change.

See the github page for more details.