Sunday, August 2, 2015

Ultrasonic Sensor HC-SR04

How to set up HC-SR04 Ultrasonic Sensor with Arduino:

Project 22:  Ultrasonic Distance Sensor
This project teaches you how to measure distance using HC-05 sensor. 
The sketch will be used as we start building our robots.







Block Diagram:

Hardware:
Ultrasonic Distance Measuring Sensor HC-SR04
connecting wires
breadboard
Arduino R3 UNO board

Code # 16:
/*
  Project 15: Ultrasonic Distance Sensor
  powered by Arduino
  sketched by J.B. Wylzan
  modified by Lawsinium

  Detects distance object by sending out pulses of high-frequency waves
  which are reflected back and analyzed by a bar of leds. This device detects
  distance from 1 cm to 50 cm. The more lights on the led bar graph are switched on
  the greater is the distance between the object and the emitting source.

  This example code is public domain.
 */

 // initialize variables
int time;
int range;
int distance = 50;

// Setup pins 2 to 12
void setup() {
  pinMode(2, INPUT);             // echo pin
  pinMode(3, OUTPUT);         // trigger pin
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
 }

void loop() {
 digitalWrite(3, LOW);
 delay(10);
 digitalWrite(3, HIGH);
 delay(10);
 digitalWrite(3, LOW);

 time = pulseIn(2, HIGH);
 range = time/58.3;
 if (range >= distance)
 {
digitalWrite(4, LOW);
 }
 else if (range == 5)
 {
  digitalWrite(4, HIGH);
  delay(500);
  digitalWrite(4, LOW);
 }
 else if (range == 10)
 {
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  delay(500);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
 }
 else if (range == 15)
 {
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  delay(500);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
 }
 else if (range == 20)
 {
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  delay(500);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
 }
 else if (range == 25)
 {
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  delay(500);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
 }
  else if (range == 30)
 {
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
  delay(500);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
 }
 else if (range == 35)
 {
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
  digitalWrite(10, HIGH);
  delay(500);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
 }
 else if (range == 40)
 {
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
  digitalWrite(10, HIGH);
  digitalWrite(11, HIGH);
  delay(500);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
 }
 else if (range == 45)
 {
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
  digitalWrite(10, HIGH);
  digitalWrite(11, HIGH);
  digitalWrite(12, HIGH);
  delay(500);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
 }
 else if (range == 50)
 {
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
  digitalWrite(10, HIGH);
  digitalWrite(11, HIGH);
  digitalWrite(12, HIGH);
  delay(500);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
 }
 } // end

Challenge: Measure the total distance in centimeter between the sensor and  your hand as it waves back and forth in front of the sensor. The maximum distance is equivalent to when all Leds light up. The minimum distance is when the first led lights up. The middle distance is when five leds are turned on. What is the middle distance in centimeter?


Future Project:






Procedure:
1. Use the same prototype as shown above
2. Run the Arduino Interface
3. Select File > New
4. Copy Code #16 above
5. Paste Code #16
6. Click File > Save
7. Click Verify
8. Click Upload
9. The Led Bar Graph will light up depending on the measured distance by the sensor.


Serial Monitor Test:
VCC to arduino 5v
GND to arduino GND
Echo to Arduino pin 7
Trig to Arduino pin 8

#define echoPin 7                      // Echo Pin
#define trigPin 8                        // Trigger Pin
#define LEDPin 13                     // Onboard LED

int maximumRange = 200;           // Maximum range 
int minimumRange = 0;               // Minimum range 
long duration, distance;              // Duration to calculate distance

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDPin, OUTPUT);         // Use LED indicator 
}

void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);                   
distance = duration/58.2;                 // distance (in cm) 

if (distance >= maximumRange || distance <= minimumRange){
Serial.println("-1");                            // "out of range"
digitalWrite(LEDPin, HIGH);
}
else {
Serial.println(distance);
digitalWrite(LEDPin, LOW);
}
delay(50);                                           //Delay 50ms 

}



Disclaimer:  We shall not be liable for any loss or damage of whatever nature - direct, indirect, consequential, or otherwise - which may arise as a result of your use of any information on this website. 

Public Domain Notice: Copyright (c) 2000. All rights reserved. This article is part of a book entitled iHackRobot. Copies are welcome to be shared or distributed publicly as long proper citations are observed. Please cite as follows: Biotronics: The Silver Species, Joey Lawsin, 1988, USA.
================================================================== 
The Homotronics® and Homodruinos® logos are registered trademarks.
Copyright Biotronics© Inc. iHackRobot®. All Rights Reserved.
Patent Pending. 2000 © ®

L.A.W.S.I.N. Educational Production
 ================================================================== 

No comments:

Post a Comment