How to Make an Object Detection Robot | Arduino

In this post, I’ll show you how to make an object detection robot using Arduino. This robot will move around a room and when it bumps into an object, it will turn around and go in another direction. Reminds me of the iRobot vacuum cleaner!

Shout out to the late Gordon McComb for this project idea. He is the author of an excellent book that I recommend buying if you’re getting started with robotics: How to Make a Robot.

roomba_discovery

Requirements

Here are the requirements:

  • Make a robot that can turn around and go in a different direction after bumping into an object.

You Will Need

The following components are used in this project. You will need:

Directions

First, I position the snap action switches so that the corners are aligned with the side and front of the lower base of the robot. I will make a mark with a pencil to indicate where I should glue (we’ll glue them down later).

object_detection_robot-2
object_detection_robot-1

Grab three pieces of 6-inch male-to-female jumper wire.

object_detection_robot-3

Cut off the female end of the jumper wire.

object_detection_robot-4

Remove 0.25 inches of insulation from the end of the wire that you just cut.

Twist the loose ends of each wire.

object_detection_robot-5
object_detection_robot-6

Before you solder, it is helpful to first stick the wire strands through the pinhole of the terminal.

object_detection_robot-8
object_detection_robot-9

Solder one wire to the NO metal terminal of a switch (black wire).

Solder one wire to the NO metal terminal of the other switch (brown wire).

Solder one wire to the COM terminal of the switch that will go on the right side of the robot (white wire).

Get 3 inches of solder wire and connect the COM terminals of both switches (yellow wire).

object_detection_robot-7
object_detection_robot-11
object_detection_robot-12
object_detection_robot-10

If you don’t know how to solder, check out this video (skip to the soldering part):

Now we need to test the switches.

Plug the jumper connected to the Right switch COM into Digital Pin 4 on the Arduino.

Plug the jumper connected to the Right switch NO into Digital Pin 2 on the Arduino.

Plug the jumper connected to the Left switch NO into Digital Pin 3 on the Arduino.

object_detection_robot-13

Now, upload the following code to the Arduino. When you press down one of the switches, the built-in LED on the Arduino should light up.

/**
 * This program is to test the snap action switches
 * 
 * @author Addison Sears-Collins
 * @version 1.0 2019-05-12
 */

/*   
 *  This setup code is run only once, when Arudino is 
 *  supplied with power.
 */
void setup() {

  // Pin 13 is LED output
  pinMode(LED_BUILTIN, OUTPUT);
  
  pinMode(2, INPUT);  // Right Switch
  pinMode(3, INPUT);  // Left Switch
  pinMode(4, OUTPUT); // Ground for Switches
  
  digitalWrite(2, HIGH); // Enable pullup resistors
  digitalWrite(3, HIGH); // Enable pullup resistors
  digitalWrite(4, LOW);  // Set ground to 0 volts
}


/*   
 * Light LED pin13 if pin 2 or 3 goes LOW (i.e. pressed)
 */
void loop() {
  if ((digitalRead(2) == LOW) || (digitalRead(3) == LOW))
    digitalWrite(LED_BUILTIN, HIGH);
  else
    digitalWrite(LED_BUILTIN, LOW);
  delay (100); 
}

If everything is working OK, go ahead and glue the switches to the lower base of the board.

object_detection_robot-14
object_detection_robot-15

Now, upload the following sketch to the Arduino, and then place the Arduino on the floor.

#include <Servo.h> 

/**
 *  This robot will move around a room and when it 
 *  bumps into an object, it will turn around and 
 *  go in another direction.
 * 
 * @author Addison Sears-Collins
 * @version 1.0 2019-05-12
 */

// Create two servo objects, one for each wheel
Servo right_servo;
Servo left_servo;

// Volatile keyword is used because these variables
// can change at any time without any action having been
// taken by the compiled code. 

volatile int left_switch = LOW;   // Left switch flag
volatile int right_switch = LOW;  // Right switch flag
boolean already_started = false;  

/*   
 *  This setup code is run only once, when Arudino is 
 *  supplied with power.
 */
void setup() {
  // Set the pin modes for the switches
  pinMode(2, INPUT); // Right switch is input
  pinMode(3, INPUT); // Left switch is input
  pinMode(4, OUTPUT); // Pin 4 is ground
  
  // Turn on the internal pull up resistors for the switches
  // Keeps input from floating when the switches are not
  // pressed
  digitalWrite(2, HIGH);   // Right switch default to high
  digitalWrite(3, HIGH);   // Left switch default to high
  digitalWrite(4, LOW);    // Pin 4 default is ground

  right_servo.attach(9);    // Right servo is pin 9
  left_servo.attach(10);    // Left servo is pin 10
 
  // Declare the interrupts
  // attachInterrupt(digitalPinToInterrupt(pin), ISR, mode)
  // Interrupt when go from high to low
attachInterrupt(digitalPinToInterrupt(2), hit_right, FALLING); 
attachInterrupt(digitalPinToInterrupt(3), hit_left, FALLING);  
  
  already_started = true;  // Bot can now move
}

