LED Matrices Control

Originally published at: LED Matrices Control – GHI Electronics

The market is flooded with addressable LEDs. These are LEDs that have very tiny microcontrollers built into each single LED. This allows a system, like SITCore, to control hundreds of LEDs using C#, thanks to TinyCLR OS.

This close up picture shows the micro which is connected individually to three RGB LEDs (Red, Green and Blue).

TinyCLR OS includes C# drivers for multiple LED types, including LP8006, APA102C and the most common WS2812. All drivers are hosted on NuGet and the source drivers are found on the drivers repo as well https://github.com/ghi-electronics/TinyCLR-Drivers.

The code to drive LEDs is simple and self explanatory. Here is the code to control 4 WS2812 LEDs.

const int NUM_LED = 4; // 4 leds
var pin = GpioController.GetDefault().OpenPin(SC20260.GpioPin.PA0);
leds = new WS2812Controller(pin, NUM_LED, WS2812Controller.DataFormat.rgb888);
leds.SetColor(0, 0xFF, 0, 0); // red
leds.SetColor(1, 0, 0xFF, 0); // green
leds.SetColor(2, 0, 0, 0xFF); // blue
leds.SetColor(3, 0xFF, 0xFF, 0xFF); // white

But you wouldn’t just use it to control 4 addressable LEDs! How about an LED matrix? Or even multiple matrices that are chained together, showing stock market ticker or weather info?

![](upload://ezW4Cg4K1Ma5FPudz8t4upxFYaE.png)

TinyCLR OS includes a library called BasicGraphics that is a perfect fit for driving small displays and LED matrices. Of course, TinyCLR OS includes full native graphics support that can be used as well, but BasicGraphics is simple and lean, and works on all SITCore devices, even the tiniest ones!

The LEDs on an LED matrix are connected as a zigzag chain. This means that the first row is left to right but then the second row is right to left. This can be easily corrected when overriding the SetPixel method of the BasicGraphics library.


class LedMatrix : BasicGraphics {
    private uint row, column;
    WS2812Controller leds;
public LedMatrix(GpioPin pin, uint column, uint row) { 
    this.row = row;
    this.column = column;
    this.leds = new WS2812Controller(pin, this.row * this.column, WS2812Controller.DataFormat.rgb565);
    Clear();
}
public override void Clear() {
    leds.Clear();
}
public override void SetPixel(int x, int y, uint color) {
    if (x < 0 || x >= this.column) return;
    if (y < 0 || y >= this.row) return;

    // even columns are inverted
    if ((x & 0x01) != 0) {
        y = (int)(this.row - 1 - y);
    }
    var index = x * this.row + y;
    leds.SetColor((int)index, (byte)(color >> 16), (byte)(color >> 8), (byte)(color >> 0));
}
public void Flush() {
    leds.Flush();
}

}

We now can use the available drawing methods in the BasicGraphics library, including DrawString.


var pin = GpioController.GetDefault().OpenPin(SC20100.GpioPin.PC6);
var screen = new LedMatrix(pin, 8, 32);
screen.Clear();
var col = LedMatrix.ColorFromRgb(0, 20, 50);
var c = 0;
while (true){
    screen.Clear();
    screen.DrawString(c++.ToString(), col, 0, 0);
    screen.Flush();
    Thread.Sleep(10);
}

Demo

This is a video what was contributed by the community, thanks @mifmasterz. Where he used (the super tiny) FEZ Flea with an LED Matrix.

Resources

Addressable LEDs are documented here and the BasicGraphics library is documented on the Software Utility page.

5 Likes