This tutorial will show you how to read data from the Arduino analog input. There are few types of analog I/O components out there, therefore it will be very useful once you have mastermind the Arduino analog input. Some example of the analog components are thermistor (temperature dependent resistor), photo-resistor (light dependent resistor, LDR) and the widely used potentiometer.
Potentiometer is a simple knob that provides a variable resistance. Therefor you can use the Arduino analog input to read its value. Click here to read more about potentiometer.
What you need for this tutorial?
- Any of the Arduino Board, I am using Arduino UNO here,
- 1 x Potentiometer,
- 1 x LED (any color based on your mood).
Connect your circuit as below (I am using Fritzing to draw my diagram, you can read my previous article about Fritzing here):
Left pin of the potentiometer connects to the 5v, right pin connects to the ground and the middle pin connects to any of the analog in (I am using analog in pin 0 in this example). Connect the long LED pin to the Digital pin 13 and short pin to the ground, you have to ensure that it is in the correct direction as LED is a type of the diode, it only allow current to flow in 1 direction.
Arduino Code:
int inputPin = A0; // set input pin for the potentiometer int inputValue = 0; // potentiometer input variable int ledPin = 13; // set output pin for the LED void setup() { // declare the ledPin as an OUTPUT: pinMode(ledPin, OUTPUT); } void loop() { // read the value from the potentiometer: inputValue = analogRead(inputPin); // turn on the LED: digitalWrite(ledPin, HIGH); // pause the program for milliseconds: delay(inputValue); // turn off the LED: digitalWrite(ledPin, LOW); // pause the program for for milliseconds: delay(inputValue); }
No mater how you turn the potentiometer, it will always give you the analog voltage value between 0 – 5v, and the Arduino analog input will convert it to the digital value of 0 to 1023 (total of 1024 levels). When you turn the potentiometer to the 0v end, the analogRead() function should return 0 and when you turn the potentiometer to the 5v end, the analogRead() function should return 1023. Then this value will be stored into the integer variable named inputValue and will be used by the delay() function for the LED blinking duration.
< BACK
Pingback: Potentiometer as an adjustable voltage divider in Arduino | Arduino Tutorials
Great web site. A lot of useful information here. I’m sending it to a few buddies ans additionally sharing in delicious. And obviously, thank you on your sweat!