HC-SR04 am Arduino Uno: Unterschied zwischen den Versionen

Aus eLAB Wiki
Wechseln zu: Navigation, Suche
K (Programm für den Arduino)
K
Zeile 9: Zeile 9:
 
== Programm für den Arduino ==
 
== Programm für den Arduino ==
  
<syntaxhighlight lang=c>
+
<syntaxhighlight lang="Arduino">
 
#define TRIGGER_PIN  3
 
#define TRIGGER_PIN  3
 
#define ECHO_PIN    2
 
#define ECHO_PIN    2

Version vom 19. Februar 2018, 18:10 Uhr

Anschluss des HC-SR04 an den Arduino

zum Anschluss des HC-SR04 Ultraschallsensor an ein Arduino Board werden 2 Digital I/Os benötigt. Ein Trigger Ausgang, der den Sende Burst startet. Sowie ein Echo Eingang, der das empfangene Echo Signal einlist. Zur Auswertung wird die Länge des Echo Impulses gemessen, um darüber die Laufzeit und damit die Entfernung zum Objekt zu bestimmen.

HC-SR04 Verdrahtung

HC-SR04 Schaltplan

Programm für den Arduino

#define TRIGGER_PIN  3
#define ECHO_PIN     2

long duration;
long cmMsec, inMsec;

void initHCSR04()
{
  pinMode(TRIGGER_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  digitalWrite(TRIGGER_PIN, LOW);
}

long readHCSR04()
{
  long pulsein;
  // The HC-SR04 is triggered by a HIGH pulse of 10 or more microseconds.
  digitalWrite(TRIGGER_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGGER_PIN, LOW);
 
  // The same pin is used to read the signal from the HC-SR04 a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pulsein = pulseIn(ECHO_PIN, HIGH);
  return pulsein;
}

long microsecondsToInches(long microseconds)  
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  return microseconds / 74 / 2;
}
 
long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

uint8_t EnPwmCmd[4]={0x44,0x02,0xbb,0x01};    // distance measure command

void PWM_Mode_Setup(){ 
  pinMode(TRIGGER_PIN,OUTPUT);                     // A low pull on pin COMP/TRIG
  digitalWrite(TRIGGER_PIN,HIGH);                  // Set to HIGH
  
  pinMode(ECHO_PIN, INPUT);                      // Sending Enable PWM mode command
  
  for(int i=0;i<4;i++){
      Serial.write(EnPwmCmd[i]);
   } 
}
 
void setup(){                                 // Serial initialization
  Serial.begin(9600);                         // Sets the baud rate to 9600
  PWM_Mode_Setup();
  initHCSR04();
}

void loop()
{
  duration = readHCSR04();
  
  cmMsec = microsecondsToCentimeters(duration);
  inMsec = microsecondsToInches(duration);
  
  Serial.print("CM: ");
  Serial.print(cmMsec);
  Serial.print(", IN: ");
  Serial.println(inMsec);

  delay(50);
}