Controlling RGB LEDs with smooth colour transitions

Welcome to my first tutorial!

In this tutorial, I will show you how to drive any number of RGB LEDs with the Raspberry Pi, by using a library I have written. This is going to be in python and there will be smooth transitions between colours.

Whilst learning how to drive RGB LEDs using PWM (Pulse Width Modulation), I found that when changing colours, they were sudden and wasn’t very appealing to the eye. There was nothing on the internet, so I decided to write my own library.


So let’s get started. Assuming that you have wired your RGB LED(s), correctly, I will show you how to use my library. This will simplify the process of changing colours so you will be able to focus on your actual project. Here is the link to GitHub .

Firstly, create a test script, eg. Then, import the library so that all the functions are available to use:

from rgbled import rgbled

Now, you have to create a new rgbled object. This can be done for as many LEDs as you want. You can name your LED whatever you want:

led = rgbled(rpin, gpin, bpin)

And pass in the parameters of the red GPIO pin, green GPIO pin and blue GPIO pin. That’s it!

Now you can change the colour of your by:

led.changecolour(redValue, greenValue, blueValue, speed)

Each value has to between 1 and 100 and correspond to how much of the colour you want eg.

  • white – 100,100,100
  • red – 100,0,0
  • blue  – 0,0,100
  • green – 0,100,0
  • yellow – 100,100,0
  • cyan – 0,100,100
  • purple – 100,0,100

The speed is how fast you want the colour change to be and is in seconds. This has to be below 2 seconds. You could also play around with the different colour values to tweak to your own needs.

Also, you can turn it off completely:

What I found was that by just changing the colour to black (0,0,0) didn’t turn the LED off completely so this function turns it off. It just went really dim but still had a faint colour. Like above, the speed is just how fast the LED turns off and is in seconds. However, before doing any colour changes you need turn on the LED again, like this:

led.on(redValue, greenValue, blueValue, speed)

The parameters are the same for the changecolour() method.

At the end of your code, you must do:


So that the pins being used can be used again, after exiting the script. Hope you have fun making cool colours!

Please add comments or ask questions below.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s