0

I'm setting up 3 LED lights to turn on automatically in a specific order using a Raspberry Pi 3. I coded it with Python. At first I had two and it worked. Then, I added a third with the same program and it broke. Here is my code:

import RPi.GPIO as GPIO             ## Import GPIO Library
import time                         ## Import 'time' library (for 'sleep')

yellow = 17
green = 18
red = 22
## These are our LEDs
ourdelay = 0.1                      ## Delay
# pins 4,17,18,21,22,23,24,25

GPIO.setmode(GPIO.BCM)            ## Use BOARD pin numbering
GPIO.setup(yellow, GPIO.OUT)        ## set output
GPIO.setup(green, GPIO.OUT)
## function to save code

for num in range(1,100):
  print(num)
  GPIO.output(red, GPIO.HIGH )
  GPIO.output(yellow, GPIO.LOW)
  GPIO.output(green, GPIO.LOW)
  ## set HIGH (LED ON)
  time.sleep(ourdelay)                ## wait
  GPIO.output(red, GPIO.LOW)
  GPIO.output(yellow, GPIO.HIGH)
  GPIO.output(green, GPIO.LOW)
  time.sleep(ourdelay)                ## wait
  GPIO.output(red, GPIO.LOW)
  GPIO.output(yellow, GPIO.LOW)
  GPIO.output(green, GPIO.HIGH)
  time.sleep(ourdelay)        ## wait


  ##activateLED(blue,ourdelay)

GPIO.cleanup()                      ## close down library

When I run it, it says this:

Traceback (most recent call last):
  File "/home/pi/blink.py", line 19, in <module>
    GPIO.output(red, GPIO.HIGH )
RuntimeError: The GPIO channel has not been set up as an OUTPUT

I would put a picture of my breadboard but I don't know how

  • to add the picture edit your question and in the editor toolbar there is an icon of a picture click it and follow the prompts. – Steve Robillard Mar 13 '16 at 20:55
  • Thanks by the way I just don't know how to upload a picture to here. I had already seen the picture icon. – Sergey Barskiy Mar 13 '16 at 21:01
  • 1
    The screen prompts you for what to do. – Steve Robillard Mar 13 '16 at 21:02
  • 2
    Not an issue wrt the question but something to consider: per PEP0008 Use 4 spaces per indentation level. – Ghanima Mar 13 '16 at 21:07
  • 2
    Isn't the error pretty obvious? "The GPIO channel has not been set up as an OUTPUT" - and if you check, you set yellow and green to output mode, but not red! – immibis Mar 13 '16 at 23:56
8

You need to set the pin as an output before you use it.

To do that add the following line:

GPIO.setup(red, GPIO.OUT)

below the matching lines for green and yellow:

GPIO.setup(yellow, GPIO.OUT)        ## set output
GPIO.setup(green, GPIO.OUT)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.