We are going to be walking through the proximity sensor for these arduinos and were not just going to be making a little example of a print statement, but were even going to be causing a little alarm to go off or in our case a little led to Turn on if our proximity sensor is reading, something thats too close for comfort, and so this is going to be a complete guide from scratch on how to do this stuff. Ive got my little setup right here. Ive got a breadboard ive got the proximity sensor. Ive got it all wired up and ill. Tell you guys through all that stuff, and basically the output of this is were going to see an led uh for specifically on pin 13 light up when this thing gets too close. So, as we get closer and closer, we can see that led turn on its going to stay. That way, and its going to be that way as long as its too close within five centimeters, what ive defined in the program and as we move this thing away led, will turn off so its pretty cool. So it can alarm us if something is too close to one of our proximity: sensors for instance. So that is how were gon na get started with this stuff ill post, all the code to github, so you guys can download this change it play around with it, and so we will get started alright, guys before we get too deep into any of the programming Stuff, i just want to walk through the high level the hardware thats going on here, so this is a proximity sensor and its very basic, its just two identical cylinders, t and r, so youve got a transmitter and a receiver, and so the way these things work Is that they emit a sound wave at 40, 000 hertz, which is higher than the human ear, can attack that 20 000 hertz.

So basically, you dont hear this, but its literally just a bunch of sound waves that bounce off stuff and then it will reflect and then go back from the transmitter to the receiver and so based on the time it takes for that sound wave to make its Way back to the receiver is how this thing knows how far away something is. So as we move this closer and closer the time between the sender sending a signal and the receiver receiving that signal at 40, 000 hertz is going to get smaller and smaller. So closer and closer smaller and smaller uh times equals closer and closer distance. So that is the key thing to note its basically on these things. Weve got four wires and ive stuck this thing into a breadboard just to have a nice little base for it. Um. You know you can come with other solutions, but for the sake of just a little demo, this is whats working for me and so youre going to note how youve got a vcc thats literally just 5 volts youve got ground on the other side and then youve Got trig and echo, and so trig is the basically the transmitter so thats the thing thats making that that pulse and then r or the echo is whats receiving that so its the echo of the trigger so thats going to be corresponding to your receiver. So, in our case uh, what i did is i just took on this breadboard and its on pins or the rows.

One two three and four so pin three is going to or row three will map to trigger so thats gon na be this orange wire orange wire is gon na go to pin six on my arduino and then weve got on row, two thats where weve got Our echo or our receiver thats this green wire and that guy is just going to go into, pin five, so thats a key thing to make note of and then in terms of the actual power. For this thing, for the sensor itself, weve got the 5 volts and ground coming off this power things thats gon na be constant the entire time. This thing is plugged in all right guys, so we are now going to jump into the actual coding part for this. Arduino proximity sensor, slash alarm and so ive got a blank template file here. Its got empty setup and loop functions or methods in java or arduino, and so what the were going to want to do, first of all, is import a great library, its called newping.h, and if you havent used this before the way you can get, it is, if You go to tools and then go to manage libraries, itll pull up a window in the arduino ide, and if you have this little search bar right here, give it a second just type in new ping hit enter and give this thing a second ive already installed. It, but if you havent, you just click install and it literally downloads the necessary packages to make this thing work, and so once youve done that in your ide, you can close this guy out and then what youre going to do is youre going to use pound Include and then open and close chevron brackets newping dot h so thats going to include this library that were going to be using for our proximity sensor and now what were going to want to do is define the trigger, pin the echo pin and the max distance.

In centimeters that our proximity sensor should be reading out to so to do that in javascript. We need to preface this with the int keyword and then im going to say that this is the trigger underscore pin, and this is going to be equivalent to pin 6, which is what we just looked at and then make sure to have that semicolon to end. That line were also going to have the int echo pin, and this is going to be on pin 5 because thats what our arduino has been wired up to do and then also im going to define int max distance centimeters and were going to say that this Is going to be equivalent to 15., and so basically this is saying to our proximity sensor dont read anything thats more than 15 centimeters away uh and again. This is something that you can pick depending on the sensors youre going with, but for our cases, whats working for us, and so now were going to do, is were going to go into our still outside of that setup method were going to now create an object Of the new ping class that weve just imported sonar and then open and close parentheses and now were going to enter the trigger, pin the echo pin and the max distance in centimeters, and so now that weve defined this object. We will move into our setup loop and were going to define the bode rate and its going to be serial.

