Tutorial 4 for Arduino: Analog Inputs


This tutorial was featured on the official Arduino blog on 3/9/2011

This week is all about analog inputs for the arduino. I’ll show you how you can use a voltage divider circuit (see episode 3) and a variable resistor to make an analog sensor. We’ll also use a Sharp IR distance sensor as an analog input to detect distance and movement (with some clever programming). By the end of this episode, you will be able to create your own emergency lighting system! Without further adieu, check out this week’s tutorial on analog inputs for the arduino…

One more thing: You only have 1 more week to submit your arduino projects and win a soldering station – get going!

You can download the files associated with this episode here:

GNU GPL License Distributed under the GNU General Public (Open-Source) License.
Please Attribute and Share-Alike.


  1. Pingback: Tutorial 11 for Arduino: SD Cards and Datalogging |

  2. I need your help;; in my project simply i have to read analod values ,amplify it and have to display on the lcd but my serial monitor and lcd are showing different values..please help.

  3. I don’t know why but mine does the opposite when I put my hand over it it turns off, and I copyed and pasted your code. I also used different pins to see if my board was defective.

    • Eko, I’m new at this stuff but excited to tinker and learn, I had the same problem, here’s what I did, under the “if” and “else” lines reverse the high and low and it will work.

  4. Hi Jeremy,

    I followed your video several times, why does my project work backwards, when there is light the led lights up and when it is dark it turns off. I’ve reversed the lines in the “If” and “else” statements from high to low to get it to work however, when I implemented the dimmer program, it goes back to being backwards. what am I doing wrong? or what can I do to get this right….

  5. Hi,
    Ur tutorial videos are awesome for beginners. I would like to get some code for arduino to transmit wireless messages to a remote user. Can u just help me?

  6. hii,
    how to connect a negative voltage to arduino board, like a ph sensor which starts with -414 mv to 414 mv

  7. I appreciated your video. I am trying to get my Arduino to turn on WS 2801 LED pixels whenever a person walks past an IR detector at the top or bottom of the stairs. I have the code from Adafruit to run the LED’s, and I can run a sketch to read the values of an IR detector. Most example codes I have found show that if a condition is true then the Arduino turns on or off a digital output. What I need is for my Arduino to run the LED code or loop back to monitoring the IR sensors, depending upon what the IR sensors read. How do I code the sketch to continue the program or keep reading the sensors?

    • use a state variable to track the state of the IR sensor. If changes by a certain threshold between two passes, then execute a separate function to controls the LEDs. once that function is done running, the code will continue in the loop() and start checking the IR sensor again.

  8. Hey Jeremy, just wondering if there’s an alternative to the photoresistor that you are recommending? It’s currently out of stock.

  9. Hey Jeremy, is it compulsory to use 150 ohm resistor?
    Actually am not getting it in the market .
    So, please suggest which alternate resistor can be used.

  10. And what are those values that we see on the serial monitor when we darken or brighten above photo resistor?
    are they the values of analog o/p voltage?

  11. how to connect cfl lamp instead of that led

  12. Hi everyone!
    I’m trying to connect temperature sensor TMP36 to my Arduino Uno. I’ve found that output range of the sensor: from 0.1V (-40°C) to 2.0V (150°C). I also found sample sketch that get value from analog input and convert it to voltage using following code:
    return (analogRead(pin) * .004882814);

    But this line of code assumes that sensors’s output range is up to 5V (5/1024 = 0.004882814). Is it correct? How should I get correct voltage value from sensor output? Thanks for answer!

  13. Hello,

    I just created the nightlight but am having two problems: First, my photo resistor values are less than 100 and are between 0 and 100.

    Second, I have noticed that my led stays on even though I entered the correct code.

    Can you please help?


  14. Hi Jeremy,

    I am new to Arduino and love your videos.

    One question, the SCH schematic file won’t open. Says it not valid. I do have PBExpress. Can you send me a new file or convert it to another format?


  15. Your lectures are awesome, and the whole idea of arduino is amazing as it reduces the whole lot of maths and equations thrown around by lecturers. It makes learning simple and will enthuse a lot of youngsters hopefully in the future …[I am 71]

  16. Hi, I need to read in temperature and voltage. temp on A0, voltage on A5, separately they work fine, when running the code together as one sketch they give “junk values” as if they might be adding to each other, how can I successfully read in temp and voltage?


  17. really cool. you make it so easier.

  18. I am a youngster 72!

  19. hi … I’M A BEGINNER. please help .
    when i write the word “serial” it appear in black colour and it is not being compiled. (error: ‘serial’ was not declared in this scope). i tried including SoftwareSerial.h but no good. i downloaded your code and just cut paste the word “serial” into my code and it was brown and all went fine. what should i do?

  20. Pingback: Robotics….. Controling a servo motor with a potentiomenter. | KYchem

  21. Pingback: Robotics….. Controlling a servo motor with a potentiometer. | KYchem

  22. Hey!! I want to give output to spycamera on arduino to get on….and start recording using arduino uno how it can be done please help

  23. Hi, i really loved your video, you speak clear, and it’s all really simple explained by you!
    I was trying to understand why in my circuit, where i have a DHT11 and LM35 sensor i get wrong readings when I add a LDR (photoresistor). I actually use the photoresistor to detect if its dark or clear to understand if i have to turn the heater on for my cats house. I don’t have an RTC so i use the LDR but i can’t get correct readings on the LM35 sensor. Do you know why? I’ve tried it on a Arduino Uno Rev.3 but also on another Nano and Arduino Uno rev.3
    Thanks for any help…probably i’m missing something…some sort of lack of knowledge probably on resistance or internal voltage..IDK.


  24. Pingback: Better Thermistor Readings with an Arduino: Series Resistors & Aref | Arduino based underwater sensors

  25. Hey Jeremi ,

    I want to know that while connecting LED ,one wire get connected to digital output pin and other is connected to Ground on breadboard .

    Why don’t the other end get connected to 5v power line on breadboard.

    Please tell me. I am unable to understand this logic.

  26. I would like to know how would I program the Arduino to only print a “float” value once to the serial monitor. I am monitoring the distance moved on a digital slider. However, the value infinitely prints to the serial monitor, but I want it to print only when the value has changed. It appears that the program doesn’t want to compare to two float variables.

Leave a Reply

Required fields are marked *.