 • Blogs
• Circuit 3B: Distance Sensor

## Circuit 3B: Distance Sensor

• Public
By Adam Kroeker July 9, 2022 - 1:45pm

I had no issues building this circuit. I modified the example code so that it works as a speed sensor instead of a distance sensor. When it senses no movement or if it senses an object moving at a speed of 0.1 m/s or slower, the LED turns green. When an object is moving between 0.1 and 0.5 m/s, the LED turns yellow. When an object moves at a speed of 0.5 m/s or faster, the LED turns red. I imagine that the LED is a signal for the object, where the green LED means that the object’s speed is well below the speed limit of 0.5 m/s, yellow means it is nearing the limit, and red means it is meeting or exceeding the speed limit. Here is the code:

`const int trigPin = 11; //connects to the trigger pin on the distance sensorconst int echoPin = 12; //connects to the echo pin on the distance sensor`
`const int redPin = 3; //pin to control the red LED inside the RGB LEDconst int greenPin = 5; //pin to control the green LED inside the RGB LEDconst int bluePin = 6; //pin to control the blue LED inside the RGB LED`
`float travelSpeed = 0; //stores the speed calculated in speedCalc()`
`void setup(){ pinMode(trigPin, OUTPUT); //the trigger pin will output pulses of electricity pinMode(echoPin, INPUT); //the echo pin will measure the duration of pulses coming back from the distance sensor`
`//set the RGB LED pins to output pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT);}`
`void loop() { travelSpeed = speedCalc();`
`if (travelSpeed <= 0.1) { //The object is either stopped or moving very slowly //make the RGB LED green analogWrite(redPin, 0); analogWrite(greenPin, 255); analogWrite(bluePin, 0);`
`} else if (0.1 < travelSpeed && travelSpeed < 0.5) { //The object is moving at a medium speed`
`//make the RGB LED yellow analogWrite(redPin, 255); analogWrite(greenPin, 50); analogWrite(bluePin, 0);`
`} else { //The object is moving very quickly`
`//make the RGB LED red analogWrite(redPin, 255); analogWrite(greenPin, 0); analogWrite(bluePin, 0); }}`
`//------------------FUNCTIONS-------------------------------`
`//This function returns the speed of the object in m/sfloat speedCalc(){ float dist1 = getDistance(); delay(100); //0.1 second delay float dist2 = getDistance(); float metres = abs((dist1-dist2)/100); //Total distance in metres float result = metres/0.1; //Result in m/s return result;}`
`//RETURNS THE DISTANCE MEASURED BY THE HC-SR04 DISTANCE SENSORfloat getDistance(){ float echoTime; //variable to store the time it takes for a ping to bounce off an object float calculatedDistance; //variable to store the distance calculated from the echo time`
`//send out an ultrasonic pulse that's 10ms long digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW);`
`echoTime = pulseIn(echoPin, HIGH); //use the pulsein command to see how long it takes for the //pulse to bounce back to the sensor`
`calculatedDistance = echoTime / 58.0; //calculate the distance of the object that reflected the pulse (half the bounce time multiplied by the speed of sound)`
`return calculatedDistance; //send back the distance that was calculated}`

Most of the code is like the example program. I replaced the distance variable with a travelSpeed variable, and in the loop() function it is assigned the return value of the speedCalc() function that I added. The speedCalc() function calculates the speed of the object by making two calls to the getDistance() function 100 milliseconds apart, and taking their difference and returning the object’s speed in metres/second. The getDistance() function is the same as in the example program except for the second last line where I divide echoTime by 58.0 instead of 148.0. This is because I wanted the distance in centimetres rather than inches, and I read in the datasheet for the distance sensor that dividing the signal interval by 58 produces the distance in centimetres.

Here is a video of the code in action: One issue I had while writing this program was that I did not initially include a call to the abs() function in the assignment statement for the metres variable in the speedCalc() function, so the line was just "float metres = (dist1-dist2)/100;" instead of "float metres = abs((dist1-dist2)/100);". This resulted in the LED staying green when the object was moving away from the distance sensor, even if it was moving very quickly. This happened because the result of dist1-dist2 is negative when the object is moving away from the sensor, so the return value of speedCalc() was negative and since negative numbers are less than 0.1, the first if-statement in loop() was true, causing the LED to stay green. I fixed this problem by wrapping (dist1-dist2)/100 with the abs() function to get its absolute value. This allows speedCalc() to return the correct speed as a positive number even when the object is moving away from the sensor. 