Arduino Workshop - Inputs/Outputs & Sensors
Section outline
-
-
-
Required Resistor: 270 ohm (Red, Violet, Brown)
-
Analog Input
As you learned in the previous section, Arduino is able to detect whether there is a voltage applied to one of its pins and report it through the digitalRead() function. This kind of either/or response is fine in a lot of applications, but the light sensor that we just used is able to tell us not just whether there is light, but also how much light there is. This is the difference between an on/off sensor (which tells us whether something is there) and an analogue sensor, whose value continuously changes. In order to read this type of sensor, we need a different type of pin.
In the lower-right part of the Arduino board, you’ll see six pins marked “Analog In”; these are special pins that can tell us not only whether there is a voltage applied to them, but if so, also its value. By using the analogRead() function, we can read the voltage applied to one of the pins.
This function returns a number between 0 and 1023, which represents voltages between 0 and 5 volts. For example, if there is a voltage of 2.5 V applied to pin number 0, analogRead(0) returns 512.
If you now build the circuit that you see in figure, using a 10k resistor, and run the code listed in Example 6, you’ll see the onboard LED (you could also insert your own LED into pins 13 and GND as shown in “Blinking an LED” example, blinking at a rate that’s dependent upon the amount of light that hits the sensor.
Required Resistor: 10k
-
In some cases we may need to capture the data from an analogue sensor to determine its value, this can been used with other statements to provide different triggered events.
-