Friday, 27 December 2013

Mark-Space RGB LED Modulation through GPIO

Quick experiment to see if I can set a colour from NetMash on an RGB LED using mark-space modulation driven by the GPIO pins of my Christmas Pi.

I grabbed GPIO 23/24 from these pins:


and wired them into a Red-Green LED. Adding Blue should be easy once I have that going.

The core of the program to drive the LED is this:

        FileWriter l1 = new FileWriter("/sys/class/gpio/gpio23/value");
        FileWriter l2 = new FileWriter("/sys/class/gpio/gpio24/value");

        int mark = 0;
        int total = 1024;
        int d = 8;

        while(true){
            int m=mark/64;
            int s=(total-mark)/64;

            l1.write("1"); l1.flush();
            l2.write("0"); l2.flush();
            Thread.sleep(m);

            l1.write("0"); l1.flush();
            l2.write("1"); l2.flush();
            Thread.sleep(s);

            mark+=d;
            if(mark>=total || mark<=0) d= -d;
        }

This causes the LED I have to pulsate between red, yellow, green, yellow, red, ...

I caught it in its yellow state in this photo:


The earth wire takes the long way round and the battery is unused, in case you were suspicious.. Also I used HDR to take the picture, so that the light wouldn't wash out completely.

My main concern with this was the load on the CPU and the file buffers, constantly turning the light on and off at speed. Here's a run of vmstat 2, to ease my worried mind:

root@raspberrypi:/home/pi/Cyrus# vmstat 2
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
 0  0      0  37984   4848  41576    0    0     6     3  590  517  6  2 93  0
 0  0      0  37956   4856  41576    0    0     0    22  643  600  1  2 97  1
 0  0      0  37956   4856  41576    0    0     0    10  657  607  2  3 95  0
 0  0      0  37956   4856  41576    0    0     0     2  685  616  1  1 98  0
 0  0      0  37956   4864  41576    0    0     0    22  665  607  2  1 97  0
 0  0      0  37956   4864  41576    0    0     0     2  705  632  1  1 98  0
 0  0      0  37956   4864  41576    0    0     0     2  645  601  2  2 97  0

Everything has to run as root. Since these are small devices, not multi-user servers, that's not a big issue.

No comments:

Post a Comment