Car Reversing Sensor
A while back I got my first Arduino and after playing around with it for a while I decided that a good first project would be a reversing sensor for my car. I live in rural Australia and drive a 4x4 ute that I purchased at a car auction. Its a pretty basic model and had no reversing sensors. Being quite a big car this made reversing into tight spaces a bit of pain.
Initially I though this project was going to be easy. If it was just one sensor it probably would've been straight forward. But as soon as I added in more sensors (I was going to need at least 4) it soon became evident that for things to work properly the coding side of things were going to get a bit more complicated. Hardware wise its all pretty simple.
The first version I built used the standard two horn HC-SR04 ultrasonic sensors you usually find. I built a contraption to keep the water off but the rain eventually destroyed them. So I ended up buying Waterproof Ultrasonic Sensor (I think you can now buy them from the Arduino store - but probably get them cheaper from Ali-Express or something). Since using these I have not had any trouble.
Supplies
You'll need:
- 1 Arduino
- 4 Waterproof Ultrasonic Sensors and their drivers
- Buzzer
Build a Test Rig
Before drilling holes in your bumper for the sensors and running wires through your car I would strongly advise building a test rig to trial your reversing sensor with.
Above is the wiring diagram of the sensors and buzzer to the Arduino
The Need for "Timer Interrupts"
In essence the code in the next step differs from code I usually see for ultrasonic sensors in that the delay function is not being used. The reason is because when the delay function is "delaying" nothing else can happen. This works fine for one sensor, but when you have multiple sensors attached things can slow up pretty quick. Instead you need to use a thing called timer interrupts - this kinda gives the impression that the arduino is multi-tasking - but its not really. Its just much better at managing its time. The analogy I have seen used is as follows:
Imagine you were in pizza shop and there is a ten minute wait for orders to be processed.
A delay function would be the equivalent of you going to sleep in a chair whilst you wait for your pizza to be made. In the 10 minutes that takes you can't do anything else...cos you're in the land of nod.
The timer interrupt would be more akin to you saying "Great - I'll come back in 10 minutes then". So you set a timer and pop out to the off-licence to grab a few beers to enjoy with your pizza. This way you are optimising your use of time.
So getting back to reversing sensors. If you use the delay function your response to changing in distances is going to be real slow when you have 4 sensors. So use timer interrupt. The timer interrupt means you can check in every so often on one of your 4 sensors and then update the distance accordingly. This is then used to set the frequency of the beeps on your little buzzer to let you know when things are getting closer to your rear bumper. You also need to make sure that you time the checking of the sensors and the frequency of the beeping just right - if you get the timing wrong your beeping is going to be all over the place and you wont have a clue to what its trying to tell you.
The Code
The software is the real challenge with this project.
If you're whiz on coding your'e probably going to have a laugh at my attempts.
Please see the attached Arduino code
Downloads
Testing
Ok. So by now you should have:
- Wired up the test rig
- Downloaded the NewPing library to your Arduino code editor
- Copied the program from the previous step
- Powered up the Arduino
- Uploaded the program to the Arduino
Once all the above is done you should be able to open the serial monitor on your Arduino code editor and see what distances the micro-controller is recording. The code takes the median of 5 readings (to reduce errors) from each sensor and then takes the minimum reading across all 4 to set the beeping frequency on.
Now is your chance to check each sensor is working and providing sensible outputs. You can also check if the beeping is to your liking (I think I did most of my testing with a blinking LED as mucking around with the beeping was driving my family around the bend - thats why it referred to "ledstate" and "ledpin" in the code). If you want to change things play around with the parameters and see what it does.
Once you're happy with everything you're ready to install in your car
Installing Onto Your Car
Okay. You're going to have to take a drill to your car. If you are in any way precious about your car you had better stop here. I don't care that much about my car - so I drilled four holes in my bumper with the following in mind:
- Make sure the holes are pointing horizontal to the ground - i.e. not pointing down towards the ground too much or facing up to the sky. If they are pointing downwards you're going to get readings of how far your bumper is from the ground and not how far it is from an object.
- Make sure the holes are not so big that your sensors will just fall out. Better off starting off a bit too small and increasing the size if required.
Once you have your holes in place you need to have a location for keeping the Arduino and drivers for the sensors. You need this to be out of the elements and protected from bumps. I built mine into a little enclosure and had the wires going from this to the sensors. I tapped into the cars reversing light cable to provide power and it also has the added advantage that things only turn on when you engage reverse.
Lastly you will need to run a wire from the enclosure to the buzzer that should be located somewhere in the cabin where it wont get wrecked and can be heard easily. I snuck mine behind the plastic trim on the B-Pillar on the driver side.
The other option is to buy a newer car with this all done for you.