testtestboom
Published © GPL3+

Ultrasonic Sensor HY-SRF05 One Pin Mode

Measuring Distance with the Ultrasonic HY-SRF05 with only one Pin per Sensor.

BeginnerFull instructions provided2,947
Ultrasonic Sensor HY-SRF05 One Pin Mode

Things used in this project

Story

Read more

Schematics

Single Pin Wiring

Code

Code to measure the distance

C/C++
const unsigned int TRIG_PIN=12;
const unsigned int ECHO_PIN=12;
const unsigned int BAUD_RATE=9600;

void loop () {}
void setup() {

 Serial.begin(BAUD_RATE);
 pinMode(TRIG_PIN, OUTPUT);
 digitalWrite(TRIG_PIN, LOW);
 delayMicroseconds(2);
 digitalWrite(TRIG_PIN, HIGH);
 delayMicroseconds(10);
 digitalWrite(TRIG_PIN, LOW);
 delayMicroseconds(2);

pinMode(ECHO_PIN, INPUT);
const unsigned long duration= pulseIn(ECHO_PIN, HIGH);
int distance= duration/29/2;

if(duration==0){
  Serial.println("Warning: no pulse from sensor");
  } 
 else{
     Serial.print("Distance: ");
     Serial.print(distance);
     Serial.print(" cm\n");
 }
delay(100);
}

Code to keep track of the area

C/C++
const unsigned int TRIG_PIN=12;
const unsigned int ECHO_PIN=12;
const unsigned int BAUD_RATE=9600;

void setup() {}
void loop () {
  
  Serial.begin(BAUD_RATE);
  pinMode(TRIG_PIN, OUTPUT);
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  
  delayMicroseconds(2);

pinMode(ECHO_PIN, INPUT);
 const unsigned long duration= pulseIn(ECHO_PIN, HIGH);
 int distance= duration/29/2;
 if(duration==0){
   Serial.println("Warning: no pulse from sensor");
   } 
 if(distance <50){
   Serial.println("Warning: Somebody has breached the bunker!");
    }
  else{
      Serial.print("You are safe. For now...\n");
      }
 delay(2000);
 }

Credits

testtestboom

testtestboom

0 projects • 0 followers

Comments