Monday, July 13, 2015

PROJECT 6a: Interfacing ULTRASONIC MODULE with ARDUINO

Hey guys,lets look at another quick project. Lets learn interfacing a basic & inexpensive ultrasonic module.The module name is HC-SR04. Ultrasonic Distance Sensor  is an amazing product that provides very short (2CM) to 400cm detection and ranging. The sensor provides precise,Stable non-contact distance measurements from about 2cm to 4 meters with very high accuracy. Its compact size, higher range and easy usability make it a handy sensor for distance measurement and mapping. Any analog ucontroller can easily be interfaced where the triggering and meassurement can be done using Single I/O pin. The sensor transmits an ultrasonic wave and produces an output pulse that corresponds to the time required for the burst echo to return to the sensor.  By measuring the echo pulse width, the distance to target can easily be calculated.This UltraSonic Distance Sensor is perfect for any number of applications that require you to perform measurements between moving or stationary objects. Naturally, robotics applications are very popular but you'll also find this product to be useful in security systems or as an infrared replacement if so desired.     Ultrasonic Distance Sensor Features •Range: 2 cm to 4 m   • Accurate and Stable range data • Modulation at 40 KHz • Triggered externally by supplying a pulse to the signal pin • 5V DC Supply voltage • Current - <20mA


We'll be connecting it one by one to all our featured controllers 
  1. Starting with Atmega 328 mounted on an arduino board.


The fundamentals remain same:
The controller sends a trigger pulse to ultrasonic sensor via trigger pin.
The echo pin sends back the received signals from the ultrasonic sensor to micro-controller.
Depending on the coded algorithm, distance is calculated by micro-controller.
According to the distance you can actuate various actuators. Most basic of which is a Piezo-buzzer using PWM.
You can also use LCD to display output distance.
Another cool project could be to serial transmit the distance data over serial port using arduino pin 0 & 1 to a xbee or matlab for some funky projects.
Log ultrasonic data from a remote sensor and track motion in remote surroundings etc.

The basic circuit diagram is as follows:

























Code for the above circuit diagram is below: Copy the code and burn it onto your Arduino AVR & run your project.
********************************************************************************


/*
VCC to arduino 5v
GND to arduino GND Echo to Arduino pin 5 Trig to Arduino pin 12 buzzer+ve on pin 10 buzzer-ve on gnd pin */


#define echoPin 5// Echo Pin#define trigPin 12 // Trigger Pin#define LED 13 // Onboard LED
int maximumRange = 400; // Maximum
int minimumRange = 2; // Minimum
long duration, distance; //
void setup() {
Serial.begin (57600);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(LED, OUTPUT);
pinMode(10, OUTPUT);
}

void loop() 
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//Calculate the distance (in cm) based on the speed of sound. distance = duration/58.2;

if (distance >= maximumRange || distance <= minimumRange){
Serial.println("out of range");
digitalWrite(LEDPin, HIGH);
}

else {
Serial.println(distance); //display distance on serial monitor
digitalWrite(LEDPin, LOW);
if(distance<=50 && distance>=3) // if object is in 3-50 cm near range
{
analogWrite(10,200); // generate a tone alarm on buzzer
delay(50);
analogWrite(10,50);
delay(250);
analogWrite(10,200);
delay(50);
analogWrite(10,200);
delay(50);
analogWrite(10,50);
delay(250);
analogWrite(10,200);
delay(50);
analogWrite(10,200);
delay(50);
analogWrite(10,50);
delay(250);
analogWrite(10,200);
delay(50);
}
}

delay(10);
}



*******************************************************************************

P.S.: keep it stable for better response. As it is a short range (4mtr) distance sensor, industrial level stability is not to be expected.
Do post your queries and feedbacks.
have fun with this ultrasonic sensor.