Project tutorial
Transformer's Baby: Arduino Avoiding Obstacle Robot

Transformer's Baby: Arduino Avoiding Obstacle Robot © CC BY-NC

A robot my son calls Transformer's Baby. It is an intelligent toy which can autonomously drive on the floor avoiding obstacles.

  • 3,756 views
  • 0 comments
  • 13 respects

Components and supplies

About this project

What:

A robot, as called by my son, Transformer's Baby ;-) It's an intelligent toy which can autonomously drive on the floor avoiding obstacles.

Why:

To learn some robotics, have fun, play with your kids, make an intelligent toy or just to try programming.

What is needed:

  • Arduino Uno
  • Plywood or cardboard for the robot frame
  • 2 DC motors with controller (the ready-to-use kit is best)
  • distance sensor
  • electronic switch
  • 9V battery
  • Glue (best to use a hot glue gun)

Step 1: Build & Frame

First you need to make the build. Don't skip this step! The build doesn't have to be very accurate. As you see in the picture, it's enough if you draw the shape of the robot's frame and try to place elements on the project to see if all will fit.

If you have a build, take a piece of plywood/cardboard and cut the needed parts. That is: the robot's floor, top and sides.

Step 2: Mounting All Together

If you have all parts, try to glue them all. Start from the robot's floor, then add DC motors, other electronics and then glue the rest of the frame parts.

At the end, add the robot's top. I've used a cabinet's hinge to make the top openable.

Step 3: Electronics & Programming

The electronic circuit for the robot is very easy. On top of the Arduino Uno there is a DC motor controller, two motors are connected and two pins of the Arduino are used for the distance sensor connection.

The entire device is powered by 9V battery which is connected to Arduino via power switch. The C program for Arduino is quite easy. I've attached it to this tutorial.

Step 4: Final Touch

Raw plywood/cardboard doesn't look very nice, so it's good idea to paint the robot with spray paint and give it a mouth and nose. And that's all. Now you can enjoy your self-moving, autonomous robot.

Thanks for reading this. Now, if you like my tutorial, follow me as well as subscribe to my YouTube channel.

Code

Arduino avoiding obstacle robot programmArduino
#define CW  0
#define CCW 1

// Motor definitions to make life easier:
#define MOTOR_A 0
#define MOTOR_B 1

// Pin Assignments //
// Don't change these! These pins are statically defined by shield layout
const byte PWMA = 3;  // PWM control (speed) for motor A
const byte PWMB = 11; // PWM control (speed) for motor B
const byte DIRA = 12; // Direction control for motor A
const byte DIRB = 13; // Direction control for motor B
int Trig = 6;  //pin 2 Arduino poczony z pinem Trigger czujnika
int Echo = 7;  //pin 3 Arduino poczony z pinem Echo czujnika
int CM;        //odlego w cm
long CZAS;     //dugo powrotnego impulsu w uS
int wPrawo=0;
const int SPEED=200;
const int HALF_SPEED=127;
const int QUARTER_SPEED=80;
long randNumber;
 

void setup()
{
  setupArdumoto(); // Set all pins as outputs
  setupCzujnik();
  randomSeed(analogRead(0));
}

void loop()
{
  distanceMeasurement();              //pomiar odlegoci
  
  if (CM > 40) {
    //both motors forward
    driveArdumoto(MOTOR_A, CW, HALF_SPEED); // Set motor A to CCW at max
    driveArdumoto(MOTOR_B, CCW, HALF_SPEED); // Set motor B to CCW at max
  };
  

  if (CM <= 40 and CM >17) {
   randNumber = random(300);
   if (randNumber>=200) {
    //turn left
    driveArdumoto(MOTOR_A, CW, HALF_SPEED); // Set motor A tQUARTER_SPEEDo CCW at max
    driveArdumoto(MOTOR_B, CW, HALF_SPEED); // Set motor B to CCW at max
    delay(300);
   } else {
    //turn right
    driveArdumoto(MOTOR_A, CCW, HALF_SPEED); // Set motor A to CCW at max
    driveArdumoto(MOTOR_B, CCW, HALF_SPEED); // Set motor B to CCW at max
   }
  };  

  if (CM <= 17) {
    //both motors backwards
    driveArdumoto(MOTOR_A, CCW, HALF_SPEED); // Set motor A to CCW at max
    driveArdumoto(MOTOR_B, CW, HALF_SPEED); // Set motor B to CCW at max
    delay(300);
  };
  
  delay(550);
}

void driveArdumoto(byte motor, byte dir, byte spd)
{
  if (motor == MOTOR_A)
  {
    digitalWrite(DIRA, dir);
    analogWrite(PWMA, spd);
  }
  else if (motor == MOTOR_B)
  {
    digitalWrite(DIRB, dir);
    analogWrite(PWMB, spd);
  }  
}

void stopArdumoto(byte motor)
{
  driveArdumoto(motor, 0, 0);
}


void distanceMeasurement ()
{
  digitalWrite(Trig, HIGH);   
  delayMicroseconds(10);
  digitalWrite(Trig, LOW);
  CZAS = pulseIn(Echo, HIGH);
  CM = CZAS / 58;             
}

// setupArdumoto initialize all pins
void setupArdumoto()
{
  // All pins should be setup as outputs:
  pinMode(PWMA, OUTPUT);
  pinMode(PWMB, OUTPUT);
  pinMode(DIRA, OUTPUT);
  pinMode(DIRB, OUTPUT);
  
  // Initialize all pins as low:
  digitalWrite(PWMA, LOW);
  digitalWrite(PWMB, LOW);
  digitalWrite(DIRA, LOW);
  digitalWrite(DIRB, LOW);
}

void setupCzujnik()
{
 Serial.begin(9600);                        //inicjalizaja monitora szeregowego
  pinMode(Trig, OUTPUT);                     //ustawienie pinu 2 w Arduino jako wyjcie
  pinMode(Echo, INPUT);                      //ustawienie pinu 3 w Arduino jako wejcie
}

void setupDiody() {
  pinMode(9, OUTPUT); 
}

Comments

Similar projects you might like

How to Make Obstacle Avoiding Robot

Project tutorial by Darwin Dela Cruz

  • 25,346 views
  • 9 comments
  • 37 respects

Bluetooth Controlled & Obstacle Avoiding Arduino Robot

Project showcase by KureBas Robotics

  • 8,435 views
  • 11 comments
  • 20 respects

Project 1: 2WD Obstacle Avoiding Robot

Project showcase by HDA Robotics

  • 7,535 views
  • 6 comments
  • 17 respects

Ground-Based Obstacle Avoiding Robot

Project showcase by Xavier Tan

  • 5,313 views
  • 1 comment
  • 20 respects

MeArm Robot Arm - Your Robot - V1.0

Project tutorial by Benjamin Gray

  • 19,302 views
  • 3 comments
  • 34 respects
Add projectSign up / Login