While we read about digital inputs in the last post, we are going to talk about Analog input.
Arduino is a digital tool but can still interpret data from the surrounding using analog sensors. These analog values from the sensors are converted by the Arduino using the built in Analog to Digital Convertor (ADC).
Arduino Uno A0 – A5 Pins can read the converted values in the range of 0 – 1023, which are mapped between the values from 0 – 5 Volts.
Simplified – Analog Values are basically anything that changes in nature with respect to time. For example your age, it keeps on changing every year. Another example is the size of your finger nails – keeps on growing. So basically these can be understood as analog in nature.
We will write a code to read analog values with Arduino. So lets gather the components we need.
Things Needed
- 10k Variable Potentiometer
- Breadboard
- Jumper Cables
- Arduino Uno
What is a Potentiometer?
A Potentiometer is a 3 terminal device with a rotating or sliding contact that increases or decreases the output resistance.
This device is used in many appliances. For example to control the speed of your Fan to adjusting the volume of your stereo.
Circuit Diagram
Code
/*
Reading Analog Voltage
A0 pin converts it to voltage and its ranges from 0-1023 in the mapped form
Graphical representation is available using serial plotter (Tools > Serial Plotter menu)
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
*/
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage);
if (voltage>3.0){
digitalWrite(13,HIGH);
}
}
In this project you will observe that the voltage reading changes as we rotate the Knob on the potentiometer in the range 0-5V. This will be included later in higher projects.
One Reply on “Reading Analog Input with Arduino”