loading
This is the third part of our BaW-Bot (Bells-and-Whistles Bot) build – 5 separate instructables that look into different Arduino-related technologies, combining to create a Bot with all the bells-and-whistles.

Part 1: Build an Arduino on a Board
Part 2: Build the motor-controller & body
Part 3: Adding Sight and Touch
Part 4: Blinging up the BaW-Bot
Part 5: Taking it to the Next Level

In this instructable, we’ll be giving our Bot some freedom by adding sensors to keep it safe.  We’ll be adding an ultrasonic sensor (“eyes”) as well as 2 lever microswitches (“whiskers”).  We’ll also be building a “leash” so that we can keep it out of trouble while we develop the bot.

Step 1: The Parts

You'll need the following parts, as well as the body and Arduino you built in Parts 1 & 2 of this series.  There are many different models and manufacturers - I've simply linked to the parts I have, you can opt for your own preferred supplier/model:

1 x IR Receiver (38KHz)
1 x Household Infrared Remote Control (TV / stereo / iPod / etc.)
1 x Ultrasonic Range Sensor
2 x Lever Snap Action switches
Connector wires
2 x 10k Ohm resistors

Step 2: Rein the Bot In

You may have struggled with the Bot during the previous instructable, as it starts driving around as soon as you connect the power.  Let’s get it under control, so that it only starts moving once you press a button on a remote – and stops moving when you press the same button again.  The “stop” button will become useful as you start experimenting with sketches that help the robot avoid obstacles.  As you know, most remotes use Infrared (IR) – so the next few steps will take us through implementing this.  We could have used a simple push-button, but this is more fun…

