# Fading a RGB LED on BeagleBone Black

## Overview

In this tutorial, you will learn how to control the color of an RGB LED using a BeagleBone Black (BBB).

![](https://cdn-learn.adafruit.com/assets/assets/000/009/503/medium800/beaglebone_overview_web.jpg?1396892026)

Because the BBB runs Linux, there are many ways in which it can be programmed. In this tutorial we show how to control the power output of a GPIO pin and hence control the color of an RGB LED using Python.  
  
The tutorial uses a technique called PWM (Pulse WIdth Modulation) to vary the brightness of each color channel between 0 and 100.

# Fading a RGB LED on BeagleBone Black

## You will need

To make the project described in this tutorial, you will need the following:

# Fading a RGB LED on BeagleBone Black

## Installing the Python Library

This tutorial uses Ångström Linux, the operating system that comes pre-installed on the BBB.  
  
Follow the instructions here, to install the Python IO BBIO library. [http://learn.adafruit.com/setting-up-io-python-library-on-beaglebone-black](http://learn.adafruit.com/setting-up-io-python-library-on-beaglebone-black "Link: http://learn.adafruit.com/setting-up-io-python-library-on-beaglebone-black")

# Fading a RGB LED on BeagleBone Black

## Wiring

This tutorial is compatible with both common cathode and common anode LEDs.   
  
Common anode LEDs have the positive connections of the red, green and blue LEDs connected together to one lead (the longest lead). A common cathode LED has the negative connections connected together (again, the longest lead).  
  
Select the next step according to the type of RGB LED that you have.

# Fading a RGB LED on BeagleBone Black

## Wiring (Common Cathode LED)

Wire up the breadboard using the header leads as shown below.

![](https://cdn-learn.adafruit.com/assets/assets/000/009/509/medium800/beaglebone_fritzing.png?1396892133)

Push the LED leads into the breadboard, with the longest (common negative) lead on the second row of the breadboard. It does not matter which way around the resistors go.  
  
The top two connections on the BBB expansion header we are using (P8) are both GND. The three outputs to control the brightness of the red, green and blue channels are connected to sockets P8\_13, P8\_19 and P9\_14 of the BBB.  
  
The pins are numbered left to right, 1, 2 then on the next row down 3,4 etc. You can also use the pin P9\_16 for PWM output.  
  
You can find out about all the pins available on the P8 and P9 connecters down each side of the BBB here: [http://stuffwemade.net/hwio/beaglebone-pin-reference/](http://stuffwemade.net/hwio/beaglebone-pin-reference/ "Link: http://stuffwemade.net/hwio/beaglebone-pin-reference/")

# Fading a RGB LED on BeagleBone Black

## Wiring (Common Anode LED)

Wire up the breadboard using the header leads as shown below.

![](https://cdn-learn.adafruit.com/assets/assets/000/009/510/medium800/beaglebone_fritzing_ca.png?1396892174)

Push the LED leads into the breadboard, with the longest (common positive) lead on the second row of the breadboard. It does not matter which way around the resistors go.  
  
The common anode of the LED is connected to 3.3V. The three outputs to control the brightness of the red, green and blue channels are connected to sockets P8\_13, P8\_19 and P9\_14 of the BBB.  
  
The pins are numbered left to right, 1, 2 then on the next row down 3,4 etc. You can also use the pin P9\_16 for PWM output.  
  
You can find out about all the pins available on the P8 and P9 connecters down each side of the BBB here: [http://stuffwemade.net/hwio/beaglebone-pin-reference/](http://stuffwemade.net/hwio/beaglebone-pin-reference/ "Link: http://stuffwemade.net/hwio/beaglebone-pin-reference/")

# Fading a RGB LED on BeagleBone Black

## The Python Console

Before writing a Python program to control an LED, you can try some experiments from the Python console to control the LED's color and make sure that everything is working.  
  
To launch the Python Console type:

```auto
# python
Python 2.7.3 (default, Apr  3 2013, 21:37:23) 
[GCC 4.7.3 20130205 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; 
```

First, we need to import the library, in particular its PWM feature that will allow us to control the brightness of the three color channels. so enter the command:

```auto
&gt;&gt;&gt; import Adafruit_BBIO.PWM as PWM
```

To turn on the red component of the LED type:

```auto
&gt;&gt;&gt; PWM.start("P8_13", 0)
```

Next turn on the Green by doing:

```auto
&gt;&gt;&gt; PWM.start("P8_19", 0)
```

The LED should now look yellow / orange.   
  
Finally lets turn on the blue channel using:

```auto
&gt;&gt;&gt; PWM.start("P9_14", 0)
```

The LED should now be whitish in color as all three color channels are at full brightness.  
  
Lets now turn all the LEDs off again by sending the following commands:

```auto
&gt;&gt;&gt; PWM.stop("P8_13")
&gt;&gt;&gt; PWM.stop("P8_19")
&gt;&gt;&gt; PWM.stop("P9_14")
```

# Fading a RGB LED on BeagleBone Black

## Writing a Program

To make the LED cycle through a range of colors, we can write a short program. First, exit the Python console by typing:

```auto
&gt;&gt;&gt; exit()
```

This should take you back to the Linux prompt.  
  
Enter the following command to create a new files called fade.py

```auto
nano fade.py
```

Now paste the code below into the editor window.

```auto
import Adafruit_BBIO.PWM as PWM
import time

red = "P8_13"
green = "P8_19"
blue = "P9_14"

PWM.start(red, 0)
PWM.start(blue, 0)
PWM.start(green, 0)

def fade(colorA, colorB, ignore_color):
    PWM.set_duty_cycle(ignore_color, 100)
    for i in range(0, 100):
	    PWM.set_duty_cycle(colorA, i)
	    PWM.set_duty_cycle(colorB, 100-i)
	    time.sleep(0.05)
	
while True:
    fade(red, green, blue)
    fade(green, blue, red)
    fade(blue, red, green)
```

![](https://cdn-learn.adafruit.com/assets/assets/000/009/511/medium800/beaglebone_nano.png?1396892209)

Save and exit the editor using CTRL-x and the Y to confirm.  
  
To start the program, enter the command:

```auto
$ python fade.py
```

When you want to stop the program, use CTRL-c.  
  
You will notice that the LED will remain frozen in its last color. Since we are not stopping the PWM channels, they will run in the background.  
  
The program fades colors in pairs. First from red to green, then from green to blue and finally from blue to red, before continuing the cycle again.  
  
The set\_duty\_cycle changes the brightness of the led color. In the Adafruit\_BBIO.PWM library, brightest is 0 and 100 means the LED for that color is off.  
  
The program above assumes that you are using a common cathode LED. If you are using a common anode LED, then change these lines:

```auto
PWM.set_duty_cycle(ignore_color, 100)

PWM.set_duty_cycle(colorA, i)
PWM.set_duty_cycle(colorB, 100-i)
```

to look like this:

```auto
PWM.set_duty_cycle(ignore_color, 0)

PWM.set_duty_cycle(colorA, 100-i)
PWM.set_duty_cycle(colorB, i)
```

# Fading a RGB LED on BeagleBone Black

## PWM

Pulse Width Modulation (or PWM) is a technique for controlling power. We also use it here to control the brightness of each of the LEDs.   
  
The diagram below shows the signal from one of the GPIO pins on the BBB.

![](https://cdn-learn.adafruit.com/assets/assets/000/009/512/medium800/beaglebone_pwm.png?1396892273)

Every 1/5000 of a second, the PWM output will produce a pulse from 3.3V down to 0V. The length of this pulse is controlled by the 'set\_duty\_cycle' function.   
  
If the output is high for 90% of the time then the load will get 90% of the power delivered to it. We cannot see the LEDs turning on and off at that speed, so to us, it just looks like the brightness is different.

# Fading a RGB LED on BeagleBone Black

## Next Steps

You could try using analog readings, say from light or temperature to control the color of the LED. Perhaps, just set the red channel to 50% duty cycle and use a temperature reading to control the blue channel and the light reading to control the duty cycle of the green channel.   
  
You can find tutorials for sensing temperature and light [here](http://learn.adafruit.com/measuring-temperature-with-a-beaglebone-black "Link: http://learn.adafruit.com/measuring-temperature-with-a-beaglebone-black") and [here](http://learn.adafruit.com/measuring-light-with-a-beaglebone-black "Link: http://learn.adafruit.com/measuring-light-with-a-beaglebone-black").  
  
  
  
**About the Author.**  
As well as contributing lots of tutorials about Raspberry Pi, Arduino and now BeagleBone Black, Simon Monk writes books about open source hardware. You will find his books for sale [here](http://www.adafruit.com/index.php?main_page=adasearch&q=simon+monk) at Adafruit.


## Featured Products

### BeagleBone Black - Rev B

[BeagleBone Black - Rev B](https://www.adafruit.com/product/1278)
**[Adafruit is no longer shipping the BeagleBone Black Rev B, it has been replaced with the Rev C as of 5/12/14](https://www.adafruit.com/products/1876) - the Rev C now has 4G flash and also comes with Debian, it also costs slightly more. There are no exchanges or...**

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/1278)
[Related Guides to the Product](https://learn.adafruit.com/products/1278/guides)
### Diffused RGB (tri-color) 10mm LED (10 pack)

[Diffused RGB (tri-color) 10mm LED (10 pack)](https://www.adafruit.com/product/848)
Make some beautiful colors with these diffused 10mm tri-color LED with separate red, green and blue LED chips inside! They make a nice indicator, and fun to color-swirl. We like diffused RGB LEDs because they color mix inside instead of appearing as 3 distinct LEDs.  
  
These are...

In Stock
[Buy Now](https://www.adafruit.com/product/848)
[Related Guides to the Product](https://learn.adafruit.com/products/848/guides)
### Half Sized Premium Breadboard - 400 Tie Points

[Half Sized Premium Breadboard - 400 Tie Points](https://www.adafruit.com/product/64)
This is a cute, half-size breadboard with&nbsp;400 tie points, good for small projects. It's 3.25" x 2.2" / 8.3cm&nbsp;x 5.5cm&nbsp;with a standard double-strip in the middle and two power rails on both sides.&nbsp;You can pull the power rails off easily to make the breadboard as...

In Stock
[Buy Now](https://www.adafruit.com/product/64)
[Related Guides to the Product](https://learn.adafruit.com/products/64/guides)
### Premium Male/Male Jumper Wires - 40 x 3" (75mm)

[Premium Male/Male Jumper Wires - 40 x 3" (75mm)](https://www.adafruit.com/product/759)
Handy for making wire harnesses or jumpering between headers on PCB's. These premium jumper wires are 3" (75mm) long and come in a 'strip' of 40 (4 pieces of each of ten rainbow colors). They have 0.1" male header contacts on either end and fit cleanly next to each other...

In Stock
[Buy Now](https://www.adafruit.com/product/759)
[Related Guides to the Product](https://learn.adafruit.com/products/759/guides)

## Related Guides

- [Running Programs Automatically on Your Tiny Computer](https://learn.adafruit.com/running-programs-automatically-on-your-tiny-computer.md)
- [Nokia 5110/3310 LCD Python Library](https://learn.adafruit.com/nokia-5110-3310-lcd-python-library.md)
- [Using the BMP085/180 with Raspberry Pi or Beaglebone Black](https://learn.adafruit.com/using-the-bmp085-with-raspberry-pi.md)
- [DHT Humidity Sensing on Raspberry Pi or Beaglebone Black with GDocs Logging](https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging.md)
- [Measuring Light with a BeagleBone Black](https://learn.adafruit.com/measuring-light-with-a-beaglebone-black.md)
- [LedGames - a BeagleBone Black 64x64 LED Game](https://learn.adafruit.com/ledgames-beaglebone-black-64x64-led-game.md)
- [FONA Tethering to Raspberry Pi or BeagleBone Black](https://learn.adafruit.com/fona-tethering-to-raspberry-pi-or-beaglebone-black.md)
- [Trellis Python Library](https://learn.adafruit.com/trellis-python-library.md)
- [Adafruit WebIDE](https://learn.adafruit.com/webide.md)
- [Measuring Temperature with a BeagleBone Black](https://learn.adafruit.com/measuring-temperature-with-a-beaglebone-black.md)
- [Bone Box](https://learn.adafruit.com/bone-box.md)
- [User-space SPI TFT Python Library - ILI9341](https://learn.adafruit.com/user-space-spi-tft-python-library-ili9341-2-8.md)
- [Character LCD with Raspberry Pi or BeagleBone Black](https://learn.adafruit.com/character-lcd-with-raspberry-pi-or-beaglebone-black.md)
- [BeagleBone Black: Installing Operating Systems](https://learn.adafruit.com/beaglebone-black-installing-operating-systems.md)
- [Connecting a Push Button to BeagleBone Black](https://learn.adafruit.com/connecting-a-push-button-to-beaglebone-black.md)
