1

So, I was wondering if I could light up an led a little bit, so the led wasn't neither off, or completely on.
The only way I found on Google to do that was with pigpio, so I installed the program on my Pi 3 and ran it by using sudo pigpiod, then I decided to write a code to see if it worked, but with no success, here is the code:

import pigpio

pi = pigpio.pi()

try:
    while(True):

        red = raw_input("Red. 1-255.-->")
        redCol = int(red)
        if (redCol >= 1 and  redCol <= 255):
            pi.set_PWM_dutycycle(11,redCol)
        else:
            print("Invalid Value")

        green = raw_input("Green. 1-255.-->")
        greenCol = int(green)
        if (greenCol >= 1 and greenCol <=255):
            pi.set_PWM_dutycycle(13,greenCol)
        else:
            print("Invalid Value")

        blue = raw_input("Blue. 1-255.-->")
        blueCol = int(blue)
        if (blueCol >= 1 and blueCol <= 255):
            pi.set_PWM_dutycycle(15,blueCol)
        else:
            print("Invalid Value")

except KeyboardInterrupt:
        pi.stop()

When I run the code It prompts me to the color 'selection' input, but when I input something the led does nothing.

2 Answers 2

2

The code works as expected.

$ python script.py
Red. 1-255.-->40
Green. 1-255.-->80
Blue. 1-255.-->200
Red. 1-255.-->

produces the proper result.

enter image description here The most likely explanation is you don't have your LEDs connected to (Broadcom) numbered GPIO 11/13/15 or they are incorrectly connected.

2
  • Ok, I fixed it, I had selected the wrong pins, by their number, not their corresponding name. But now I have another question, when i Interrupt the program, the led is still on, how can i fix this? @joan
    – Iliyan
    Commented Jul 1, 2017 at 10:52
  • @Iliyan The (pigpio) daemon will still be transmitting PWM at the last settings. You need to switch off PWM as part of your tidy up code (as you seem to have done in your answer).
    – joan
    Commented Jul 1, 2017 at 11:19
0

The error was that I had set the pins to their corresponding pin number, not pin name, which meant that pin 11 was pin 17, pin 13 was pin 27 and pin 15 was pin 22.
Here is the final code:

import pigpio

pi = pigpio.pi()

#Set pins
RED_PIN = 17
GREEN_PIN = 27
BLUE_PIN = 22

try:
    while(True):

        red = raw_input("Red. 1-255.-->")
        redCol = int(red)
        if (redCol >= 1 and  redCol <= 255):
            pi.set_PWM_dutycycle(RED_PIN,redCol)
        else:
            print("Invalid Value")

        green = raw_input("Green. 1-255.-->")
        greenCol = int(green)
        if (greenCol >= 1 and greenCol <=255):
            pi.set_PWM_dutycycle(GREEN_PIN,greenCol)
        else:
            print("Invalid Value")

        blue = raw_input("Blue. 1-255.-->")
        blueCol = int(blue)
        if (blueCol >= 1 and blueCol <= 255):
            pi.set_PWM_dutycycle(BLUE_PIN,blueCol)
        else:
            print("Invalid Value")

except KeyboardInterrupt:
        pi.set_PWM_dutycycle(RED_PIN,0)
        pi.set_PWM_dutycycle(GREEN_PIN,0)
        pi.set_PWM_dutycycle(BLUE_PIN,0)
        pi.stop()

Hope somebody else finds this helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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