Ultrasonic Sensor HC-SR04


 

 

 

 

 

Video Tutorial: http://vimeo.com/44836674

 

nt echoPin=6;
int trigPin=7;
//set up ultrasound pins on HC-SR04
int led = 13;
//assign LED pin
int threshold = 25;
//assign threshold distance

 

void setup() {

Serial.begin (9600); //open serial communications
pinMode(trigPin, OUTPUT);
pinMode (echoPin, INPUT);
pinMode(led, OUTPUT);

//describe inputs and outputs

}

 

 

void loop () {

int duration, cm;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite (trigPin, HIGH);
delayMicroseconds (10);
digitalWrite(trigPin, LOW);
duration= pulseIn(echoPin, HIGH);
cm= duration /29 /2;

Serial.print(cm);
Serial.println("cm");
delay(100);
 
if (cm > threshold) {
digitalWrite(led, HIGH); //turn on LED
}
else {
digitalWrite(led,LOW); //otherwise turn it off
}
}

 

-------------------------------------------------------------------------

Another Version:

  /*
HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 13 Trig to Arduino pin 12
Red POS to Arduino pin 11
Green POS to Arduino pin 10
560 ohm resistor to both LED NEG and GRD power rail
More info at: http://goo.gl/kJ8Gl
Original code improvements to the Ping sketch sourced from Trollmaker.com
Some code and wiring inspired by http://en.wikiversity.org/wiki/User:Dstaub/robotcar
*/

#define trigPin 13
#define echoPin 12
#define led 11
#define led2 10

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
}

void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 4) { // This is where the LED On/Off happens
digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
digitalWrite(led2,LOW);
}
else {
digitalWrite(led,LOW);
digitalWrite(led2,HIGH);
}
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}

 

---------------------------------------------------------------------------------------------------------------------------------------------

 
int echoPin=6;
int trigPin=7;
//set up ultrasound pins on HC-SR04
int led = 13;
//assign LED pin
int threshold = 10;
int note=60;

//connect buzzer to pin 8 and ground

//assign threshold distance
 
void setup() {
 
  Serial.begin  (9600); //open serial communications
  pinMode(trigPin, OUTPUT);
  pinMode (echoPin, INPUT);
  pinMode(led, OUTPUT); 
 
  //describe inputs and outputs
 
}
 
 
void loop () {
 
  int duration, cm;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite (trigPin, HIGH);
  delayMicroseconds (10);
  digitalWrite(trigPin, LOW);
  duration= pulseIn(echoPin, HIGH);
  cm= duration /29 /2;
 
  Serial.print(cm);
  Serial.println("cm");
  delay(100);
 
    tone( 8, cm * note, 100); //play note proportional to distance
}