Welcome to our second tutorial on Arduino board. Today I will show you how to use make an obstacle scanner using servo and an ultrasonic sensor. I am assuming we know how to use the Arduino IDE.

The components you will need to follow this tutorial:

  • Arduino board
  • Laptop
  • Printer USB cable to upload code to the Arduino
  • 3 female to male jumper wires
  • 3 male to male jumper wires
  • 1 servo motor
  • Ultrasonic holder to mount your ultrasonic sensor to the servo

What is a servo motor?

Some of us have tinkered with a DC motor before. I want you to picture a DC motor that has gears restricting it from going more than 180 degrees and back. Now that’s a servo motor, it does not make full rotation, although with a simple hack you can get it to work like a normal motor once again. A servo has so many uses, especially in robotics, that it is also called a steering gear.

How does the servo work?

The signal modulation chip in the servo receives signals from the controller board. This gives the servo the basic DC voltage. There is also a reference circuit inside the servo which will produce a standard voltage. These two voltages are compared to each other and the output is the difference between the two. The motor receives the differences and decides the rotational speed, direction and angle. When there is no difference between the two voltages, the servo stops.

Connecting the servo

Normally, a servo has 3 controlling lines:

  • Ground (brown line)
  • Power supply (5V, red line)
  • Signal or control line (orange line)

Now we know the pins and what they do, we can connect them now to our Arduino board with our male to male jumper wires. I connected my control pin to pin 3 on the Arduino (you can connect it to
any pin number, but be sure to use the same in your code).

image-.-000.png

Controlling the servo

Import the library to get started. Then:

Servo myservo;  // create servo object to control servo
myservo.attach(3);  // attach servo on pin 3 to servo object (i.e control pin)
myservo.write(90);  //set servo position according to scaled value of (90 degrees)

With these three lines of code you can get your servo moving. You can play around with the code – try changing it and look what works and what doesn’t.

If you are up for a little challenge you can try writing a code that will allow you control the servo movement manually using the serial monitor input.

The ultrasonic sensor

Next, let’s have a look at the ultrasonic sensor module (HC-SRO4).

image-.-002.pngimage-.-004.png

The module can measure distance with high precision. Ultrasonic is used in robotics for a lot of things:

  • Obstacle avoidance
  • Distance measurement
  • Liquid testing
  • Public security
  • Parking lot testing

The main technical parameters are:

  • Voltage: DC – 5V
  • Static current: Less than 2mA
  • Level output: Higher than 5V
  • Level output: Lower than 0
  • Detection angle: Not bigger than 15°
  • Detecting distance: 2-450cm
  • Precision: Up to 0.2cm

The VCC pin is for the power supply. The trigger pin is for controlling the sensor, and the echo pin is for receiving. GND should be connected to ground.

Use your female to male jumper cables to connect your ultrasonic to the Arduino.

How it works

  1. Apply IO port of TRIG to trigger ranging, give high level signal, at least 10us one time;
  2. The module sends 8 square waves of 40kz automatically, tests if there are signals returned automatically;
  3. If there are signals received, the module will output a high level pulse through IO port of ECHO, the duration time of high level pulse is the time between the wave sending and receiving. So the module can know the distance according to the time.

Testing distance= (high level time* velocity of sound (340M/S))/2);

Actual operation

The Timing diagram is shown below. You only need to supply a short 10uS pulse to the trigger input to start the ranging, and then the module will send out an 8 cycle burst of ultrasound at 40 kHz and raise its echo. The Echo is a distance object that is pulse width and the range in proportion .You can calculate the range through the time interval between sending trigger signal and receiving echo signal.

image-.-006.png

The formula:

uS / 58 = centimeters or uS / 148 =inch;

Or:

range = high level time * velocity (340M/S) / 2;

We suggest to use over 60ms measurement cycle, in order to prevent trigger signal to the echo signal.

This formula is very important because we would use it in our code, feel free to switch out the one in the code. The following code is a sub function from our main code used to test the distance to the object in front of it.


/* Ultrasonic distance measurement Sub function */
int Distance_test()
{
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(20);
digitalWrite(Trig, LOW);
float Fdistance = pulseIn(Echo, HIGH);
Fdistance= Fdistance / 58;
return (int)Fdistance;
}

Now we know how an ultrasonic sensor and a servo works. Next step is to mount the ultrasonic on the servo using the ultrasonic holder, you have done that it should look like this:

image-.-008.png

Cool! Now we can code. Below is an example of my code, you should try it yourself and compare them.

#include <Servo.h>  //servo library
Servo myservo;  // create servo object to control servo
int Echo = A4;
int Trig = A5;
int rightDistance = 0, leftDistance = 0, middleDistance = 0;

/* Ultrasonic distance measurement Sub function */
int Distance_test()
{
  digitalWrite(Trig, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig, HIGH);
  delayMicroseconds(20);
  digitalWrite(Trig, LOW);
  float Fdistance = pulseIn(Echo, HIGH);
  Fdistance= Fdistance / 58;
  return (int)Fdistance;
}

void setup()
{
  myservo.attach(3);  // attach servo on pin 3 to servo object
  Serial.begin(9600);  // Serial baud rate
  pinMode(Echo, INPUT);
  pinMode(Trig, OUTPUT);
}

void loop()
{
  myservo.write(90);  // set servo position according to scaled value
  delay(500);
  middleDistance = Distance_test();
  #ifdef send
  Serial.print("middleDistance= ");
  Serial.println(middleDistance);
  #endif
  if (middleDistance >= 20)
   {
     delay(500);
     myservo.write(5);
     delay(1000);
     rightDistance = Distance_test();

    #ifdef send
     Serial.print("rightDistance= ");
     Serial.println(rightDistance);
     #endif
     delay(500);
     myservo.write(90);
     delay(1000);
     myservo.write(180);
     delay(1000);
     leftDistance = Distance_test();

     #ifdef send
     Serial.print("leftDistance= ");
     Serial.println(leftDistance);
     #endif

     delay(500);
     myservo.write(90);
     delay(1000);
     if (rightDistance > leftDistance)
    {
      Serial.println("Right is clear");
    }
    else if (rightDistance < leftDistance)
    {
      Serial.println("Left is clear");
    }
    else if ((rightDistance <= 20) || (leftDistance <= 20))
    {
      Serial.println("Obstacle detected! No way forward");
    }
    else
    {
      Serial.println("No obstacle detected");
    }
  }
  else
  {
    Serial.println("No obstacle detected");
  }
}
Advertisements