Raspberry Pi LESSON 27: Understanding and Using Active and Passive Buzzers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is paul mcquarter with top tech boy.com and we're here today with episode number 27. in our incredible new tutorial series where you're teaching your raspberry pi boss what i'm going to need you to do is pour yourself a nice tall glass of ice cold coffee that is straight up black coffee poured over ice no sugar no sweeteners none needed and as you're pouring your coffee as always i want to give a shout out to our friends over at sun founder sun founder is actually sponsoring this most excellent series of lessons and in this class we will be using the sun founder raphael raspberry pie kit the sun founder raphael raspberry pie kit most of you guys probably already have your kit if you don't look down in the description there is a link over to amazon and you can pick the kit up and believe me your life and my life will be easier if we're using identical hardware sun founder putting the tech back in top tech boy.com but enough of this shameless advertising let's jump in and talk about what i am going to teach you today and what i'm going to teach you is i'm going to teach you how to add sound to your raspberry pi projects and the easily easiest way to do that is with buzzers alright now there are two different types of buzzers there are active buzzers and there are passive buzzers okay so let me move over here and kind of show you our overhead view of our raspberry pi let me get out of your way and what you can see is there are two different types of buzzers and you have two each in the kit now they pretty much look identical okay they pretty much look identical but let me tell you how you can tell the active from the passive buzzer the active buzzer has this little tape this little piece of tape over the top of it and the passive buzzer does not have the piece of tape over it they are almost identical other than that but what you can see is we're going to take that piece of tape off when we start using it but if you look at the underside of the buzzers the active buzzer is sealed and the passive buzzer is open you can see the little circuit board on the passive buzzer so that's how you can take them apart tell them apart after you take the tape off of it another way that you can take tell the difference is well let me tell you kind of functionally how the active and passive buzzers are different the active buzzer is very easy to use if you put 3.3 volts on it it buzzes if you take take the 3.3 volts off it turns off so an active buzzer is very easy to use it is on or it is off and so the easiest way to incorporate a buzzer into your project is with the active buzzer the downside of the active buzzer is it's either on or off and it makes the same sound either way the passive buzzer you can control the tone of it all right so that's the advantage of the passive buzzer the disadvantage of the passive buzzer is you can't just go in and turn it on you have to use pwm and you have to set a frequency and that frequency of your pwm will control the tone so if you just want something that beeps your best bet is the active buzzer if you want something that you can control the tone of the pitch or the tone then you want to use the passive buzzer i'm going to show you how to use both of them today and we're going to be hooking up the first one the active buzzer which is the easiest and then we'll switch over and we will use the passive buzzer all right now what i should tell you is is that a little bit there's another very important component that we are going to be using uh the other things that you're going to need in the project you're going to need the raspberry pi you're going to need the 40 pin the 40 wire ribbon cable and then you're going to need the sun founder breakout board okay you are also going to need you're also going to need this p-type transistor this p-type transistor and i should probably say a little bit about why we need the transistor and say a little bit about how a transistor works so i'm going to come over here and bring up a schematic on top of what we're doing here okay and the transistor so you can see what we have is we we're coming off of gpio pin 17 we have a 1k resistor and then it goes into the transistor you can see the transistor has three legs we are coming into what is called the base of the transistor the leg with the little arrow is called the emitter of the transistor and the leg that doesn't have the arrow on it that is the collector so you've got base emitter collector what a transistor does is it takes a small current and it amplifies that current by 10 50 100. so a transistor is an amplifier now why do we need a transistor really those gpio pins on the raspberry pi are not to power something they're to control something now yes we did power the leds by turning them on and off but the leds are very low current devices you can very easily burn out a channel burn out a gpio pin on the raspberry pi if you try to draw too much current and so if you hooked a motor up to pin 17 and then tried to run that motor you're very likely to burn out that gpio pin if not completely fry your pie and we don't want to do that so how do we do it we get a control signal from the gpio pin we amplify that control signal with the transistor and then the output device the motor or in this case the buzzer is controlled through the transistor its current is going through the transistor not coming out through the not coming out through the uh [Music] gpio pin and so if you look at this we have a 1k resistor and then the voltage drop from the base to the collector is very very small okay and so what current would you have you would have something like 3.3 volts which is what the gpio pin is divided by 1000 ohms because we're using a 1k resistor so you're only drawing three milliamps you're only drawing three milliamps from the gpio pin but that three milliamps is amplified and the current going through the buzzer will be something you know much much larger than that like 300 milliamps or 30 milliamps now if you were really driving something big like a motor you would not hit hook the 3.3 volts up to the uh up to the power supply of the raspberry pi but for a small motor or a buzzer you can actually run it off the 3.3 volt supply but this same concept if you have something bigger like a bigger buzzer or a motor you could contr you could power it through not the 3.3 volts on the uh on the raspberry pi but you could you could power it by an external power supply if you use an external power supply know that you have to hook all the grounds up so the grounds of the power supply have to be hooked up to the ground of the raspberry pi so in a circuit all your grounds have to be hooked together okay but in this since we're using a small buzzer we will be powering it off the 3.3 volts on the raspberry pi the 3.3 volt supply understand that those gpio pins really should only have a very small amount of current but the 3.3 volt supply the 3.3 volt supply which we are getting right here the 3.3 volt let me turn that uh turn this back on the 3.3 volt output is a larger current device so we are going to be getting our power from 3.3 volts we are going to be controlling the device through the gpio pin 17. small current from there the real power to run the buzzer is going to be coming from the 3.3 volts this is very very very important because you don't want to burn out your pi so i need you to go ahead and hook up your breakout board you can see i've got a couple of wires in here to help me with the build but but basically we're using pin 17 to provide the control signal and then what we're going to actually try to be hooking up is this circuit here and this is just a physical implementation a physical realization of the circuit schematic that we have over here does that make sense i hope it makes sense so what i'm going to do is i'm going to switch over here to the board build and now we're going to start trying to build this a couple of things that i want you to see i want you to see here in my circuit i have already powered up the 3.3 volt rail from the 3.3 volt pin on the raspberry pi it comes all the way across the second to the bottom row and then i jumper it up to the top row so that the top row is also 3.3 volts then i've got the second row the ground rail i've got it connected to ground on the breakout board so this whole row the second row is ground i jumper that down to the bottom row and so the bottom row is ground as well and so what that means is the row that's labeled plus all along that whole row i've got 3.3 volts this one that's labeled plus all along that row is going to be 3.3 volts this is going to be ground and this is going to be ground so we've got that set up and now we're going to try to put together this circuit here and remember that is a 1k resistor and i got the 1k resistor out of the sun founder kit now we're going to start with the active buzzer but i think i think we'll start with the transistor and this transistor is in your little goody bag in the raspberry pi kit but make note there are two different types of this little transistor device there are two different types and it says what type it is on the flat face it is it is almost impossible to read so i use this magnifier and i can see that this is the s8550 d331 so you've got to make sure that you get the right one and let me warn you that other one has almost identical markings i think it's an s 505 or something like that but you've got to make sure that you have the s 8 5 5 0 and then it says d 331 now when you put this in your circuit down here what you want is you want the flat side pointed towards you the flat side is pointed towards you then the left leg is going to go to the buzzer as you see up up here the left leg is going to go to the buzzer the middle leg is going to come through a 1k resistor down to your gpio pin 17 and then the right leg you're gonna ground okay the right leg you are going to ground if that makes sense so i'm going to come in here and i'm going to go ahead and hook it up you can see that i have a i have a leg that is already going up to ground i have a leg that is already going up to ground and so that is going to be my right leg is going to be the one that goes up to ground so i am putting my right leg my right leg to this little yellow wire that hooks to ground now the middle wire the middle wire the middle leg needs to come down to the gpio pin 17 output through our 1k resistor so i've got the 1k resistor i connect it to the middle leg of the transistor and then i put the other leg of the resistor down there connected to pin 17 the wire that goes to pin 17. now what you can see is the top the left leg the left leg of the transistor is going to come over and then i need to put that on the minus i need to put that on the minus side of the active buzzer now what you can see is on the active buzzer one leg is labeled plus okay one leg is labeled plus what is the negative leg one label one leg is labeled plus what is the other leg the other leg is the minus leg and the minus leg the shorter leg needs to come over here to the transistor so i need to put the negative leg here and the positive leg here because you can see this wire here is taking me up to the positive rail so i'm going to pause a second to just let you get that and i hooked mine up a it's the same schematic but i grabbed some of the things off the top rail instead of jumping over to the bottom rail so i am going to put the positive leg to the brown wire that goes up to my 3.3 volts and i'm going to put my short leg my negative leg into the wire that goes back to the transistor and so get that hooked up be kind of careful with it and then also make sure this resistor is in good because it's kind of hard to get a good connection so at this point i think we have it hooked up and i think we're ready to come over to the raspberry pi and i think we are ready to start coding and so basically this is just really simple we're going to put an output signal to gpio17 and then the transistor is going to amplify that current and then we're going to have an audible beep we're going to have an audible beep coming out of our active buzzer all right so let's switch over to our code view and it will take me just a second here switch over to our code view and then what i need you to do is go ahead and fire up phony so you come up to programming and you come down to phony and then that thing pops up with an untitled i'm going to go ahead and save this i'm going to save it as passive the passive or this is the active this is the active beep activebeat.pie activebeat.py boom now we're ready to begin programming well we're going to be using a gpio pin so we're going to need to import rp rp little i dot gpio okay as gpio now we need to import time because we're going to need some delays in there now we're going to need to define our buzz pin what is our buzz pin it is pin 17 in the bcm numbering system we need to tell it that we're going to use the bcm numbering system by doing a gpio.set mode and we're going to set it to gpio dot bcm because the breakout board on the sun founder kit in the sun founder kit uses the bcm numbering system so now it knows what numbering system we're going to do we need to go ahead and set up that pin so we're going to say gpio dot set up and then what are we going to set up we're going to set up buzz pin and then what is it going to be a gpio dot out because we are sending a signal to it now we're ready to go so i'm going to set up my try and accept and then i will put here my my loop while true when is true true true is always true so this will create an infinite loop that will keep looping until it sees the keyboard interrupt the control c and then it will jump down to accept and then what do we want to accept we want to accept on keyboard keyboard interrupt like that and then what do we want to do on interrupt we want to do a p i o dot clean up to clean up our pins and then we want to do a print to make sure that we know that it got down here and we're going to say gpio good to go and that just tells us that we've cleaned up our pins that we actually got down there successfully now we need to come up and write the actual program which will be in this loop and this is very simple i'm just going to say gpio.output and then what do i want to output to the buzz pin and then what do i want to output gpio dot hi like that and then i want to let's just say that we'll leave it on for one second so time dot sleep of one okay now after we buzz we then want a gpio dot output where do we want to output buzz pin what do we want to do we want to take it low now gpio dot low low like that and then we'll leave it off for a second as well [Music] could it really be that easy do you think that perhaps i could have made a mistake in here somewhere i hope not we're going to try to run this so please hold your breath oh good to go i didn't put my closing quote okay the real problem is someone out there didn't hold their breath you know who you are please hold your breath this time set mode man did i get sloppy here all right hold your breath boom giddy up can you guys hear that it is almost an ear piercing alarm so you could really set an alarm like this okay we'll control c out of that you can play around with the delay time that you want we could put a instead of a 1 we could put a 0.1 like that 0.1 okay and now let's run it i hope it's picking it up on the microphone it is quite loud okay so do you see how easy it is to add an active buzzer you just turn it on and off and then you are good to go but what we really want is we want to also look at what if we wanted a fancier alarm like something more like a siren okay well what we would do is we would take our active buzzer out i'm going to put the lid back on it i'm going to put the little i'm going to put the little cover back on it i'm going to move it out of the way and now i'm going to get the passive buzzer same thing on the passive both sir the the positive leg is is uh the positive leg is shown here in this case my legs are the same size but you can see on it the positive is going to go to the brown wire and then the negative the other leg is going to go to the yellow wire so now i have that in there let's just see what happens if we try to run a pass let's see what happens if we try to run a passive buzzer with just a dc signal nothing maybe you can hear a slight clicking but there's no buzzing action happening why because for this we are going to need i'll ctrl c out of that for this what we are going to need is we're going to need to do pwm so let's come back over to our code view let's come back over to our code view and now i'm going to change this i'm just going to save it as because a lot of this is going to be very similar i'm going to save this as passive beat passive beat like that okay now we're going to modify this code for a passive buzzer well still we want to still we want to import rp gpio we still want time we still want pin 17 we still want the bcm numbering system but this time in the setup we're going to go ahead we're going to set it up as an out that's correct but what we're also going to have to do is we're going to have to create a pwm object and i'm going to call my pwm object buzz and then that is going to be equal to gpio.pwm [Music] where are we going to do it on the buzz pin like that and we have to set an initial frequency i'll set a frequency of 400 that should be good and now i want to go ahead and start the buzzer and i'll just say buzz dot start and i'm going to start it at a duty cycle of 50 percent all right now if we just ran this and didn't have anything else in the in the code what it would do is you would have a 440 hertz signal audible tone with a 50 duty cycle now in the other times when we were doing pwm we were changing the what the duty cycle we're not going to change the duty cycle because what does your ear hear your ear hears frequency what are we going to need to change we're going to need to change the frequency but let's see if i could just go ahead and what i'm going to do here let's just see if we can buzz it okay let's just see if we can let's just see if we can buzz it and so that should already be uh okay what what i would do down up here i'm gonna go ahead and change this back to one second in one second like we had it and then what i'm gonna do is i'm gonna say gpio dot change [Music] frequency and then what am i going to change the frequency of i'm going to i don't have to tell it what i'm going to change the frequency of because buzz yeah it's not gpio i'm going to take buzz and i'm going to change the frequency and i'm going to change it from what let's say i change it from 400 to 100 okay i'm going to leave it there for one second and then i am going to come here and i am going to buzz change frequency and then i'm going to change it back to 400 [Music] okay and now i'll leave it there for a second so i should hear a hundred hertz signal a 400 hertz signal 100 400 so it should be sort of almost a little bit of a siren type effect do you think this will work let's see if we can make this thing work [Music] what did i do wrong [Music] boy i've really got it messed up now that sure looks right i set the frequency to 400 in a 50 duty cycle okay let's completely kill out of that let's try it again okay let's control c you know what i think is i think that maybe i didn't completely for some reason release that gpio pin or it doesn't seem to want to go from a standard output it doesn't seem to want to become a it doesn't seem to want to become a pwm pin let's see if i just kill thony and come back and start it again unexpected result but we learn from our mistakes so let's come bring it back up okay and now let's see if we run it okay it's still thinking that thing is already in use keyboard interrupt except keyboard interrupt that looks good gpio cleanup [Music] it's just not exiting out properly i probably have an error and you guys probably see it but i'm going to go ahead and i'm going to reboot the pie [Music] if all else fails sometimes you just got to reboot and let me come here and [Music] make sure that i'm on the right monitor give it a second to reboot the gpio pins sometimes they're a little bit unpredictable so let's go back to thony and hopefully it'll pop up in passive buzz okay it's on passive beep and i'm just going to look at my code again change ah frequency that was the problem change frequency all right let's try this again okay that all looks good let's run it can you guys hear that what do we like about it we have control of the tone we have control of the tone what i notice is it's distinctly it is distinctly less loud but it is nice that you're getting kind of a siren type effect it's nice that you're getting a siren type effect so let me see if i can call something else back up here i had some notes that i lost on the reboot and let's see if we can control c out of that yeah okay we controlled c out of it nicely that time trying to get my notes back if i don't have my notes i'll just wing it let me move that out of the way and uh i think i might have my notes back i just can do a little bit better job if i have my notes all right so what i'm going to do is instead of just an on off thing let's see if we could make something that's a little bit more of a siren type effect so what would i do i would say for for i in range and let's go from let's just try let's go from 50 to 1000 like that okay and then what i'm going to do is i'm going to do a buzz dot change frequency like that and i'm going to change it to what to i alright and then i'm going to let's go ahead and do a time dot sleep of let's say like point oh we can come back and play with these numbers depending on what it sounds like now that's going to go like that and now we're going to say for i for i in range and then we're going to go from a thousand back to 50 and there we will need to go in steps of minus one i think i gotta tell it and then don't forget your colon all right and now we're going to say buzz dot change frequency to i [Music] and then we're going to do a time dot sleep of 0.001 all right now it should go up and down it might go too fast it might not go over enough of a range but let's see if we can get sort of a siren effect here [Music] hopefully that's picking up all right boom we got that working okay what if we go from 50 to 2000 and then from 2000 to 50 like that let's see what that [Music] does okay [Music] i don't like how low it's going though and so i'm going to ctrl c and let's go maybe from 150 to 2000 and from 2000 back to 150 and then let's see what that does that's a nice siren effect we made a siren okay so you see we could uh [Music] let's see if we can make it go faster i like to just play around with it i'm hoping you guys are picking it up i'm gonna make it go a lot faster okay so we made ourselves a siren all right guys we learned some important stuff today and what we learned was what we've learned was and don't go away yet i'm going to give you your homework but what we have learned is it's hard to find my mouse sometimes there it is all right what we have learned is we have learned how to use an active buzzer we've learned how to use a passive buzzer we've learned how to control the tone on the passive buzzer and then we have also learned the very important concept of use looks like we got a fire back there i typically am not too paranoid but i do live in a house with a grassroots so when you start seeing smoke you get a little bit concerned just saying the other thing that we learned is we learned the very important concept of don't use a gpio pin for power you use a gpio pin for control you power a device from an external power supply or if it's not too big you use the 3.3 volt or the 5 volt on the on the raspberry pi but don't use a gpio pin to power things all right what is the assignment the assignment is going to use this and the things that we learned earlier and what you're going to be doing is you're going to be making a temperature sensor okay you're going to be making a temperature sensor i want an lcd display and i want a temperature sensor and i want the buzzer okay now what i want is i want an alarm to go off under a condition of let's say what if you got to 35 degrees f you could have an alarm you could have a freeze alarm that you know something's happened and your pipes are going to freeze something's going to freeze because the temperature is getting down towards freezing okay or you could say if it's above a certain temperature you could give an alarm that you are overheating now you can choose whether you're giving the alarm on the upside or you're giving the alarm on the down downside but it needs to be something when the temperature crosses a certain level you're going to alarm now what i will tell you is i'm going to tell you that you need to have the output on an lcd display and then i want one other thing i don't want you to hardwire into the program i don't want you to hardwire into the program what that trip point is i want that to be set by a potentiometer so you're going to have a potentiometer and that is going to set the set point at which you alarm what does that mean your project has to have two modes program mode where you set the trip temperature and then monitor mode where it's sitting and waiting to alarm how do you go from program mode to monitor mode with a push button so you're going to have a push button to set the mode you're going to have a potentiometer a potentiometer to create the set point when you are in program mode and then you're going to have a display and when you're in monitor mode it's going to be showing you what the temperature is so you could sit there you could press the button you could set your trip point you could press the button again and then you're going to go to monitor mode and then you could sit and watch the temperature on your uh on your display and then when it gets above your trip point then you're going to sound the alarm all right that's a little bit of an involved project but what you got to do is you got to be practicing these skills you got to be using these tools that we have developed in our earlier lessons and so this is going to put together this project is going to in this homework assignment is going to put together a whole lot of the stuff that you've learned in the earlier lessons what i need you to do then is post your solution to youtube okay and then in the description of your homework solution leave a link back to this lesson and then down below in the comments put a link over your homework solution and i do indeed look at every single homework solution that you post also look in the comments find someone else's solution see how different people did things comment on the other people's solutions so you know we want to continue to try to develop a little bit of a community on this channel guys i hope you're having as much fun taking these lessons as i am making them if you like the video make sure you give it a thumbs up subscribe to the channel if you haven't leave comments down below all that stuff helps me with the old youtube juice and the more youtube juice i get the more people that will get to see this video and that's important because the world needs more people doing engineering and fewer people sitting around watching silly cat videos paul mcquarter with toptechboy.com i will talk to you guys later
Info
Channel: Paul McWhorter
Views: 6,143
Rating: undefined out of 5
Keywords:
Id: 4CquOUVMvJc
Channel Id: undefined
Length: 38min 37sec (2317 seconds)
Published: Thu Sep 15 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.