Ultrasone sensor (afstand)

Met deze ultrasonische sensor kan men gemakkelijk een afstand meten.

Deze sensor gebruikt geluidspulsen en meet de tijd tot de echo om een afstand te bepalen. 

Specificaties:

  • Voedingsspanning: 2.8 – 5.5V DC
  • Signaalspanning: 2.8 – 5.5V (zelfde als voedingsspanning)
  • Detectiebereik: 2-450cm
  • Stroom: <2mA
  • Resolutie: 3mm
  • Sensor hoek: <15°
  • Afmetingen: 45 x 20mm

Demo-programma SR04 sensor

Onderstaand programma demonstreert de werking van de SR04 sensor.

Om de meting te starten moet je een puls genereren op de trig pin. Die puls activeert een reeks van 8 tonen van 40 kHz.

Van het moment de echo van deze geluidspuls terugkomt aan de sensor zal de sensor de Echo-pin hoog maken met een identieke hoog-tijd als de tijd dat de puls onderweg geweest is.

Vermits geluid een snelheid heeft van 340m/sec en de meting een waarde is in microseconden moeten we deze waarde vermenigvuldigen met 0.034. Vervolgens moeten we dit resultaat delen door 2 omdat het geluid de dubbele afstand heeft afgelegd (heen en terug)

In de seriële monitor ziet u de meetwaarde.

// ---------------------------------------------------------------- //
// Arduino Ultrasoninc Sensor HC-SR04
// ---------------------------------------------------------------- //

#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04

// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement

void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
  Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
  Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor
  Serial.println("with Arduino UNO R3");
}
void loop() {
  // Clears the trigPin condition
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
  // Displays the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
}