Raspberry Pi – RGB LED conversion

I was looking at the cost of an Arduino and Ethernet shield and thinking that they are quite expensive compared with a Raspberry Pi.  So time to update my Arduino RGB LEDs project to run on a RPi.

As noted all over the web the Raspberry Pi only has one PWM channel on the GPIO.  But there is a cool software PWM workaround called Pi-Blaster that does the business and opens up 8 channels of PWM, over kill for the 3 channels I need.

Raspberry Pi – RGB LED conversionInitial trial with two PSUs:

Modify the driver board with a bigger 7805!:

Powering the RPi from the new PSU (I know I could have injected 5V back in through the GPIO header, but I have run out of 0.1″ sockets!):

The finished project:

The MQTT Python code:

#!/usr/bin/python
#
# rgb_mqtt_listener.py
# listen on MQTT queue for RGB light values
#

import os
import mosquitto

#define what happens after connection
def on_connect(rc):
        print "Connected"

def on_message(msg):
        print "OK " + msg.payload;
        hex_red = int(msg.payload[:2], 16);
        hex_green = int(msg.payload[2:4], 16);
        hex_blue = int(msg.payload[4:6], 16);
        print ("R:%d, G:%d, B:%d" % (hex_red, hex_green, hex_blue) );
        val_r = ( hex_red / 255.0 );
        val_g = ( hex_green / 255.0 );
        val_b = ( hex_blue / 255.0 );
        print ("R:%0.2f, G:%0.2f, B:%0.2f" % (val_r, val_g, val_b) );
        pwm_r = "echo 23=%0.2f > /dev/pi-blaster" % val_r;
        os.system(pwm_r);
        pwm_g = "echo 24=%0.2f > /dev/pi-blaster" % val_g;
        os.system(pwm_g);
        pwm_b = "echo 25=%0.2f > /dev/pi-blaster" % val_b;
        os.system(pwm_b);

#create a broker
mqttc = mosquitto.Mosquitto("python_sub")

#define the callbacks
mqttc.on_message = on_message
mqttc.on_connect = on_connect

#connect
mqttc.connect("mqtt.server.ip.address", 1883, 60, True)

#subscribe to topic test
mqttc.subscribe("rgblight", 2)

#keep connected to broker
while mqttc.loop() == 0:
        pass

Raspberry Pi – RGB LED conversion

The code listens on my Mosquito server for traffic on the “rgblight” topic.

Changing the colour of the strip is simple:

mosquitto_pub -h <mqtt server> -t "rgblight" -m "ff0000"   # Red
mosquitto_pub -h <mqtt server> -t "rgblight" -m "00ff00"   # Green

The Raspberry Pi is much quicker at processing the MQTT requests which I guess is to be expected, 16MHz vs 800MHz!


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top