Easiest RGB LED Strip With Raspberry Pi!

8,603

17

10

Intro: Easiest RGB LED Strip With Raspberry Pi!

If you've looked into driving RGB LED strips from your raspberry pi, you may have found it to be more effort than you were willing to put in to it. Either you had to build a driver from scratch with transistors, or you needed to modify the circuitry of a mini amplifier to make sure it didn't blow up your Pi.

Well, now you don't have to - there's a plug and play solution, thanks to a fortuitous discovery about a particular model of RGB LED amplifier.

This is the kind you DON'T want: very cheap, highly tempting mini amplifiers, such as https://www.amazon.com/FAVOLCANO-Amplifier-Control...

They have a fatal flaw - the +12V input pin is connected to the +12V output pin. Connect it to your Pi and you'll blow up the Pi.

This is the kind you DO want: slightly more expensive and larger: https://www.amazon.com/TSSS-Repeater-Signal-Amplif... (Cost - $5.75!)

These ones do NOT connect the input and output 12V pins and are safe to drive directly from the +5V pin on the Pi and the GPIO output pins. (Though just to be sure in case there's ever a new design using the same packaging, check that for yourself with an Ohm meter)

Step 1: Connecting Is Trivial!

Connect the +5V pin of your Pi to the +12V input of the amp with a male-female jumper lead.

Connect 3 GPIO pins (GPIO 4,5,6 are a good choice) to the R, G & B inputs of the amp with three male-female jumper leads.

The amp will come with two short connectors for the LED strips. Use one to connect to your LED strip. They'll have color-coded wires - red, green and blue plus one other for the power line. (Black, in my case)

Step 2: Now Test It...

You will need the "pigpiod" system on your pi in order to take advantage of software PWM so that you can drive the LED strips at different intensities. "sudo apt-get install pigpiod" if it isn't already installed.

Go to https://www.raspberrypi.org/forums/viewtopic.php?f... to see how to configure your Pi so that pigpiod starts when you power up.

Here's a shell script that will let you set the LED strip to an arbitrary color:

#!/bin/bash
if [[ "$1" == "on" ]] ; then
  pigs pwm 23 0 pwm 24 0 pwm 25 0
else
  if [[ "$1" == "off" ]] ; then
    pigs pwm 23 255 pwm 24 255 pwm 25 255
  else
    # trying to compensate for eye's log response to brightness
#   tmpR=`echo 'scale=4; (e(l(2) * (((100 - '$1') * 8) / 100))-0.01)' | bc -l`
#   tmpG=`echo 'scale=4; (e(l(2) * (((100 - '$2') * 8) / 100))-0.01)' | bc -l`
#   tmpB=`echo 'scale=4; (e(l(2) * (((100 - '$3') * 8) / 100))-0.01)' | bc -l`

    # but simple linear seems to work better :-(
    tmpR=`echo 'scale=4; ((100-'$1')*255/100)' | bc -l`
    tmpG=`echo 'scale=4; ((100-'$2')*255/100)' | bc -l`
    tmpB=`echo 'scale=4; ((100-'$3')*255/100)' | bc -l`

    # convert to integer
    tmpR=`echo 'scale=0; ('$tmpR'/1)' | bc -l`
    tmpG=`echo 'scale=0; ('$tmpG'/1)' | bc -l`
    tmpB=`echo 'scale=0; ('$tmpB'/1)' | bc -l`

    # echo "RGB: "$tmpR $tmpG $tmpB
    pigs pwm 23 $tmpR pwm 24 $tmpG pwm 25 $tmpB
  fi
fi

Call it "ledstrip" and use it like this: ledstrip 50 50 0

The 3 parameters are the values of R, G & B in a range of 0 to 100.

That's it. Easiest LED strip project ever!

Share

    Recommendations

    • Optics Contest

      Optics Contest
    • Audio Contest 2018

      Audio Contest 2018
    • Plastics Contest

      Plastics Contest

    10 Discussions

    0
    None
    GoHskrs

    Tip 7 weeks ago

    Came back here and remembered that I intended to post my Perl code that controls my LEDs. It uses the X11 color database to set the color, or uses a random color (although some of the colors don't look like you'd expect because the LEDs don't render them as faithfully as a monitor would). You will need to install pigpiod as stated in the article and make sure it's running before using this script.

    Usage: ./ledrgb.pl [--color=color-name] [--random]

    --- start file ledrgb.pl --- cut here ---

    #!/usr/bin/perl -w
    use Getopt::Long;
    my $colordb = "/usr/share/X11/rgb.txt";
    open(COLORDB, $colordb)
    or die "Can't open $colordb - $!";
    my $random = 0;
    my $color = undef;
    GetOptions( "color=s" => \$color, "random" => \$random );
    if ($random) {
    $lineout = `/usr/bin/wc -l $colordb`;
    if( $lineout =~ m/^\s*(\d+)\s+/ ) {
    $lines = $1;
    } else {
    die "Can't understand output from wc - (" . $lineout . ")";
    }
    $rvalue = int(rand $lines);
    for($i=0; $i<$rvalue; $i++) {


    }
    chomp($selcolor =
    );
    } else {
    #$color = $ARGV[0];
    unless( $color ) {
    print "Usage: $0 [--color=color-name] [--random]\n";
    exit 1;
    }
    while(
    ) {
    next unless m/$color/i;
    push(@matches, $_);
    # $n++;
    }
    if( scalar(@matches) == 0 ) {
    print "Color $color not found in $colordb\n";
    exit 1;
    } elsif ( scalar(@matches) == 1 ) {
    $index = 0;
    } else {
    for($i=0; $i<=$#matches; $i++) {
    print $i, "\t", $matches[$i];
    }
    select STDOUT; $| = 1;
    print "Selection: ";
    chomp($input = );
    $input =~ s/[^\d+]//;
    $index = length($input) ? $input + 0 : 0;
    }
    chomp($selcolor = $matches[$index]);
    }
    print $selcolor, "\n";
    # Remove leading spaces
    $selcolor =~ s/^\s*//;
    ($r, $g, $b) = split /\s+/, $selcolor;
    $r1 = 255 - $r;
    $g1 = 255 - $g;
    $b1 = 255 - $b;
    $cmd = "/usr/bin/pigs pwm 23 $r1 pwm 24 $g1 pwm 25 $b1";
    print $cmd, "\n";
    exec $cmd;
    die "exec $cmd returned - shouldn't happen";

    --- end file ledrgb.pl --- cut here ---

    3 replies
    0
    None
    gtoalGoHskrs

    Reply 7 weeks ago

    Appreciate the code posting, shame it got mangled by Instructables, though would have been much worse if it had been python and lost the indentation!

    0
    None
    GoHskrsgtoal

    Reply 7 weeks ago

    Heh, yep. Guess we can't post code blocks in the comments. <code>Or can we?</code>

    0
    None
    GoHskrsGoHskrs

    Reply 7 weeks ago

    Nope, guess the code block formatting is reserved for articles only.

    Anyway, since it's Perl, indentation isn't a deal-breaker. Ping me via the site or george <dot> rapp <at-sign> Google's mail product <dot> com if you have any questions or problems with the code.

    0
    None
    EddN3

    Question 4 months ago on Step 2

    Awesome tutorial! I have a question in regards to setting up something similar. I’m planning to use this setup to control a strip, but I need the colur to change on a gpio pin change (i.e, the LEDs Aden blue, but turn red once a certain pin is high). Any ideas how to implement this in Python?

    1 more answer
    0
    None
    GoHskrs

    Tip 8 months ago on Step 2

    Thanks OP for a straightforward and easy Instructable. My build notes:

    * There is a new product on Amazon, and unfortunately the price has risen to US $13: https://www.amazon.com/gp/product/B00MN7UQVQ
    * My 12V power supply is on the floor, and the rPi and LED amplifier are up near the ceiling. I only wanted to run the 12V power up the wall, but I needed USB power for the rPi. I wired the output Power + / - leads to a cigarette lighter USB car adapter, and then used a Micro USB cable from that adapter to connect to the rPi.
    * My 5A power supply is sufficient to drive 10 meters of RGB LED strips with 30 LEDs/meter.

    instructables 1.pnginstructables 2.pnginstructables 3.pnginstructables 4.png
    2 replies
    0
    None
    gtoalGoHskrs

    Reply 8 months ago

    Thanks - the best thing about Instructables is when someone builds something you've suggested!

    It's coincidental that you just now posted this - this week I ran LED strips all round my living room for use as power-failure lighting and pulled this exact hardware from my box of parts to drive it. I'll be setting up a 12V lead-acid battery on trickle charge (same as most burglar alarm systems) and am contemplating the same wiring issues as you have. In my case I've decided to use a 12v->5v USB adapter for the Pi, and house the amplifier and Pi (ZeroW, if it works) on a shelf close to the strip to keep the wiring short & tidy.

    PS If you search around on Amazon or eBay you can find better prices for the same amplifier - currently $5.99 w/ Prime shipping is the best I can see on Amazon. Also there are now RGBW amps with an extra channel.

    0
    None
    GoHskrsgtoal

    Reply 8 months ago

    I was looking for 12v->5v adapters on Amazon, but decided to try the cigarette lighter adapter first because I have dozens of them lying around. :) 3+ days continuous uptime on the rPi and no issues thus far.

    Thanks for the tip on the cheaper amplifiers.

    0
    None
    gtoal

    1 year ago

    I hear that Broadcom pin numbers 12/13/18/19 are capable of hardware PWM support so may be a better choice than the ones above. OTOH I had previously understood that only pin 18 was supported by hardware PWM so I don't know if this applies to all models.