Add Obstacle Detecting Whiskers to Your Robot




Introduction: Add Obstacle Detecting Whiskers to Your Robot

After a particularly tasty cheese sandwich I found myself thinking of mice. More precisely how to make my robot more mouse like. The answer, whiskers. So here is an Instructable on how to add the simplest of sensors to your robot, to get it detecting obstacles in its path and making decisions based on what it encounters. Well let's get started.

While this Instructable uses some extra acrylic pieces and special wire this is an upgrade that could easily be accomplished using several paper clips and a little ingenuity.

A quick video of my Arduino Controlled Servo Robot - (SERB) bumping its way around.

(Shameless Plug)
To check out more delightfully fun open source projects please visit

Step 1: Parts & Tools

The items we need to collect before getting started:

Robot - (Arduino Controlled Servo Robot - (SERB) )
( build your own , or purchase a lovely kit ($175)
  • We use an Arduino Controlled Servo Robot - (SERB) however any Arduino powered dual servo robot will work. Other options (here)

Acrylic - (several options)
  • Cut Your Own (Scroll Saw) -- Download the scroll saw pattern (02-WHIS-Scrollsaw Pattern.pdf) glue it to a piece of 3mm Acrylic, cut and drill
  • Cut Your Own (Laser Cutter) -- Download (00-WHIS-Whisker Holders.eps) and cut it on your laser cutter using 3mm acrylic
  • Purchase A Whisker Kit -- Available from (here)

Hardware - (all available from Home Depot)
  • 3mm x 15mm bolt (x3)
  • 3mm x 10mm bolt (x4)
  • 3mm nut (x7)
  • 3mm washer (x1)
  • Whisker Wire (14 Gauge) (120 cm)
  • Wire (22 AWG solid)

Step 2: Whisker Making

Step 1 - (Twisting the Whisker Wire)
  • To make our whiskers a tad more stiff we will double up the wire. To do this cut your wire into two 60 cm lengths.
  • Secure one end of the wires in a vise. and the other end in the chuck of a drill (squeezing the two pieces together so when you turn the drill on it twists the wire)
  • Sandwich one 15mm bolt, between the two wires 30cm from the end.
  • Turn the drill on and go until the wire is tightly twisted, with the 15mm bolt secured in the middle.

Step 2 - (Bending the Whiskers)
  • Print out the whisker bending guide (04-WHIS-Whisker Bending Guide.pdf)
  • Bend your wire into the pattern (I used a pair of needle nose pliers)
(no need to fret about being precise, the included pattern is one which works well but any number will suffice)

Step 3: Assembly

Time to attach all the pieces to your robot.

Step 1 - (Whiskers to WHIS-01)
  • Slip the whisker bolt through the top middle hole in WHIS-01
  • Attach a 10 cm length of black wire to the back of this bolt before adding a nut

Step 2 - (WHIS-01 to the robot)
  • Using two 3mm x 15mm bolts to attach WHIS-01 and the whiskers to your robot

Step 3 - (Whisker Pickup Wire to WHIS-02) x2
  • Strip about 5cm of insulation off the end of two pieces of wire (15cm long)
  • Wrap the wire through the slot and around the back of WHIS-02' opening (example of how it looks in the foreground of the photo below)

Step 4 - (Fastening WHIS-02 to the Robot)
  • Using two 3mm x 10mm bolts per side fasten the two WHIS-02's to the robot so they extend over the front of the robot (on the inside)

Step 5 - (Sloting in the whiskers)
  • Tuck the whiskers into the slots in each of the WHIS-02's
  • Bend the whiskers so they naturally rest at the front of the WHIS-02's opening (and it is then pushed to the back when your robot bumps into anything)

Step 6 - Enjoy the Mousiness of your Robot
  • enjoy

Step 4: Wiring

Time to wire everything up. To do this download the wiring diagram (05-(WHIS)-Wiring Diagram.pdf). and follow the steps.

What's Happening
  • Essentially we have added two switches to the front of our robot. The pickups on the two WHIS-02's run to the Arduino (we will make these high by default in our code). When your robot bumps into something the whisker which is connected to ground, presses against these pickups and pulls your Arduino pin low.

On to the programming.

Step 5: Programming

With the whiskers now connected all we need to do is add some code.

What it does
  • The program will drive your robot forward until one of the whiskers encounters something, it will then reverse for 500 milliseconds then turn randomly left or right for a time between 300 and 1500 milliseconds, before continuing forward to encounter another obstacle.

For those interested in simply getting it going there are two options.

Option 1 - Downloading and unzipping
  • Download the attached zip file (
  • Unzip it to your Arduino sketch directory (default /My Documents/Arduino/)
  • Start the Arduino environment and load the sketch (_SERB_Whiskers)
  • upload and start playing

Option 2 - Copy and Pasting
  • Copy the Arduino code from below
  • Paste it into the Arduino development environment.
  • upload and start playing

Appendix 1 - Arduino Code (_SERB_Whiskers)
/***************************************************************** * Arduino Controlled Servo Robot (SERB) - Whisker Avoidance     * * For more details visit:           * *                                                               *   * Behaviour: Uses two wire whiskers, to bump into obstacles and * *            change direction accordingly. It will poll the     * *            whiskers and when one is found to be tripped it    * *            will reverse for 500 milliseconds then             * *            turn randomly left or right for a random time      * *            between 300 and 1500 milliseconds                  *  *                                                               * * Wiring: --Arduino Controlled Servo Robot--                    * *             Right Servo Signal - pin 9                        *   *             Left Servo Signal  - pin 10                       * *         --Whisker Wiring--                                    *  *             right whisker  - pin 6                            * *             left whisker   - pin 7                            * *             whisker common - ground                           * *                                                               * * License: This work is licenced under the Creative Commons     * *          Attribution-Share Alike 3.0 Unported License. To     * *          view a copy of this licence, visit                   * *       * *          or send a letter to Creative Commons, 171 Second     * *          Street, Suite 300, San Francisco, California 94105,  * *          USA.                                                 *   *                                                               *          ****************************************************************///----------------------------------------------------------------// START OF WHISKER PREAMBLE#define RIGHTWHISKER 6  //the pin the right whisker is attached to#define LEFTWHISKER 7   //the pin the left whisker is attached to//----------------------------------------------------------------// START OF ARDUINO CONTROLLED SERVO ROBOT (SERB) PREAMBLE#include <Servo.h>#define LEFTSERVOPIN  10  //The pin the left Servo is connected to#define RIGHTSERVOPIN  9  //The pin the right Servo is connected to#define MAXSPEED 30  //due to the way continuous rotation servos                      //work maximum speed is reached at a much                      //lower value than 90 (this value will change                      //depending on your servos) (for Parallax servos)                     //20 will give you full range 10 makes it very                     //controllable but a little slowServo leftServo; Servo rightServo; int leftSpeed;        //sets the speed of the robot (left servos)                       //a percentage between -MAXSPEED and MAXSPEEDint rightSpeed;       //sets the speed of the robot (both servos)                       //a percentage between -MAXSPEED and MAXSPEEDint speed = 100;      //used for simple control (goForward etc.)                      //a percentage between 0 and MAXSPEED// END OF THIS PROGRAMS PREAMBLE//----------------------------------------------------------------void setup(){  beginSerial (9600);   //Starts the serial port   serbSetup();          //adds the servos and prepares all                         //SERB related variables  whiskerSetup();                        }/* * The main loop * Sends the robot forward then checks it&aposs whiskers, if one is pressed * the robot will reverse then turn and head forward once more   */void loop(){  goForward();      //sends the robot forward  checkWhiskers();  //checks to see if a whisker is pressed}//------------------------------------------------------------------------//START WHISKER ROUTINESvoid whiskerSetup(){  pinMode(RIGHTWHISKER, INPUT);    //Sets the right whisker&aposs pin to be an input  pinMode(LEFTWHISKER, INPUT);     //Sets the left whisker&aposs pin to be an input  digitalWrite(RIGHTWHISKER, HIGH);//Sets the right whisker pin&aposs internal pullup                                    //resistor (this means the pin is high when                                    //there is no signal attached and reads low                                    //when a negative signal is attached  digitalWrite(LEFTWHISKER, HIGH);//Sets the left whisker pin&aposs internal pullup                                   //resistor}void checkWhiskers(){  if(!digitalRead(RIGHTWHISKER)){reverseAndTurn();} //if the right whisker is pressed                                                    //then reverse and turn  if(!digitalRead(LEFTWHISKER)){reverseAndTurn();}  //if the left whisker is pressed                                                    //then reverse and turn}void reverseAndTurn(){ goBackward();         //goes backward delay(500);           //for half a second turnRandom(300,1500); //turns randomly for a time between 300 and 1500 milliseconds}//END OF WHISKER ROUTINES//------------------------------------------------------------------------//------------------------------------------------------------------------//START OF ARDUINO CONTROLLED SERVO ROBOT (SERB) ROUTINES/* * sets up your arduino to address your SERB using the included  * routines */void serbSetup(){  setSpeed(speed);  pinMode(LEFTSERVOPIN, OUTPUT);     //sets the left servo signal pin   //to output  pinMode(RIGHTSERVOPIN, OUTPUT);    //sets the right servo signal pin   //to output  leftServo.attach(LEFTSERVOPIN);    //attaches left servo  rightServo.attach(RIGHTSERVOPIN);  //attaches right servo  goStop();}/* * sets the speed of the robot between 0-(stopped) and 100-(full speed) * NOTE: speed will not change the current speed you must change speed  * then call one of the go methods before changes occur. */void setSpeed(int newSpeed){  if(newSpeed >= 100) {    newSpeed = 100;  }     //if speed is greater than 100         //make it 100  if(newSpeed <= 0) {    newSpeed = 0;  }         //if speed is less than 0 make             //it 0   speed = newSpeed * MAXSPEED / 100;        //scales the speed to be                                             //between 0 and MAXSPEED}/* * sets the speed of the robots rightServo between -100-(reversed) and 100-(forward) * NOTE: calls to this routine will take effect imediatly */void setSpeedRight(int newSpeed){  if(newSpeed >= 100) {    newSpeed = 100;  }     //if speed is greater than 100         //make it 100  if(newSpeed <= -100) {    newSpeed = -100;  }   //if speed is less than -100 make       //it -100   rightSpeed = newSpeed * MAXSPEED / 100;   //scales the speed to be                                             //between -MAXSPEED and MAXSPEED  rightServo.write(90 - rightSpeed);        //sends the new value to the servo}/* * sets the speed of the robots leftServo between -100-(reversed) and 100-(forward) * NOTE: calls to this routine will take effect immediatly */void setSpeedLeft(int newSpeed){  if(newSpeed >= 100) {newSpeed = 100;} //if speed is greater than 100                                         //make it 100  if(newSpeed <= -100){newSpeed = -100;}//if speed is less than -100                                         //make it -100   leftSpeed = newSpeed * MAXSPEED / 100;//scales the speed to be                                         //between -MAXSPEED and MAXSPEED  leftServo.write(90 + leftSpeed);      //sends the new value to the servo}/* * turns the robot randomly left or right for a random time period between * minTime (milliseconds) and maxTime (milliseconds) */void turnRandom(int minTime, int maxTime){  int choice = random(2);                     //Random number to decide                                               //between left (1) and right (0)  int turnTime = random(minTime,maxTime);     //Random number for the pause                                               //time  if(choice == 1){ goLeft();}                 //If random number = 1 then turn                                               //left  else {goRight();}                           //If random number = 0 then turn                                               //right  delay(turnTime);                            //delay for random time                         }/* * sends the robot forwards */void goForward(){  leftServo.write(90 + speed);  rightServo.write(90 - speed);}/* * sends the robot backwards */void goBackward(){  leftServo.write(90 - speed);  rightServo.write(90 + speed);}/* * sends the robot right */void goRight(){  leftServo.write(90 + speed);  rightServo.write(90 + speed);}/* * sends the robot left */void goLeft(){  leftServo.write(90 - speed);  rightServo.write(90 - speed);}/* * stops the robot */void goStop(){  leftServo.write(90);  rightServo.write(90);}//END OF ARDUINO CONTROLLED SERVO ROBOT (SERB) ROUTINES//---------------------------------------------------------------------------

Step 6: Finished

Hopefully you have yourself a driving bumping robot with some twist thrown in.

If not drop us a comment or send an e-mail to and we'll try our very best to get you up and running.

We love sharing. If you've added whiskers to your Robot and would like to have a picture and a link back to you here send us a message and some photos. (

(Shameless Plug)
If you'd like to check out more delightfully fun open source projects you can find them at

Be the First to Share


    • Puzzles Challenge

      Puzzles Challenge
    • Rice & Grains Challenge

      Rice & Grains Challenge
    • Lamps Challenge

      Lamps Challenge



    7 years ago on Step 5

    Yes, I found the error in the code. It is a bit too long to place in this comment, however I included the .ino file as an attachment to my blog entry in classroom 2.0. It is called SERB Whiskers2.ino.

    It has been tested and tried in my fifth grade classroom and all works well.


    9 years ago on Step 5

    Awesome idea! I have a problem on the last step, bummer. When I try to verify the code in the Arduino code builder it gives me an error saying something is wrong with this line

    beginSerial (9600); //Starts the serial port

    Any idea what's wrong? Thanks again.


    13 years ago on Introduction

    Cool but I think that's way too stiff to make whiskers it's even pushing the obstacles when it bumps into them. I'd use guitar string or something more sensitive and whisker-like.