Obstacle Avoider Robot

The Arduino Nano is a small, complete, and breadboard-friendly board based on the ATmega328 (Arduino Nano 3.x). It has more or less the same functionality of the Arduino Duemilanove, but in a different package. It lacks only a DC power jack, and works with a Mini-B USB cable instead of a standard one

Not much is needed for this lesson, just a USB cable and an Arduino. If you have an older Arduino you may also need an LED. Any LED is fine as long as it looks sorta like the photo, with a plastic bulb and two legs

Make sure you’ve gone through Lesson 0 first!
1)Assembelled arduino board , preferably a decimila (or whatever the latest version
2)USB cable. Standard AS-B cable is required . any length is ok.
3)This is 40pcs female to femal color breadboard jumper cable dupont wire for arduino . female to female jump wire.
Length 20cm.

Introduction:

Ok  you’ve gotten your Arduino set up  and also figured out how to use the software to send sketches to the board

Next step writing  your own sketches . we’ll start off easy by just modifying something  that already work.

]to start we will venture deep into the motor sketch, looking each line and trying understand what its doing.

 DC MOTOR:

A DC motor (Direct Current motor) is the most common type of motor. DC motors normally have just two leads, one positive and one negative. If you connect these two leads directly to a battery, the motor will rotate. If you switch the leads, the motor will rotate in the opposite direction.

Warning − Do not drive the motor directly from Arduino board pins. This may damage the board. Use a driver Circuit or an IC.

We will divide this chapter into three parts −

  • Just make your motor spi
  • Control motor speed
  • Control the direction of the spin of DC motor

MOTOR DRIVER :

Spin ControlArduino Code


int motorPin = 3;

void setup()
{
void loop()

digitalWrite(motorPin, HIGH);
}
}

Procedure

 Following is the schematic diagram of the DC motor interface to Arduino nano board.

 The above diagram shows how to connect the L293D IC to control two motors. There are two input pins for each motor, Input1 (IN1), Input2 (IN2), ,

 Since we will be controlling only one motor in this example, we will connect the Arduino to IN1 (pin 5), IN2 (pin 7), of the L293D IC. Pins 5 and 7 are digital, i.e. ON or OFF inputs, The following table shows which direction the motor will turn based on the digital values of IN1 and IN2.

IN1

IN2

Motor Behavior

   

BRAKE

1

 

FORWARD

 

1

BACKWARD

1

1

BRAKE

Warning :

Connect Arduino using Arduino USB cable and upload the program to Arduino using Arduino  software

1)first upload  the program in your arrduino board.

2)After completed the uploading .Remove the USB cable

3)Then start  the connection for citrcuitConnecting steps:

  • Connect  battery 12v and Gnd  pin  arduino vin pin and Gnd pin
  • Connect Battery 12vèto arduino vin pin.
  • Connect Battery Gndèto arduino Gnd pin.
  • Connect arduino 5v èto motor driver 5v pin.
  • Connect arduino  Gndèto motor driver Gnd pin .
  • Connect  arduino  8pin to èmotor driver in1(M1) pin.
  • Connect  arduino  9pin to èmotor driver in2( M1 )pin.
  • Connect motor driver 0/p pin to èdc  motor  +ve pin.
  • Connect motor driver o/p pin to è dc motor _ve pin

Pin Diagram

Arduino Code for motor :

Arduino Code for motor :
const int in_1 = 8 ;
const int in_2 = 9 ;
//For providing logic to L298 IC to choose the direction of the DC motor
void setup()
{
pinMode(in_1,OUTPUT) ;
//Logic pins are also set as output
pinMode(in_2,OUTPUT) ;
}
void loop()
{
//For Clock wise motion , in_1 = High , in_2 = Low
digitalWrite(in_1,HIGH) ;
digitalWrite(in_2,LOW) ;
//Clockwise for 3 secs
delay(3000) ;
//For brake
digitalWrite(in_1,HIGH) ;
digitalWrite(in_2,HIGH) ;
delay(1000) ;
//For Anti Clock-wise motion – IN_1 = LOW , IN_2 = HIGH
digitalWrite(in_1,LOW) ;
digitalWrite(in_2,HIGH) ;
delay(3000) ;
//For brake
digitalWrite(in_1,HIGH) ;
digitalWrite(in_2,HIGH) ;

delay(1000);}

Lesson 1

Ultrasonic Sensor HC-SR04 and Arduino Tutorial

How It Works – Ultrasonic Sensor:

It emits an ultrasound at 40 000 Hz which travels through the air and if there is an object or obstacle on its path It will bounce back to the module. Considering the travel time and the speed of the sound you can calculate the distance.

In order to generate the ultrasound you need to set the Trig on a High State for 10 µs. That will send out an 8 cycle sonic burst which will travel at the speed sound and it will be received in the Echo pin. The Echo pin will output the time in microseconds the sound wave traveled.

