# Wheeled Robot Motoric With 3 Ultrasonic Sensors

by : Antonius (@sw0rdm4n)
http://www.ringlayer.net

A. PART LISTS

- 1 sn75441one or l293dne (h-bridge ic)

- 1 Arduino Uno

- 3 HC-SR04 Ultrasonic Sensor (Front, Left and Right Sensor)

- Arduino smart Car Robot Plastic Tire Wheel with DC 3-6v Gear Motor

- Any Wheeled Chassis

You can make any chassis easily using acrylic, For example here:

For this robot, I modify my kitchen plastic supplies in order to make my own wheeled chassis:

- Mini Trolley Wheel (Front Wheel)

- Jumper Wires

- 9 volt / 8,4 volt battery

– Some Bauds

B. H-BRIDGE CIRCUIT

In order to make a robot who can avoid front, left and right obstable. We need to make a robot who can turn right, left and turn back.  For that reason, we need a circuit that can switch direction of voltage.  For that, we can use h-bridge, For example here, we can use sn75441one or l293dne. Since a motor dc can reverse direction of spins when a voltage switch to other direction. To make a h-bridge circuit, you can follow this tutorial : http://www.instructables.com/id/Duel-Motor-Driver-with-Arduino-using-a-SN754410NE-/

The method to turn right or left is easy, this robot actually ony has 2 wheel for motoric : left wheel and right wheel.  In order to turn left, right wheel must turn forward and left wheel must turn backward. Meanwhile, in order to turn right, left wheel must turn forward and right wheel must turn backward.

C. HC-SR04 ULTRASONIC SENSOR

HC-SR04 works based on this formula : Speed = Distance / Time. This sensor works by sending a ping of ultrasonic sensor, then calculate. In order to use hc-sr04 we can use NewPing library taken from here : http://code.google.com/p/arduino-new-ping/

For front sensor, connect trig pin to pin 12 on arduino, connect echo pin to pin 13 on arduino.

For left sensor, connect trig pin to pin 10 on arduino, connect echo pin to pin 11 on arduino.

For right sensor, connect trig pin to pin 6 on arduino, connect echo pin to pin 7 on arduino.

Here’s the code for arduino :

```/*
Simple wheeled robot motoric with 3 sensor(s) - front, left and right

http://www.ringlayer.net

*/
#include <NewPing.h>

/* front sonar */
#define TRIG_DEPAN_ATAS  12
#define ECHO_DEPAN_ATAS     13

/* left sonar */
#define TRIG_KIRI  10
#define ECHO_KIRI     11

/* right sonar */
#define TRIG_KANAN  6
#define ECHO_KANAN    7

#define MAX_DISTANCE 200
NewPing sonar(TRIG_DEPAN_ATAS, ECHO_DEPAN_ATAS, MAX_DISTANCE);
NewPing sonar_kiri(TRIG_KIRI, ECHO_KIRI, MAX_DISTANCE);
NewPing sonar_kanan(TRIG_KANAN, ECHO_KANAN, MAX_DISTANCE);

/* Modified from http://itp.nyu.edu/physcomp/Labs/DCMotorControl */

int motorkanan_arah_depan = 4;
int motorkanan_arah_belakang = 5;
int enablePin = 9;
int motorkiri_arah_depan = 2;
int motorkiri_arah_belakang = 3;
unsigned int sensor1_val = 0;
unsigned int sensorkiri_val = 0;
unsigned int sensorkanan_val= 0;
int i = 0;

void setup() {
Serial.begin(9600);
pinMode(motorkanan_arah_depan, OUTPUT);
pinMode(motorkanan_arah_belakang, OUTPUT);
pinMode(motorkiri_arah_depan, OUTPUT);
pinMode(motorkiri_arah_belakang, OUTPUT);
digitalWrite(enablePin, HIGH);
}
void mundur_long()
{
digitalWrite(motorkanan_arah_depan, LOW);
digitalWrite(motorkanan_arah_belakang, HIGH);
digitalWrite(motorkiri_arah_depan, LOW);
digitalWrite(motorkiri_arah_belakang, HIGH);
delay(500);
}
void berhenti()
{
digitalWrite(motorkanan_arah_depan, LOW);
digitalWrite(motorkanan_arah_belakang, LOW);

digitalWrite(motorkiri_arah_depan, LOW);
digitalWrite(motorkiri_arah_belakang, LOW);
delay(500);
}
void maju()
{
unsigned int uS = sonar.ping();

sensor1_val = uS / US_ROUNDTRIP_CM;
Serial.println("\nsensor depan val : ");
Serial.print(sensor1_val);
Serial.print(" cm\n");
if (sensor1_val < 50 && sensor1_val > 0) {
mundur_long();
avoid();
}
Serial.println("\nmaju\n");
digitalWrite(motorkanan_arah_depan, HIGH);   //right
digitalWrite(motorkanan_arah_belakang, LOW);
digitalWrite(motorkiri_arah_depan, HIGH);   //left
digitalWrite(motorkiri_arah_belakang, LOW);
delay(50);

}
void op()
{
maju();
}
void avoid()
{
int loopback;
unsigned int uSkiri = sonar_kiri.ping();
unsigned int uSkanan = sonar_kanan.ping();

sensorkiri_val = uSkiri / US_ROUNDTRIP_CM;
sensorkanan_val = uSkanan / US_ROUNDTRIP_CM;
Serial.println("if block executed");
Serial.println("\nsensor kiri val : ");
Serial.print(sensorkiri_val);
Serial.print(" cm\n");
Serial.println("\nsensor kanan val : ");
Serial.print(sensorkanan_val);
Serial.print(" cm\n");
if (sensorkiri_val > 60) {
Serial.println("belok kiri");
berhenti();
belok_kiri();
}
else if (sensorkanan_val > 60){
Serial.println("belok kanan");
berhenti();
belok_kanan();
}
else {
for (loopback = 0; loopback < 2; loopback++) {
mundur_long();
}
}
}
void belok_kanan()
{
digitalWrite(motorkanan_arah_depan, LOW);
digitalWrite(motorkanan_arah_belakang, HIGH);
digitalWrite(motorkiri_arah_depan, LOW);
digitalWrite(motorkiri_arah_belakang, LOW);
delay(500);
}
void belok_kiri()
{
digitalWrite(motorkanan_arah_depan, HIGH);
digitalWrite(motorkanan_arah_belakang, LOW);
digitalWrite(motorkiri_arah_depan, LOW);
digitalWrite(motorkiri_arah_belakang, HIGH);
delay(500);
}
void loop() {
op();
}
```

Once success, When the robot avoid front obstacle, it will then check left for another obstacle existence, if no obstable on left, it will then turn left. Otherwise, if there’s another obstable on left, it will then check for right side for obstable, if there’s an obstable, it will turn back, otherwise it will turn right.