This is what it looked like once I attached everything

The most crucial part of any mobile robot is the ability to avoid obstacles. What good is a robot that just runs into everything? Thankfully, object avoidance is an easy goal to achieve. With just a few parts and less than 100 lines of code, anyone can build a basic object avoiding robot!

The avoider

The Parts

All the robotics parts (Ignore how it is wired up in this picture!)

1 x DFRobot Romeo V1.3 All-In-One Microcontroller

2 x HC-SR04 Ultrasonic Object Detectors

1 x 2WD Mini Robot Frame

1 x 9V To Barrel Jack Connector

Cardboard

Electrical Tape

Mounting The Electronics

Making the cardboard sensor mount and attaching all the electronics

You’ll need to assemble the frame and figure out a way to mount the board, sensors, and battery. I used cardboard and electrical tape… So unless you’re going for a professional appearance, don’t worry about how it’s put together. However you want to do it is perfectly fine. It doesn’t have to be perfect! We’re just having fun, anyways!

This is what it looked like once I attached everything

I used a small cardboard box that one of the sensors came in to raise the Romeo board above the wheels, then just electrical taped it to the frame. After that, I made a sensor mount using some taped together pieces of cardboard. The HC-SR04 sensors were also mounted with electrical tape, and I angled them slightly outwards to have more directional sensing. I ended up removing the rear swivel wheel and just letting the rear end slide around. The swivel wheel didn’t swivel very well, so it would get stuck facing one direction and make the robot turn unnecessarily.

 

Connecting Everything Together

Connection diagram

I wired the left sensor’s trigger pin to digital pin 2 on the Romeo, and the echo pin to digital pin 3. The right sensor’s trigger pin goes to digital pin 8, and the echo pin goes to digital pin 9. The Vcc wires coming from the sensors need to be spliced together into one wire and then that wire goes to 5v on the board. The Gnd wires from the sensors also need to be spliced together into one wire that connects to Gnd on the board. The motors just need to be hooked up to the green motor terminals on the Romeo. Left motor to M2 and right motor to M1.

Once, the robot has been assembled, the code can be uploaded and you can test it out!

Uploading The Code

Uploading code to the board is easy!

1. Download and install the Arduino IDE on your computer.

2. Plug in the board to the computer.

3. Select “Arduino Uno” from the Tools -> Board menu, if you are using the DFRobot Romeo board I recommended.

4. Copy and paste the code from this page into the IDE.

5. Click the upload button and you’re finished!

Here’s the code:

int trigPinLeft = 2;   
int echoPinLeft = 3;   
long durationLeft, cmLeft, inchesLeft;
int trigPinRight = 8;   
int echoPinRight = 9;   
long durationRight, cmRight, inchesRight;
void setup() {
 pinMode(trigPinLeft, OUTPUT);
 pinMode(echoPinLeft, INPUT);
 pinMode(trigPinRight, OUTPUT);
 pinMode(echoPinRight, INPUT);
}
void loop()
{
 digitalWrite(trigPinLeft, LOW);
 delayMicroseconds(5);
 digitalWrite(trigPinLeft, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPinLeft, LOW);
 pinMode(echoPinLeft, INPUT);
 durationLeft = pulseIn(echoPinLeft, HIGH);
 cmLeft = (durationLeft/2) / 29.1;
 inchesLeft = (durationLeft/2) / 74;
 delay(100);
 digitalWrite(trigPinRight, LOW);
 delayMicroseconds(5);
 digitalWrite(trigPinRight, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPinRight, LOW);
 pinMode(echoPinRight, INPUT);
 durationRight = pulseIn(echoPinRight, HIGH);
 cmRight = (durationRight/2) / 29.1;
 inchesRight = (durationRight/2) / 74;
 delay(100);
 if(inchesLeft<=10&&inchesRight<=10){
   analogWrite(6,127);
   analogWrite(5,127);
   digitalWrite(4,HIGH);
   digitalWrite(7,LOW);
   delay(1000);
 }
 if(inchesLeft>10&&inchesRight>10){
   analogWrite(6,127);
   analogWrite(5,127);
   digitalWrite(4,HIGH);
   digitalWrite(7,HIGH);
 }
 if(inchesLeft<=10&&inchesRight>10){
   analogWrite(6,127);
   analogWrite(5,127);
   digitalWrite(4,LOW);
   digitalWrite(7,HIGH);
 }
 if(inchesLeft>10&&inchesRight<=10){
   analogWrite(6,127);
   analogWrite(5,127);
   digitalWrite(4,HIGH);
   digitalWrite(7,LOW);
 }
}

Robots are easy so get the making started!

6 COMMENTS

  1. Hi, I do believe this is an excellent blog. I stumbledupon it
    😉 I will come back yet again since i have bookmarked it. Money and freedom is the
    best way to change, may you be rich and continue to guide other people.

  2. Simply want to say your article is as surprising.
    The clarity on your submit is simply nice and that i can suppose you’re an expert in this subject.
    Well together with your permission allow me to seize your
    RSS feed to stay up to date with coming near near post.
    Thank you a million and please carry on the gratifying work.

LEAVE A REPLY

Please enter your comment!
Please enter your name here