In this Instructable, we will demonstrate one of the technologies that are often used in autonomous vehicles: ultrasonic obstacle detection.
Within self-driving cars, this technology is used for recognizing obstacles over a short-distance (<4m), for instance during parking and lane-switching.
For this exploration, we aim to build a breadboard that (1) drives, (2) recognizes obstacles and (3) makes decisions for its route accordingly.
Specifically, we will build a two wheeled breadboard, with an ultrasonic sensor up front, that drives forward when no obstacle is detected, turns when almost hitting an object, and reverses when a collision seems unavoidable.
Step 1: Getting the Components
The following components were used for this instruction:
- (A) 830 pin breadboard (1pc)
A smaller one may suffice, but make sure to get a good quality one because the pins on the ultrasonic sensor are a bit fragile.
- (B) Arduino UNO (1pc)
Works great with the Motor Shield, does not need to be an original version.
(C) Adafruit Motor Shield v2.3 (1pc)
The motor shield simplifies the process of connecting motors to an Arduino. Compared to tinkering with resistances and transistors, it is much safer for the Arduino board, especially if you are a beginner. The Adafruit Motor Shield comes with separate pins, that need to be soldered onto the chip.
(D) HC-SR04 Ultrasonic Sensor (1pc)
This is a four pin sensor. It works by sending out a short ultrasonic pulse through the left 'speaker' unit and listening (while measuring the time) when it comes back through the right 'receiver' unit.
(E) DAGU DG01D Mini DC motor with 48:1 gearbox (2pc)
When using a Motor Shield, any 5V DC motor will work, however, the gearbox in this version is beneficial, as it makes the wheels turn nice and slow.
(F) Plastic wheels (2pc)
Ideally, try to buy wheels that are directly compatible with the motor of your choice.
Also needed: a computer with the latest Arduino software, a soldering iron, soldering tin, a small power bank, some wires.
Step 2: Setting Up the Circuit
Connecting the ultrasonic sensor
The ultrasonic sensor consists of four pins, called: Vcc, Trig, Echo and Gnd (Ground).
Trig and Echo are connected to the Motor Shield in respectively Digital Pin number 10 and 9. (Other digital pins are suitable as well, as long as the appropriate coding is applied.)
Vcc and Gnd are connected to 5V and Gnd on the shield.
Connecting the DC motors
The DC motors have a black and a red wire each. These wires should be connected to the motor ports, in this example M1 and M2.
Step 3: Writing the Code
Loading the library
First, it is necessary to download the right library in order to use the Adafruit Motor Shield v2.3.
In this ZIP-file, there is a folder, which can be placed in the Arduino installation folder, in our case:
C:\Program files (x86)\Arduino\Libraries
And make sure to name it Adafruit_MotorShield (restart your Arduino software afterwards).
Downloading the code example
Our code example 'Selfdriving_Breadboard.ino' is available for download.
There are several variables to tweak, most importantly there are the distances (in centimeters) when something happens. In the current code, the breadboard was programmed to reverse when an object is closer than 10 centimeters, to rotate when distance is between 10 and 20 centimeters, and to drive straight when no object is detected in 20 centimeters.
Step 4: Soldering the Pins
The soldering process consists of four steps.
- (A) Alignment of the pins
Make sure to put all the pins that come with the Motor Shield in place. This can be easily done by placing the shield on top of the Arduino board.
- (B) Soldering the pins
Do not rush this step, it is very important that pins do not connect to each other after soldering. Solder the outside pins first, to make sure the pins are not skewed.
- (C) Positioning the wires
When using the Motor Shield, the wires have to be soldered to their appropriate pins as well. It works best to stick the wires in the Motor Shield from the top, and solder them at the bottom of the Motor Shield. As a recap: for this tutorial we solder wires to digital pins 9 and 10, and to 5V and Gnd pins.
- (D) Soldering the wires
Now it is time to solder the wires, one by one. Make sure they are well positioned, maybe ask a friend to hold them while you solder it.
Step 5: Assembly of the Self-driving Breadboard
After soldering the components and testing the circuit, it is time for final assembly.
In this tutorial, the breadboard is not only used for its main functionality, but also as the backbone of the whole device. The final assembly instructions consist of four steps.
- (A) Connecting the wires
Make sure the cables are in the correct place (check Step 3 for the right way to connect everything), do not forget the two DC motors. Keep in mind where you wish to attach the components.
- (B) Connecting the sensor
Plug the sensor into the breadboard and make sure it is connected properly.
- (C) Placing the shield
Place the Motor Shield onto the Arduino UNO board. Now would be a great time to test the system before final assembly.
- (D) Fixing the components
In this step, take some double-sided tape, and fix the DC motors, the Arduino and a powerbank in place. In this case, the Arduino is placed upside down below the breadboard.
Step 6: You Did It!
By now you will probably be as excited as we were to take your creation for a test-run.
Have fun, try to tweak some parameters so that it works best for you.
Thank you for following our instruction, and let us know in case of any question!
Validation of the technology
The ultrasonic sensor that is used in this case, was supposed to have a range of 4 meters. However, the sensor loses accuracy with a larger distance than 1.5 meters.
Also, the sensor appears to experience some noise. By using the serial monitor in order to validate the distance accuracy, peaks of around 3000(mm) were visible while the object in front was only centimeters away. This is likely due to the fact that the input of the sensor is having a delay in its information, so the output is distorted once in a while.