Program to fade in and fade out LED

In Chapter 6, PWM – Writing Analog Information we saw that to control analog devices, BeagleBone uses a PWM subsystem. It generates analog values on some specific pins turning that pin HIGH and LOW very fast in patterns. Learning PWM involved learning about frequency, period, duty cycle and average voltage in Chapter 6. The resultant voltage is proportional to the duty cycle. If the duty cycle is increased, the resultant voltage will increase. We generated a range of resultant analog voltage on pin by changing the duty cycle. We did a LED fade in and fade out exercise using PWM. Let's do that program again in Python.

Connect an LED to P9_21 as shown in the diagram in Chapter 6. Type the following program in Cloud9, save it as LED_fade.py and run. You should be able to see the LED fading in and out for some time.

The following code is for LED_fade.py:

#!/usr/bin/python
import Adafruit_BBIO.PWM as PWM
from time import sleep

led = "P9_21"

PWM.start(led)

for loop in range(0,10):
    for i in range(0, 100):
        PWM.set_duty_cycle(led, i)
        sleep(0.01)
    for i in range(0, 100):
        PWM.set_duty_cycle(led, 100-i)
        sleep(0.01)

PWM.stop(led)
PWM.cleanup()

Explanation

This time we took a PWM namespace from the BBIO library. This namespace has variables and functions related to the PWM output. The function PWM.start() loads the am33_pwm device tree and changes sysfs files according to the parameter provided to this function. The prototype of this function is PWM.start(channel, duty=0.0, freq=2000, polarity=0). The parameter channel is the name of the pin on which you want to generate the PWM. Other parameters are optional. By default this function initializes the parameter duty 0, which means the duty cycle is zero and there will be no voltage generated on the pin. The frequency generated on the pin is 2,000 cycles per second by default. The parameter polarity is required to do advanced PWM configuration about rising or falling edge selection. Please refer to the Technical Reference Manual for more details on PWM.

Once the PWM is initialized on the pin, we can change the duty cycle and frequency run time. In order to fade in the LED, we used the function set_duty_cycle(). This function takes the name of the pin and the duty cycle value as parameters. Valid values of the duty cycle in the BBIO library are 0 to 100. We iterated from 0 to 100 and set that duty cycle after each millisecond. So we get the voltage on P9_21 incrementing each millisecond. As a result, we see the attached LED fading in. Then we set the duty cycle from 100 to 0 decrementing after each millisecond. This results with the LED fading out.

Unlike the GPIO and ADC, it is important to de-initialize PWM. The function stop() does that. If it is not called, BeagleBone keeps on generating PWM on that pin. This function unloads the device tree from that particular channel and closes related sysfs files. Finally, the cleanup() function disables all exported PWM channels.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.144.248