This is the 11th lesson of Arduino tutorial. In this post, we are going to talk about Arduino potentiometer controlling and map function. Let’s get started.
First of all, we have to make the circuit. Circuit diagram
is below. If you don’t know how this potentiometer is working, click the button
below. You can have a knowledge about how this potentiometer is working.
We connected out potentiometer to Arduino’s 2nd
pin and connected one LED to 8th pin. LED is for getting output. Potentiometer
connected to VCC and GND for getting suitable value. Code is below. You can press
the start simulation button and see how this thing is going to work. Copy the
code by going to code section.
In this code, we created integer named value for storing potentiometer
value and also making it suitable value for LED brightness adjust. In the void
setup, we have initialized 8th pin as output and A0 pin as input.
You have learned about analog pins before. Analog pins are used to get analog
values. So, that why we have to put potentiometer on analog pins. Otherwise it
doesn’t work. Inside the void loop, I assigned the value of analog readings in
A0 pin. Analog pins are come with a range as 0 to 1023. Its minimum value is 0
and maximum is 0. You know digitalWrite can only accept values between 0 and
255. So, we have to make that 0-1023 to 0-255. That’s why we are going to put
the map function. This is the code part of map function. “value = map(value, 0,
1023, 0, 255);”. We reassigned the variable named value to map value. In the map
function, first we put out main value. The value which we need to convert to
another value. Then we put the range of that value. At least we put the range
of value what we want to convert. It’s very simple math function. You can use
the map functions at any place where it needed. You don’t need to use calculators
and water your time by using this function. Then we make an analogwrite in the
8th pin. Now you are wondering how analog values could be assigned
to LED. It can be done by digital pin but can’t get analog readings. You can’t
put digitalWrite in here because it’s an analog value. Then at least we put 10
milliseconds delay to make more smoother actions.
I hope you have learnt a lot in this post. Try out this at
your home or by pressing start simulation button. If you have any doubt about Arduino
or something, please comment below. Our team is here to help you. You can contact
with us using our official email or telegram account. Reddit and Discord replies
maybe late to reply. But we are available at telegram and comment box. You can
discus your problems in the forum. Good bye and stay connected.