Glow/Dim an LED using Rasp Pi

This is an introduction to PWM generation with Raspberry pi using Python.
We all love blinking LEDs. Setting the GPIO pins  high or low to lit or off LED seems so simple. But creating a dim/glow LED requires a little bit info about PWM generation.Simply PWM is all about varying the percentage of duty cycle of a square wave. Duty cycle means the diff between ON time and OFF time in one cycle of a wave. Duty cycle refers to the actual work done time in a single cycle, which is the ON time.
We can say a DC signal is '100% ON time wave or has 100% duty-cycle.
Paradox : DC signal has 0% OFF time, so we can't call it a wave ... :)

Okey... lets start.. Raspberry has particular numbers of GPIO pins which could be used for this experiment.
And we would use python to code with...So let us first write the program and discuss the logic...

Assuming you are working through SSH to conect to your Pi. And no GUI is available...Login to R pi.. and assuming home folder as working directory.

open a new file named glow.py using 'vi' editor
pi@raspberrypi ~$ vi glow.py
 This opens an empty file named glow.py in terminal.Copy the below python code to vi, save and quit.

import RPi.GPIO as pins import time pins.setmode(pins.BOARD) pins.setup(22,pins.OUT) led= pins.PWM(22,40) led.start(0) try: while True: for i in range(50): led.ChangeDutyCycle(i*2) time.sleep(0.02) for i in range(50): led.ChangeDutyCycle(100-i*2) time.sleep(0.02) except KeyboardInterrupt: pass led.stop() pins.cleanup()
 

using 'vi' editor is a breeze once you understand it.. starters will get crazy... i am sure...
Use INS key once and type to add content
Use DEL key to delete a letter

Some helpfull commands are
press 'Esc' and issue :wq to save & exit editor.
press 'Esc' and issue :q! to discard last changes & exit editor.
press 'Esc' and issue :g/^$/d to remove blank lines.

Now run our python code glow.py by issuing
pi@raspberrypi ~$ sudo python glow.py
 If you have everything else perfect then you should see LED conected to pin number 22 on P1 header of Rpi board glow and dim resembling a Sinewave light intensity variation.

Warning : Understand that python is an interpreted language so that code indentation has big importance. So if you run into such errors, then carefully recheck for unwanted tabs and spaces that is present in the code.. I ran into many ... :)

So lets understand the python code.. line by line
import RPi.GPIO as pins
this is the part where the python imports the raspberry gpio pins as a module to be used for the program. This is somewhat similar to the #include <> in C.
Here RPi.GPIO is the standard module name and pins is what user like to call it in his below code...

import time
it imports the time module to be used with sleep functions and etc:-

pins.setmode(pins.BOARD)
this sets the pins aka RPi.GPIO to be called with header pin number in your program. This means you can simply count the pin position where you connect the LED on the P1 header of Rasp Pi board, irrespective of the model A/B difference. Anther option is to use pins.setmode(pins.BCM). This means user needs to know the BCM IC pinout name for that particular position.

pins.setup(22,pins.OUT)
here we set the 22th pin of P1 header to be used as an output type. We used the word 22 because we already told to setmode(pins.BOARD). See the previos command. Actual name of this pin is "GPIO 25" in model B which i use.

led= pins.PWM(22,40)
we create a name for the pin 'led' and tell it is needed as a PWM pin. 40 refers to the base PWM frequency we like to use (in Hz).

led.start(0)
start the LED output with 0 duty cycle set.
  
try: while True: for i in range(50): led.ChangeDutyCycle(i*2) time.sleep(0.02) for i in range(50): led.ChangeDutyCycle(100-i*2) time.sleep(0.02) except KeyboardInterrupt: pass
This try-except block is the logic part. It runs infinitely until a Keyboard Interrupt occurs.

Two for loop iterations, one for glowing from 0% to 100% duty cycle at 50 steps each with a 2% increase in pulse width of 40Hz square wave. The other for loop to dim from 100% to 0% duty cycle at 50 steps each with a 2% decrease in pulse width of 40Hz square wave. Together they produce the effect.

pins.cleanup()
releases the GPIO pins on program exit...

Connecting LEDs
I have used a green LED from an old PC cabinet. It is recommended to use a 270 ohm resistor in series with LED for current limiting. But i was so lazy to solder it out  ... :)


Last Word:  it is always nice to experiment and see what happens.. So i recommend you to change frequency of PWM (40 Hz here). sleep time b/w iterations (0.02) , PWM duty steps (2% each for 50 iterations) etc... etc.. and keep the excitement..









Comments

Popular posts from this blog

Bidirectional 3.3v 5v Level Shifter

DIY Fixing a Laptop Battery

USB-UART console using old nokia DKU-5 datacable. (3.3v TTL)