ROBOTICS: pulse width modulation..

01 - What is PWM?

Submitted by Webbot on November 30, 2008 - 1:08pm.

PWM stands for Pulse Width Modulation. This means that we can generate a pulse whose width (ie duration) can be altered.

The digital world

Since microcontrollers live in a digital world then their output pins can be either low (0v) or high (5v). However: the rest of the world tends not to speak such an open-or-shut case ie the rest of the world tends to be analogue. Rather than just being on or off: motors tend to need speed control, lighting may need to be dimmed, servos need to move to a particular position, buzzers need a sound frequency etc.

AVR microcontrollers have Analogue To Digitals Convertors (ADC) to convert a voltage from the analogue world to a number but do not have Digital to Analogue Convertors (DAC) to convert digital numbers back into variable voltages.

PWM is the closest solution.

By turning an output pin repeatedly high and low very quickly then the result is an average of the amount of time the output is high. If it is always low the result is 0v, always high then the result is 5v, if half-and-half then the result is 2.5v.

Why does this work? Well most real world devices have some kind of latency (ie they don't do what you ask immediately). This could be caused by a mixture of momentum, inductance, capacitance, friction (amongst others).

For example: if you connect a motor to a battery then it will, eventually, rotate at full speed. Disconnect the battery and the motor will take a little while to slow down until it stops. Equally if the motor is only connected to the battery for a very short time before being disconnected then it wont have enough time to get up to full speed. So if we repeatedly connected and disconnected the battery then the motor would start turning, then slow down, start turning, slow down etc. Obviously if we only did this a few times a second then it would be kind of jerky - but if we did it fast enough then we could control the speed of the motor dependent on the percentage of time the battery was connected versus not connected.

Similarly - if we wanted to dim lights or LEDs then they take a little while to get up to 'full glow' and, once disconnected from the power, the glow fades away. So we could create a dimmer by varying the amount of time on or off.

Servos are another example. They tend to expect a pulse every 20ms - depending on the width of the pusle they move to a given location.

How do we create a PWM signal

Before we discuss the intricacies of how we program a microcontroller then let's consider some basics to get a general idea of what we want to achieve.

Microcontrollers are very good with whole (integer) numbers. So assuming we have two numbers: one called BOTTOM and a higher number called TOP. By making the microcontroller start at BOTTOM, and then count upwards until it reaches TOP, and then repeat the process - if we were to then plot the resulting numbers on a graph then we end up with what is called a Sawtooth waveform that looks like this.

Of course you can never output this signal from your controller as it can only cope with on or off and not all these numbers - it just shows how the number starts at BOTTOM, counts up to TOP, and then starts all over again.

So the next step is to add a 'comparator' which is used to decide whether our output pin should be high or low. This comparator is yet another number which is somewhere in the range between BOTTOM and TOP. If the current Sawtooth number is less than the comparator value then the output will be low, otherwise the output will be high.

If the value of the comparator was equal to BOTTOM then the Sawtooth value could never be lower than bottom so our output pin would always be high. Equally if the comparator value was equal to TOP then the output pin would always be low. However: if the comparator value was the mid-value between BOTTOM and TOP then the output pin would spend 50% of its time being low and the other 50% being high. By varying the comparator value we can change the 'high' time anywhere between 0% and 100% of the time.

Looking back at the previous diagram we can see that the sawtooth waveform is 6 units high and repeats every 3 units across. So if we were to set our comparator to be 2 units above the BOTTOM value then what would happen?
The sawtooth waveform would spend 1/3 of the time below this value and the remaining 2/3 of the time above value. So our digital output pin would be a square wave that is low for 1/3 of the time and high for 2/3 of the time.


In the above example the sawtooth waveform repeated every 3 units. Assuming that each unit was 1ms then our waveform repeats every 3ms.

Given that Frequency = 1 / Time

then the signal frequency is 1/0.003, or 333.33 Hz. Note that with PWM this frequency remains constant - we just use the comparator value to adjust the duty cycle.

Duty Cycle

The percentage of time that our output pin is high is called the duty time. In the example above it is high for 2/3 of the time ie a 66.66% duty cycle.

stay connected for more on PWM.. and avr microcontrolers

Read Users' Comments (2)

keep swine flu away: tulsi the indian herb

Tulsi can help keep swine flu away: Ayurvedic experts

Lucknow, May 27: Wonder herb Tulsi can not only keep the dreaded swine flu at bay but also help in fast recovery of an afflicted person, Ayurvedic practitioners claim.

"The anti-flu property of Tulsi has been discovered by medical experts across the world quite recently. Tulsi improves the body's overall defence mechanism including its ability to fight viral diseases. It was successfully used in combating Japanese Encephalitis and the same theory applies to swine flu," Dr U K Tiwari, a herbal medicine practitioner says.

Apart from acting as a preventive medicine in case of swine flu, Tulsi can help the patient recover faster.

"Even when a person has already contracted swine flu, Tulsi can help in speeding up the recovery process and also help in strengthening the immune system of the body," he claims.

Dr Bhupesh Patel, a lecturer at Gujarat Ayurved University, Jamnagar is also of the view that Tulsi can play an important role in controlling swine flu.

"Tulsi can control swine flu and it should be taken in fresh form. Juice or paste of at least 20-25 medium sized leaves should be consumed twice a day on an empty stomach."

This increases the resistance of the body and, thereby, reduces the chances of inviting swine flu," believes Patel.

source : trusted

Read Users' Comments (0)

Bhuvan: India's answer to Google Earth

Bangalore: The Indian Space Research Organization (ISRO) has launched Bhuvan, a mapping application website like Google Earth, which promises to give better 3D satellite images of India and provides India specific features. The scientific community of the country also remembered the father of the Indian space program, Dr. Vikram Sarabhai on his birth anniversary.

Bhuvan, which means earth in Sanskrit, allows users to see any part of the subcontinent barring sensitive locations such as military and nuclear installations. The 3D mapping tool uses images taken a year ago by ISRO's seven remote sensing satellites, including Cartosat-1 and Cartosat-2. The satellites can even capture the images of objects as small as a car on a road.

Bhuvan displays satellite images of varying resolution of India's surface, allowing users to visually see things like cities and important places of interest looking perpendicularly down or at an oblique angle, with different perspectives and can navigate through 3D viewing environment. The degree of resolution showcased is based on the points of interest and popularity, but most of the Indian terrain is covered up to at least six meters of resolution with the least spatial resolution being 55 meters from Advanced Wide Field Sensor (AWifs). Bhuvan maps up to 10 meters compared to 200 meters of Google and 50 meters of Wikipedia.

Hyderabad based National Remote Sensing Agency (NRSA), which is a part of ISRO, had a lead role in designing and developing 'Bhuvan'.

"We were extremely enthusiastic and right from the word go our focus was that it should be useful to users in India," said V Jayaraman, Director, NRSA.

The features include: Access, explore and visualize 2D and 3D image data along with rich thematic information on Soil, wasteland, water resources, superpose administrative boundaries of choice on images as required, visualization of AWS ( Automatic Weather Stations) data/information in a graphic view and use tabular weather data of user choice, Heads-Up Display ( HUD) naviation controls ( Tilt slider, north indicator, opacity, compass ring, zoom slider), navigation using the 3D view pop-up menu (Fly-in, Fly out, jump in, jump around, view point), drawing 2D objects (Text labels, polylines, polygons, rectangles, 2D arrows, circles, ellipse), drawing 3D Objects (placing of expressive 3D models, 3D polygons, boxes), snapshot creation (copies the 3D view to a floating window and allows to save to a external file)

Advanced functionalities which will be provided in the future are urban design tools, contour map and terrain profile.

Madhavan Nair, ISRO Chairman informed that the space agency had started the preparations for a mission to Mars within the next six years. It was looking at launch opportunities between 2013 and 2015.

Read Users' Comments (1)comments