Arduino Lesson 8 - Analog Read

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so we did analog right in the last tutorial and now we're going to be doing analog reads so so far in the Arduino tutorials we've been dealing with the digital pins over here in the last four tutorial we did analog writes which use the pulse width modulation which we see over here on these ports as marked with the squiggly line on pins 3 5 6 9 10 and 11 now we're doing analog reads so we're moving over here to the analog input of the arduino which is these six pins here which are marked from a0 to a5 as you can see here now for all animal breeds we have to use these pins here so why would we use the analog read over the digital read well digital read cannot only have two values it's like that on or it's off so that's great for a simple component like a button but if we want any complicated or varied signals coming in then analog read is much better because it gives us a much wider range of values to test for so for example if it was a light sensor or a distant sensor a microphone a temperature sensor a pressure sensor anything like that that isn't just on or off you use the analog port so for example you're not going to ask someone what's the temperature they're not going to say all the temperatures on all the temperatures off they're gonna say it's 20 degrees so that's the sort of thing we're going to use the analog port for [Music] in this tutorial I'm going to be using an infrared sensor to take an analog value from so this is the infrared sensor I'm not going to teach you guys how to wire these up they're not that complicated normally it's just a five volt line of ground and an analog in pen if you want to know how to wire your own IR sensor up there's loads of different coins just look on the datasheet but in this video it's the theory of the matter it's not necessarily the wiring of the component the basic theory of an IR sensor is that there's an emitter bulb and a receiver bulb as you change the distance you are away from the IR sensor it changes the amount of infrared light reflected from the emitter back to the receiver which then in turn changes the voltage given out on the voltage outline which on this circuit is the green line so now let's look at how it's wide up so we have our five volt on our ground line here we're running to five volts and ground an Arduino if we look over here we can see there's the five volts there's the ground and we have our signal pin which is a green one here wide to the the analog input pin at a zero we can see that there so now avoid up our infrared sensor we're going to code it up so we start by saying int and we're going to call it I are equals and then normally if we were setting up a digital pin we just write the pin number say digital pin five but when we're wiring things up to the analog port we have to write a before the number because there's a different pour so by writing a we're telling it we're writing or talking about one of the analog pins so I've read it to pin a zero then we've in the void set up we set out with pin mode exactly how we would with any other normal pin so we say pin mode and it's an input because we're reading from it and then also I'm going to use the serial monitor to print the value of the IR sensor so we're going to set up the serial monitor as well which I showed you how to do in the last tutorial we're going to do a baud rate of 9600 if you don't understand that then look up my tutorial then we're gonna do I'll show you how to set up an animal greed and then we will actually print an or grid so if you're gonna do an animal group you just write analog read with a capital R and they need brackets and then say what your analog reading so in this case we're an awaiting the IR sensor so if you just wanted to do an analog read that's what you'd write now that returns a value between 0 and 1000 and 23 and the reason it's 1023 is that is a 10 bit binary number so if you take 10 bits in binary and there were all ones and you added up the value of all of them it would equal 1023 so we can now do a see a real print of that so all we'd have to do is say serial dot print and then we're gonna say LM because you want to print each number on a new line brackets and then I'm just gonna copy this into the brackets because we want that to be part of the serial print and then we're gonna close the serial print so we couldn't win the line of code and we're gonna add a delay just so it doesn't print really quickly notice it 50 milliseconds and now we're going to upload that to our board and open our serial monitor and now we see the values of the IR sensor printing every 50 milliseconds so now I'll switch to my normal camera and we can see how distance away from the IR sensor affects this value so we're looking our IR sensor over here and this is the serial print of the values we're getting out and as you can see as I move my hand closes the IR sensor it's starting about 800 900 as I move my hand closer it's dropping to 700 at 600 it's 400 200 100 as I move away it's gonna go up again so I'm going to quickly take that principal entry you guys something useful you can do with it so if we delete all of our code and start fresh so you can get some practice in start by setting up the infrared sensor again so we're gonna say int I R equals and then remember wide it to pin a zero and then we're gonna say int LED equals we're going to white to pin 13 then we're gonna go to the pin modes and the void setup pin mode IR sensor and remember that's an input because we're reading from it pin mode LED and that's an output remember because we're writing to it and that's how setup is done it's now going to move to the void loop and we're going to use an if statement and we say if the animal grade of the infrared sensor and then remember when we were using if statements with digital reads we'd say equals equals high or equals equals low because the IR value that we're getting could be anywhere between zero and 1023 instead of writing high or low here we write a number say 500 and then instead of saying equals equals because the analog read value the chance of it being exactly equal to 500 is very low so instead of saying equal to 500 we usually say the analog grade of the infrared sensor is less than or equal to 500 or whatever the value you want to set it as or more than an equal to 500 so we're gonna say if it's if the analog read value is less than or equal to 500 then we want to do something and what we're gonna save for it to do is to turn on the LED so we're gonna say digital write LED high and then we're gonna add an else statement to turn it off otherwise we're gonna operate that so I've got my LED hooked up to ground and pin 13 over there so if pin 13 is working as our fault five volt line when we digital write it high and our infrared sensor is coded to turn on the LED attached to pin 13 when we come within a certain distance of it so in other words when the analog read value goes below 700 so let's see if we can get so as I move my hand closer as I get within a certain distance the LED turns on so my hand away it turns off again on so we can use this theory with a number of analog sensors in the context of a vending machine we could use the infrared sensor to turn on an LCD screen only when someone walks close enough [Music]
Info
Channel: Benduino
Views: 21,593
Rating: 4.911602 out of 5
Keywords: analogread, analog read, analogread arduino, analog read arduino, how to analog read from an arduino, analog voltage processing on arduino, analog in pins, analog in arduino, rduino tutorial, rduino, arduino tutorial for beginners, how to analog read on an arduino, arduino uno tutorials
Id: eVbuRAn-pZA
Channel Id: undefined
Length: 8min 55sec (535 seconds)
Published: Sun Dec 11 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.