Through previous blogs (#1, #2, #3, #4, and #5), we looked at working with Genuino board, Arduino software IDE and range of hardware components. We also learnt how to interface external components – basic input and output devices, with Genuino board. An LED and a switch, both are example of digital output and input respectively. In this tutorial Pakronics shows how to focus on how to read data from a device that is generating continuous data instead of just two levels of (0V and 5V) signal.
As you may have noticed, LED and switch are an example of digital component – they either produce output signal or use input signal that are at discrete levels. A switch gives 0V or 5V when we operate open/close it. The LED turns on/off when is supplied (through digital pin of Genuino) a 5V or 0V signal to it.
An analog signal is continuous in nature – like 0-5V variation that could take any value between 0 – 5V (say 3.453V).
Since Genuino (and other microcontrollers, like many devices around us) work with digital signals encoded as 1’s (Logic HIGH or 5V signal) and 0’s (Logic LOW or 0V signal). So how an analog sensor interfacing with our Genuino microcontroller board?
Most of the physical phenomenon are analog data in nature – sound we make, temperature we feel and light we see, force, pressure… and the list goes on. Sensors used to measure such physical phenomenon also generate a continuously varying signal proportional to physical parameter being measured. A circuit is then used to convert these analog signals into digital and is known as Analog-to-Digital Converter (or ADC in short). These ADCs discretise the signal and assign a digital code to each of those discrete levels. Such ADCs are mostly integrated in modern day microcontrollers.
Most commonly an ADC is specified in terms of number of bits used to ‘encode’ the discrete voltage level. Total number of levels in an ‘n’ bit ADC is given by (2n). The Genuino board has 10-bit ADC and that means 0 – 5V (Vcc or supply voltage) is divided into 210 = 1024 levels. The ADC output or quantization level count starts with 0 and goes up to 1023 corresponding to 0V and 5V respectively.
|ADC Supply Voltage (Vcc)||ADC Input Voltage||ADC Output (Quantization Level Count)|
|5 V||0.0 V||0|
We will select the LED module (digital output device) and angle-sensor module (or potentiometer) from the grove kit. These modules are connected to the Genuino board using Grove Base Shield and when the sketch is executed, the LED flash rate (on and off – digital in nature) will be controlled by the potentiometer value (which is analog in nature).
So let’s take out the Genuino board, Grove Base Shield, connectors, LED module with an LED and angle-sensor module. Connect then as show below:
- You will have to put the Grove base shield on top of Arduino
- Connect the LED module to D3 of base shield using connector cable
- Insert the LED into the LED module – take a note of polarity! The shorter lead or one near the flat spot is the ‘cathode’ or ‘negative’ terminal of the LED.
- Connect the angle-sensor module to A0 of the base shield using connector
and, like always, we follow the 4 basic steps:
- Connect the Genuino board to computer using USB cable
- Open the Arduino IDE (the software)
- Open the sketch – BUTTON example code using the menu: File > Examples > 03.Analog > AnalogInput
- Ensure the BOARD and PORT settings are correct: Tools > Board & Tools > Port
In the sketch, you’ll have to modify the ‘LED Pin’ from 13 to 3, as we connected the LED module to D3.
As you would notice, the analog read value of potentiometer is read using “analogRead” command and this value is used as delay between on/off of LED. Thus by changing the potentiometer, we can dynamically control the LED flash rate.
Compile and upload the sketch, you should see LED flash rate changing with potentiometer.
The potentiometer can also be used as rotary angle sensor – the analog voltage is proportional to the rotation. Can you try making the LED on for clockwise rotation and OFF for anticlockwise rotation of the potentiometer from its mid position?
Hint: You will have to measure the voltage (output of analgRead command) when the potentiometer is at the mid position and compare it against the potentiometer value anytime for make LED on/off.