For example, if the object is 10 cm away from the sensor, and the speed of the sound is 340 m/s or 0.034 cm/µs the sound wave will need to travel about 294 u seconds. But what you will get from the Echo pin will be double that number because the sound wave needs to travel forward and bounce backward. So in order to get the distance in cm we need to multiply the received travel time value from the echo pin by 0.034 and divide it by 2.

CONNECTION FOR ULTRA SONIC SENSOR:

Step 1: Components

  1. 1. One Arduino compatible board (I use Arduino Nano, because I have one, but any other will be just fine)
  2. 2. One Ultrasonic Ranger Sensor Module – I used HC-SR04, but US-015, or very much any other will also work
  3. 3. 4 Female-Female jumper wires
    1. 1. Connect Ground(Black wire), Power(Red wire), Trigger(Green wire), and Echo(Yellow wire) to the Ultrasonic Ranger Sensor Module (Picture 1)
    2. 2. Connect the other end of the Power wire(Red wire) to the 5V power pin of the Arduino board(Picture 2)
    3. 3. Connect the other end of the Ground wire(Black wire) to Ground pin of the Arduino board(Picture 2)
    4. 4. Connect the other end of the Trigger wire(Green wire) to Digital pin 2 of the Arduino board(Picture 3)
    5. 5. Connect the other end of the Echo wire(Yellow wire) to Digital pin 3 of the Arduino board(Picture 3)
    6. 6. Picture 4 shows where are the Ground, 5V Power, Digital 2, and Digital 3pins of the Arduino Nano

    After compiling, uploading and running it, in the Serial Monitor (Tools -> Serial Monitoror Ctrl + Shift + M) the sensor was sending correct data!

    Lesson 2

    PROJECT FOR OBSTACLE AVOIDER


    CODE FOR OBSTACLE AVOIDER

    #include //Servo motor library. This is standard library
    #include //Ultrasonic sensor function library. You must install this library
    //our L298Ncontrol pins
    const int LeftMotorForward = 7;
    const int LeftMotorBackward = 6;
    const int RightMotorForward = 4;
    const int RightMotorBackward = 5;
    //sensor pins
    #define trig_pin A1 //analog input 1
    #define echo_pin A2 //analog input 2
    #define maximum_distance 200
    boolean goesForward = false;
    int distance = 100;
    NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
    Servo servo_motor; //our servo name
    void setup(){
    pinMode(RightMotorForward, OUTPUT);
    pinMode(LeftMotorForward, OUTPUT);
    pinMode(LeftMotorBackward, OUTPUT);
    pinMode(RightMotorBackward, OUTPUT);
    servo_motor.attach(10); //our servo pin
    servo_motor.write(115);
    delay(2000);
    distance = readPing();
    delay(100);
    distance = readPing();
    delay(100);
    distance = readPing();
    delay(100);
    distance = readPing();
    delay(100);
    }
    void loop(){
    int distanceRight = 0;
    int distanceLeft = 0;
    delay(50);
    if (distance <= 20){
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceRight = lookRight();
    delay(300);
    distanceLeft = lookLeft();
    delay(300);
    if (distance >= distanceLeft){
    turnRight();
    moveStop();
    }
    else{
    turnLeft();
    moveStop();
    }
    }
    else{
    moveForward();
    }
    distance = readPing();
    }
    int lookRight(){
    servo_motor.write(50);
    delay(500);
    int distance = readPing();
    delay(100);
    servo_motor.write(115);
    return distance;
    }
    int lookLeft(){
    servo_motor.write(170);
    delay(500);
    int distance = readPing();
    delay(100);
    servo_motor.write(115);
    return distance;
    delay(100);
    }
    int readPing(){
    delay(70);
    int cm = sonar.ping_cm();
    if (cm==0){
    cm=250;
    }
    return cm;
    }
    void moveStop(){
    digitalWrite(RightMotorForward, LOW);
    digitalWrite(LeftMotorForward, LOW);
    digitalWrite(RightMotorBackward, LOW);
    digitalWrite(LeftMotorBackward, LOW);
    }
    void moveForward(){
    if(!goesForward){
    goesForward=true;
    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(RightMotorForward, HIGH);
    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(RightMotorBackward, LOW);
    }
    }

    void moveBackward(){
    goesForward=false;
    digitalWrite(LeftMotorBackward, HIGH);
    digitalWrite(RightMotorBackward, HIGH);
    digitalWrite(LeftMotorForward, LOW);
    digitalWrite(RightMotorForward, LOW);
    }
    void turnRight(){
    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(RightMotorBackward, HIGH);
    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(RightMotorForward, LOW);
    delay(500);
    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(RightMotorForward, HIGH);
    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(RightMotorBackward, LOW);
    }
    .

Leave a Reply

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