Jeremie
Published © GPL3+

Little Rover

Very basic obstacle avoidance robot that I used to introduce my 9 yo nephew and 7 yo niece to robotics...

BeginnerFull instructions provided2 hours15,406
Little Rover

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Adafruit Wheel for Micro Continuous Rotation FS90R Servo
×2
Adafruit Continuous Rotation Micro Servo - FS90R
×2
LED (generic)
LED (generic)
Any color. It's just to make the thing fun and blink as it raoms around
×2
4xAA battery holder
4xAA battery holder
I used a barrel battery holder... This is just for reference.
×1
SparkFun Ball Caster Metal - 3/8"
×1
Plastic container
Like a cheap tupperware or a 500g magarine box.
×1
stickers
This is only an example to show what sort of stickers I'm refering to...
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Hot glue gun (generic)
Hot glue gun (generic)
carpet knife

Story

Read more

Schematics

LittleRover Diagram

Code

LittleRover

Arduino
This is the version of the code for the 4 pins ultrasonic sensor
#include <Servo.h>
/*
If you have a sensor with only 3 pins, 
Uncomment line 6 and comment out line 7
*/
//int distanceR = A0;   //Trigger pin
int distance = 6;       //Trigger pin
int pingDelay = 10;     //length of the pulse of the Ultra-sonic sensor
int distanceR = A0;     //Echo pin
int led1 = 8, led2 = 9; //LEDs

Servo leftPower;
Servo rightPower;

unsigned long flashTime;  

unsigned long nextSensorTime;

float obstacleDist = 0.0f;

void setup() {
  //Serial.begin(115200);
  leftPower.attach(3);
  rightPower.attach(5);
  
  leftPower.write(90);
  rightPower.write(90);
  
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);

  delay(1000);
}

void loop() {
  LEDs();

  /*
  This prevents the sensor from draining
  the battery too quickly by limiting
  the checks to 5 times per second
  You can increase this number to check
  less often but then it might run into
  the wall...
  */
  if(millis() - nextSensorTime > 200){  
    nextSensorTime = millis();
    /*
    I do 2 readings here because sometimes the sensor gets confused 
    and returns nothing... 
    */
    float reading1 = TakeReading();
    delay(100);
    float reading2 = TakeReading();
    Serial.println(reading1);
    Serial.println(reading2);
    //Then we takes the biggest distance for obstacleDist
    obstacleDist = reading1 >= reading2 ? reading1 : reading2;
    //If the distance is less that 10cm, turn, otherwise, drive straight.
    if(obstacleDist < 10.0f){
      //Serial.println("turn");
      turn();
    }else{
      //Serial.println("forward");
      forward();
    }
  }
}

void LEDs(){
  if(millis() - flashTime > 1000)
    flashTime = millis();
    
  if(millis() - flashTime < 500){
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
  }else if(millis() - flashTime > 500){
    digitalWrite(led1, HIGH);
    digitalWrite(led2, LOW);
  }
}

void forward(){
  /*
  If you find that your robot is going backwards,
  swap the 135 and the 45 around.
  */
  leftPower.write(135);
  rightPower.write(45);
}

void turn(){
  /*
  If you want your robot to turn the other way,
  Uncomment lines 92-93 below and comment out 95-96
  */
  //leftPower.write(90);
  //rightPower.write(135);
  
  leftPower.write(45);
  rightPower.write(90);
  
  delay(350);
}

float TakeReading(){
  unsigned long echo = 0;
  float result = 0.0f;
  pinMode(distance, OUTPUT);  
  digitalWrite(distance, LOW);
  delayMicroseconds(2);
  digitalWrite(distance, HIGH);
  delayMicroseconds(pingDelay);
  digitalWrite(distance, LOW);

  pinMode(distanceR, INPUT);
  digitalWrite(distanceR, HIGH);
  echo = pulseIn(distanceR,HIGH,38000);
  result = echo/58.138;
  
  return result;
}

Credits

Jeremie

Jeremie

5 projects • 31 followers
Happy guy, Robot enthusiast, Dog lover

Comments