void loop() {
  if (left_switch == HIGH) {   // If the left switch is hit
    go_backwards();            // Go backwards for 1 sec
    delay(1000); 
    go_right();             // Turn to the right for 1 sec
    delay(1000);
    go_forward();              // Move forward
    left_switch = LOW;         // Reset the flag
  }
  
  if (right_switch == HIGH) {  // If the right switch is hit
    go_backwards();           // Go backwards for one second
    delay(1000); 
    go_left();                // Turn left for one second
    delay(1000);
    go_forward();             // Move forward
    right_switch = LOW;       // Reset the flag
  }
}
  
// Interrupt routine for left switch bumping into an object
void hit_left() {
  if (already_started)     // Valid if the program has begun
    left_switch = HIGH;    // Set flag high to get serviced
}

// Interrupt routine for right switch bumping into an object
void hit_right() {
  if (already_started)    // Valid if the program has begun
    right_switch = HIGH;  // Set flag high to get serviced
}

/*   
 *  Forwards, backwards, right, left, stop.
 */
void go_forward() {
  right_servo.write(0);
  left_servo.write(180);
}
void go_backwards() {
  right_servo.write(180);
  left_servo.write(0);
}
void go_right() {
  right_servo.write(180);
  left_servo.write(180);
}
void go_left() {
  right_servo.write(0);
  left_servo.write(0);
}
/*
void stop_all() {
  right_servo.write(90); // Tweak the 90
  left_servo.write(90);  // Tweak the 90
}
*/

Power it up by turning on the servos:

  • a13 to e3 = left servo ON
  • e13 to e28 = right servo ON

Then plug in the Arduino’s power.

In order to actually start the program, you need to depress one of the switches. Make sure the Arduino is on the floor before you start the program.

Watch the Object Detection Robot move!

Video

How to Make an Autonomous Wheeled Robot Move | Arduino

In this post, I’ll show you how to make a basic autonomous wheeled robot move forwards, backwards, to the right, and to the left using Arduino.

Shout out to the late Gordon McComb for this project idea. He is the author of an excellent book that I recommend buying if you’re getting started with robotics: How to Make a Robot.

Requirements

Here are the requirements:

  • Make an autonomous wheeled robot move forwards, backwards, to the right, and to the left using Arduino.

You Will Need

The following components are used in this project. You will need:

Directions

First, we need to test the servos to see if they are working. Code up the sketch below into the Arduino Integrated Development Environment (IDE).

Give it a descriptive name like test_servos.ino.

1_code_the_sketchPNG
// Include library that enables us to work with servos
#include <Servo.h> 

/**
 * This program is a test of the servo motors 
 * 
 * @author Addison Sears-Collins
 * @version 1.0 2019-05-11
 */

// Create two servo objects, one for each wheel
Servo right_servo;
Servo left_servo;

/*   
 *  This setup code is run only once, when Arudino is 
 *  supplied with power.
 */
void setup() { 
  
  // Right servo is connected to digital pin 9
  right_servo.attach(9);   

  // Right servo is connected to digital pin 9
  left_servo.attach(10);
} 

/*   
 *  This code repeats as long as the Arduino has power.
 */
void loop() { 
  
  // On the continuous rotation servo, the write() 
  // method sets the speed of the servo.
  // 0 is full speed in one direction.
  // 180 is full speed in the other direction.
  // ~90 is no movement (You will have to tweak to
  //   get no movement).
  
  // Move forwards
  right_servo.write(0);  
  left_servo.write(180);

  // Wait 2 seconds
  delay(2000);

  // Move backwards (reverse)
  right_servo.write(180);  
  left_servo.write(0);

  // Wait 2 seconds
  delay(2000);
/*
  // Turn right
  right_servo.write(180); 
  left_servo.write(180);
  
  // Wait 2 seconds
  delay(2000);

  // Turn left
  right_servo.write(0);    
  left_servo.write(0);     
  
  // Wait 2 seconds
  delay(2000);
  */
}

Make sure the right and left servos are OFF:

  • a13 to j2 = left servo OFF
  • e13 to j28 = right servo OFF
2_servos_are_off

Compile the sketch in the IDE by clicking the checkmark in the upper left corner of the IDE.

3_compile_the_sketchPNG

Upload the sketch to the Arduino board by connecting the USB to your computer. Then click the right arrow (Upload) button on the IDE. The LED on your Arduino should be illuminated.

4_connect_usb
5_right_arrow_arduino_uploadPNG

Remove the USB cord from the Arduino.

Turn ON both servos:

  • a13 to e3 = left servo ON
  • e13 to e28 = right servo ON
6_servos_are_on

Hold the robot in your hand or place the robot on the floor.

