Arduino #8 - Using Potentiometer with arduino | analogRead() function, variables and Serial function
Hello friend's, welcome to the another tutorial of How to Electro. In this tutorial, we will know, how to use potentiometer with arduino.
We will also know about the potentiometer (Detailed information of the potentiometer will be in the upcoming tutorials) along with analogRead() function, variables and Serial function. So friend's, let's begin.....
H.T.E. Highlighted :- Above is the video of the whole tutorial in hindi.
Potentiometer
Potentiometer is a type of resistor, which has a sliding or rotating contact which rotates over a resistive material, forms a adjustable voltage divider. We will know about the voltage divider in the upcoming tutorials.
Construction
There are 3 terminals coming from the potentiometer, in which middle terminal is connected to the slider or wiper, and is known as signal/wiper/slider/output pin, and the another two are connected to the both ends of the resistive material respectively.
And these all are arranged properly within a covered structure, forming a full potentiometer.
Working
It works as an adjustable voltage divider, when all the terminals of the potentiometer are connected in the circuit. The connection will be like, the signal pin of the potentiometer is connected to the output, and another two terminals is connected to +5V and 0V respectively. We can connect any terminal of the potentiometer excluding signal pin, to the +5V or 0V. When working as a voltage divider, its output voltage is a fraction of the input voltage. We will know about this in the voltage divider tutorial. We can read the various outputs from the signal pin by rotating the knob.
It also works as a variable resistor ( variable resistor means, that the resistance value changes due to an cause. e.g. LDR, potentiometer etc.) , when only two of its terminals are connected to the circuit, including signal pin and one of the end of the resistive material. We can use this property to change the brightness of light by rotating the knob. The connection of this wil be like, the signal pin is connected to +5V or output, and the another pin is connected to output or +5V respectively. Now, by rotating the knob, we can change the resistance of the potentiometer, and hence, the brightness of LED can also be controlled.
Uses
The potentiometer is available in various designs specific for various purposes.e.g. trimmer, variable resistor etc.
It is used to control volume, control brightness of light, control speed of fan, manually defining the value etc.
Connecting the Circuit
Connection
Working
The potentiometer works as a voltage divider here, as we had used all its terminals. The +5V is supplies to the potentiometer and it goes to the ground via a resistive material, as the another end is connected to the GND. When we rotate the knob, the resistance of the voltage divider changes, as we rotate it clockwise, the resistance increases, and decreases when rotated anti-clockwise. This change in resistance provides different output from the signal pin, which slides over the resistive material, and we use this output to convert it into integer value programatically.Code uploading and Reading value
int POTpin = A0;
int POTvalue = 0;
void setup(){
Serial.begin(9600);
}
void loop(){
POTvalue = analogRead(POTpin);
Serial.println(POTvalue);
delay(100);
}
In the above code, first two line of code is used for declaring two variables, one for declaring the input pin ( the pin to which signal pin of the potentiometer is connected ), and the another is used for storing the value from the potentiometer. Both of these stores integer type value, so these are assigned int type, and each of these has given unique names, so that we can use it anywhere, within the code.
void setup()
This function runs once, when the arduino is turned on or reset button is pressed, and stores all the initialization, that need to be run once, like pin mode setup, starting of serial communication etc. In the above code, it contains Serial.begin(9600) function which is used to start the serial communication, and it takes the baud rate of the communication as it's parameter for reading the values and sending it. The default baud rate for the serial monitor is 9600.
void loop()
This function runs consecutively, till the arduino is powered off. All the code or function, that needs to be consecutively used must be used inside this.
e.g. Turning light on or off, controlling the motor etc.
In the above code, it contains three lines of code, in which first is of reading the value of the potentiometer, second is to send it to the serial monitor, and third line sets the delay for 100 ms. Let's know about these in details.....
1.
POTvalue = analogRead(POTpin);
This line of code contains analogRead() function, which reads the value given at the analog pin ( the pin number is specified as it parameter, which it had to read ) and converts into an integer range of 0-1023 ( 0 for 0V and 1023 for +5V ). POTpin is specified as it's parameter, which is an integer type variable and stores the pin number A0.
The output value is then, assigned to the variable with name POTvalue. Now, we can use the name of this variable to read the value stored in it.
2.
Serial.println(POTvalue);
This line of code contains Serial.println(POTvalue) function, which is a serial type function, and it is used to send some value to the serial monitor, and displays to the user. It takes the value, we want to display to the user, as it's parameter.
POTvalue is specified as it's parameter, whose value has been changed by the previous line of code, and hence the value of the potentiometer is displayed to the user.
3.
delay(100);
This line of code contains delay(100) function, which is used to add delay to the arduino i.e. it stops the arduino for specified time in ms declared in it's parameter, before going to the next line of code.
Hence, in the above code, 100ms is used as the parameter, so, the arduino will wait for 100ms before re-running the loop.
We had now learnt, how to use potentiometer with arduino, and also know the way of reading the value of the potentiometer.
So friend's, that's all for this tutorial. Hope u enjoyed it.
Thanks
HowtoElectro ( Samridh Sharma )
Comments
Post a Comment