The ultrasonic sensor emits pulses at a frequency of 40KHz (imperceptible to the human ear and animals), these pulses when hitting an object, they are reflected and return to the sensor after a certain time; knowing the total time round the wrists and the speed of sound in the medium (the air), it is easy to calculate the distance traveled by the wrists and thus determine the distance of the object relative to the sensor.
See the post about PING sensor for details in https://www.instructables.com/id/Basic-Principles/
We will use the same chassis of the BUGBot https://www.instructables.com/id/Basic-Principles/ ... but currently with a dual H-BRIDGE / L298H to control two DC motors (control direction and speed), which will give greater flexibility to our prototype.
The basic operation is as follows:
When booting the robot will remain stopped and the sensor calculates the distance FRONT, RIGHT and LEFT, he will compare these distance with a threshold distance (i.e.20 cm) so it can decide which is the best way to forward (the longest path will always be the best option! ).
After that the Robot will begin to running forward until the moment they will find an object interfering with your progress (it should approach the maximum limit up to a distance, in inches, of the object), when it happens: it will stop and analise again distances: FRONT, RIGHT and LEFT, so he could again decide which is the best way to forward - returning again to the operating cycle.
Step 1: Programming Logic
- including libraries;
- initialize variables and constants;
- define and initialize the pins;
- initialize serial communication;
- call routine to stop motors;
- positioning servo to 90 degrees;
- read FRONT distance and store;
- servo position at 0 degrees;
- read LEFT distance and to store;
- positioning servo to 180 degrees;
- read RIGHT distance and to store;
- comparison (1): FRONT is greater than LEFT?;
- yes: - comparison (2): FRONT is greater than RIGHT?;
- yes: - call routine to move forward;
- no: - call routinel to turn right;
- no: - (comparison (1) is less than: - call routine to turn left;
- If any condition is possible (all distances are exactly equals: - move forward and stop for a while, and so begin the cycle again.
- walking distance to detect less than 20 cm;
- loop to the "BEGIN";
Note - 1:
The motors must be connected in order to have the same rotation speed and direction, so that to take the robot forward - high value on pin 2 of each motor and pin 1 maintained at a low level.
The flowchart show us, how to perform these programming functions (only as example):
Note - 2:
Using the same hardware, a second version with a simpler code was created. Where the operation is as below:
Initially the robot walks forward until he encounter an obstacle at a distance less than the distance limit;
Then he analyzes the "Right" distance - If the "Right" distance is greater than the limit distance - he chooses to turn "Right" and move forward;
If the "Right" distance is smaller than the limit distance - he analyses the "Left" distance - if the "Left" distance is greater than the limit distance - he chooses to turn "Left" and move forward;
If none of the distances, both "Right" and "Left" is not satisfactory - he turn 180 degrees and move forward.
Starting a new cycle.
In the Software and Programming section, you will find both versions for download.
Step 2: Hardware and Materials
We will use the chassis built for BUGBot - Light Follower Robot.
See the post for more construction details... https://www.instructables.com/id/Basic-Principles/
1 x Arduino UNO, Mega, Duemilanove, Duecemila or Teensy + + 2.0
1 x Ultrasonic Sensor HC-SR04
1 x Dual H-BRIDGE
1 x Servo Motor HXT900 9g / 1.8 Kg
1 x Kit Tamiya Gear Box with two 3-6V DC motors
Battery pack to power the Arduino and motors (9V)
Chassis for mounting
Wires and cables for connections.
Step 3: Layout and Schematics
I advise to use an independent power feed but with GNDs in common
between Arduino and H-Bridge. (due to high current drawn by the motor)
Take care to not reverse the connections of power feed ... the damage may be irreversible.
Arduino pins -X- External devices' pins:
0 -> N / A
1 -> N / A
2 -> N / A
3 -> PING Sensor echo pin
4 -> PING Sensor Trigger pin
5 -> H-BRIDGE port-A enable pin
6 -> H-BRIDGE in1 pin
7 -> H-BRIDGE in2 pin
8 -> H-BRIDGE in3 pin
9 -> H-BRIDGE in4 pin
10 -> to attach the servo motor
11 -> H-BRIDGE port-B enable pin
12 -> buzzer
13 -> N / A
"I had many problems with noise in the distance sensor system, which was solved by
adding 100nF capacitor between pins of the DC motors and their
carcasses (besides the capacitor which is usually added among its pins).
Step 4: Software and Programming
Many versions were created in the construction initial phase of the PINGBot.
It were created a version to manage the DC motors (direction, speed, to turn left, to turn right and for to turn 180 degrees).
And also were created the continous rotation servo motor's version. If you need this version, contact me please and I am willing to help you.
I had a lot of work to do the final adjustments in the Robot (and I think that is not too good so far). First, because the DC motors noise as I already commented, and then for the movements adjust and batery consuption.
About 30 hours were spent in this project. I believe that for any kind of chassis, we will need spent time for the adjustments and debug.
You can download the arduino sketches and libraries directly from the GitHub below:
Step 5: Videos
I am so sorry, but the videos are in Portuguese yet.
PINGBot version 4 (video 1):
PINGBOT version 4 (video 2):
PINGBot version 5:
PINGBot - General operation of the Robot, in action - leaving a deadlock.
Step 6: Pictures and Photos
The parts were placed on top of the 360 degrees wheel, using the same fixing screws (or ball caster if you are using).
In this way the problem was solved and gave more stability to the chassis.
Note that the DC motor battery is rechargeable type ... it helps us to save money and give more energy to the car.
Use a 9V/900mAh Nickel Metal Hydride (NiMH) batery.