4

I connected an RGB LED(common anode) via the GPIO, and tried to get it to light up, but for some reason when I run the command, the LED does not light up. Here's the code I'm using

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)

GPIO.setup(11, GPIO.OUT) GPIO.output(11,1) GPIO.setup(13, GPIO.OUT) GPIO.output(13,1) GPIO.setup(15, GPIO.OUT) GPIO.output(15,1)

try: while(True): request = raw_input("RGB:") if(len(request) == 3): GPIO.output(11, int(request[0])) GPIO.output(13, int(request[1])) GPIO.output(15, int(request[2])) except KeyboardInterrupt: GPIO.cleanup()

And this is what my setup looks like enter image description here enter image description here

Any ideas what I'm doing wrong? It's my first week working with my Raspberry Pi.

Idris
  • 43
  • 1
  • 3

1 Answers1

3

A common anode LED should have the common leg connected to 3.3 volts and the others connected to 3 seperate GPIO pins. Connect the common pin to 3.3v, then wire the other three legs of the LED to the desired GPIO pins. In your code pull these pins low to light the individual colors. The resistors should go between the LED color pins and the GPIO pins.

Here is a quick check connect the common pin and one color pin with a resistor and wire the common pin to 3.3v and the other to the Pi's ground pin. If you have the polarity right it should light one color. if it does not light switch the ground and 3.3v wires.

Once you are sure the polarity is correct connect your chosen GPIO pins to the LED's other three legs (non common), including current limiting resistors.

The voltage and specific resistor values don't apply assuming you are using the 3.3v pin to power your LED, but the image below should make clear the difference between common anode and cathode, and how to wire them. Note that the picture shows the three color legs connected to a single pin, but you need to use three pins to control all three colors.

enter image description here

Steve Robillard
  • 34,988
  • 18
  • 106
  • 110