KY-050 Ultraschallabstandssensor: Unterschied zwischen den Versionen
Aus Linkerkit.de
Zeile 1: | Zeile 1: | ||
==Bild== | ==Bild== | ||
− | [[Datei:KY-0050.png | + | [[Datei:KY-0050.png|none|394x274px]] |
+ | |||
Zeile 9: | Zeile 10: | ||
==Pin-Belegung== | ==Pin-Belegung== | ||
− | [[Datei:4_G_S_T_V.png | + | [[Datei:4_G_S_T_V.png|none|450x235px]] |
==Codebeispiel Arduino== | ==Codebeispiel Arduino== | ||
− | <pre class="brush:cpp"> | + | <pre class="brush:cpp"> |
+ | /* | ||
+ | HC-SR04 Ping distance sensor: | ||
+ | VCC to arduino 5v | ||
+ | GND to arduino GND | ||
+ | Echo to Arduino pin 7 | ||
+ | Trig to Arduino pin 8 | ||
+ | This sketch originates from Virtualmix: http://goo.gl/kJ8Gl | ||
+ | Has been modified by Winkle ink here: http://winkleink.blogspot.com.au/2012/05/arduino-hc-sr04-ultrasonic-distance.html | ||
+ | And modified further by ScottC here: http://arduinobasics.blogspot.com.au/2012/11/arduinobasics-hc-sr04-ultrasonic-sensor.html | ||
+ | on 10 Nov 2012 | ||
+ | Further modified by Peter Takacs on 04/02/04 https://patakacs.wordpress.com/2014/02/04/ultrasonic-sensor-hc-sr04-and-piezo-speakers-measuring-distance-and-playing-a-tone-with-piezo/ | ||
+ | */ | ||
+ | |||
+ | int echoPin = 7; | ||
+ | int trigPin = 8; | ||
+ | int LEDPinYellow = 2; // Yellow LED | ||
+ | int LEDPinGreen = 4; // Green LED | ||
+ | int soundPin = 12; // Piezo | ||
+ | |||
+ | int maximumRange = 100; | ||
+ | int minimumRange = 0; | ||
+ | long duration, distance; | ||
+ | |||
+ | void setup() { | ||
+ | Serial.begin (9600); // Init communications to serial monitor | ||
+ | pinMode(trigPin, OUTPUT); | ||
+ | pinMode(echoPin, INPUT); | ||
+ | pinMode(LEDPinYellow, OUTPUT); | ||
+ | pinMode(LEDPinGreen, OUTPUT); | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | /* The following trigPin/echoPin cycle is used to determine the | ||
+ | distance of the nearest object by bouncing soundwaves off of it. */ | ||
+ | digitalWrite(trigPin, LOW); | ||
+ | delayMicroseconds(2); | ||
+ | |||
+ | digitalWrite(trigPin, HIGH); | ||
+ | delayMicroseconds(10); | ||
+ | |||
+ | digitalWrite(trigPin, LOW); | ||
+ | duration = pulseIn(echoPin, HIGH); | ||
+ | |||
+ | //Calculate the distance (in cm) based on the speed of sound. | ||
+ | distance = duration/58.2; | ||
+ | |||
+ | if (distance >= maximumRange || distance <= minimumRange){// Yellow led indicated out of maximumRange. Prints “Out Of Range” to serial if target is outside maximumRange. | ||
+ | Serial.println(“Out Of Range”); | ||
+ | digitalWrite(LEDPinYellow, HIGH); | ||
+ | digitalWrite(LEDPinGreen, LOW); | ||
+ | } | ||
+ | else { | ||
+ | // When ultasonic sensor picks up a signal _within_ maximumRange, print distance in cm to serial monitor, turn off YellowLed and turn on GreenLED. | ||
+ | Serial.print(“Distance = ” ); | ||
+ | Serial.print(distance); | ||
+ | Serial.println(” cm” ); | ||
+ | digitalWrite(LEDPinYellow, LOW); | ||
+ | digitalWrite(LEDPinGreen, HIGH); | ||
+ | tone(soundPin, 800, 300); | ||
+ | delay(distance); // Distance is the delay in ms between tones, ie Near maxRange -> Long tones, Near minimumRange -> Rapid tones. | ||
+ | noTone(soundPin); | ||
+ | |||
+ | } | ||
+ | |||
+ | // Mandatory delay | ||
+ | delay(50); | ||
+ | } | ||
</pre> | </pre> | ||
+ | |||
+ | Quelle: https://patakacs.wordpress.com/2014/02/04/ultrasonic-sensor-hc-sr04-and-piezo-speakers-measuring-distance-and-playing-a-tone-with-piezo/ |
Version vom 24. Februar 2016, 08:04 Uhr
Inhaltsverzeichnis
Bild
Technische Daten / Kurzbeschreibung
Wird am Trigger-Eingang ein Signal (fallende Flanke) eingegeben, so wird eine Abstandsmessung durchgeführt und am Echo-Ausgang als PWM-TTL Signal ausgegeben
messbare Distanz: 2cm—300cm Messauflösung: 3mm
Pin-Belegung
Codebeispiel Arduino
/* HC-SR04 Ping distance sensor: VCC to arduino 5v GND to arduino GND Echo to Arduino pin 7 Trig to Arduino pin 8 This sketch originates from Virtualmix: http://goo.gl/kJ8Gl Has been modified by Winkle ink here: http://winkleink.blogspot.com.au/2012/05/arduino-hc-sr04-ultrasonic-distance.html And modified further by ScottC here: http://arduinobasics.blogspot.com.au/2012/11/arduinobasics-hc-sr04-ultrasonic-sensor.html on 10 Nov 2012 Further modified by Peter Takacs on 04/02/04 https://patakacs.wordpress.com/2014/02/04/ultrasonic-sensor-hc-sr04-and-piezo-speakers-measuring-distance-and-playing-a-tone-with-piezo/ */ int echoPin = 7; int trigPin = 8; int LEDPinYellow = 2; // Yellow LED int LEDPinGreen = 4; // Green LED int soundPin = 12; // Piezo int maximumRange = 100; int minimumRange = 0; long duration, distance; void setup() { Serial.begin (9600); // Init communications to serial monitor pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(LEDPinYellow, OUTPUT); pinMode(LEDPinGreen, OUTPUT); } void loop() { /* The following trigPin/echoPin cycle is used to determine the distance of the nearest object by bouncing soundwaves off of it. */ digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); //Calculate the distance (in cm) based on the speed of sound. distance = duration/58.2; if (distance >= maximumRange || distance <= minimumRange){// Yellow led indicated out of maximumRange. Prints “Out Of Range” to serial if target is outside maximumRange. Serial.println(“Out Of Range”); digitalWrite(LEDPinYellow, HIGH); digitalWrite(LEDPinGreen, LOW); } else { // When ultasonic sensor picks up a signal _within_ maximumRange, print distance in cm to serial monitor, turn off YellowLed and turn on GreenLED. Serial.print(“Distance = ” ); Serial.print(distance); Serial.println(” cm” ); digitalWrite(LEDPinYellow, LOW); digitalWrite(LEDPinGreen, HIGH); tone(soundPin, 800, 300); delay(distance); // Distance is the delay in ms between tones, ie Near maxRange -> Long tones, Near minimumRange -> Rapid tones. noTone(soundPin); } // Mandatory delay delay(50); }