begin and 9600 and then semicolon and then comments in java are those double forward. Slashes, and this just for clear, is the boat rate, and we are also going to define a delay in our method right here and 50 milliseconds just for the initial setup and then were also going to set, pin 13 so pin mode 13 as an output. And so basically, what this is going to do is its going to tell the arduino that it should treat the pin 13, which is that built in led on the arduino uno itself, to to treat that led as an led that can be can be turned on Or off, depending on some condition, thats how were going to be setting our alarm so important thing to make note on that, one now were going to move into the actual loop method itself, and so this is the thing thats going to be running indefinitely. So im going to do serial dot print and were just going to basically preface our log outputs as follows. So distance from proximity sensor in centimeters and then were also going to grab what the proximity sensor is reading in this. Given time interval, so int distance centimeters will be equivalent to that sonar. So now were going to be calling that sonar object, dot, ping, centimeters and so its basically going to be grabbing. Whatever is that current value from the sensor? And now what were going to also do is were going to print that to the console so serial dot print line, because we want to basically make a new line character after each one of these outputs um and were going to just uh print with that value.

Like so and now what i want to do to actually have an alarm here is were going to define a condition so im going to say if the and i also before i do that sorry, i want to just define an alarm distance that im specific here. So int alarm distance in centimeters and well say that this is equivalent to 5. So, basically, if anythings closer than five centimeters, when we should freak out uh so basically were gon na now do an if statement in our java java and were going to say if distance in centimeters is less than or equal to the alarm distance in centimeters. Then what were going to want to do so open, curly bracket, close curly brackets im going to say digital right – and this is where were going to say, pin 13 set this to high, so basically thats going to turn on led. If object is too close to sensor, essentially its alarm high else, im going to want to basically turn off our uh alarm, led if thats not the case so im just going to copy. This guy also were going to be setting the pin 13 to low and thats in all caps uh and so turn off led at pin 13 if object is within normal limits and so thats whats going on and then finally were going to add in a delay. Here and so this delay will have units of milliseconds, and so basically, i will say: 250 milliseconds or basically, four times a second wait: 0.

25 seconds between state between measurements and so basically thats thats, the time window that im comfortable with uh for my particular alarm here. Next, im going to do is im going to click on this verify uh command right here, just to see if weve messed anything up in the java – and we have not – which is great – and you guys can just copy paste all this stuff, because im going to Put it in github, but now were going to do, is were going to click on upload and sometimes you might get prompted to specify, which is the com port for your arduino itself. If you havent, but in our case it knows and so thats great and so now that its running, what you can do is you, can click on tools and go to serial, monitor or press ctrl shift down on the keyboard and so right now uh its kind Of acting weird because theres – nothing really oh its, so i have the sensor right here. I dont know if you guys can see that um. Let me try to get that in frame. So weve got our sensor right here and im going to do something like take a breadboard, and we can see that its measuring, where our breadboard is and as we get closer and closer uh its now. Also, turning on that led because we just were within five centimeters of this thing and were going to take this thing up and out, and we can see how cool it is that its able to see that this things getting closer and farther away.

Just like that, its turning on this led as an alarm like that. So now, weve just created our own complete working alarm. You can close this out. You can also disconnect the um and get this in frame that the usb cable from this arduino and we can take a little power supply here and all that logic is still living on the chip itself. For this guy. So, even though, were not necessarily able to see any more whats going on in the computer, because weve just disconnected the usb thing, we will still see, pin 13 light up on this guy once uh. Something is too close for comfort to our proximity sensor, so that is going to wrap things up for this video. But i hope this helps all those codes on github and let me know if any questions.

https://www.youtube.com/watch?v=K8d8BDtR9Jc