Power up the Arduino by plugging in the 9V jack.

Observe the robot move.

7_moving_robot
8_moving_robot

When you are done watching it move, pull the 9V jack out of the Arduino and turn the servos OFF. Remember:

  • a13 to j2 = left servo OFF
  • e13 to j28 = right servo OFF

Here is another sketch (named basic_wheeled_bot.ino) that you can upload to the Arduino that adds some different movements to your robot and breaks up the movements we had in the test code into separate methods. Feel free to play around with this and create your own movement sequence:

// Include library that enables us to work with servos
#include <Servo.h> 

/**
 * This program makes a basic wheeled robot
 * do different movements.
 * 
 * @author Addison Sears-Collins
 * @version 1.0 2019-05-11
 */

// Create two servo objects, one for each wheel
Servo right_servo;
Servo left_servo;

/*   
 *  This setup code is run only once, when Arudino is 
 *  supplied with power.
 */
void setup() { 
  
  // Right servo is connected to digital pin 9
  right_servo.attach(9);   

  // Right servo is connected to digital pin 9
  left_servo.attach(10);
} 

/*   
 *  This code repeats as long as the Arduino has power.
 */
void loop() { 

  // On the continuous rotation servo, the write() 
  // method sets the speed of the servo.
  // 0 is full speed in one direction.
  // 180 is full speed in the other direction.
  // ~90 is no movement (You will have to tweak to
  //   get no movement).
  
  go_forward();

  // Pause 2 seconds
  delay(2000);

  go_backwards();

  // Pause 2 seconds
  delay(2000);
  
  go_right();

  // Pause 2 seconds
  delay(2000);
  
  go_left();

  // Pause 2 seconds
  delay(2000);
  
  stop_all();

  // Pause 2 seconds
  delay (2000);
}

/*   
 *  Forwards, backwards, right, left, stop.
 */
void go_forward() {
  right_servo.write(0);
  left_servo.write(180);
}
void go_backwards() {
  right_servo.write(180);
  left_servo.write(0);
}
void go_right() {
  right_servo.write(180);
  left_servo.write(180);
}
void go_left() {
  right_servo.write(0);
  left_servo.write(0);
}
void stop_all() {
  right_servo.write(90); // Tweak the 90
  left_servo.write(90);  // Tweak the 90
}

Video

How to Wire the Batteries and Motors of a Basic Wheeled Robot

In this post, I’ll show you how to wire the batteries and motors of a basic wheeled robot.

Shout out to the late Gordon McComb for this project idea. He is the author of an excellent book that I recommend buying if you’re getting started with robotics: How to Make a Robot.

Requirements

Here are the requirements:

  • Connect the left servo, right servo, and AA batteries to the solderless breadboard.

You Will Need

The following components are used in this project. You will need:

Directions

Take off all of the insulation from a 6-inch long male-to male jumper wire.

Cut the jumper wire into eight pieces that are each 0.5 inches in length.

wiring_the_robot-1

Use pliers or the end of your wire stripper to push the jumper wire pieces into the connectors of the two servos and the 4 x AA battery holder.

wiring_the_robot-3
wiring_the_robot-4
wiring_the_robot-2

Connect the black lead of the left servo to cell b4 of the solderless breadboard.

wiring_the_robot-5
wiring_the_robot-6

Connect the black lead of the right servo to cell b29 of the solderless breadboard.

wiring_the_robot-7

Connect cell a4, a16, and a29 to the blue (-) negative rail of the solderless breadboard using a jumper wire.

wiring_the_robot-8

Connect the black lead of the 4 x AA battery connector to cell b16. The red lead should sink down in b13.

wiring_the_robot-9

Connect the Digital 10 pin of the Arduino to cell a2 of the breadboard.

wiring_the_robot-11

Connect the Digital 9 pin of the Arduino to cell a27 of the breadboard.

wiring_the_robot-10

Connect the Ground (GND) pin of the Arduino to the blue negative rail of the breadboard (brown jumper wire in the image below).

wiring_the_robot-11-1

Connect a jumper wire from cell a13 to j3 (or any cell on the other side of the groove of the breadboard). This wire provides power (i.e. turned ON) to the left servo when it is connected from cell a13 to cell e3. When it is connected from cell a13 to j2, the left servo is turned OFF.

  • a13 to e3 = left servo ON
  • a13 to j2 = left servo OFF

Connect a jumper wire from cell e13 to j28 (or any empty cell on the other side of the groove of the breadboard, excluding row 2). This wire provides power (i.e. turned ON) to the right servo when it is connected from cell e13 to cell e28. When it is connected from cell e13 to j28, the right servo is turned OFF.

  • e13 to e28 = right servo ON
  • e13 to j28 = right servo OFF
wiring_the_robot-12

That’s it for the wiring. In the next post, we will create a program on Arduino that enables the robot to make basic movements autonomously.