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.

official.arduino
2019-10-02T19:32:52+0000

👋 Want to bring Tony Stark-like gesture control to your projects? Learn how with the BLE-enabled MKR WiFi 1010 and Nano 33 BLE Sense boards using the ArduinoBLE library.
official.arduino
2019-10-02T19:08:01+0000

Arduin-yo ho ho! A fairground favorite, the pirate ship is a fun way to explore the oscillation of a pendulum. How much fun, you ask? Access our Science Kit Physics Lab preview and see for yourself: http://bit.ly/2oC6L0Q

🔬 Now that you’ve gotten your feet wet, dive into the kit and enjoy all nine experiments. Order yours today: http://bit.ly/2MnQ7fr

pankaranta
Fri Nov 08 19:30:06 +0000 2013

Arduino MokkiMittari – SpaceWx status: Cloud sensor/sky: -8.1C, SpaceWX ambient: 0.6, Sky LUX 0 as adc 0-1024 / 0=dark Energy 38 W

pankaranta
Fri Nov 08 19:30:06 +0000 2013

Arduino MokkiMittari – SpaceWx status: Cloud sensor/sky: -4.3C, SpaceWX ambient: 4.4, Sky LUX 15 as adc 0-1024 / 0=dark Energy 38 W

ARDUINO BASED CYLINDER HEAD FLOW BENCH

ARDUINO BASED CYLINDER HEAD FLOW BENCH

Originally posted 2016-07-14 20:05:30.

(Visited 53 times, 1 visits today)

## Comment (20)

1. Abhi Bhattii says:

How to do Sampling of Analog Signal in Arduino Uno?

2. Dr.Curious says:

Awesome !

3. Selected Free says:

Do you have to put 0 or can you put A0 for the analog pin 0

4. Francis Dela Cruz says:

Your videos really helped me to understand how Arduino works and how to apply the basic concepts I’ve learned from school. Thank you!

5. Alb Creationzz says:

Sir in the program we are not assigning the sensePin as input ie. pinMode (sensePin, INPUT);
Why is that so..??

1. fragwits says:

default state of the pin is input , he says so in the video at the start of typing.

6. Zomel De Vera says:

Sir, what if im going to send an analog input to c#? How?

7. уве вонг says:

if the current distance from the in loop, before even making its first loop, is greater than or less than 200, the first part of the if statement will be true cause the last distance is 0, right?

8. Nhia Vang says:

What does it mean when “”motorPin” was not declared in this scope”? How do I fix this?

9. yhwhdesigntruth says:

Firstly, thank you for all your teaching here. It is helping me get up and running very quickly. I am a web developer and coder so once I get a decent grasp of this I will be fine, but I do need some decent support to get me there. Maybe more that I would like to admit 🙂

I am needing help with a project and if you could or anyone has the time to walk through this with me I would be so greatful!

Here is my project :

I need to take 12v from a total of 8 sources ( each has its own 12v wire and ground ) ( 4 left and 4 right )

I need to take that 12v and step it down to 5v. I have purchased (2) 4chnl opocouplers and I have a voltage stepper to bring my voltage down to 5v.

What I want to do…

the base of the project would be when pin “x” sees 5v it will tell pin “x” it has a signal and a loop will run until that signal is no longer there. This would apply to all four inputs.
the advanced part of the project would be when multiple pins see signal it can run multiple loops at the same time.

Real world application ( vehicle parking lights, brake lights, turn signal, and reverse ) <-- want to build this out on a bread board or via using my ws2812 led strip to 'bench test' first before we build this out. I am wanting to do the following pll ( when on ) turns on leds at 50% (red) bll ( when on ) turns on leds at 90% (red) revl ( when on ) turns on leds at 90% ( different color --- white ) tsl ( when on ) runs a sequential effect ( red ) i would have to have if statements... that would be similar to.... if pin "x" that pll is hooked up to gets signal and pin "x" that bll is hooked up to also gets signal, tell the leds to go to 90% if pin "x" that pll is hooked up to gets signal and pin "x" that tsl is hooked up to also gets signal, tell the leds to drop to 50% run a sequential loop that essentialy turns 2 or 3 on at 95% and runs through the line in a loop if pin "x" that pll is hooked up to gets signal and pin "x" that revl is hooked up to gets signal, tell leds to drop to 50% and take 1/2 of the leds and switch to white with brightness of 95%

10. Ujjwal Kesri says:

I didnt get the use of the conditions in if statement in ir sensor and photodiode cide

11. ken santoss says:

great job

12. Taniela Fameitau says:

I need to send three sensor values from Arduino Uno to google spreadsheets then take spread sheet results and display them on a phone application. any help would be much appreciated

13. Mark Sloat says:

Wont these every need a header?

14. John Kumar says:

Can we give analog inputs to the pins by shorting all the pins, ie giving single signal to the all inputs at a time by shorting them.

15. Walid Eid says:

how do you get the COM23 Window ??

16. Cattle Ranch says:

I like your video. Will you help me create a Script to do the following:
1. Accept an input from a simple Normally Open contact Closure (push button) to start sequence.
2. When button is pressed: fire relay 1 for 30 seconds. After 30 seconds, release relay 1. Then need the script to fire relay 2 for 30 seconds and then Release relay 2. No loop is required.

17. Zahid Faheem says:

if we were using analog pin 4 for example would we write “int sensePin =4”?
how would the program know if we mean digital or analog pin number 4?

18. Prakhar Tibrewal says:

the values in my serial monitor are like 60-70 on primary testing (just reading analog values) and when i bring the torch really close to the sensor it jumps to like 300-320…is that okay??

19. Bloomin’ Food says:

Fantastic.