#67 Vibration and other switches for your Arduino

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
and welcome back now today we're going to be talking about some vibratory things on the Arduino nothing to do with buzzers or anything like that this is all about controlling the Arduino without actually having any on/off switches or at least no switches to press that might cancel an action now as you can see on the workbench that moment we've got this rather nice little LED flashing away this is one of the Christmas LEDs by the way from that video from anyone's ago this one here and it's been controlled by this little thing here now for those of you long enough in the tooth to remember such devices this is what's known as a mercury switch you can see it's all glass tube with a little tiny bit of mercury in it now oh I was convinced that these were sort of banned a little while ago well several years ago in fact because what the problem was that this mercury of course would eventually find its way into the environment but apparently no they're still around so this is some a very simple switch in fact I had one of these in an alarm clock but I built when I was a teenager for the snooze button so you imagine it was probably about that angle imagine the LED here represents like the alarm buzzing away and you'd pick up the clock and just tilt it like that and as you can see the LEDs gone off and then you set it back again and there was a 10 minute delay I think before it went off again and you just tilt it again being a teenager of course alarm clocks a who needs them so that was a very simple switch and very easy to use and of course you didn't need to actually press a snooze button on top of the clock which is obviously difficult to find when you're half asleep and all that so having a little mercury switch like this Eco concerns notwithstanding is a very easy thing to do now I have actually measured the resistance across there but the multimeter and it's about 3 ohms when that little blob you love mercury well I'm assuming it's mercury maybe they've recorded mercury or something else on sort of odd I don't know but I've measured the resistance in to about three ohms on that that little bit of mercury or whatever has done down the bottom there and I'd like to actually get into the macro mode just so you can really really see well that's the bow but I'll try that in just a sec so basically this little piece of mercury ball whoops now we've pulled out the socket just floats backwards and forwards as you tilt it and it just makes contact with well one wire and then there's a shorter wire down the bottoms that's where it makes contact and as you can see the Arduino sketch here simply turns this off when it detects that there's a short circuit here I say short circuit three ohms that's close enough for an Arduino very simple it is - now on my coffee cup coaster video I didn't use one of these I used something else so let me show you that instead but first of all I'm gonna try and get a real close up of this if my video camera allows it so let's have a go there we are now that's a pretty good shot actually is nearly you can see that little ball of mercury will call it mercury I think it probably is I don't know what else would would be liquid at room temperature and you can see very clearly there the short little tiny lead it sort of cropped at the base and then the longer lead on which of the glob of mercury is now resting look and that just floats down touches and makes the circuit which is why the lamp on the back here has now gone off as soon as it rolls off the LED comes back on again so that's that's all very simplistic isn't it so let's see what alternative there might be for this and here's the alternative now this looks like a capacitor the in fact isn't I don't think we can get close enough on the right thing if I don't think even describes what it is it's basically a tilt switch so inside here there are two wires much like that mercury switch and a ball bearing a set of a ball of mercury and as that runs down inside it breaks the circuit so once again the LEDs on and when the ball runs back and makes that soaked in just the same way as that Merc Club as you can see this goes off their foot tap that like you notice the LED does flash occasionally which means that the ball is only just on there and I'm thinking maybe there might be a little bit of problem to switch bounce with this as well because you know there's a bit of vibrations there look and of course this video is all about vibrations and touch and well detection of such things so I'm thinking if you were to use one of these you probably have to be careful about switch bounce in the same way as you would do with a standard switch and just in case you don't know I've described it elsewhere but when it when a switch makes contact you know a standard on/off switch we humans tend to think that the switch simply comes down snap and makes contact that's not happened if you were to do it in ultra ultra slow motion any switch would come down bounce go up bounce come up bounds like that and then finally settle now even though it might only take a nanosecond or you have a few microseconds to bounce like that as it does it that's more than slow enough for the Arduino to detect it so you'll tend to get multiple pulses coming down the wire and if your code doesn't deal with that well you're doing I think you've made the switch broken the switch made this much broken switch and your logic could be totally flawed in much the same way as what's happening here at the moment if we consider now that this is now tilted the lights off but if I just tap this like you say so you really wouldn't want that to happen and you however as a as a device did tip to detect whether somebody is touching something of yours whether it's an alarm clock so you could tilt it from the alarm clock of course then have it get back again or whether it was some kind of security device where you had something on show over her say this item here was on the show but you didn't want people fiddling with it well if you had it like that as soon as somebody touched this even slightly assuming this was all built up into a case of what as soon as they touched it like and that could set off an alarm you know first stage alarm just a little tiny alarm to begin with it soon as it went like that set off a full alarm who knows multiple choices for you there so that's one way of detecting sort of touch and vibrations and stuff and there's okay but of course if only there were a better way yeah you've guessed it there is a better way and there's a a bespoke vibration detector so let's have a look at that now on a different board and we'll look at the arduino ide as well a new feature that appeared in 1.66 though I haven't used very much there's a little bit it's a little bit basic but it's useful when we come to look at this sort of thing right let's do that next and here we have it this little device here is in fact a bespoke vibration detector no it's app it's a detector and just the same ways that you can generate a voltage square wave voltage and send it to a pizzo transducer don't make a sound well similarly if this receives vibrations it will generate a voltage back up the two wires and you can detect them now let me just unplug this they say you have to be a little bit careful about soldering this I'll take it out yeah that's what it looks like on these two little pegs and as you can see from the scale compared with my chubby fat fingers you can see that it's pretty small actually so this is the pits a bit on here and this bit on the end it's some this brass thing it's not some kind of antimatter dolly thin crystal realignment it's just a bit of brass thing on the end actually it's all it does is act like a weight so that as this starts vibrating the weight keeps it vibrating so you can detect it that's all there is to that so let me just pull this back and we'll see how this hangs together right so here we have an example sketch which is simplicity itself let's just see it in action first if I tap this board keep your eyes on the debug window or serial monitor window over yeah they are so as I as I tap you this stream of numbers coming up because that's what it's detecting and as you can see the little LED at the back there the green one it just sort of flickers and then off again and if I really whack it you see the red one is now stuck on until it clears okay so this is detecting something in the fight if I tap around the board's on this cutting mat you can see the numbers are still scrolling up quite happily so it's quite sensitive and if I do nothing it's well pretty stable actually doesn't suddenly spring into life now when you connect this device to the Arduino analog input is what we've got down here you do need to put a hundred K resistor across the terminals there is talk on the internet might be in the four of us or this I don't know about one mega ohm you can't do that on an analog input it's just not low enough you'll get all sorts of spurious noise and stuff I mean these these wires here alone as I'm touching it you see the numbers scrolling up on the debug window they will pick up enough noise as it is so I think a hundred K is what you need obviously if the hundred K was was up this end more then it probably wouldn't pick up the noise as I touch these wires and Lord forbid that you ever actually I have any pmw was floating around like this because it will pick them up in an instant but 100 K with this sensor is absolutely fine that really does work so anyway let's just leave that for a minute and talk through the code because there's a couple of little things in here that are worth looking at right so we've defined our piece a pin on a1 just analog input one got a couple of LED pins okay defined them as output well come on to this bit in just a while just ignore that for now and what we're saying here is in go and read the PSO pin and if it's greater than zero go and display it I've kept it at 50 though so if the Pizzo is greater than 50 then make it 50 others will take the absolute value for the piezo vibrator down here now absolute value being that we don't really care about negative values in here so we're just ignoring those we're just well no that's not true we're not ignoring them or turning them into positive values so if by some chance you were to get a minus one bolt generated on here we'd actually turn it into a plus one volt because it just makes your whole coding a little bit is isn't it if you're talking about positive values and then we're saying light up the green LED which I've called LED pin low low tapping low low effort whatever you want to call it and just basically flip it if it's if it's not if it's not already on make it on and if it is on turn it off us well this is not digital read LED pin low this is a shorthand way of doing it really you could have said if not on then make it on else turn it off but there we are now I'm saying if it's more than ten the value that we're reading then do exactly the same for the red one and that is all your code okay so and that works that works well enough I mean a little tiny taps make the green one go on and off and if it'll work it well actually that the ready source stuck now yeah because what it actually does is turn it on and then turns it off the next time around it goes over that number but because that's we've already discussed the whole point about this particular device is that it's it's got a weight on the act on the interact as a pendulum and keep it moving so it will go through this loop several times a second perhaps even hundred times a second depends how quickly it can display this to the the peg window frankly that's a bit that's gonna be slowing it down so what else have we done up here what what is this analog reference X term what's all that all about well under normal circumstances your peripheral that you're going to plug into an analog port or analog pin is going to be generating voltage between 0 and 5 volts that's standard sometimes it's between zero and three point three but normally not to five volts and that's what you a neural port can handle but what if as in this case the voltage being generated by this is so tiny that five volts just doesn't come into any order near to it yes all right you can get pulses and spikes of potential up to 20 volts above us actually ping this yeah actually touch this and ping it yeah I'm sure you could get a 20 volt pulse on there not that we want therefore the Arduino of course hence the hundred K resistors to sort of show that our pin reduce it back down but we're not really talking about 20 volts and we're not even talking about 5 volts we're probably talking about in normal course events as we're tapping in like this you know half a volt maybe if you're lucky short-lived as well so what we're saying is we want to change the reference by which the analog pins measure things and that's where this analog reference comes in now by default this word in the middle here would say the word default in other words just do what you would normally do and do nothing special but of course we don't ever write that in normally why would we bother is is already doing it on this occasion though we're saying I'm going to give you an external voltage which I want you to mark as the 102 3 value remember there's a thousand 24 value starting at 0 and we're saying I'm giving you a voltage whatever that voltages that is the top of the range of your thousand and twenty four steps so what we've done here is supply 3.3 volts in what one of these cables will go through in a minute and send a voltage back to the Arduino a ref back here to bring all that damn Mar something a bit more manageable sisters have a a quick look at that and measure that voltage right let's put the EEM let's put the multimeter on that then and see what we've got and then we'll talk about how we did it well this is squeeze the multimeter in here there we are we just about see that current we right so what we want to see is what voltage are we supplying at the back here I'll just tie myself off cuz I can't otherwise get my head around the camera right so on that pin there which is the capacitor and that one there now you just see their lot on the most meter at one point five six nine so one and a half volts basically so what we're supplying here on this gray cable right from back here is three points free so we've already dropped it down to three point three from the five volts that makes it a bit more sensitive and then we're putting it through a one mega ohm resistor here to a capacitor to ground to short out any noise and stuff like that just made us absolutely as good as we can get it and the white lead here goes all the way back up to the top to the a ref pin as I say which is which is rarely used not just come into that so there's at the top there like you can just see it says a ref okay so that's where we're putting in the external voltage now be careful when you do this don't leave use the default and then stick a voltage on that I think it likes that too much and there's some warnings on the Arduino website about what you can and what you can't do in case you're gonna dump the entire 5 volts down to ground or something so just just read that instruction but as long as you've got it as xmarks as external in the codes we have here and then you can put your whatever voltage you want on there but not more than 5 compact 10 or 12 or anything like that no it's got to be 5 down to zero and no less than zero either okay it won't like that but it's it gives you a much more more granularity and measuring stuff because if we are measuring down here tops of a volt or at least most of the time you know we were just tapping that it makes sense to give the hey ref here a volt and say or volt and a half and say that's what I'm expecting now there is an alternative to using this external I've tried it it didn't work as well if you were to put internal that's not the same as saying default they vault is 5 volts and that's that's what you always get by by default if you said internal then that would mean one point one volt in fact let's have a look at the web page from the Arduino website itself that explains this in a bit more detail right so this is the page it's of interest and a log reference it says as we've got in our code and it says what kept the default is is well the default obviously and we don't need to do anything on that that's a default 5 volts now internal which we might have used is equal to one point one but I tell you now I've tried it one point one is actually too low we agree as far too sensitive you start getting the noise the picked up and and that's not available on the mega anyway the mega has got 1 V 1 and 2 V 5 6 to 2.5 6 volts but we're not using a mega here and external is what we're using here I look it does say in bold they're not to 5 volts only and there's a bit more information oh yes another bits on the morning look say all that read all that just take care where what you're doing but we're using an external voltage because we know that our input voltage that we're measuring is much lower than the 5 volts that stand up to give us so by only supplying of 1.5 volts on the a ref pin now we can make this nice and sensitive otherwise it would barely register cool so far so good now when we were talking about the tilt switch earlier on the beginning of this video we said that you know if you tap it it would act a bit like a vibrations vision and that is by design I don't know what the little ball inside is made of but it's designed to be a particularly light so that when you touch it like that you know wacky on the end very gently on my hand it does bounce up and down and create that sort of tilt mechanism but it is only on/off you can't really measure intensity as such like you can with this one whereas the mercury ball of course is is most definitely either on or off so you we've gone through a variation there Emily mercury is either on or off digital on or off this one could be connected to an analog input and potentially could detect more than just on or off okay but this one definitely has to be connected to the analog side so that's the difference between the three now I've absolutely no idea where I got this I've had it for quite a long time for project that was gonna do my car but it was the car before I've got now is it you show you how long ago that was to act as a vibration detector when the car was moving along but I never used it so I don't know where I got it from you could probably get this still on Chinese websites and things well have a quick look and see what you find I know there's a new sort of vibration detector out as well which might be worth just having a look at look up on the Internet so let's do that now right just typed in that search term on Google vibration detects our Arduino and as you can see there's there's one here that looks vaguely like ours from gearbest but that's three pound 16 there that sounds a bit expensive isn't it for what it is considering like you can get an analogue peeps out here for 170 of them that doesn't sound very cheap eBay probably not the Far East but gearbest no certainly is the Far East hmm okay well so they go oh well they got this one here this is one Pam 46 thing let me just get that off the screen yeah look 1:46 formation switch now this is the new one that I've seen just more fairly recently not that I've been looking so it's not a quick look at that one and see what it actually means it's not the bigger picture mm-hmm not very clear is it what it is though it's a tube within which there's something else that that stands up and then wobbles about and touches the side I'd like to actually have a better view of that a better picture so look at that one mmm not really let's have another look at somewhere else shall we right well have typed in vibration as part of the census in eBay and you say there's quite a few and they look like this little tubular one I don't think they're til switches either this one is this as a tilts wait slow down it says vibration as well which basically reinforces what I'm saying a while ago that it does detect vibration in its own way more like switch bounce really only you can get ten pieces of those tilts wishes for 99 pence which has got to be a good deal isn't it what else have I'm still looking for that on then this one looks like that not a tilt switch no there's no pictures on there ah there we are now there's a good picture look what you have is an outer shell or out a tube and inside you've got this thing that what was about if it touches the side are guessing it must make a circuit yeah I guess so on resistance less than 5 ohm well that compares well with the 3 ohm that we had for the mercury ball switch and the point 2 ohm of my tilt switch so there we are there so an alternative and that could be connected in much the same way it's not quite the same though is it yes it would detect vibration in the sense that if you were to bang whatever this thing is connected to this middle bit would swing about and touch the side a few times but no better than the tilt switch that we used right let me just interrupt my own video here because I've actually acquired some of those little vibration sensors as you can see here now only wanted a couple but you can only buy them like packs of 10 for 99 pence so I thought fine let's do it so they're the little tubes now I would say you have to be a little bit careful with these because as it says on the eBay page itself it says that one of these wires is very very thin you can see look it's a little tiny filament and it's easy to break not that I would be so clumsy as to break one of those of course yes well we'll come on to that one in a minute so what I've actually done is soldered one onto a little bit of strip board and put a little header on here just that we can play about so all I've done is a sketch to say can we detect this it's an input pull up pin on pin 8 can I detect something on here now at first I thought this isn't working something's wrong but let's see what happens when I switch on the debug window then so there's the debug window shooting through the counters on there just so you can see that it is actually running now if I flick this like we did with the tilt switch look no you might seen a zero just shoot by there but not a lot well you have less you do with these is actually move this overall I think if you shake this like they are now some zeros are coming up you see that but it's some I don't know it's not the not the simplest thing in the world I wanna said to program I mean you can see some zero is obviously shooting up past the debug window there now bottom ammeter quite violently shake this really you can see it so um yeah even if I bash it on my hand and things maybe there's some something about these type of switches that I do they need some different handling I don't know anyway if I just shake it they all look so the zeros are coming up but that's that's quite boiling compared to the tilt switch isn't it I've actually taken the one that I destroyed accidentally apart well taken off the cover now I'm gonna get this into macro mode so we can get really close up on here and it does look very much like the picture we saw no I'm gonna have to I'll have to get my camera zoomed in on this and I'll be back in just a sec right I think you can just about make that out now you can see that there's a coil of wire around a center pin the center pin is the solid one on here and then the coil wrapping around the middle of that which you can just about see there though is that little tiny thin thing that I managed to snap off and that's like what goes round so what happens is as I shake this that coil obviously moves because it's a a very light filament and eventually touches the center pin but as we've discovered you do have to wake it quite hard what not whack it I mean doesn't matter how hard you whack this it doesn't seem to do anything what what you do is shake it quite hard to get those zeros to counter say some I'm shaking that quite a bit and then okay then you get the zeros but there we are I'd be care if you think this is gonna act like some kind of vibration sensor or touch sensitive of the sensitivity that they had with the tilt switch you know you're going to be disappointed quite frankly and I'm I'm in two minds about this I mean why would you want something that's so insensitive you really have to whack it like this what would you use it for robot arms cars I can't believe it this is too insensitive I think anyway at least we've covered all bases now so we know about this we know about the thin little while that snaps off too easily but there are only ten 499 P that's why anyway we've covered that so back to the main video now which I'm sticking this video into okay right when when you think about it though what we've got here with this vibration detector is an analog version of that so you can go from nothing all the way up to 102 three steps and by specifying an AF voltage then you can make it as sensitive as you want really now having tracked that value on screen in this window here that you're looking at which is the serial monitor or bug window whether you want to call it it would be a better in some ways if we could track that on the oscilloscope but rather than drag you all the way downstairs to this Scylla scope there's something else we can do so we're going to close down the serial monitor window like that we're going to come up to tools now this is a facility that's been around since 1.66 serial plotter course nobody uses 1.67 up to one point six eleven because there was a few bugs in the arduino ide allegedly i came across a couple of must amend it drove me mad so we've got to reverted to one point six five one point six six but this serial plot has been around since then and if we click that one not bring it into view now what that's doing while we're waiting is trying to read stuff off the serial port just the same way as the serial monitor did this gang can I detect anything or not well if we whack this a few times oh there we are look it's coming in a bit like an oscilloscope and the y-axis that's this thing here automatically scales as well so if we just tap it a little tiny bit moment you can see the scale is at some 30 but if I get all those little spikes off bust by tapping it gently there are let's gone down to 12 log so 0 to 12 and the reason why it's it stops when I stop is because the code says here look only if it's above zero did we do any serial printing if I change that to equal zero you'll see that in fact it just continues to display a bit like an oscilloscope or tray so let's upload that and you'll see what I mean let's bring the trace back wires compiling oh I think I've managed to close it let's bring it back up no it's disappeared it's up to me yesterday actually I was trying to do something and it just disappears then you just think oh I'll just use the monitor for now and then you get an error let's say let's see if we get an error yes we've got an error look it says their serial monitor not available well the plotter is open well where is the plotter it's disappeared off the screen we can't find it I'm gonna have to try and get it back somehow oh there it is right so there's a serial plotter now it shoot as you can see now it's got zero and it should be tracking everything yeah look there we are looks carries on tracking stuff so now when I touch the board you can see the the actual was sensor values fly across the screen quite happily now the wise scale should reset itself again when it detects enough there we are look so it's frequently enough I think or no I think it has to be more often than what I'm tapping it on this board here but that's basically how it works if I tap it nice and fast they can't see it hang on let me just move things about a bit right so I'm tapping it there we are the Y scales as you would expect why it suddenly shut up like that you'd almost think it's detected some kind of negative pulse with you but there is no negative pulse as far as I can tell because I'm only plotting positive values as we've already discussed now there's something else about this serial plotter that's um quite helpful you can have more than one trace running at a time bit like a dual-trace oscilloscopes and the way you do that is by outputting oh hang on Benny my cat has decided to join us because he's quite interested in serial plots and everything now Benny what do you reckon we should be doing about this serial plot there should be yeah should we show another one yeah yeah I think he says yes I think he wants definitely see that I can I can tell what he's thinking obviously because he's my accountant used to the way he's talking yeah yeah that's that's the direction to go buddy that way there that's it thanks for joining us by the way it's been been a pleasure right let's just get back to what I was doing right so we're gonna get a double trace on oh he's back hang on let me sort out Benny and then I'll get the double trace working right so there we have a dual trace running at the moment one in orange one in blue and if I tap the board you see that they guess they're obviously in sync because the what I'm actually displaying is exactly the same value but they needn't be they could be you know entirely different signals but you can see we have two there and that's jolly useful feature and the way you do it if you look at the code there is that you print your first value then you print a space or the tab characters you slash T and then you print the final value but it must be print lion that's how it knows you've finished and what it tries to do it interprets what is being output in anything that's numeric it will display on that screen so but just go back to the monitor to have a look and bring that to the front there it is so tap the board again and we have the two traces running now I haven't actually had need to have two traces running myself but this is the sort of a sounds a bit disingenuous isn't it call it a poor man's oscilloscope because it's actually quite good I think to be cornice so you know you can detect pulses and things yeah very good that anyway I'm hoping they're gonna improve it a little bit stop this white race automatically rescaling itself I dropped i've liked about to put in a maximum trace myself then I wouldn't have to do things like this you know saying maximum over fifty but that's that's a minor issue issue for me quite honestly and well no you can play with that now you know that it's there and what it's useful just remember to get this right here you must say print a value and then a space and then print on the value with a print line or if you're just using the one value of course forget all that it's just print just that one as we had it before and they'll print it on this plotter quite happily yeah cool okay then I think well leave it there as a few things about vibrations and detentions plus a little bit about the plotter which is always useful to know great we'll leave it there thanks very much indeed for watching and I'll see you in the next video I hope you're finding these videos useful and interesting please leave comments down below subscribe share and give me a thumbs up thanks for watching see you in the next video
Info
Channel: Ralph S Bacon
Views: 5,634
Rating: 5 out of 5
Keywords: Arduino, electronics, C++, microcontrollers, programming, gadgets, tilt switch, vibration, mercury, switch, Serial Plotter, Serial Monitor, built in oscilloscope
Id: Yo6MGrIvyLg
Channel Id: undefined
Length: 34min 57sec (2097 seconds)
Published: Fri Dec 23 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.