LESSON 10: Analog Reads on the Arduino

In today’s lesson we will learn how to use the analog pins on the arduino to read voltage values from a simple potentiometer circuit. The anolog pins are the pins marked A0 to A5 on the Arduino Uno. These are the pins that can be used for making Analog Voltage measurements.

The circuit we will be using today is a simple voltage divider using a potentiometer. Lesson 9 gives a description of how the potentiometer works.

Potentiometer
This simple circuit allows you to create a voltage divider with a potentiometer

 

52 thoughts on “LESSON 10: Analog Reads on the Arduino”

    1. Since it is a voltage divider, the magnitude of the resistance does not matter. Any potentiometer should work.

      1. this lesson was actually pretty FUN! i got my kit off of amazon and the company kit that i used was ‘kuman’ and at first, i didnt understand what it looked like. but its cool though.

  1. Please tell me where to use ‘INPUT’ and ‘OUTPUT’ in pinModes as I didn’t understand.

    Thanks again for all the help you are giving me .

    1. “Input” is used when your arduino is reading a voltage . . . live you want to read a value from a potentiometter. Output is used when you want to apply a voltage from the arduiono to a device like an LED. Input and output are from the perspective of the arduino.

  2. Hey,
    Thanks for the great videos.

    I don’t understand when the voltage scale is between 0-1023 and when between 0-255
    Can you explain when we use each case?

    1. These are not voltages, but are an arbitrary scale. When you read, you read a number between 0 and 1023. This corresponds to a real world voltage between 0 and 5. When you write a value, you write a value between 0 and 255. Again, an arbitrary scale that corresponds to a real voltage between 0 and 5 volts.

    2. You use 0-1023 when you are inputting because you want precision which uses more memory. You use 0-255 for Output because you want to conserve memory for processing. For example I am flying at 1012 miles per hour and anything at less than 1010 or more than 1013 and I will run out of fuel before I reach a landing spot. If my input were only accurate to 255 I would be told to eject or die and my 100 billion dollar plane isn’t covered by insurance. 1012 only needs 10 bits of storage space. Collecting that data, testing too see if it is within my safety parameters and then letting me know by turning on either a red or a green LED requires more memory.
      All of the remaining memory I am using to fly the plane and play a world record breaking game of pong.

  3. Hi,
    Thank you very much for the great series, I’m enjoying it.
    I have one question please, when you first started reading values from the A0 pin you got some voltage, how come you have a voltage knowing that you didn’t write any code to make the Arduion create the 5V signal?
    In other words, there is no digitalWrite or analogwrite command, so were did the voltage came from?
    Thanks alot for the lessons.

      1. The default value or standard setting for all input output pins should be open circuits. When you assign them as outputs they are switched to HIGH which provides power. When they are assigned to input they are switched to LOW which leads to ground. The reset button restarts the last program uploaded I hope that means it resets all unassigned pins to their default of open. I run the blink program before I connect anything when I don’t know for certain what the end settings were on the last program I uploaded.

  4. String topTechBoy=” awesome teacher”; //declaring my thanks for the fantastic lessons. You have a very easy to understand teaching style and the lessons are well structured, sequenced and delivered in a way that almost anyone can follow.

  5. Sir,
    I am confused between squiggle line number voltage on arduino and anolog voltage like A0 on arduino.

  6. HI
    I am a fouteen years old boy from india .
    I was going through the arduino lessons step by step.
    In this lesson I wanna know that when potentiometer gives reading then from where does it get supply as we do not declared any pin as output in the program.
    We only declared pin input for reading but i think we need to get voltage supply to vary it .
    Inspite of this the program is working , m not able to understand.
    Paul sir plss make me undertand the program.
    Its my humble request

    1. The potentiometer is supplied a voltage from the 5V pin. This pin does not have to be declared in pinMode, it is always an output and always on.

  7. Paul sir i also wanna know that if I in this age of fourteen is learning arduino …..is it a unique thing or many individuals of my age learn it…..???
    I think that its meant for adult and if it is so m doing a great job according to me
    Plss tell me that..
    Paul sir , it is awkward to ask but I want ur facebook id if u have as i have to ask many questions to you????? Plsss sir kindly give me ………sorry if u felt it awkward…..

    1. I teach high school, so most of my students start as Freshmen. In the US, this would be 14 year olds. Then I teach them for 4 years, until they go to college.

  8. Thanks for giving my answers sir
    And wanna say that u are a best teacher atleast for me as u worked as a guideline for me coz many web videos made me confuse…u r awesome teacher

    Sir plss do tell me whether this knowledge will help me in robotics enginering in high reputed college……???

    1. Yes, this is what I teach my students. I teach in the classroom in person, but if they miss, they can go back and watch the video.
      PJM

    1. Stan, thanks for the comment and interest in the topic. My goal with these lessons is to “teach” so you can learn to do it yourself. Part of that is typing code in, not cutting and pasting. HOpe this makes sense.
      PJM

  9. i know that but i m thinking of devising it by 4 ( 4* R1 ) and then make the arduino read the voltage for one resistance and when he read it he multiplicate the value .. should that work ? sorry for my bad english i hope u did understand me 🙂 and thanks for this videos …

  10. excuse me could you talk about the ldr because I’m trying to do a counter sensor with ldr and I’m struggling Example when the sensor1 is active, that means the person is in front of the door. Sensor1 active but the sensor2 is till inactive, when the person is through the door the sensor2 become active as sensor1. When the person moves forward the sensor1 become inactive but the sensor 2 is still active until the person moves from the door then sensor2 become inactive that means the person entre, otherwise if sensor1was active and then inactive before the sensor2 become active or the sensor2 become inactive in the same time as sensor1 that means the operation was cancelled

  11. Need help plz…old guy try to learn new stuff. It was giving me readings and adjusting the “pot” did not change any thing.
    Check my wiring, end up unplugging everything from the Arduino and was still get reading!? It did not seem to change even when i changed pins? The TX and or RX LEDS on the board were blinking?

    int potpin=A0;//Assigning Pot pin to A0 on Arduino
    int readValue;//declaring read value
    float Voltage;//Declare Voltage

    void setup() {
    pinMode(potpin,INPUT);//Define pin
    Serial.begin (9600); //Open serial port

    }

    void loop() {
    readValue=analogRead(potpin);//Read Value from pot pin
    Voltage=(5./1023.)*readValue;//Calculate Voltage
    Serial.println (Voltage);//Prinnt Calculated Voltage
    delay (500);//Wait

  12. hi sir!!! I am really enjoying your videos and your explanation is simple and easy to understand .
    sir can you explain how multiplexing is done with arduino ??

  13. Hello Sir!!! I am beginner in this Arduino stuff and really enjoying your tutorial. Easier to understand and learn. Thank you for sharing it with us. Can I ask for explanation on using the ADC using the ADS1115 16 bit? Thank you Sir.

  14. Cool !!!

    float Voltage;

    void setup() {
    Serial.begin(9600);
    pinMode(A0,INPUT);

    }

    void loop() {

    Voltage = (5./1023.)*analogRead(A0);
    Serial.println(Voltage);
    delay(250);

    }

  15. I’m not entirely sure how you hook up the potentiometer could you please send a pic or describe how its done. THis is my humble request

  16. Thanks so much for the tutorials. It’s structure and fun combined, starting at the very beginning – great stuff for learning.

    I have a question: To calculate the slope in a former lesson you’ve put voltage on the x-axis and the write value on the y-axis. In this lesson you’ve changed the axis. Why?

    When I get an idea of what you will do next in your lesson I stop the video and try it by myself first – as I did in this lesson just before you started calculating slope and writing the code for defining and calculating Voltage.

    I did it the other way and it worked as well and I was a bit surprised seeing your version. I can’t really see a difference – both ways seem OK for me. Here is my code for calculating voltage (my variable for Voltage is called printValue)

    printValue = readValue/204.6;
    or (let the program do the math)
    printValue = readValue/(1023./5.);

  17. Hi i initially don’t have idea on arduino but your tutorials have been so helpful, please I want to use four LDRs to control four LEDs lightning sequence,please help me with the codes ? thanks .Jerry

  18. Hi Sir,
    Your tutorials are very good.I searched for a lot tutorials and atlast found yours.
    What is the resistance of the potentiometer you are using here.

    What are the best project you would suggest your students.

  19. //Blink the LED with Potentiometer with different on and off time

    int potPen=A1; //Assigning potPen to A0
    int readValue; //Declaring our read valu variable
    int greenLEDPin=9; //Declaring greenLEDPin 9
    int onTime; //Declare onTime as integer
    int offTime; //Declare offTime as integer

    void setup() {
    // put your setup code here, to run once:
    pinMode(potPen,INPUT); //Declare potPen an input
    Serial.begin(9600); // Start serial monitor
    pinMode(greenLEDPin,OUTPUT); //Declare greenLEDPin as output
    }

    void loop() {

    // put your main code here, to run repeatedly:
    readValue = analogRead(potPen); //Read potPen
    onTime = readValue*2; //Multiply the readValue *2 and define onTime
    offTime = readValue*3;//Multiply the readValue *3 and define offTime
    Serial.print(readValue); //Print readValue
    Serial.print(” “);// Print space between
    Serial.print(onTime); //Print onTime value
    Serial.print(” “); // Print space between
    Serial.println(offTime); //Print offTIme value
    digitalWrite(greenLEDPin,HIGH);//Switch on the LED
    delay(onTime); //Keep in on for onTime
    digitalWrite(greenLEDPin,LOW);//Switch off the LED
    delay(offTime); //Keep it off for off time

    }

  20. Hello, I just wanted to know if u could give me the list of contents for the future videos so that I could buy them all in one shot.
    By the way I am still 13years and the way u explain is awesome and u r the best teach 😀

    1. Thankyou for the kind words. I do not have anything for sale, just enjoy teaching. Hope you will continue to watch these and learn more and more.

Comments are closed.