Object avoidance with the Ultrasonic sensor bar


For this example we will show you how to build a simple (but cool) object avoiding bot, using the BoxBot robot chassis. This will hopefully give you an understanding of simple logic and connecting sensors to your microcontroller.

Items you will need for this project



BoxBot robot chassis

BoxBot Ultrasonic Sensor Bar

Arduino

Servos

Jump Wire Kit

You will also need 4 AAA Batteries, and one 9v PP3 rectangular battery.

Prerequisits


This example assumes you are familiar with the Arduino IDE, connection and uploading. If not we suggest you visist www.arduino.cc and read their getting started and learning guides.


Overview & Descripton


This project aims to get your BoxBot roaming around avoiding objects without ever coming into contact with them! To do this we use ultrasonics. This method is essentially the same as "sonar". A ultrasonic sound is emitted from the sensor, this is reflected back by the target object and received by the sensor. The amnount of time taken for the sound to be refected back can be calculated to give a distance measurement.

To get the bot to avoid objects, we set it off on it's travels and have it look for objects in it's way. Everything in front of it is measured and when an object gets closer than a set threshold, the bot turns away from it. This loop continues indefinitely (or until the batteries rundown).

The ultrasonic sensor in the sensorbar is actually a very smart bit of gear. It has an onboard micro controller, temperature correction and a host of interface methods. As part of the kit the sensor bar is connected and configured for TTL (Transistor to Trasistor Logic). As you progress further in your robot experiments, you may want to take advantage of the other things you can do with the sensor, but for this example we will use it as it comes in the kit.

The sensor will output distance readings in centimeters (cms), which is ideal for easy object avoidance code. As we are using just one sensor, the code will simply turn the bot in the same direction everytime it detects a turn is needed. Adding another sensor will enable the bot to make a decision on the best clear route. As the BoxBot is small, there is only enough space for one ultrasonic sensor.

Project video





The circuit


White wires are shown in grey for clarity.


Make up this circuit on your breadboard. The wire colours are just for clarity on the diagram and do not relate to the jump wire kit colours, however the wires on the servos and sensor are true. The circuit connects the Arduino to the servos, and attaches the ultrasonic sensor to pins 5v, GND, Digital 2 & 3.
Pay attention to the polarity, and the servo connections. Double check all your connections are correct before connecting power. Short circuits and reverse polarity can damage the Arduino, sensors and servos.

Adding the required library to your Arduino IDE


As the ultrasonic sensor needs a library installed to allow easy access to it, you will need to do the following:
  • Download this zip file
  • Extract the contents to your arduino-XXXX/hardware/libraries folder (xxxx is your version number)


The code


(Arduino only)

// Bot builder BoxBot sonar code for the ultrasonic sensor bar #include "URMSerial.h" #include <Servo.h> Servo lservo; Servo rservo; // The measurement we're taking #define DISTANCE 1 #define TEMPERATURE 2 #define ERROR 3 #define NOTREADY 4 int ledPin = 13; URMSerial urm; void setup() { Serial.begin(9600); // Sets the baud rate to 9600 urm.begin(2,3,9600); // RX Pin, TX Pin, Baud Rate pinMode(ledPin, OUTPUT); lservo.attach(9); // attaches the servo on pin 9 to the servo object rservo.attach(10); // attaches the servo on pin 10 to the servo object } void loop() { // Request a distance reading from the URM37 urm.requestMeasurement(DISTANCE); // You may call this as many times as you like. It will only send once // Avoid fetching the distance until we're sure the reading is ready if(urm.hasReading()) { int value; // This value will be populated switch(urm.getMeasurement(value)) // Find out the type of request { case DISTANCE: // Double check the reading we recieve is of DISTANCE type Serial.println(value); // Fetch the distance in centimeters from the URM37 if (value < 25) // Less than 25cms turn, else carry on. { digitalWrite(ledPin, HIGH); lservo.write(110); rservo.write(110); } else { digitalWrite(ledPin, LOW); lservo.write(110); rservo.write(50); } delay(200); break; } } }

Testing


Power up your bot. Servos 1st, Arduino 2nd. The light on the sensor bar will light up if you have connected everything correctly. The bot will start to move. Place an object in front of it and it should turn away from it and carry on. If it does not, remove all power and double check your blue and green wires from the sensor bar are in the correct places on the Arduino.

That's it, now you can experiment with different distances, or make you bot reverse and turn the other way.