BLOG > 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.


116 comments

  1. hello …. I like to do new project in arduino to control the home and must using new technology and new ideas …….. so if you have the new ideas and can help me to do it …. please contact to me at [email protected] thanks

  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.

    1. 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?

    1. 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.
    Thanks!

  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. 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!

  12. 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?

    Thanks,

  13. 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?

    Thanks
    Ralph

  14. 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]

  15. 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?

    Thanks

  16. 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?

  17. 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

  18. 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.

    Thanks.

  19. 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.

  20. 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.

  21. Hi Jeremy, I m working on project with arduino where it consist of two sensor inputs as LDR and PIR/IR, and the logic running is same as the arduino tutorial for ‘analog inputs’ episode as when its dark and motion is detected output is generated. But my project is based on two relays which are connected to two declared output pins of the arduino. Here the logic goes when input condition matches two output pins becomes HIGH at the same time but if the PIR/IR input becomes LOW the one of the output pin goes LOW whereas the other output pin holds its HIGH state and then goes LOW after a certain period of time depending from the time of 1st output pin when goes LOW. I am using a delay function for which is where I am having a problem. As if the PIR/IR again detects the motion 1st output doesn’t goes HIGH unless the delay time loop ends as it is subroutine function. Can you please help me with this what to do.?? Thank you loved your works..

  22. Hello
    My name is Emmanuel Urrieta, I am student in Instituto Politécnico Nacional in Mexico, I am working with a sensor Lidar XV11 in Arduino but I do not know how to obtain the data obtained from the sensor and make an arrangement of these, I have searched for this sensor but I have not found enough. I would really appreciate it if you can help me.

  23. Hi
    I like your videos which are good. However I find that with many like this it is difficult to listen and watch what you are doing and to which pins you are using. Would it be possible to put a drawing on your website showing which pins are used. Could i suggest something like Tinkercard which has a “circuits” page to which you can add items and show wiring etc. I have found the data on your web page which again is great but an additional schematic would help. Thanks.

Leave a Reply to August Hoecker Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Advertisement