Arduino Tutorial 26: More Fun with Photo Resistors

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Palma quarter from top tech boy comm and we are here today with lesson number 26 in our new our improved our exciting Arduino tutorial series as you probably know by now I will need you to pour yourself a large mug of iced coffee no sugar especially not those little sweetener packets it is delicious and refreshing just the way it is I need you to get your super starter kit from a Lego if you don't have one look in the description click the link cook a brother up 35 bucks you get an Arduino in every imaginable component and that's the the kit that we're using in this tutorial series actually it helps us if we use the same kit because our hardware is the same easier to play along at home if you do that ok what are we gonna do today we are gonna have more fun with photo resistors in lesson 25 I showed you the band diagrams and the little bit of the solid-state physics about how a photo resistor works we saw that as light hits the photo resistor it generates electron hole pairs those electron hole pairs make it more conductive therefore the resistance goes down the higher the light the lower the resistance and then if we create a simple voltage divider by putting that resistive sensor in series with another resistor then we can create a voltage divider read the voltage across that center pin and then we can determine in the Arduino whether the lights are on or off or how bright they are and we did it kind of a little warning signal where if the lights were on everything was good the green LED was on if the lights were off you would turn the green LED off and you would turn the red LED on okay so this is what we are going to do today it is going to be a little bit of a pop test for you because I'm going to ask you to do this on your own what I want you to do is I want you to get first of all I need to get out of your way a little bit I want you to get your Arduino your breadboard this is the passive buzzer which you learned how to use in lesson number 24 our 5k resistor in our photo resistor this is what I want you to do I want you to hook up the photo resistor in series with the with the 5k resistor remember make that voltage divider like we did earlier and then read the voltage off of that Center of the voltage divider and then I want you to hook up your passive buzzer and then what I want is I want you to generate a pleasant and continuously varying audible tone think it's an audible indication of how bright the lights are in the room like imagine that you were blind and you wanted kind of an audible indicator of how bright the lights were now I will give you a hint in order to do this correctly you are going to have to use math yes I know I'm getting a lot of thumbs down on these videos because I'm trying to get you to think I want it guys it's great that I know how to do this and it's great that you can watch me and copy what I do but I need you to start doing things on your own and so what I'm doing is I showed you how to do a I showed you how to do B and I know with a and B you can figure out C in the earlier lessons I have given you enough information where you can figure out how to do this okay so I want a pleasant tone dark is on one end and then very brightest on the other in all white levels in between smoothly vary the tone okay go do that and then come back and I'll do it with you but really man you're gonna get stuck but figure it out don't watch me do it you get it working first and then you go down in the description and said I made it work by your myself and then I will give you a gold star okay go do it alright are you back and you want to watch me do it and now let's see if you did it the same way I did it would love to get comments from you guys down below let me know if you did it this way if you had a better way to do it or if you did it differently but let me start by hooking the circuit up so we're gonna start with their voltage divider I am going to I'm going to put the 5k resistor between column 10 and column 15 between column 10 and 15 and then comes the photo resistor between 15 and 20 okay and what is key here is that the resistor and the photo resistor share a common column there at that connection alright then the top or the left of the resistor there that leg of the resistor the left leg of the resistor is going to go to five volts alright then we need to ground the other end of the photo resistor so the bottom of the photo resistor is going to go to G in D have your photos detector up pointing towards the light now we are going to hook up our passive buzzer and so we are just going to choose to appropriate and remember the buzzer has a polarity so what you need to do is make sure that the positive polarity has the little plus sign the little plus symbol there that that will go to a digital pin I think I will use pin eight today alright and now the other leg the one that's not marked Plus needs to go to ground and so there is another G and D over there by five volts and I think that I have this thing all right now to make it work we need to do a little math and if you remember you you need to do the measurements in your room with your setup because your measurements might not be the same as mine but when I came in in the last lesson oh I forgot one thing we need to measure the voltage at this voltage divider so right here in that column 15 I need to they take that signal off let's see maybe I can zoom in here give you a little better view all right so do you see this part of the circuit where one leg of the photo resistor is hooked to the resistor in that column 15 I need to put a wire in that column 15 so I can snag I want to snag that voltage and I want to bring it over to pin a zero so I can read it okay so that is going to read that voltage again this is very very very similar kind of identical to what we did in the most excellent lesson 24 and so this is all hooked up now this is all hooked up but now we have to do some math and what you need to do is you need to go in B and you need to read the signal off of that photo detector and what I could call that is I could call that like let me now switch where you can see what I am writing and he doesn't mount okay and remember when we did this in the earlier lesson let's call it light Val that is the value that's coming off of that when read in a zero what we found was that light val was 750 when the light was on right when the room lights were on very brightly it was 750 the light val was 200 when the lights were off okay so those are two numbers that are very important 750 and 200 those are sort of our end points also then do you remember when we were doing the passive buzzer what we could see is is that a really nice tone started at about one millisecond in a really nice tone on the higher end was at ten milliseconds all right so if I want to go smoothly between these values what I can say is is that when the lights let's see when the lights are on this is what I'm going to read I'm going to read 750 and then what I want to generate is one milliseconds then when it's 200 I want to generate 10 milliseconds so this is the way you've got to do it right again this is a line and I'm hoping you guys are getting comfortable in doing this because you can't just wing it you really need to do the math so let's draw our graph and what is the independent axis is the horizontal axis the independent variable here is what light Val light Val that is what we measure and then what do we want to generate what we want to generate is the delay time so this one millisecond in 10 milliseconds that's the delay time so what did we say well let's I can't remember exactly what I said but let's say that if I read 750 I want to generate 1 millisecond so if I come over here to 750 Reid I want to generate 1 millisecond so this is the point 750 and 1 millisecond so if I read a lot valve of 750 I want to create a delay time of 1 then what is the other point the other point I mean let me I did this backwards I need to use the lower one 200 because that's further to the left so 200 on light valve would generate 1 millisecond in delay time and then a light valve way out here of 750 would generate 10 milliseconds so this would be 750 and then 10 milliseconds all right so I've got 2 points now and I can draw a line between them I can draw a line between them like that so let's create our two points very clearly here I've got the point 200 comma 1 and I've got the point 750 comma 10 I need to find the slope of that line the slope M is going to be equal to y2 minus y1 which would be kind of delay time to minus delay time 1 divided by light valve 2 minus light val1 right normally in a line you think of y2 minus y1 over x2 minus x1 well here we're just using delay time to minus delay time 1 over a white value 2 minus life value 1 so I want you to see that a line actually makes sense when you're doing you know real systems like this and you can't work this problem if you don't do the math so now what is the slope or what is the point-slope form of a line it is y minus y1 is equal to M onto X minus x1 and for this case what I would have is my Y value is delay time so I would say delay time - delay time 1 is equal to M onto lot Val - slight vowel one all right so now let's go ahead here and calculate the slope so the slope let's calculate the slope here delay time - - delay time 1 is going to be 10 - 1 / and that's going to be 750 - 200 750 - 200 and so the slope of the line is going to be 9 over 550 I hate doing this in my head but I think that's right now we've got to take this and we've got to put it in for slope here and we've got to put the numbers in so I have delay time delay time - delay time one what is delay time one it is 1 delay time - delay time 1 is 1 is equal to slope which is 9 over 550 on - white valve - white valve - white valve 1 what is light valve one white valve one is 200 okay so now what do I have delay time I'm going to add the one back over here and I'm going to have 9 over 550 times white vowel now I'm going to distribute this over 2 here which is going to be minus 9 times 200 right I'm distributing this over 550 okay and then I still need to add the 1 this is the equation that I need to program and normally you would work this out and combine those but we can just put this whole equation into Arduino so let's bring this back and now let's see if this crazy thing is going to happen did you guys go in and do your math like I've just done that is the way that you have to do it to make it work so let me call up Arduino over here and let me see if I can get a nice view for you to see all right that's not too bad so now what do we need to do well we have got a pen that we're doing we've got the light pin right so I'm going to have int and light pin and that is the pin that we are reading from this light sensor and so that was a what did I say a zero and I think I better make it a capital A zero all right then we have an int and we have a buzz pin and buzz pin was pen eight buzz pin is eight so I have my two pen set up I don't think there's any other pins that I need so I'll come in now and I will do my pin modes so pin excuse me pinmode a a zero is a what output all caps output all caps all right pin mode the buzz pin is an output all right so I got my pen mode son now what do I want to do well I need to read the value that is coming off of the sensor that will be light Val so I read and then I need to calculate delay time and then I need to use tool a time and send that in the delay that I use on the buzzer all right so what's the first thing I need to do I need to read off what do I need to read I need to read light valve which means I need to declare that so int light thou and I don't put a value there because we're going to calculate that value and then also I need an int and that we called initially we called it delay time so the lay time and I will calculate that all right so we come down here what's the first thing we do we read light valve so light val is equal to analog breed of what of light pen so we're gonna read that value that is coming off of this sucker okay no is it light valve that I want no I want delay time so I have to calculate delay time so I will call it delay time is equal to what open parenthesis nine point divided by 550 point to make sure that we are doing floating point math you've got to put those points in there and then times what flight Val that I just read and then I need to subtract again use floating point math nine point times 200 point divided by 550 point all right and now I need to add one point like that all right now what do I do with this delay time well that is sets the tone for the Buzzard that will set the tone for the buzzer and remember the buzzer is on buzz pins so now to buzz what do I do I want to do digit toll write digital right buzz pin hi how long do I leave it hi well I leave it high for delay time now what do I want to do I want to turn it off did you toll oh man I forgot my semicolon up here did not were you guys yelling at me don't forget your semicolons okay digital bright buzz pin low and then what delay by D play time so that's going to make it buzz and the noise that it makes buzzing depends on this delay time and so it's the light value chain is this vet red-light val is I changed the brightness of the lights light val is going to change and then that's gonna calculate once I have that light Val it's going to calculate delay time and then it's gonna buzz at that delay time so when I download this with a little luck we should hear something we should hear a tone okay better get a strong shot of coffee on this one okay I need you guys to hold your breath it downloaded but I'm getting no noise I'm getting no noise oh this is not plugged in good alright what did I do wrong buzz pin is what buzz pin is eight that is very good buzz pin is eight all right in a case like this I am hooked up I do believe let me make sure that this is you know that is plugged in so what am i checking making sure my buzzer is plugged in my blood buzzer is plugged in it is plugged into pin eight and I do have a ground so all of that is very very good have you got seen where my problem is so the only thing I am really having let's print delay time and see if it makes sense so here we're going to go serial dot print bail in and then we are going to print the delay time like that and so let's just see guys really I did not do I did not mess this one up on purpose but when something goes wrong I want you to learn how to debug and so this is debugging I'm going to put that down that came down I need to find a different view where you can actually see the serial monitor hey let's see I'm kind of gotten things locked up here a little bit but let's see let me see if I can get that serial monitor okay there is the serial monitor is it printing anything it is not printing anything serial print line delay time 96 I didn't set up I didn't turn on the serial monitor serial dot begin 9600 alright so now at least I should be able to print out that delay time and see okay looks like that downloaded so I am going to come over here and I am not getting good values here so let's see what is wrong okay if if you're gonna play that way let's just come in and just flat-out do light valve because that should certainly work right because I'm just reading light valve and printing light bow so let's try that okay that looks happy open up the serial monitor so you see I am NOT reading off of here it is like the program is not running my baud rate is 9600 let's make sure that I can see the let's see if I can get a better view for you guys okay you see nothing's happening on the serial monitor so what I need to do is I need to try to understand I'm just printing out light valve it's like the program is not running com3 still looks happy I want to you know I'm gonna download this I'm just not sure pinmode pinmode how this thing could be hanging it should I do a lot Val as an analogue read calculate this and delay time I wonder if like delay time is some crazy wrong number so I'm gonna comment out this part of the code because if delay time is a crazy wrong number then it could just get be getting hung there so you can I'm going to switch to an even better view I think that you can see let's see if that one will I need to get the one that shows you the screen this one okay so I'm going to come up here and I'm going to comment out so I'm going to comment out that region so it takes those lines of codes out and now when i download it hopefully i will see a value when I call up the serial monitor okay yeah so that is reading that is reading that is reading but the values don't make any sense for light Val so let me see I have this whole thing hooked up into that center I do have five volts coming in I go across five K did that make a difference no that plugged in it is as if my circuit is not connected and then did I do a zero on the pin light pin a zero isn't ah why weren't you guys yelling at me I made a zero of what output that is an input I did not do that one on per and so let's change that and see what happens okay now if I come over to this view you can see that we are getting good numbers from the serial monitor okay so I don't I didn't do that one on purpose and I didn't mean to waste your time but do you see how I go through the debugging process first thing I tried to see was was it a circuit problem then I'd start looking at the code and then when I look at the code I simplify the code by taking off these things alright by taking off these things and now let's go back and let's print out let's print out delay time and let's see if those makes sense for delay times delay time should be between 1 and 10 right because that's what we were trying to calculate so that's 1 and as I get dark it goes up yeah so this is working like we would expect so now what do we need to do we need to come back in and we need to put these in what is that nonsense somebody is trying to call me but I am busy so I will not take the call so we will say uncomment all right we're gonna uncomment that region and I have got to make that blasted thing stop all right so now I'm going to put this over here so we can listen in and now I'm going to download it do you hear that I hope you can hear it it is working perfectly okay let me go turn the lights off okay you keep listening are you guys able to pick that up very nice boom we have an audible signal that is smoothly that is smoothly switching as the light goes from full off to full on alright guys we really you know III feel silly when I make a mistake like that but the thing is you will believe me you will make mistakes when you code and the thing that you've got to do is you've got to learn how to troubleshoot you've got to learn how to debug so let's review maybe the most important thing that you learn today is well first of all math works this worked that calculating that delay value based on the light value allowed us to control this smoothly the math works but you can't do this unless you do the math and so whenever you have a problem like this I want you to start thinking about lines the slope of a line is y2 minus y1 over x2 minus x1 but your y's and x's depend on real values you know your X's is something you measure and then your Y is something that you're trying to calculate you calculate the slope then Y minus y1 is M onto X minus x1 you come up with the equation of the line and then you can get this magical performance in your programs then when this thing didn't work the first thing that I thought is well I could have hooked to a wrong pen I could have had a loose connection so I fumbled around with the circuit but it seemed like the circuit was right then I came over and I looked at the code and I thought well it would be very easy to have a mistake in this equation so I want to just print out that value to see if it's the anticipated values turned out it was the anticipated value but I wasn't getting a buzz why was I not getting a buzz I was not getting a buzz because I had set this up wrong I'd done the pen mode as an output instead of an input so all the numbers that I were reading were in fact very wrong ok guys man put comments down below did you do this without my help or when you followed it along did you see the mistake I made were you screaming at the screen about the mistake I made when you did it to yourself did you have similar mistakes you know share let's get a little bit of discussion a little bit of dialogue going on these things okay this is palma quarter from top tech boy calm I will talk to you
Info
Channel: Paul McWhorter
Views: 58,422
Rating: undefined out of 5
Keywords: Arduino, Buzzer, Photoresistor, STEM
Id: 42ahyWUkTiU
Channel Id: undefined
Length: 31min 38sec (1898 seconds)
Published: Tue Sep 03 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.