Help from Ken
Ken Shirriff has written a few great blogs on getting IR working on an Arduino (check out http://www.arcfn.com/), and has also written a very useful library.  We’ll use that library for this instructable (thanks Ken!) – even though we won’t use close to all the functionality it offers.

To begin, download the library from the link on Ken’s page on his Infrared Remote Library and unzip it into your Arduino folder:
arduino\libraries. 

If you’re using  version 1.0 or higher of the Arduino  IDE, you’ll need to edit the file “IRRemoteInt.h” and change the line:
#include <WProgram.h>
to
#include <Arduino.h>

Then restart the IDE.

Step 3: Connect the IR Receiver

Remove the Boards
Remove the breadboards from the body, so we can work on them more easily.  Make sure you disconnect the motor power supply - we don’t want the Bot to run away from us while we’re testing.

Connect the IR Receiver
Refer to the fact sheet for the IR Receiver – you’ll see it has 3 pins – Vs, GND and OUT.  For this test, mount the IR Receiver on the Arduino breadboard, and connect:
  • Vs to the +ve power rail
  • GND to the GND power rail
  • OUT to pin 12 (i.e. physical pin 18 on the ATmega chip).
This is pretty self-explanatory – the module will be powered by Vs and GND, and will send a signal to Pin 12 when it receives an IR signal.

Step 4: Test the IR Receiver

Upload a Test Sketch
Upload the sketch below using the FTDI breakout board.  This was written by Ken, and simply prints out any IR codes the sensor receives, to the Serial monitor (I have modified to print decimal instead of Ken’s original hex).

Test a few Remotes
Once the sketch is uploaded, start the Serial Monitor in the Arduino IDE (Tools, Serial Monitor).

Point a number of remote controls at the sensor, and watch what comes out on the serial monitor when you press their various buttons.  You should see output similar to the image above.  Do you notice that each button generates a slightly different code?  These unique codes will allow us to write a sketch that reacts to a particular code – i.e. a particular button on a remote.
In some cases one button might generate multiple codes, or holding a button down could result in a single code repeating – we’ll ignore these for this project.

Copy the Code for your chosen button
Now that you’ve seen how the codes appear, press the button that you want to use to stop/start the BaW-Bot.  Now copy and paste the code from the Serial Monitor into a temporary test document – we’ll need it later.

For my bot, I used the play/pause button on my iPod docking station remote.  This generated 2 codes, so I just took the first one:  “2011265621

Now let's put this code to good use.

Step 5: Get Things Under Control

Before we continue, let’s see how the remote works with your bot.  Put the boards back into the BaW-Bot, and download the sketch below.  This is a very simple sketch that moves and turns the bot as you press your chosen button on the remote.

Once you’ve opened the sketch in your Arduino IDE, make sure you replace the IR code from the previous step onto the line that reads (replace the portion in bold):
const unsigned int irCodeStartStop = 2011265621;

Connect your batteries, and the bot should not move.  Press the button and it should for each press, move forward / stop / turn / stop.



Now that you’ve got your BaW-Bot under control, you are one step closer to setting him free to explore.

Step 6: Give the Gift of Sight

To keep our BaW-Bot safe, we’re going to give him sight – this will stop him from bumping into most things.  In fact, we’re not actually giving him sight, but more sound – incredibly fine-tuned hearing – by connecting an ultrasonic rangefinder.

To use this module, we send out a “ping” (like a submarine’s sonar) and time how long it takes to bounce back.  The time taken tells us how far away the object is, and whether we should keep going or panic and drop the anchors.

First up, we need to mount the ultrasound module onto the robot.  If you’ve also used cardboard for the body, then you can simply cut a hole and fit the sensors through the holes – if you’ve used a conductive material you’ll need to mount it and ensure that it is insulated from the body.

Connections
The connections are also extremely simple – the same as the IR Receiver, in fact:
  • Connect VCC to the +ve power rail
  • Connect GND to the –ve power rail
  • Connect SIG to pin 4 on the Arduino (i.e. physical pin 6)
You’ll note when you look in the sketch, that we use pin 4 as an output pin (to generate the “ping”) and then switch it to an input pin (to receive the “ping”).

Step 7: Add a Sense of Touch

If you read the fact sheet on the ultrasonic rangefinder, you’d see that it has some fairly large blind-spots – particularly when approaching an obstacle at an acute angle.  To cover our bases, we’re going to give the robot some whiskers, to pick up those small items that escape the sonar.  These whiskers will take the form of lever microswitches which project beyond the edge of the body.


Solder Connectors onto the Switches
The best way to connect the switches to the project is to solder wire connectors on the switch contacts.  You’ll see that I am using SPDT (Single Pole, Double Throw) switches, but for this project you can use SPST (Single Pole, Single Throw) switches as well.


Cut Holes in the Body
Cut small rectangular holes so that the whiskers project beyond the side of the robot, but the switches are hidden inside. Bolt them onto the body to keep them in place.


Wire the Switches Up
As with all switches and buttons, these do not provide a reliable signal when they are not closed (i.e. making contact) – they float, randomly sending HIGH or LOW signals.  Therefore we need to either pull the switch up or down when it’s open – in this case we want the switch to be LOW so we pull it down with a 10k pull-down resistor.

If you're using a SPDT switch, you'll have 3 connectors - which 2 of these should you use? An easy test is to use a multimeter (or even test it on a breadboard like the circuit in the image above, adding an LED) and make sure that the circuit is complete when the switch is closed (activated).

For each switch:
  • Connect one terminal to +ve rail
  • Connect the other terminal to breadboard. On the same row in the breadboard:
    • connect a 10k Ohm resistor to GND (the pull-down resistor)
    • connect a jumper to the relevant pin on the Arduino

Connect the left-hand switch to Pin 2 and the right-hand one to Pin 3.

As the board is getting complex now (a bit of a ratsnest), I’ve included an image above of the wiring needed on an empty breadboard for clarity.

Step 8: Set the BaW-Bot Rolling

Now that we’ve finished with all the sensors, let’s get the BaW-Bot moving.  All you need to do now is to upload the sketch below and power the Bot up.

The logic in brief:
As the Bot approaches an obstacle, it slows down and pings the ultrasound more frequently.
When the Bot comes within 10cm (approx. 4 inches) of an obstacle it reverses and turns left.
When the left lever-switch activates it brakes, then reverses and turns right.
When the right lever-switch activates it brakes, then reverses and turns left.

Some comments on the sketch:
All reverse and turn manoeuvres are based on time, not distance – so depending on your motor speed and wheel diameter you may need to alter these.  I’m using slow motors and small wheels, so everything takes longer.

The ultrasound sensor doesn’t always give accurate readings, so I’ve used a simple algorithm I borrowed sometime in the past to calculate an average of 5 values – this smoothes the Bot’s behaviour to an extent.

The obstacle-avoidance logic is far from perfect – it’s meant as a basic platform to get the BaW-Bot running and is there to for you to build on – please share any clever algorithms you come up with!

The sketch is close to utilising the maximum available memory – if you add a whole bunch of variables and Serial.print commands for debugging, it will start overflowing and likely hang at odd moments.  There are ways around this – but that’s for another instructable.

The lever microswitches are connected to the Arduino pins that are interrupt-enabled.  You could expand the sketch by using interrupts to drive the behaviour when the lever-switches are activated.

Thanks for building this with me - please let me have any comments


Coming up next - blinging the BaW-Bot up a little.
Hi! <br>I'm wondering if it's possible to connect up the motor-controller to a Raspberry Pi, so the usage of an Arduino wouldn't be neccesary, because I have no access to one, but I have access to a RPi. <br> <br>Greetings from Germany! ;)
Hi <br> <br>I'm not really sure - as the Pi only has USB ports, you'd need: <br>- some way to turn a single USB port into multiple I/O channels to control the motor driver <br>- some way to address and program these multiple channels. <br> <br>I recommend you make the leap and get an Arduino - they go for about EUR24 and seriously will be worth it! Look at: http://arduino.cc/hu/Main/Buy <br> <br>Good luck <br>
Yeah I will definitely buy one, or build it with your great instrcution. <br>So the motor-controller only needs some I/O which can be programmed? <br> <br>That would be perfect, because the Pi has 26 GPIO Pins which can be programmed thorugh Python, C and other languages :) <br>http://elinux.org/RPi_Low-level_peripherals <br> <br>I'm nearly a beginner to these kind of electronics, so sorry if i ask too much..
OK then that should work OK - I see the Pi only tolerates 3.3V inputs. The motor driver takes 3V as well, so you should be ok. Check the datasheet at: <br>http://www.semicon.toshiba.co.jp/docs/datasheet/en/LinearIC/TB6612FNG_en_datasheet_080509.pdf <br> <br>Good luck - and make sure you post your results! <br> <br>P.S. keep asking - it's a steep learning curve (and I'm still struggling up it!)
One other thing to consider is the amperage of the motor. make sure you got enough current. It seems like the voltage is there, if not enough current you could adopt an arduino motor controller or H bridge to run off your 3 volts I would think. <br> <br>Let us know how it turns out, because im curious at all the possibilities of the Pi, Just a matter of time before I get mine! <br>Good day.
You can get arduino for much cheaper then that almost half that price shop around<br> I got my mini for $14 free shipping dollars.<br> MY Mega I got for 27 or 28 dollars free shipping.<br> <br> <a href="http://dx.com/p/arduino-compatible-component-basic-element-pack-starter-kit-130922?utm_rid=22337885&amp%3Butm_source=affiliate" rel="nofollow">My Uno in a kit for $50 FREE shipping and the kit came with:</a><br> ( This is my affiliat link but I truly think it is a good deal, they also have tons of other arduino related parts for very good prices, all with FREE shipping. )<br> - Package includes:<br> - 1 x Arduino UNO board<br> - 1 x Development expansion board<br> - 1 x Breadboard (10 X 20cm)<br> - 1 x Acrylic plate<br> - 1 x LED emitter kit (red / green / yellow; each 5pcs)<br> - 2 x Buzzer<br> - 4 x Push button switch<br> - 2 x Seven segments display<br> - 2 x Mercury switch<br> - 3 x Light dependent resistor<br> - 1 x Adjustable resistor<br> - 1 x Flame sensor<br> - 1 x Infrared receiver<br> - 1 x Resistor kit (200ohm / 1K ohm / 10K ohm; each 10pcs)<br> - 1 x LM35 temperature sensor<br> - 1 x USB cable (48cm)<br> - 20 x Breadboard cable<br> - 1 x 9V battery<br> - 1 x Battery slot<br> - 1 x Remote control (1 x LR1130 / included)<br> - 2 x Storage case (1-large / 1-small)<br> - 1 x Software CD<br> <br> The only thing to worry about is that once you start playing with arduino you cant go back! it will mack a great companion to your Pi. im waiting for my next invite, my first one the early sign up coupon, got lost in my spam box, and by the time I noticed, they were sold out&nbsp;&nbsp; $@#%!!!!!!

About This Instructable

11,878views

32favorites

License:

Bio: I’m not a rocket scientist. I don’t have a master’s degree in Electrical Engineering. I love automating, hacking, robotics, creating, building, understanding ... More »
More by AndyTallack:Bootload an ATmega328 BaW-Bot Part 3: Adding Sight and Touch to the Bot BaW-Bot Part 2: Build the motor-controller & body 
Add instructable to: