Tutorial beginner 4:

Analog Input and Analog Output

Content of the tutorial:

- Analog output: PWM.

- How to configure a PWM.

- Analog input.

- Build the hardware.


Dear User, this tutorial will present two project examples. The first is very similar to the previous tutorial example. The only difference is in the Led that is connected to a PWM pin instead to a simple digital pin. The second example shows how to use an analog input pin to read the status of a temperature sensor.


Analog output: PWM

PWM means Pulse Width Modulation.

Despite to the complex teory that exists behind the word PWM, for our purpose we will semplify a lot this concept.

Basically, when we configure an Arduino pin as PWM, we are going to use it as it was an Analog Output pin, in the sense that its behavior is very close to the behavior of an Analog pin. If you want to understand more about the PWM theory, there are lots of resources in the web. It is out of the scope of this tutorial to go deeply in this analisys.

On Arduino there are some digital pins that can be used as PWM. On Arduino Uno for instance the pins: 3, 5, 6, 9, 10, and 11 can do that. You can recognize these pins on the board, having a '~' attached to the pin numbers:

Please refer to the Arduino official website to know about other Arduino models.


How to configure a PWM

If we look at the project, we see that it is very similar to the "Switch on a Led" project, the previous one. The only visible difference is that the digital Led (that means a led connected to a digital pin) has been replaced with an analogic Led (that means a led connected to a PWM pin):

Actually, looking deeply at the project, also the Timer is configured in a different mode:

- mode on = fade;
- mode = pulse;
- mode off = fade;

- time on = 3s;
- time = 3s;
- time off = 3s;

The result of this configuration is shown in the following picture:

When the button is pushed, the Led increases its luminosity for three seconds, then holds it for other three seconds and then, at last, decreases again for three further seconds.


Analog input

Depending on the model, Arduino provides a set of pins that are internally connected to A/D converters. So they can used as Analog Inputs to read the status of an analog sensor, in our case a NTC temperature sensor.

The example project that we are going to describe here is the last of the beginner examples. It shows how to implement a thermostat, that is a systems that switched off a device (i.e. a heater) when the temperature becomes higher than a specific value, and switches it on when the the temperature goes down again.

As usual it is a very simple example. Even if it shows how to use an analog temperature sensor to switch on or off a device, you can use any other analog sensors you like, i.e. liquid level, light, humidity sensors etc.

In our example we have a temperature sensor that is connected to the Analog Pin A2. The status of the sensor is read by the 'xSwitch' software component and a Led (that simulates the heater) will switches on or off depending on the read temperature.

Also in this example we need to make some configurations to allow the thermostat to work fine. In this case the actual configuration is in the xSwitch software component. In fact the temperature sensor is a hardware component that works as it is: it provides a voltage that depends on the detected temperature.

So we need to configure the value of the desired temperature threshold in the xSwitch properties and the hysteresys amplitude. The hysteresys amplitude is a property we need to avoid the risk that minimum temperature variations around the threshold, will lead continuos switchings very close in time:

- switch level = 20;
- hysteresys = 0.5;
- negate = checked;

In this case the Led will be switched off when the temperature raise the value of: switch level + hysteresys = 20 + 0.5 = 20.5° Celsius and will be switched on when the temperature goes down under: switch level - hysteresys = 20 - 0.5 = 19.5° Celsius.

There is one more property we need to configure: the negate property that should be checked. It defines the switch out polarity. When checked the output switches off at sensor value rising, otherwise it switches on and vice-versa.


Build the hardware

The PWM 'bill of material' is listed below:

- 1 Arduino Uno, the controller.

- 1 Push Button.

- 1 Led.

- 1 330ohm resistor.

- 1 USB cable, to connect Arduino to the PC.

- 1 Small breadboard.

- Some pieces of unipolar cable.

Now we have added a Led to be connected to the PWM pin 9 and a resistor that is needed as current limitation to protect the Led. Here is the connction schematic:

As you can see, with a few changes on the previous circuit diagram, this new hardware implementation is ready to be tested.

For the Thermostat example, I will let you get the hardware connection schema through the button in the Devise Home tool page. Try it and will see how simple it is!

This is the last tutorial belonging to the beginners section. In next advanced tutorial we can see how to contact Arduino from an external web browser, so you can give it commands from a remote position instead of through a direct connected devices.

<< - Index - >>