bclose

10 – THE ALMOST ANALOG PINS

Objectives

 

 

    • Understand the differences between analog and digital
    • Get to know Arduino almost analog outputs.
    • Pulse Width Modulation (PWM)

 

Bill of materials

Arduino Uno Arduino Uno or equivalentWe can use any other Arduino board in this chapter.


We will also need a PC computer with the Arduino IDE properly installed and set up.

Breadboard  A solderless Breadboard.
Red LED A LED diode.
330 Ohm Resistor A 330 Ohm resistor
Jumper wires Some jumper wires

ANALOG AND DIGITAL

 

All types of signals we have used so far with our Arduino, either input or output, share a common feature: they are digital signals, meaning they can take a HIGH or LOW value but no intermediate values.

If we graphically represent the value of a digital signal over time we would see something like this:

A digital signal

 

In life many things are this way, you pass or fail an exam, you turn on or off the light, but many others are continuous and measurable variables can take any value you can imagine, as the angle of the clock or temperature, even within finite values they can take as many intermediate values as we can imagine.

These kind of variables are called analog and their representation, as opposed to digital, would be something like this:

An analog signal

 

 

It is not uncommon to control something in the outside world with an analog signal so that the system behavior follow that signal. We may, for example, want to change the brightness of a LED diode and not just turn it off or on.
In this chapter we will learn to send analog signals to the Arduino output pins.

ALMOST ANALOG OUTPUTS

 

So far we have seen how to use the Arduino output pins to turn on and off a LED diode, for example. But we have not seen how to change the brightness of this LED. To do this, we need to modify the output voltage of our Arduino output pins, or in other words we have to be able to provide an analog output value.

First we must make it clear that Arduino lacks pure analog outputs that can do this (with the notable exception of Arduino DUE).

But as Arduino guys are clever, they decided to do a trick, so that using a digital output we can simulate an almost analog output.

This trick is called PWM, that stands for Pulse Width Modulation. The basic idea is to use digital outputs that vary very quickly so that the effective value of the output signal is equivalent to a lower voltage analog signal.

And surprisingly the trick works!

Pulse Width Modulation

Pay attention to the width of the square pulse above. The wider, the more average voltage is applied between pins, and that in the outside world is equivalent to an analog voltage value between 0 and +5V. A fifty percent duty cycle is equivalent to the fifty percent of an analog signal of 5V, which is 2.5V. If we apply 5V, 75% of the time it will be the equivalent of the 75% of an analog signal of 5V, that is, 3.75 V. And so on.

To use an Arduino digital pin as analog output, we declare it in the setup() loop as if it were a digital pin:

pinMode(9, OUTPUT) ;

The difference comes when sending the signal to the pin:

digitalWrite(9, HIGH);  // Puts 5 Volts in the output
digitalWrite(9, LOW);   // Puts 0 Volts in the output
analogWrite(9, V) ;     // Puts V Volts in the output, ranging from 0 to 5 Volts in 256 steps

In this case we use the analogWrite() function, that applies in the output pin a value between 0 and 5V, depending on the voltage value, that must fall between 0 and 255.

This way if we connect a LED diode to one of these PWN outputs, we can modify its brightness simply by varying the value we write in the pin.

But there is a restriction. Not all Arduino digital pins accept PWM output values but those who have a tilde, ~, before the number. Pay attention to the pin numbering of the following picture:

Arduino UNO PWM pins

Only those who have the tilde, ~, support PWM

 
  • Only the pins 3, 5, 6, 9, 10 and 11 can be used to send PWM signals and simulate an analog value as an output. Note that they are labeled with a tilde, ~.
  • If you try to do this with a different pin, Arduino quietly accepts the command without showing any errors, but it casts the values from 0 to 127 to a LOW voltage value and to a HIGH voltage value for the rest, that is, from 128 to 255. After that Arduino goes on duty completely satisfied with its job.
 

MODIFYING THE BRIGHTNESS OF A LED DIODE

 

Let’s make the typical assembly with a resistor and a LED diode, similar to that in chapter 3, but this time making sure to use one of the digital pins that can provide PWM signals. In the following sketch we will use the pin 9.

We can write an sketch similar to this:

Sketch 10.1

 

void setup()                                               
{  
    pinMode( 9, OUTPUT) ;  
}
void loop()
{
    for ( int i= 0 ; i<255 ; i++)
    {
       analogWrite (9, i) ;
       delay( 10);
    }
}

The LED brightness increases to a maximum and starts again abruptly. We may change the sketch a little to make the transition less violent:

Sketch 10.2

 

void setup()                                     
{ 
    pinMode(9, OUTPUT) ;  
}

void loop()
{
    for ( int i= -255 ; i<255 ; i++)
    {
        analogWrite (9,abs(i)) ;
        delay(10);
    }
}

Here I make good use of a single loop (out of sheer laziness) to turn up and down the brightness of the LED. The abs(num) function, absolute, returns the absolute or unsigned value of a number, num, and so while i takes values from -255 to 255, abs(i) takes values from 255 to 0 and back up to 255. What you think of the trick?

Summary

 

 

    • We have described roughly the difference between digital and analog values.
    • We have seen how to simulate analog values in an Arduino digital output.
    • It does only work in the pins that accept PWM, that is, pins 3, 5, 6, 9, 10 and 11.
    • We can assign values ranging between 0 and 255 to these PWM output pins.

 

 

No Comments

Give a Reply