Computers can’t talk analog, so somebody invented PWM (Pulse width modulation)

Computers are not able to output analog signal, or at least they can’t while talking through a serial port and a serial communication. At the same time they cannot read analog signals too. This is the reason why modems exist and what they do is to transform analog signal to digital and viceversa.

How do they do this? Trough the use of Pulse-width modulation, which is a method to codify the information with the use of the duration of a signal impulse.
Take an Arduino Board, for example. The only signals that the board can receive are the “on” and “off” instruction for one of its pin. If you instruct the board to switch one pin “on”, that pin will output the maximum voltage permitted, which is 5 Volts approximately. If the instruction received is to switch the pin off, the output voltage would obviously be zero.

So how do you make the board output a value of 3.5 Volts?

If the computer switches the pin on and off with a certain velocity, and you read the output values with a tester, you will see that magically, as faster as the switching becomes, the output voltage will increase from zero to five Volts gradually! So we are in some way faking an analog voltage, and with the tester readings, I must say that it can get very accurate.

Below you will find a representation of what I have tried to explain:

Pulse width modulation image

Image from Wikipedia

The image shows source signals and the corrispective PWM signals. But to understand in an easier way, invert the grafic. With Arduino, you can decide how long the single signal should stay in an “on” status. The longer it stays “on”, the higher is the voltage read by the tester. This is a method to remedy at the problem of not having intermediate steps.

As I said before, you can control the Arduino Board by telling, time after time, how long should the impulse stay on the “on” status. Arduino has a built in function that does set the leght of time that a signal should be on, to fake the desidered voltage. This function is called “analogWrite()”. You can find more information at this address: Arduino analogWrite
Here is the code (to be loaded into the Arduino Board):

As we did before, we set up first which pin should output the desidered current. Be carefull as PWM outputs are only avalaible from pin 11 to 13 into the Arduino board.

int ledPin = 11; // TESTER connected to digital pin 11

We declare that we want the pin as an output

void setup()
{
pinMode(ledPin, OUTPUT); // sets the pin as output
}

We set the output current. The first value of the funcion is the output pin, the second is the output Voltage. A value of 255 will give you an output of 5 Volts approximately, while 0 will give you 0 Volts output.

void loop()
{analogWrite(ledPin, 255); // analogWrite values from 0 to 255
}

No Comments

No comments yet.

Comments RSS

Sorry, the comment form is closed at this time.