Edison, http://schemas.google.com/blogger/2008/kind#post, Intel Edison, Intel Galileo, IoT Linux image, mraa, python, Tutorial, upm

Python on Intel Galileo/Edison – Part 3: PWM

In this blog post, we are going to look into the process of using mraa methods for controlling the PWM module available on the Galileo/Edison.

What is PWM?
 PWM stands for pulse width modulation. As the name suggests, there is a “pulse” and with “width”, we mess(“modulation”). The idea here is to change the width of the pulse, resulting in another pulse that meets our needs.

Why do we need it? 
With digital systems like Arduino, Galileo/Edison, most often, we are dealing with two discrete voltage levels, 0 (GND, logical low) or 1 (logical high, represented by 5 V, 3.3V or 1.8V). PWM is one of the means of achieving (keep in mind the raw output is still digital. The pulse that is being generated still toggles between logical low and logical high) voltages between the two designated logical voltage levels. You could use PWMs to generate signals for driving things like speaker, buzzers etc.. PWMs are also used to control servo motors.

How does it work?
The basic principle behind PWM is that we turn on and off the voltage so fast across a device such that, due to the physical limitations of the device across which the PWM is applied, perceives the voltage as something between logic 0 (GND) and logic high (Vcc).

How do we make it work?
There are two parameters in the pulse that can be controlled to achieve the desired effect, period and duty cycle.
Period is the time interval after which a wave or signal would repeat itself. In a pure digital system, we can generate only two(binary) voltage levels, either logical 0 or logical 1. This kind of signal is called square wave.

Square wave

So going by the definition of period, the following shows how period is measured (the time interval after which pattern repeats)

The other thing that can be controlled is the duty cycle. Duty cycle defines the amount of time the signal stays “on”. It is measured in terms of percentage. In the above images, the duty cycle is said to be 50% because the signal stays on for half the time in any given period. The key here is, if you increase the duty cycle, the output voltage perceived would be somewhere nearer to logical high (say 5V). Conversely, decreasing the duty cycle results the perceived voltage to tend to 0. Hence 100% duty cycle would mean that the signal stays on all the time, which means the output voltage is Vcc and 0% duty cycle means that the output voltage is 0V.

Using mraa to control the PWM on Galileo/Edison:
mraa provides methods “period_us” to control the period and “write” to control the duty cycle.

Hardware connections:
In the script below, we are going to control the intensity of LED connected at D5

The script:
The intensity with which LED glows depends on the amount of voltage applied across the LED.
In the script, using PWM, the intensity with which the LED glows is increased to maximum and once it reaches that point, the intensity is reduced until the LED turns off. This cycle keeps repeating within an infinite loop. 


In the above script, instance for the selected PWM pin, pin 5 is created using

pwm = mraa.Pwm(PWM_PIN)

Then set the period using

pwm.period_us(5000)

Once that is done, start outputting the pulse train at the selected pin :

pwm.enable(True)

and finally, the duty cycle is controlled using the write() api:

pwm.write(value)

Other parts:
part0: Getting started on Galileo/Edison
part1: GPIO output
part2: GPIO input(button)
part3: pwm
part4: adc
part5: Temperature sensor
part6: Light sensor

Advertisements

6 thoughts on “Python on Intel Galileo/Edison – Part 3: PWM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s