Autonomous Rover with Arduino Code

SUPPLIES AND COMPONENTS

  • Arduino UNO
  • Breadboard (generic)
  • Jumper wires (generic)
  • DC motor (generic)
  • SparkFun Dual H-Bridge motor drivers L298
  • Ultrasonic Sensor – HC-SR04 (Generic)
  • Arduino Nano R3
  • LED (generic)
  • Buzzer
  • TTP223B Touch Sensor

TOOLS AND MACHINES REQUIRED

  • Soldering iron (generic)
  • Hot glue gun (generic)

ONLINE SERVICES AND APPS

Arduino IDE

A PROJECT SUMMARY

Chassis

The Bogie Runt Rover from Servo City served as the basis for the Probability rover’s chassis, and I built it together by following a tutorial. The package does not include any electronics; it only includes a full set of motors and chassis.

Motors

The rover’s wheels are propelled by six DC motors. The wires for each motor pass through a hole in the chassis and emerge at the top of each motor. After that, they join an L298N motor driver. Probability may go at a speed of up to 4 mph with to these strong engines!

Motor Driver

A L298N dual bridge motor driver chip powers each of the six motors. The right and left sides of the chip are linked to each motor wire. The Arduino is wired to the chip’s 5V and GND pins to supply power. Then the GND and 5V connect to their respective voltages. The ENA, IN1, IN2, IN3, IN4, and ENB pins on the Arduino must be linked to the L298N motor driving chip in order for all of the motors to function. The six digital pins of the Arduino are then linked using these cables.

long-range ultrasonic sensor

The device that detects items in front of it and steers clear of them is called the HC-SR04 or (Ultra long distance sensor). It functions by producing a high-pitched sound using the echo pin and picking it up with the trig pin. After cutting a paperclip in half, I burned a small hole in the ABS plastic using it to place the sensor. I attached the sensor after first wrapping paperclips around its tiny screw holes. The GND pin is connected to the arduino’s GND, while the VCC pin is connected to the 5V pin. The Trig and Echo pins are then connected to one of the Arduino’s digital pins.When writing the code, be sure to mention the pins you’re utilising for the Trig and Echo pins.

Servo

Probability has a servo that is designed to spin its head so that it may use an ultra-long distance sensor. The black or brown wire is connected to the Arduino’s GND pin, while the red wire is connected to the 5v pin. Any digital I/O pin is connected to the opposite wire. Do not insert paper clips into the chassis if you intend to utilise the servo. Read the part that describes the ultra-long distance sensor if you still don’t understand. Always ensure to enter the correct digital pin you used when entering the code for it.

Buzzer

In order to create noise, the buzzer is set to Probability. Don’t turn on the buzzer if you don’t want your rover to make any noise. One side of the buzzer’s top has a plus sign. Its connecting cable, which may be attached to any Arduino digital pin, is located beneath. The other wire is connected to the Arduino’s GND pin. Do not forget to state the digital pin you are using. You must create a piece of code that specifies the tone in order for it to play sound. You may find the tone code you need in the Probability soft 1.9.

SCHEMATICS

CODE

// connect motor controller pins to Arduino digital pins
// motor one
int enA = 10;
int in1 = 9;
int in2 = 8;
// motor two
int enB = 5;
int in3 = 7;
int in4 = 6;

#include <Servo.h>

#define TRIG_PIN 13 
#define ECHO_PIN 12 
#define MAX_DISTANCE 200 
#define ctsPin 11 
Servo servo;


// defines pins numbers
const int trigPin = 13;
const int echoPin = 12;

// defines variables
long duration;
int distance;

void setup() {
  pinMode(2, OUTPUT);
analogWrite(enA, 90);
  analogWrite(enB, 90);
  // set all the motor control pins to outputs
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);  
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW); 
  delay(1200);
}


void loop() {
int ctsValue = digitalRead(ctsPin);

  if(ctsValue==HIGH) {
    digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);  
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  delay(800);
     //forward
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);  
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  delay(800);
  digitalWrite(in1, HIGH);//Turn Back 
  digitalWrite(in2, LOW);  
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH); 
  delay(800);
  digitalWrite(in1, LOW);//Turn Back 
  digitalWrite(in2, HIGH);  
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW); 
  delay(800);
  tone(2, 250); //A
  delay(15);
   tone(2, 280); //A
  delay(15);
   tone(2, 310); //A
  delay(15);
   tone(2, 340); //A
  delay(15);
  tone(2, 380); //A
  delay(15);
   tone(2, 420); //A
  delay(15);
   tone(2, 460); //A
  delay(15);
   tone(2, 500); //A
  delay(75);
  
  tone(2, 420); //A
  delay(100);
   tone(2, 380); //A
  delay(90);
   tone(2, 340); //A
  delay(80);
   tone(2, 300); //A
  delay(70);
 tone(2, 260); //A
  delay(60);
  
  tone(2, 240); //A
  delay(60);
  tone(2, 220); //A
  delay(60);
   
   tone(2, 180, 100); //A
  }
  while(ctsValue==LOW) {
  //forward
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);  
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  
   
  // Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(8);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);


  if (distance < 27.5){
    
  digitalWrite(in1, LOW);//Motor off for 2 sec
  digitalWrite(in2, LOW);  
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW); 
  delay(800);
  tone(2, 250); //A
  delay(15);
   tone(2, 280); //A
  delay(15);
   tone(2, 310); //A
  delay(15);
   tone(2, 340); //A
  delay(15);
  tone(2, 380); //A
  delay(15);
   tone(2, 420); //A
  delay(15);
   tone(2, 460); //A
  delay(15);
   tone(2, 500); //A
  delay(75);
  
  tone(2, 420); //A
  delay(100);
   tone(2, 380); //A
  delay(90);
   tone(2, 340); //A
  delay(80);
   tone(2, 300); //A
  delay(70);
 tone(2, 260); //A
  delay(60);
  
  tone(2, 240); //A
  delay(60);
  tone(2, 220); //A
  delay(60);
   
   tone(2, 180, 100); //A
   digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);  
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW); 
  delay(400);
  digitalWrite(in1, HIGH);//Turn Back 
  digitalWrite(in2, LOW);  
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH); 
  delay(800);
  
  digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);

  while(distance < 60){
    digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
  }
if(ctsValue==HIGH) {
     //forward
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);  
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  }
    
  
    }
 
  }
}

Busy person will never get sad, Sad person will never get busy

KP

“Behaviour > Character, Attitude > Gratitude, curiosity > fidelity” – KP

It’s better to invest in long-term business than having short-term profit

KP

Helping Hands.

About the author

pondabrothers

You can download our apps and books for free..
Search - Incognito Inventions

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *