Introduction: BEEP Like a Car! Sonar Sensor
I don’t like much the noisy BEEP you get with modern cars when parking sensor are enabled, but hey...it is quite useful, isn't it?!
Do I need a portable sensor telling me how far I am from an obstacle? Probably not, at least until my eyes keep working.
However, I still wanted to experiment and make my own portable “parking” sensor (or audible distance measurement tool).
Car sensors are IR, but I had not a spare IR receiver at home, instead I found an HC-SR04 ultrasonic sensor in the drawer. Some easy wiring/coding and...here it is: How to BEEP like a car!
Bill of materials:
- HC-SR04 x 1: ultrasonic sensor
- uChip: Arduino IDE compatible board
- Piezoelectric Buzzer
- 10 KOhm, 820 Ohm resistors (or any other value you find around getting close enough)
- NPN BJT
- micro-USB cable ( plus a 5V USB power source if you want to make it portable)
Step 1: Wiring
The micro-USB connector provides the power that uChipdelivers on VEXT (pin_16) and GND (pin_8).
As for the GPIO wiring, any combination is possible as long as you use PWM enabled pin ports.
In my case, I used pin_1 to control the buzzer, while pin_9 and pin_10 are connected to the ECHO and TRIGGER signal pins respectively of the ultrasonic sensor.
Independently whether you are using an active or a passive buzzer (which are a buzzer with integrated driving circuit or a simple piezoelectric membrane respectively), the control circuit is equivalent. However, be careful when wiring an active buzzer since you must check the polarity of the pins, while using a passive that is negligible.
TIP: How do you check whether your buzzer is active or passive?
Usually an active buzzer carries a + mark somewhere on it indicating the polarity. On the other hand, passive transducers do not have such a mark.
Step 2: Programming
Load the updated sketch “BeepLikeACarMillis.ino” into uChip using the Arduino IDE. This version of the code does not make use of delay() and is thus more reliable! The MCU continuously monitors the distance using the sonar HC-SR04.
Set the various #define accordingly to your needs. As default, the minimum distance is 200 mm while the maximum is 2500 mm. Furthermore, you are more than welcome to modify the BUZZ_DIV define in order to change the frequency with which the beep occurs.
Check the differences in the code comparing the updated sketch (“BeepLikeACarMillis.ino”) with the old one (“BeepLikeACar.ino”).
The old version of the code uses the delay() function, which keeps busy the processor with the wasteful time counting and as a consequence, the MCU cannot process any other info. What happens is that, in case we move too fast, the low scan rate will not detect the changing distance and thus our beeper won’t respond quickly enough to see the obstacle since it is busy on “waiting”.
On the other hand, the updated code, which uses millis(), allows for a faster and continuous reading of the distance. Thus, it is safer since its refresh rate of the distance from the obstacle is much higher.
Step 3: Enjoy!
Connect the micro-USB cable to uChip and go around your house, BEEP like a car!
4 years ago
Code UPDATE released!