3

What is a good way to make an LED turn on and off slowly using a Raspberry Pi? Im trying to hook up an led to become a Tardis lamp, but since the Pi doesnt have ADC/DAC, Im kind of stuck. What are some generic ways to do it? I don't have a wide selection of components lying around, and would prefer not to buy anything, if possible.

The best I have found is this, which seems kind of excessive for a single LED.

1
  • Changing voltage on a LED doesn't really work. PWM is the way to go, like user 8693 said.
    – Gerben
    Commented Jul 27, 2013 at 12:54

3 Answers 3

9

You can do this with the PWM pin on the Pi. Here's a link to a blog post dealing with this that uses the C WiringPi library to do it:

http://lifebydesignuk.wordpress.com/2013/02/20/how-to-breathing-led-in-python-for-raspberrypi/

I would paste the code in here but the editor isn't co-operating.

1
  • @calccrypto When using PWM on the RPi, you may end up with weird results from time to time due to other processes utilizing CPU time. Depending on what else you are running on the RPi and your desired results, you may want to look into a hardware solution like Gerben suggested.
    – Butters
    Commented Jul 27, 2013 at 17:46
3

You could also achieve this effect with a simple 555-timer chip a capacitor and some resistors.

I myself plan on using the following circuit for my tardis project: http://www.youtube.com/watch?feature=player_detailpage&v=qLAi7hkDuYw&t=307

3

To achieve slow dimming or brightening of your LED you will require (as others have posted before) PWM.

Introduction to PWM

There are primarily two types of modulation software and hardware. There exist several libraries which can achieve a software enabled modulation whose performance lies in between software and hardware. You could read up more about the different types of modulation on this answer here.

Solution to your query (without having to buy or install anything)

The finer your application the more advanced the PWM you need to use, for your purpose (LEDs) the precision required is not too great and thus it can be achieved by simple GPIO modulation.

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)  # choose BCM or BOARD numbering schemes. I use BCM  
GPIO.setup(26, GPIO.OUT)# set GPIO 26 as output for led  
led= GPIO.PWM(26,100)   # create object led for PWM on port 25 at 100 Hertz  
led.start(0)            # start led on 0 percent duty cycle (off)  
pause_time = 0.02       # you can change this to slow down/speed up  

try:  
    while True:  
        for i in range(0,101):    # 101 because it stops when it finishes 100  
            led.ChangeDutyCycle(i)  
            sleep(pause_time)  
        for i in range(100,-1,-1):      # from 100 to zero in steps of -1  
            led.ChangeDutyCycle(i)  
            sleep(pause_time)  

except KeyboardInterrupt:  
    led.stop()            # stop the led PWM output  
    GPIO.cleanup()        # clean up GPIO on CTRL+C exit

This code will increase and then decrease the brightness. You can, of course, edit the code to your needs.

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.