Introduction |
Published date: 16/7/2017
|
Our tiny robot will scan for object in front of it by using a Ultrasonic sensor. If an object is detected, the robot will stop and "look" right and left for the best escape route!
About Ultrasonic Sensor

Ultrasonic sensors work on a principle similar to radar or sonar, which evaluate attributes of a target by interpreting the echoes from radio or sound waves respectively. Active ultrasonic sensors generate high frequency sound waves and evaluate the echo which is received back by the sensor, measuring the time interval between sending the signal and receiving the echo to determine the distance to an object. Passive ultrasonic sensors are basically microphones that detect ultrasonic noise that is present under certain conditions.
HC-SR04 Sensor
The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats or dolphins do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package. From 2cm to 400 cm or 1” to 13 feet. It operation is not affected by sunlight or black material like Sharp rangefinders are (although acoustically soft materials like cloth can be difficult to detect). It comes complete with ultrasonic transmitter and receiver module.
The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats or dolphins do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package. From 2cm to 400 cm or 1” to 13 feet. It operation is not affected by sunlight or black material like Sharp rangefinders are (although acoustically soft materials like cloth can be difficult to detect). It comes complete with ultrasonic transmitter and receiver module.
What you will need - Hardware
For this project you will need:
Tools: Screwdriver, heat glue gun |
|
Assembly the Mini Round Robot Chassis Kit
Follow the images below !
The circuit
The motor shield that we are using has some header pins labeled as Digital I/O and Analog I/O. We will use them to connect our hardware parts together with Arduino uno board.
Servo Motor
Connect your servo cable to first header (D7 5V GND). Make sure that the brown cable is connected to pin GND (right side)
Buzzer
Connect to the first pin of second header (D8) the "+" buzzer pin and the "-" to GND
Ultrasonic Sensor
We will use the third and fourth header, make the following connections:
Motors (look your robot from behind)
Right DC Motor : Red cable to "M1+" and black to "M1-"
Left DC Motor: Red cable to "M2+" and black to "M2-"
Power - Battery holder
Connect it to shield "Vin -GND" screw terminal. Add one more cable and connect it with "Vin" pin of Arduino Uno board. If you want you can add an ON/OFF SW on the red cable of your battery holder pack.
Servo Motor
Connect your servo cable to first header (D7 5V GND). Make sure that the brown cable is connected to pin GND (right side)
Buzzer
Connect to the first pin of second header (D8) the "+" buzzer pin and the "-" to GND
Ultrasonic Sensor
We will use the third and fourth header, make the following connections:
- Vcc - 5V (3d header)
- Trig - A2 (3d header)
- GND- GND (3d header)
- Echo - A3 (4th header)
Motors (look your robot from behind)
Right DC Motor : Red cable to "M1+" and black to "M1-"
Left DC Motor: Red cable to "M2+" and black to "M2-"
Power - Battery holder
Connect it to shield "Vin -GND" screw terminal. Add one more cable and connect it with "Vin" pin of Arduino Uno board. If you want you can add an ON/OFF SW on the red cable of your battery holder pack.
The code
Make it alive by program it with the following code!
Notes
Notes
- If your robot moves to fast and hit the objects, change the speed variable in line 26. (PWM value)
- If your robot doesn't move forward, go to lines 43 and 44 and add to "speedPWM" an offset. Make sure that the sum is bellow 255. In my case I need to add plus 50 to left motor, so my right motor has a PWM speed value 150 and left 250.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | /* Arduino Object Avoiding Robot with Ultrasonic Sensor * Dev: Michalis Vasilakis // Date: 15/7/2017 // www.ardumotive.com */ //Libraries #include <Servo.h> #include "Ultrasonic.h" //Constants - Connections const int buzzer = 8; const int motorA1= 2; const int motorA2= 6; const int enableA = 3; const int motorB1= 10; const int motorB2= 9; const int enableB = 5; Ultrasonic ultrasonic(A2 ,A3); //Create Ultrsonic object ultrasonic(trig pin,echo pin) Servo myservo; //Create Servo object to control a servo //Variables int distance; //Variable to store distance from an object int checkRight; int checkLeft; int pos=100; //Variable to store the servo position. By default 90 degrees - sensor will 'look' forward int speedPWM = 150; //Change speed (PWM max 255) void setup() { myservo.attach(7); //Servo pin connected to pin 7 myservo.write(pos); // tell servo to go to position in variable 'pos' pinMode(buzzer, OUTPUT); pinMode(motorA1,OUTPUT); pinMode(motorA2,OUTPUT); pinMode(motorB1,OUTPUT); pinMode(motorB2,OUTPUT); pinMode(enableA, OUTPUT); pinMode(enableB, OUTPUT); delay(5000); //Wait 5 seconds... } void loop(){ analogWrite(enableA, speedPWM); analogWrite(enableB, speedPWM+50); //Read distance... distance = ultrasonic.Ranging(CM); //Tip: Use 'CM' for centimeters or 'INC' for inches delay(20); //Check for objects... if (distance > 15){ forward(); //All clear, move forward! noTone(buzzer); } else if (distance <=15){ stop(); //Object detected! Stop the robot and check left and right for the better way out! tone(buzzer,500); // play a tone //Start scanning... for(pos = 30; pos < 170; pos += 1){ //goes from 0 degrees to 180 degrees myservo.write(pos); //tell servo to go to position in variable 'pos' delay(10); //waits 10ms for the servo to reach the position } checkLeft = ultrasonic.Ranging(CM); //Take distance from the left side for(pos = 170; pos>=30; pos-=1){ //goes from 180 degrees to 0 degrees myservo.write(pos); //tell servo to go to position in variable 'pos' delay(10); //waits 10ms for the servo to reach the position } checkRight= ultrasonic.Ranging(CM); myservo.write(100); // Sensor "look" forward again //Finally, take the right decision, turn left or right? if (checkLeft < checkRight){ left(); delay(500); // delay, change value if necessary to make robot turn. } else if (checkLeft > checkRight){ right(); delay(500); // delay, change value if necessary to make robot turn. } else if (checkLeft <=10 && checkRight <=10){ backward(); //The road is closed... go back and then left ;) left(); } } delay(150); } void forward(){ digitalWrite(motorA1, HIGH); digitalWrite(motorA2, LOW); digitalWrite(motorB1, HIGH); digitalWrite(motorB2, LOW); } void backward(){ digitalWrite(motorA1, LOW); digitalWrite(motorA2, HIGH); digitalWrite(motorB1, LOW); digitalWrite(motorB2, HIGH); } void left(){ digitalWrite(motorA1, HIGH); digitalWrite(motorA2, LOW); digitalWrite(motorB1, LOW); digitalWrite(motorB2, HIGH); } void right(){ digitalWrite(motorA1, LOW); digitalWrite(motorA2, HIGH); digitalWrite(motorB1, HIGH); digitalWrite(motorB2, LOW); } void stop(){ digitalWrite(motorA1, LOW); digitalWrite(motorA2, LOW); digitalWrite(motorB1, LOW); digitalWrite(motorB2, LOW); } |
Download the code from here and open it with Arduino IDE. Inside you will also find the ultrasonic library file.
|
![]()
|
Well Done!
Well...that's it!
I hope you liked this, let me know in the comments! Post me some photos of your Arduino robot!
I hope you liked this, let me know in the comments! Post me some photos of your Arduino robot!