Ultrasonic Distance Sensor

advertisement
Comp 2303 Elements of Computer Science : Embedded Systems
Ultrasonic Range Sensor
Portfolio
The activities in this tasksheet contribute to ILO 1&2
Activities
1
The Ultrasonic Range Sensor works rather like ultrasonic radar in submarines. The Arduino sends a
pulse to the sensor which then sends out a burst of ultrasonic sound. It then waits for the echo to
come back from an obstacle, and the sensor provides an output pulse when it arrives. Since the
Arduino sends the first pulse and receives the second, it can use the time difference between these
two pulses to calculate the distance of the obstacle.
Here’s the pulses and acoustic bursts:
(a) First carefully connect the sensor to the Arduino like this
(b) Download the Arduino Sketches for today from the zipped file ArduinoSketches and open up the
template sketch HC_SR04 and in the loop() functions, add the following lines of code. Keep an eye
on the diagram of pulses and acoustic bursts above, since your code will create these!
(i) Send the TxPin (digi10) low, and keep it low for 5 microseconds. This corresponds to the short low
part of the “Trigger” signal; use delayMicroseconds(…); to do this.
(ii) Now make the TxPin high for 10 microseconds and then send it low. This corresponds to the
remaining part of the “Trigger” signal.
(iii) Now write the following line of code which is an Arduino function to wait for a pulse to arrive on
the RxPin (digi11).
travelTime = pulseIn(RxPin,HIGH);
(iv) Now write the following line of code to convert the travel time into a distance in cms. How does
this work? Well since speed = distance/time, distance = speed x time. The speed of sound is 330 m/s
and the factor 20000.0 is to scale the travelTime (in microseconds) and the distance to cms.
dist = (float)travelTime*330.0/20000.0;
(v) Fire up the serial monitor and hold your hand in front of the sensor and check that you get a
sensible reading.
2
Now let’s perform some investigations with the sensor. It’s probably best to point the sensor
upwards to the ceiling to avoid spurious reflections.
(a) Check the accuracy of the sensor. Hold an obstacle above the sensor, e.g. 20cms, 100cms and
check the distance reading. What is the closest and the furthest the sensor is useful?
(b) Measure the height of the lab above the floor.
(c) Measure the angle from the vertical at which the sensor just detects an obstacle. Look at the
diagram below. Move the obstacle in from the side making sure that the obstacle remains 100cm
above the sensor. Note the distance x in cm where the object is detected. Then calculate the angle θ
by entering the following formula into Excel =180*ATAN(20/100)/PI() Note here my value of x was
20.The mathematical formula is
180
𝑥
𝑎𝑡𝑎𝑛 (
)
𝜋
100
(d) How could you use this sensor with a small robot?
For your portfolio you could explain your experiments, and discuss the results and answer 2(d).
Download