arduino analog pins
Let’S talk a little bit about how analog inputs are set up on the Arduino boards. Here I have the uno, the mega 2560 and the nano boards all of these allow for analog input options and on all our Lino’s. There is a 10 bit analog digital converter that allows you to convert from ground up to the input voltage of the Arduino. Usually, 5 volts to a value from 0 to 1023. That means you have a thousand 24 bits of resolution or two to the 10th power. Hence why it’s called 10 bit resolution? If you want to learn a little bit more about how analog digital converters work, please watch my tech bits episode on analog and digital signals. You can set the reference voltage that you use for the analog inputs on all these Arduino boards by default. It’S set to 5 volts or 3.3 volts on 3.3 volt on. Are we no boards, but here we’ll just be walking working with the default 5 volt boards? You can also set it to an internal 1.1 volt reference, although that’s not available on the mega and on the mega, you can use different command, set it to one point, one volts or two point: five: six volts as a reference. You can also select your own voltage reference by setting it to an external value which you input into an analog pin on the Arduino. Here we can see the analog reference pin I was talking about.
You might want to consider using an external analog reference if you’re only using sensors that go from 0 to 3.3 volts on a 5 volt Arduino. The reason for this is that the values from 3.3 volts up to 5 volts, if you use the default 5 volt reference – will basically be excluded and you’ll be getting less resolution than you potentially could use an external voltage reference allows you to eliminate that problem down. Here on the bottom, we can see the analog inputs a zero through a five. There are six inputs available on Arduino Uno. If we look now at the mega, the mega has many more analog inputs available. All these right here analog inputs 0 through 15, so there’s 16 analog inputs available on the mega. And if we look at the Nano board, we can see that there are actually 8 analog inputs available on here. For our first sensor, let’s use a simple photo: resistor photo resistors simply decrease their resistance as the ambient light in the room increases. As I decrease light in the room, the resistance will increase. So if we hook this up in a voltage, divider circuit with a fixed resistor, we can essentially feed into the Arduino a voltage that varies with light in the room. Let’S hook that up using this schematic here’s our assembled circuit, just like in the schematic. We have a photo resistor connected between 5 volts and a 10k resistor. The 10k resistor then goes to ground and at the junction between two of them we take a wire out and go to analog input 0 on the Arduino.
This is just like our voltage divider setup that we worked on in the last episode now let’s program, this to show us the values from the photo resistor, as we change light in the room. Note that you can change the position of the photo resistor and the fixed resistor and the values in the computer will inverse that’s really a quick program that will show us the value of this sensor. So first we’ll need to define our input. Pins we’ll call it sin spin that’s going to be equal to analog input on pin 0. Now we always want to do our setup function, and in here we don’t need to define as an input pin because it’s sent to that as default. We can define the analog reference as default that’s because we’re going from 0 to 5 volts, but again this line isn’t necessary. It will do this by default, but I’ll just put in here for complete coverage, and we want to print this to this screen. So let’s set up our serial connection, like we did last time and the 9600 just means the speed at which we’re communicating with the computer 9600 baud is a pretty common value that you use alright and in our loop, all we’re going to do is read the Sensor and then print it to the screen, so serial dot print line. Just like last time, analog read and the sensor of pin sense pin make sure we close our parentheses and we don’t want to spit out values constantly so we’ll just put a delay in there of 12.
Second, that should be good. All right let’s see how this works out, so we can see that ambient light we’re getting a value of about 950 and, as I put my hand over the sensor, we can see that drops down. If I cover it completely, we go down to about 750 ish. 770, ok great that’s, only giving us a range of 200 out of a potential range of a thousand 24. Why is that? Well, it’s, partially because of the resistors that we’ve chosen and the inherent fact that you’re going to lose some values by using a voltage divider, which is fine. We don’t need the full 1024 resolution. You can overcome this by setting up an op, amp and doing a few other things, but it’s not really that important to us. If we want, we can try to change the resistor to a 1k resistor and see how that changes. It now we’ve got a 1k resistor in the circuit instead of a 10k resistor, and we can say let’s change the values now ambient light is closer to around 570 and, if I put my hand over it drops down to it about 200. So that range is a little bit better, not by much, and you can keep messing with the resistor values until you get a value where you, where you’re happy, but in reality this amount of range is fine for what we want to do. We just saw that a value of around 9 ambient lighting in the room so let’s make a simple nightlight.
Now if the value goes below 800 we’ll turn on an LED that will illuminate the room. So all I’ve done here is add an LED to our circuit. With our 150 ohm resistor same as we used last time and I’ve hooked, it into pin 9 on the Arduino, so let’s go program that all right. The first thing we’ll have to do is to add our LED pin here and it’s going to be pin 9. Next up, we have to set that pin as an output, because if it hit the defaults to an input, so let’s make the LED in output and we can get rid of the serial communication we’re not going to use that anymore. Okay down in our loop let’s start by getting the value from the sensor and saving it to a variable, so we’ll make a new variable called Val, pretty standard name. Do an analog, read a sense. Pin we don’t need this one. We have Nydia these anymore right and then, if the value is less than 800 we’ll do something so I’ll use an if statement which checks the value. So if the value is less than 800, we will do a digital write to the LED, pin and try it on. Otherwise, if it’s a greater than 800 let’s make sure we turn it off. Digital write LED, pin low, which will turn it off and that’s it. So let’s CSS works. Okay, great, we put our hand over and the LED turns on just like we wanted to it, gets too dark in the room.
We’Ll have a nightlight. Now now you don’t have to be afraid of monsters in your closet. Now let’s make our nightlight a little bit more adaptive we’ll. Have it change brightness, based on the brightness in the room, as the room gets darker, the LED will get brighter to illuminate the room, better we’re going to do a few things to do this. First off. We need to map our value, so there’s a function in Arduino called the map and map takes in the original value Val in this case and values to map it to so. The reason we do this is because our value for an analog input can range from anywhere from zero to a thousand twenty three, where, as analog output, what we’ll be using to change the brightness of the LED is only zero to 255. Additionally, we want an inverse relationship as the value from the light sensor increases. We want the value on the LED to decrease and vice versa, so the map function will allow us to do that. If val is the function, we want to map and let’s say we’ll set 750 as the minimum and 900 as the maximum, and that will map to 255 and zero and we’ll set that new value equal to LED level. So precisely what this is doing is it’s. Taking in valve from the sense pin the value of 750 gets converted to 255, the value of 900 gets converted to zero anything in between scales proportionally.
So, as the value increases from 750 to 900, this value will decrease. Our LED level will decrease from 255 down to zero, which we can then feed into our LED to change its brightness. We want to do one more thing now: it’s to check for values outside of 750 or 900, if it’s less than 750 or greater than 900. We want to set it equal to those values so that we’re not dealing with anything greater than or greater than 255 or less than zero across the analog output. Won’T know how to handle that correctly. So we’ll use one more function and we’ll reset our Val first. Using constrain constrain is another Arduino function that takes in a value and then sets minimum and maximum values, so we’ll set it to the same value. 750. 900. Now what this does is takes the value from our sense pin if it’s less than 750, it resets it to 750, if it’s greater than 900, it sets it up to 900. Now we only have to worry about values between these two, which means these values will always be between 255 and 0, just as we want them to now. We can get rid of this and just use analogue right to set the brightness of the LED, directly analog right, LED, pin and will now set it to LED level. Awesome let’s see how that works. Ok, so the LED starts off like we expect it to and, as I move my hand closer, we see the LED incrementally gets brighter until it’s at full brightness.
When I have the sensor in complete darkness and it scales back just the same way perfect. We now have an adaptable, nightlight, naturally photo resistors aren’t, the only kind of variable resistors that you can use in a circuit like this. Here I have a thermistor which varies its resistance with temperature. As I hold on to it, you can see that the value drops from 460 down to that 440, so this is actually working the opposite direction of the photo resistor. If we wanted to increment the values in the same direction as the other one, we could swap it with the resistor. Another thing to experiment with is using different resistor values. If you look at the voltage divider equation that I showed you in the last episode, you’ll see how setting this resistor will affect the range that you’ve got the analog readings on and then, of course you can also use op amps to scale the readings up. So you get a better resolution, but we won’t cover that in this episode, of course, you don’t always actually have to build your own sensor. You can often buy preassembled sensors that are designed to output at a certain voltage, so you don’t have to worry about scaling them or adding a voltage, divider circuit or anything like that. This is an example of one. This is an infrared distance sensor. It uses an infrared emitter and receiver to bounce infrared, light off an object and detect the distance to it.
All sensors that come like this will usually have three wires: a voltage, wire wire and a sense wire. The red wire we give it 5 volts ground gets next the ground, of course, and then the yellow wire returns a value between 0 and 5 volts that we can feed into the analog digital converter and the hard we know and use it to measure a distance Value you’ll often notice that these kinds of sensors will give you a broader range because they’ve already designed it to operate on the whole 0 to 5 volt range. You can also buy these centers in 3.3, volt, iterations and other voltage versions. So if we look at the screen now, as I move my hand closer and closer, you can see that the value slowly increases from about 50 all the way up to about 600, something let’s wrap up today’s tutorial with our first little project. This project will combine the infrared distance sensor and the photodiode that we used today to make an emergency lighting system in the event that a room is dark detected by the photodiode and movement is detected by looking at values from the infrared distance sensor. We’Ll turn on our emergency lighting system, in this case an LED, although you can easily hook this up through a relay to control a more powerful bulb. A lot of the code we’ll be using to make this system work is very similar to what we had before.
There’S only a few modifications that we need to make to put everything in place. The first thing is we’re now using two analog inputs and I’ll again put 0 and analog input. One zero is connected to the distance tech door. In this case a motion detector as we’re using it and analog input. 1 is connected to the photodiode, which I’m calling light pin. We still have an LED connected to pin 9 next up we’re going to need to make to various to keep track of the distance. What we’ll do is every time through the loop will keep truck we’ll look at what the distance is from the infrared distance sensor and compare it to what the distance was last time. If it changes significantly from the previous detected value, we can assume that something has passed in front of the infrared distance detector and assume that movement has occurred. So we’ll use these two variables to track that in the loop our threshold defined by interest at about 200. This is basically the value 4, which is looking for deviation amount between the last distance and the current distance. You can fine tune this to make system as sensitive or as insensitive as you want. I found that 200 works pretty well. Setup is the same. We just have to configure LED pin as an output. Everything else is defaulted and here’s our loop, so first off we’re, going to read our light value and from the photodiode using analog read just like we did before and we’ll read in our current distance from analog.
Read emotion: pin note that here I have to define light valance into because this is the first time I’m using it current distance I’ve already defined as an int up here. So I don’t need to use the integer term again. The next thing we’re going to do is look if everything meets our conditions, so this, if statement, will check if the threshold has been exceeded and if the light value is less than value of 800. So how are we doing that let’s break it down? There’S two parts here, the part in this set of parentheses and the part checking for the light value. Those two parts are separated by double ampersand, which is a logic, and that means if both the contents of this parentheses are true and the end. This is true right here, then we’ll execute the if statement, which turns the LED on for one second, if we look more specifically into this part of the, if statement, we can see that we’re checking for our thresholds, these double pipes indicate a logic or so either. This or this has to be true for this entire part of the. If statement to be validated as true the first part before the double pipes says, current distance is greater than the last distance plus the threshold. So if our threshold is 200 and our previous distance was 300, then if current distance is more than 500, then this will evaluate to true, alternatively, it’ll evaluate to true.
If the last distance was something like 500 and then that minus threshold of 200 is 300. So if current distance was less than 300, that would also indicate a significant change, and that would also evaluate this to true. So, if all that’s, if one of these is true – and then this is true – also then we’ll digital write, the LED to high and turning it on leave it on for one second to make sure people have enough time to find their way it’ll stay on. If it continues to detect movement, otherwise we’ll turn the LED back off and then the last thing we have to do is set last distance equal to current distance. So, at the next time, through the loop, we can compare the distance that we use this time to the loop with the new distance. So if I just put my finger over the photodiode – or I just make some motion, they only do you want to turn on, but if room is both dark and motion is detected, the LED will turn on for a second and help anyone who’s roaming through the Dark find where they need to go thanks for watching this episode of my Arduino tutorial series. If you have any questions or comments or if you want to watch the other videos in this series, you can do so on my youtube channel. So I got 14 on my blog Jeremy, Blum comm or over at the element.
14 Arduino group. I hope you guys enjoy this and I’ll see you guys next week, thanks to element14 for helping me to sponsor this video series, they were kind enough to provide a lot of the materials that I’ll be using to create these tutorials feel free to go visit their Website at element14.
arduino analog pins Video
arduino analog pins news
Arduino MKR WAN 1310 LoRa Board Gets HW Security, Longer Battery Life and a 2MB SPI flash – CNX Software
Posted on Friday October 11, 2019Arduino MKR WAN 1310 LoRa Board Gets HW Security, Longer Battery Life and a 2MB SPI flash CNX Software … Continue Reading »
Posted on Tuesday October 08, 2019Making Music From Cardboard Hackaday … Continue Reading »
Posted on Sunday September 22, 2019A Hard Rocking Arduino Visualization Shield Hackaday … Continue Reading »
arduino analog pins Social
🔬 Now that you’ve gotten your feet wet, dive into the kit and enjoy all nine experiments. Order yours today: http://bit.ly/2MnQ7fr