Tested on:RPi3; Raspbian Jessie; Python3
Prerequisites:Raspberry Pi with Raspbian Jessie and internet connection (see steps 1-7 in the Introduction)
Hardware:3 * N-Channel-MOSFET IRLB8721
Breadboard and jumper cables
12 Volt power source
12 Volt RGB LED strip
3 * 10k Ohm resistor
(Soldering equipment)

Color mixer: control a RGB-LED-strip with the Raspberry Pi and the N-Channel-MOSFET IRLB8721

RGB LED strips are cheaply available these days, e.g. 5 meters strip for around 13€ on Amazon. Together with a Raspberry Pi and a few more components you can light your home or car individually in amazing ambient colors.
This tutorial shows how to build a LED color mixer. The circuit and code where tested on a Raspberry Pi 3, but it should work on all models without changes.

Additionally to the harmless 5 Volt 2 Ampere of the Raspberry Pi, this circuit requires an power source with 12 Volt and 1.5 Amperes. An electrocute in this order of magnitute is still harmless for (healthy) humans, but the MOSFETs as well as the Raspberry Pi can be damanged easily when wired wrongly. Double-check the circuit and the polarities before starting the device!


RGB LED Schaltkreis

RGB LED Schaltkreis

Every individual light channel is controlled with the MOSFET IRLB8721, which is capable of switching the required power safely. For connecting the LED-strip, i’ve soldered jumper cables to one end of the strip.

Color mixer script
Download the sample script, then enable the 12-Volt power source and execute the script with

Be careful with the MOSFETs: they must not get hot!

Code explanation
For mixing the colors, software pulse-width-modulation is used which is already explained in the tutorial Motor driver with sPWM. Besides, only the function morph_step is interesting:

First, a list of indices of all color channels that do not fit the target color is collected. Then, choice of the random module is used for randomly selecting one of the color channels that need to be changed. That color is then changed by one percent into the direction of the target color. morph_step is called repeatedly until the target color is reached.

Full script
Click on Full Script to show all of the code.

Turn it up!
A more advanced version of this device could look like this:

LED strip und MoPower

LED strip and MoPower

The LED circuit was soldered onto an Adafruit ProtoHAT; below lies a MoPower for power management, emergency power and physical switching.

Software PWM tutorial: http://www.knight-of-pi.org/simple-dc-motor-board-for-the-raspberry-pi-with-ic-l293-and-software-pulse-width-modulation/
MoPower on Raspbian Wheezy: http://www.knight-of-pi.org/mopower-less-problems-efficient-energy-management-for-the-raspberry-pi/

Knight of Pi

Johannes Bergs aka Knight of Pi. Diploma in Bioinformatics, some Webdesign and Python coding then. Living in the beautiful city of Vienna.


Time limit is exhausted. Please reload CAPTCHA.