15. afstandsensor HC SR 04

Afstandsensor of Ultrasone sensor HC SR 04  (dx.com)


 
 
 
  voorbeeld code:

  1. /*
  2. * Ultrasonic Sensor HC-SR04 and Arduino Tutorial
  3. *
  4. * Crated by Dejan Nedelkovski,
  5. * www.HowToMechatronics.com
  6. *
  7. */
  8. // defines pins numbers
  9. const int trigPin = 9;
  10. const int echoPin = 10;
  11. // defines variables
  12. long duration;
  13. int distance;
  14. void setup() {
  15. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  16. pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  17. Serial.begin(9600); // Starts the serial communication
  18. }
  19. void loop() {
  20. // Clears the trigPin
  21. digitalWrite(trigPin, LOW);
  22. delayMicroseconds(2);
  23. // Sets the trigPin on HIGH state for 10 micro seconds
  24. digitalWrite(trigPin, HIGH);
  25. delayMicroseconds(10);
  26. digitalWrite(trigPin, LOW);
  27. // Reads the echoPin, returns the sound wave travel time in microseconds
  28. duration = pulseIn(echoPin, HIGH);
  29. // Calculating the distance
  30. distance= duration*0.034/2;
  31. // Prints the distance on the Serial Monitor
  32. Serial.print("Distance: ");
  33. Serial.println(distance);
  34. }

werking HC SR 04







in combinatie met LCD scherm:

  1. /*
  2. * Ultrasonic Sensor HC-SR04 and Arduino Tutorial
  3. *
  4. * Crated by Dejan Nedelkovski,
  5. * www.HowToMechatronics.com
  6. *
  7. */
  8. #include <LiquidCrystal.h> // includes the LiquidCrystal Library
  9. LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)
  10. const int trigPin = 9;
  11. const int echoPin = 10;
  12. long duration;
  13. int distanceCm, distanceInch;
  14. void setup() {
  15. lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
  16. pinMode(trigPin, OUTPUT);
  17. pinMode(echoPin, INPUT);
  18. }
  19. void loop() {
  20. digitalWrite(trigPin, LOW);
  21. delayMicroseconds(2);
  22. digitalWrite(trigPin, HIGH);
  23. delayMicroseconds(10);
  24. digitalWrite(trigPin, LOW);
  25. duration = pulseIn(echoPin, HIGH);
  26. distanceCm= duration*0.034/2;
  27. distanceInch = duration*0.0133/2;
  28. lcd.setCursor(0,0); // Sets the location at which subsequent text written to the LCD will be displayed
  29. lcd.print("Distance: "); // Prints string "Distance" on the LCD
  30. lcd.print(distanceCm); // Prints the distance value from the sensor
  31. lcd.print(" cm");
  32. delay(10);
  33. lcd.setCursor(0,1);
  34. lcd.print("Distance: ");
  35. lcd.print(distanceInch);
  36. lcd.print(" inch");
  37. delay(10);
  38. }

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


///////////////////////////////////////////////////////////////////
//HC-SR05 maar vrijwel iedere andere sensor is mogelijk.
////////////
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance;

////////////////////////////
//switsch case
/////////////
const int sensorMin = 0;      // sensor minimum = 0 cm
const int sensorMax = 200;    // sensor maximum = 200 cm


void setup() 
{
// HC-SR05
Serial.begin(9600);
delayMicroseconds(2);
pinMode(trigPin, OUTPUT); 
pinMode(echoPin, INPUT);
}

void loop() 
{

//HC-SR05
digitalWrite(trigPin, LOW);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
Serial.print("Distance: ");
Serial.println(distance);

//////////////////////////////////////

Serial.println ();
int range = map(distance, sensorMin, sensorMax,0,3);

   switch (range) {
    case 0:    
      Serial.println("heel erg dichtbij");
      break;
    case 1:    
      Serial.println("dichtbij");
      break;
    case 2:    
      Serial.println("verder af");
      break;
    case 3:    
      Serial.println("heel ver af");
      break;
      
  }
  delay(1); 
}