Arduino Tutorial #2 - Reading a Potentiometer (Analog input)
Introduction
In this Arduino tutorial we will discuss about analog Inputs. We are going to use a potentiometer as an analog input device to Arduino and you will learn how to use a potentiometer to control the blink speed of an LED (control the delay time).
If you are new to Arduino please go and read the first tutorial from here.
What is a Potentiometer ?
A potentiometer is also known as a variable resistor. It has three pins and when powered with 5V, the middle pin outputs a voltage between 0V and 5V, depend on the position of the knob.
A potentiometer is acting as a variable voltage divider circuit. The voltage is divided proportionate to the resistance between the middle pin and the ground pin.
Parts you will need.
- 1x Breadboard
- 1x Arduino Uno
- 1x LED
- 1x 330Ω Resistor
- 6x Jumper Wires
- 1x Potentiometer
Let's Assemble the parts on a breadboard
Connecting potentiometer,
Since we are using potentiometer as a voltage divider, first we have to connect out side pins to GND (ground) and VCC (power) and the middle pin works as a variable anlog input to arduino.
- Connect middle pin to Analog In pin 0 (A0) on the arduino.
- Connect one outside pin to VCC (5v).
- Connect other outside pin to GND (0V).
Connecting LED,
As you already know there is an on-board LED connected to pin 13. You can use that, but in this tutorial i am going to connect a separate LED to pin 13.
- Connect the positive side / Anode (long leg) to one side of the 330Ω resister and connect the other side of the resistor to Adruino pin 13.
- Connect the Negative side / Cathode to GND (ground).
Now let us write the code.
First we have to declare variables like we did in earlier tutorial. Here we will declare three integer variables. (integers are whole numbers and can be negative values also and can range from -32768 to 32767). and same time we will initialize them to specific pins.
int ledPin = 13; // set pin 13 for the LED
int val = 0; // variable to store the sensor value
One more thing if we are declaring variables outside of the function {out side of void setup() or viodloop()} we called them as "Global variables" and if we make within the function those are called "local variable" but they can only seeing within the function. Best practice is to declare within the function; but for this tutorial global variables are fine.
Then in setup() since we are using pin 13 for our LED we need to set pin 13 which we already declared as ledPin as a OUTPUT.
we' are not going to configure val as an input. Because the "analog in" pins have the ability to read varying voltages from sensors like the potentiometer. Since they' are always used as inputs so there is no need to configure them.
By using a built-in function called analogRead() the arduino can read external voltages on the analog pins. When the potentiometer is turned all the way down 0 volts going to the pin, and Arduino reads it as "0". When the it is turned fully to other side 5 volts going to the pin and it reads as "1023". In between, analogRead() returns a number between 0 and 1023 that is proportional to the amount of voltage being applied to the pin.
loop() function should be like below,
val = analogRead(potPin); // read the value from the sensor
digitalWrite(ledPin, HIGH); // turn the ledPin ON
delay(val); // stop the program for as per sensor input value
digitalWrite(ledPin, LOW); // turn the ledPin off
delay(val); // stop the program for as per sensor input value
No comments:
Post a Comment