This circuit will introduce the HCSR04 Ultrasonic Sensor to our electronics toolkit. Unlike the Servo, the HCSR04 doesn't require a library to be included manually, nor does it use objects or methods.

Transcript

Hey guys, Christian here from Core Electronics. Today we're going to get started with circuit 3B from project set 3, using the SparkFun Red board. So, in this circuit, we'll be using an ultrasonic distance sensor, to be able to light up an RGB LED depending on the census proximity.

The good thing about this Centre is that unlike the server, it doesn't require a library, so we can just write the code for it. So, the HCS RO4 that we will be using has four pins it's got;
VCC pin
Ground pin
Trigger pin
Echo pin

So, the Trigger pin essentially you set too high for 10 microseconds and once that's been done it'll send out 8 40 kilohertz pulses and the Echo pin will wait for those pulses to come back and once they do we divide the distance by 2 because we have to take account for the time for it to get there and for it to get back and then once that's done knowing the speed of sound we can actually compute the distance of the object which reflected back those high-frequency waves.

So, let's get started assembling it. So up the top here we will try and put the sensor as close to the edge of the red board as we can so that its reception is in halted by the breadboard or the base plate from the red board. So, we'll be using the Trigger pin to be on pin 11 and we will have the Echo pin on pin 12. So, these two pins are essentially crossing each other. Next, we'll hook up the power bus, so we want ground on the ground and we want to power on power and finally, the RGB LED remember to connect it with the correct polarity. So, the longest leg is the negative pin and from left to right, they will go R, G, B (Red Green Blue). So, we'll connect the resistors across accordingly, making sure that the metal legs of the resistance don't touch each other and then we'll connect that up. So red, will go to pin 3, once again we'll be using a PWM signal to write this LED to specific colours. So, we will want to connect those two PWM pins so 3, 5 & 6 will be the pins that we're connecting them to and the ground connection or the LED will just go to the ground power rail. So that's how circuit set up, now what we're going to do is we're going to connect it and we will open the SIK code.

For this circuit, what they've set here is essentially what we've just connected there once again using this "const int", to save space on the red board so the Trigger pin is connected to pin 11, the Echo pin to pin 12, 11 is a PWM pin and we need that to send out the 8 40 kilohertz waves. Once again 3, 5 and 6 and we have a global variable float called "distance" that's set to 0 initially. So here at serial begin, we're initializing the serial com port with 9600 baud rate, we're setting Trigger pin to output as its outputting the waves and we're setting Echo pin to input as its receiving the waves and once again the RGB pins are outputs. So, distance is now set to "get distance", which is a function down here. Now the way that gets distance works is, it is going to convert the output signal or the received time using this pulsing function into a distance by dividing the Echo time by a constant. So, this constant will put the calculated distance to be in inches, now up here we're going to be printing out this distance, after it's run through the get distance function and it's going to say that it's in inches. So, if the distance is less than or equal to 10 inches we're going to light up the RGB LED red, if it's between 10 and 20 inches it'll go to yellow and if it is outside of that it'll be green. So, it's kind of like good, warning, bad and then we'll delay that so that there's time for this sensor to reread the value at it.

So yeah as I said in here this get distance functions, so we've got two floats up here, one is the one that we're going to be returning called calculated distance and the other one is the Echo time. Now the echo time is read using a function called pulse in, so it's waiting for the Echo pin to go high, now since the microcontroller is so fast from writing the trick pin "HIGH" and then writing it "LOW" again for that 10 micro second time so that it can output those waves, it's essentially instantaneous. So, it's going to know straight away oh yeah, I'm waiting for this pen to go "HIGH" and then when it does I'm going to return the time that it was high for. Then we're going to divide this by the constant that'll put it into inches, and then we're going to return that back into here so that we've saved distance. So, all that said we will upload this to the red board.

So, it's done uploading and you can see that there's an orange and green effect. Now the reason for this is, that the HCS RO4 has a cone effect. So, it has 15 degrees from the horizontal where it can measure all the way around, so sometimes your table, your breadboard, your baseplate, can get in the way of this. So now it's a green, if I bring my hand there it's orange or yellow and then as I bring it closer it should go red. So, this is within 10 inches, this is within 20 and between 10 and that is outside that range there.

So that is how we can read a distance from the HCS RO4 ultrasonic distance module and light up an LED accordingly. So that there has been circuit 3B, I hope you guys enjoyed it and come back for our next circuit, circuit 3C which will finish off project set 3 and incorporate both the use of a server which we saw in our previous circuit and the use of this ultrasonic distance sensor.

See you guys there.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.