Now that you understand how analog input pins work, let's actually make some tests with our newly installed
potentiometer so you can open here a new sketch on your Arduino ID.
And, well, first things first, as always, we are going to use define for the PIN number we use for
the potentiometer, and we are going to name it very simply, potentiometer.
You know, all the bouquets and then the number of the pin is a two, so you can write a two.
This is not a proper integer number, but this is something that is recognized by the Arduino.
This is in the library.
So you can use a zero, a one, a two until a five here for the Arduino UNO.
It will be recognized as the PIN number.
All right, then what do we need to do in the setup?
Well, usually what we did before is to use the PIN mode function to set up the mode so either input
or output for the pin.
But if you remember from the previous lesson or analog input pins, we don't need to do the pin mode
function because they are already analog input bits.
So the pin is already input, so the pin already has the input mode.
So nothing to do in the setup.
Just what I'm going to do is say that begin.
Okay, because we are going to print.
The value from the potentiometer on the style monitor so we can actually monitor it.
All right.
I know in the loop function, what we are going to do is simply to print the value from the potentiometer.
So still that.
Praying to them.
And then to read the value from an analog input pin, you will simply use the function analog.
Read with, of course, the pin number so we can replace with potentiometer pin here.
Now close to parenthesis and the semicolon.
And then so we don't print too fast, I'm going to add a delay of 100 milliseconds.
So we are going to print the value from the potentiometer at about ten hertz.
Now, as you can see, this is very, very similar to what we did with the push button.
With the push button, we simply printed the value from the button using digital read, but with the
potentiometer, because this is an analog input pin, we use analog rate.
So the name of the function here is very simple and analog read will give you a value between zero and
1023.
Let's check that.
So make sure now your board is correctly connected and let's upload code.
Let's name it.
Put on your mirror.
And now let's open the sale monitor to see what value we have.
And as you can see here, so we have the first value, which is 590.
This is just because the potentiometer here is at a certain position between the minimum and the maximum.
Now, I'm going to move it from one side here.
So I move it and you can see the value is decreasing.
I move it all the way up, all the way down to zero.
And though I can move it the other way all the way up to 1023.
So depending on your setup, the minimum and maximum value may be inverted from my setup.
But the important thing is that you can go from 0 to 1023 and back to zero, and then you can have any
intermediate value between them.
And one other nice thing you can see is with this soil plot, you will be able to plot the value from
0 to 1023.
You can make a nice plot, so you can better visualize what is the value.
All right.
This is nice to have, but not really needed for what we need to do in the following of this course
and also for the final project.
Great.
So now you can read a value from the potentiometer.