WEEK 11

For this week I used a ultrasonic sensor as input device to measure the distance of the things in front of the object.

INPUT DEVICES

Group assignment :

To see our group assignment click here

Individual assignment :

WHat is Ultrasonic sensor

An ultrasonic sensor is an electronic device that uses a transducer to send and receive ultrasonic sound waves, which are faster than audible sound waves, and measure the distance to an object by calculating the time it takes for the waves to reflect back123. Ultrasonic sensors can convert the ultrasonic signals into electrical signals24 and are used in various applications such as robotic control, vehicle detection, industry, national defense, and biomedicine confer bing chat.

Principle :

The components I used for this application are :

They are wire as shown in the table below :

ultrasonic sensor servo motor smity board
trig - 16
echo - 5
- Signal 13
Gnd Gnd Gnd
Vcc Vcc Vcc

To understand what we do with ultrasonic and how we can program it I highlighted below some informetion from the the ultrasonic senro hc sr04

It’s also important to consider

Use this link to get entire datasheet

What we notice from the datasheet for our programing

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. Formula: uS / 58 = centimeters or uS / 148 =inch; or: the 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

I displayed distance I can measure from the ultrasonic sensor on the serial monitor

The main part of the code is getting data part from the ultrasonic sensor and then change the rotation of the Servo.

Code

#include "Servo.h"

const int TRIG_PIN  = 16;  // Arduino pin connected to Ultrasonic Sensor's TRIG pin
const int ECHO_PIN  = 5;
int pos = 0;
Servo monservo;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  monservo.attach(13);
  pinMode(TRIG_PIN, OUTPUT); // set arduino pin to output mode
  pinMode(ECHO_PIN, INPUT);  // set arduino pin to input mode
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // measure duration of pulse from ECHO pin
  unsigned long duration_us = pulseIn(ECHO_PIN, HIGH);
  // calculate the distance
  int distance_cm = 0.017 * duration_us;

  if(distance_cm <=50 && distance_cm >=20)
  {
    for(int i = pos; i < 180; i++) {
    monservo.write(i);
    delay(15);
    pos=i;
    if(pos==179)
      pos=0;
    }
  }
  if(distance_cm<20)
  {
    monservo.write(0);
  }

  Serial.print("distance: ");
  Serial.print(distance_cm);
  Serial.println(" cm");

  }

Code Explaination

This code execute the program to measure the distance of obstacles in front of ultrasonic sensor.

to measure the distance we need to generate pulse signal with transmitter and determine the time taken by the receiver to get the pulse after bounced off obstacle. We determine the time with pulseIn() function which need essentially two parameters the pin used by the component and the state we want to detect. in how case we detect when the echo pin is HIGH like this pulseIn(ECHO_PIN,HIGH).

Then we calculate the distance as explain earlier with this part of code:

  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // measure duration of pulse from ECHO pin
  unsigned long duration_us = pulseIn(ECHO_PIN, HIGH);
  // calculate the distance
  int distance_cm = 0.017 * duration_us;

Then we can use the value of distance to execute instruction we want. So when the value distance is like : 50<=distance>=20 the servo turn on. if(distance_cm <=50 && distance_cm >=20) { for(int i = pos; i < 180; i++) { monservo.write(i); delay(15); pos=i; if(pos==179) pos=0; } } And then when the value is less than 20 the servo stopped by taking its initial position. if(distance_cm<20) { monservo.write(0); }