Ultrasonic Ranging Module (HC-SR04) [S011]



https://www.youtube.com/watch?v=QY1IJc7jPp4


*GitHubhttps://github.com/rdiot/rdiot-s011.git


* Specs

Working Voltage DC 5 V

Working Current 15mA

Working Frequency 40Hz

Max Range 4m

Min Range 2cm

MeasuringAngle 15 degree

Trigger Input Signal 10uS TTL pulse

Echo Output Signal Input TTL lever signal and the range in proportion

Dimension 45*20*15mm


* Contents

VCC - 5V

Trig - D8 

Echo - D9

GND - GND


- Key Code

#include "U8glib.h"

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);


const int TriggerPin = 8; //Trig pin

const int EchoPin = 9; //Echo pin

long Duration = 0;

long Distance_mm = 0;


void setup() {

  pinMode(TriggerPin, OUTPUT); // Trigger is an output pin

  pinMode(EchoPin, INPUT); // Echo is an input pin

  Serial.begin(9600); // Serial Output

}


void loop(void) {

  digitalWrite(TriggerPin, LOW);

  delayMicroseconds(2);

  digitalWrite(TriggerPin, HIGH); // Trigger pin to HIGH

  delayMicroseconds(10); // 10us high

  digitalWrite(TriggerPin, LOW); // Trigger pin to HIGH


  Duration = pulseIn(EchoPin, HIGH); // Waits for the echo pin to get high

  Distance_mm = Distance(Duration); // Use function to calculate the distance


  Serial.print("Distance = "); // Output to serial

  Serial.print(Distance_mm);

  Serial.println(" mm");


  // picture loop

  u8g.firstPage();  

  do {

    draw();

  } while( u8g.nextPage() );

  // rebuild the picture after some delay

  delay(500);

}


long Distance(long time)

{

  // Calculates the Distance in mm

  // ((time)*(Speed of sound))/ toward and backward of object) * 10


  long DistanceCalc; // Calculation variable

  DistanceCalc = ((time / 2.9) / 2); // Actual calculation in mm

  //DistanceCalc = time / 74 / 2; // Actual calculation in inches

  return DistanceCalc; // return calculated value

}


'2) Sensor > Distance' 카테고리의 다른 글

Infrared distance sensor (GP2Y0A21YK0F) [S036]  (0) 2019.02.23
Posted by RDIoT
|