2

Using this code, I can't use PWM on the pi. It's modified off of a Linux user article.

import time
import RPi.GPIO as GPIO
led = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(led, GPIO.OUT)
GPIO.output(led,GPIO.LOW)
time.sleep(0.5)
pwm = GPIO.PWM(led, 50)
time.sleep(0.5)
pwm.ChangeDutyCycle(25)
time.sleep(0.5)

The LED stays off, or if I change the line GPIO.output(led,GPIO.LOW) to GPIO.output(led,GPIO. HIGH) it stays on. I receive no error or warning messages.

EDIT: Here is the code I modified it off, when I run this (sudo python candle_light.py) it flickers as described.

#!/usr/bin/env python

# Import the modules used in the script
import random, time
import RPi.GPIO as GPIO

# Assign the hardware PWM pin and name it
led = 18

# Configure the GPIO to BCM and set it to output mode
GPIO.setmode(GPIO.BCM)
GPIO.setup(led, GPIO.OUT)

# Set PWM and create some constants we'll be using
pwm = GPIO.PWM(led, 100)
RUNNING = True
WIND = 9

def brightness():
   """Function to randomly set the brightness of the LED between 5 per cent and 100 per cent power"""
   return random.randint(5, 100)

def flicker():
   """Function to randomly set the regularity of the'flicker effect'"""
   return random.random() / WIND

print "Candle Light. Press CTRL + C to quit"

# The main program loop follows.
# Use 'try', 'except' and 'finally' to ensure the program
# quits cleanly when CTRL+C is pressed to stop it.

try:
   while RUNNING:
      # Start PWM with the LED off
      pwm.start(0)
      # Randomly change the brightness of the LED
      pwm.ChangeDutyCycle(brightness())
      # Randomly pause on a brightness to simulate flickering
      time.sleep(flicker())

# If CTRL+C is pressed the main loop is broken
except KeyboardInterrupt:
   running = False
   print "\nQuitting Candle Light"

# Actions under 'finally' will always be called, regardless of
# what stopped the program (be it an error or an interrupt)
finally:
   # Stop and cleanup to finish cleanly so the pins
   # are available to be used again
   pwm.stop()
   GPIO.cleanup()
  • It may be getting confused by the unneeded call to GPIO.output(). See if it works without. – joan Jul 13 '14 at 16:46
  • Thanks Joan, i tried that before and it didn't work either. – Luke Moll Jul 13 '14 at 16:48
  • It should work. Are you sure you are using the correct pin. gpio18 is pin P1-12. See elinux.org/… – joan Jul 13 '14 at 17:00
  • I'm sure I'm using the correct pin as when that line is GPIO.output(led,GPIO.HIGH) the LED lights up. On my Pi cobbler i think it's labeled with the BCM numbers as the sixth pin from the right on the bottom (P1-12) is labeled "#18" – Luke Moll Jul 13 '14 at 17:04
  • I'm not sure what else to suggest other than to make sure RPi.GPIO is up to date. – joan Jul 13 '14 at 17:07
2

I found the answer, simple mistake - I was missing a pwm.start(int) before using the pwm variable. The working code should look like this:

GPIO.setmode(GPIO.BCM)
GPIO.setup(led, GPIO.OUT)
GPIO.output(led,GPIO.LOW)
time.sleep(0.5)

pwm = GPIO.PWM(led, 50)
pwm.start(50)#d'oh!

time.sleep(0.5)
pwm.ChangeDutyCycle(25)
time.sleep(0.5)   
pwm.stop()
GPIO.cleanup()

This starts the LED at half brightness, waits half a second, reduces it to a quarter brightness, waits half a second, then cleans up and stops.

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.