How to use Ultrasonic Sensors
Hey, how are you going? My name’s Sam from Core Electronics and today we’re going to be taking a look at ultrasonic sensors. Now, what are these things? Well they’re actually quite a simple device and they operate on a principle that’s been around for quite a long time or at least we’ve known about it for a long time and that is if you’re ever in a big cave or a big area like an arena or something like that you’ll notice that when you clap or when you say something you can hear it echo back to you. You might hear multiple echo as well and the bigger the space generally the longer it takes for you to hear that echo and that’s because a sound wave is simply a pressure wave. Its a mechanical wave that moves through the air and propagates through until it hits something solid enough to reflect it back and when it does that it reflects on all different kinds of things and eventually it makes its way back to you where it’s picked up by your ears again. It’s really really cool. Thats actually a really common phenonmena and it happens everywhere around us. So what humans have done is created a sensor that uses that concept called an Ultrasonic Sensor.Now it’s quite simple, all an Ultrasonic Sensor does is it has two little speaker like things that you can see and one is a transmitter or an emitter and the other one is a receiver. The emitter will create a ‘ping’ sort of just like a sonar pulse which will propagate through and then the receivers job is simply to listen for when it gets that ping back and the interesting thing is that sound travelling through air moves at approximately 340 metres per second. It’s possible to break the sound barrier you know a jets or a really high performance aircraft will do that and you will hear a sonic boom. But you hear sound and you think its instantanious but it takes a while to travel. So if I’m holding the Ultrasonic Sensor this far above the table the emitter would send out a ping, a series of signals, now this sound is actually really really high. It’s called ultrasonic for a reason, not just sonic! What this means is Ultrasonic is a frequency beyond which the human ear can’t pick up, it can’t hear it. It’s so high that you can’t detect anything that is happening but it is exactly that, it is a sound wave or pressure wave moving through the air. So if I hold it here this wave gets emitted, propagates through, would hit this table and would bounce in all different directions but part of that reflection would make its way back to the receiver. The receiver would know the time that the ping was sent out and then would also be able to measure that time between when the ping was sent out and when it receives the ping. Now another thing about the speed of sound, it can quite easily, using the time that it took, it can deduce the distance between itself and whatever it reflected off which is really really cool. Quite simple, and quite a simple sensor, you can see that its got two speaker like things and some circuitry on the back to control that and you’ve got four pins here. You’ve got voltage, ground, trigger and echo pins.Trigger and echo pins do what they say, one is for emitting and one is for receiving and using a fairly simple library we can easily control these with any board of your choice. Today we’re going to be using Arduino, any Arduino compatible board will work with this, I’ve got a Teesny on board and the library is called the NEWPING Library. Now as I said any platform. Raspberry Pi, Particle, whathaveyou will have a library that will work with these, they’ve been around for ages, really simple to use but we’re going to be using the NEWPING Library.
So, all you need to do today is grab yourself an Arduino board, as I said I’m using a Teensy 3.2, any Teensy board, any Arduino, anything that will work with the Arduino IDE is comparable.Now I’ve got a simple breadboard setup going on here, I’ve got 2 LEDs with resistors going to ground, current limiting resistors that are being controlled by the Arduino. Now something to take note of is that this device is a 5 V device so I’m using the V USB Pin which taps on to the 5 V from the USB supply and powers the device and the good thing is that the Teensy 3.2 is 5 V tolerant meaning it can accept 5 V signals on certain pins however if you are using a different device be sure to be sure that it is 5 V tolerant or that you are using a logic level converter. Check out our Logic Level Conversion tutorial for more info on that but…. I’ve got this wired up and I’m going to put it in into the breadboard here. So this particular Ultrasonic Sensor is called the HC-SRO4, it’s dirt cheap and you can pick it up for a couple of dollars it is not going to set you back much at all. Bear in mind there are different types of ultrasonic sensors but this one is fairly accurate, I think the resolution is about 3mm (.3 of a centimetre) and so it does quite well but said that you can get more expensive ultrasonic sensors. Ones that cost upwards of $100 which are super sized, super accurate but just for getting a simple distance reading this one is going to do the job just fine.
So wire up your breadboard, we’ve got the circuit diagram down here as you can see. So you’ll need the Ultrasonic Sensor, the Arduino board, a few LEDs and resistors and jumper wires and away you go. Now the code for this is really quite simple, we’ll go to the Arduino IDE, now we’ve got our library inclusion some definitions so you define the trigger pin and the echo pin and the maximum distance you would like it to receive. I’ve got some LED definitions and a load distance which is we’re going to use as a trigger and I’m just going to make a simple distance trigger. So when something comes in between the ultrasonic sensor and whatever it was measuring, say, lets say it triggers within 10 cm the our load distance will equal 10, we’re going to be measuring in cm’s initialising serial so we can be getting a read output of the sensor. So let’s connect this up and we can get cracking - any USB port will be fine for this, I’m going to use this one, alright you’ll be able to see….. hangon I’ll bring this a bit closer because you can’t see, there we go, cool cool. So you can see that I have a blue light and a red light now and i’m, going to prop it up on something and the reason for this is that from where the emitter is propagating that sound from it has a 15 degree angle of a working angle where it will emit and it goes out into a cone shape, it’s not a direct line of sight and this is interesting because if I were to put it down on the table for example it would actually pick up that table which isn’t what we want. So putting it up on a higher surface just allows us to ignore that flat object. Now the code here is really simple, we just compare the sonar.ping_cm and that is just getting the ping results in centimetres, we compare that to a preset distance and if it is shorter than that, so if it’s within 10 cm the we’ll light up the red LED. You can see that the red LED lights up nicely and away we go, dead easy. Ultrasonic Sensor working nicely and you could really use this to create anything, collision detection, object avoidance, whatever it is. Something to keep in note is that this particular Ultrasonic Sensor has a minimum operating distance of 3 cm so anything shorter than 2 cm and it’s going to return, I found it returned of about 4 and 5 cm in 0 cm increments but they weren’t reliable of course and if you hold it here it’s actually not going to operate correctly until you get just outside of that range and there we go. I used just my fingers for example and you can see that it doesn’t operate super reliably and that is because ultrasonic needs a really solid surface to bounce back. So my fingers there’s quite a lot of air gap between and it might miss that sound wave reflecting it but if I use a flat hand it has a nice large surface to reflect on, something to keep in mind and the big benefit of ultrasonic sensors is that unlike infrared distance sensors things like smoke and dust aren’t going to cripple them, they can still measure through that however softer materials, cloths fabrics, things like that can cause some issues because they are soft acoustically, they don’t have a really hard reflection. That’s why when you’re in a bathroom for example with lots of tiles it’s really echo-ey because the sound waves reflect really well against that hard surface but things like foam and carpet don’t reflect that well. So yeah, that is it guys, that is our simple project with some really simple code. The library is doing all the work there of using an Ultrasonic Sensor with Arduino, again, any platform will work this is the dirt cheap sensor, it works really well for these kinds of projects. Grab one, have a play around with it and you’ll love the opportunities that it gives you - collision detection, object detection things like that, whether it’s an automated robot or not.
So thats it for today guys, have some fun and happy making :-)
Makers love reviews as much as you do, please follow this link to review the products you have purchased.