Arduino > Capteur de distance ultrasonique (Ultrasonic Distance Sensor)
Contents (hide)
/* By : Thomas Ouellet Fredericks Connect the Ultrasonic Sensor's TRIGGER pin to digital pin 7. Ground the MODE/OUT pin. */ #define ULTRASONIC_TRIGGER 7 void setup() { Serial.begin(57600); } void loop() { // TRIGGER PULSE // set as OUTPUT and send a 11 us pulse : pinMode(ULTRASONIC_TRIGGER,OUTPUT); digitalWrite(ULTRASONIC_TRIGGER, HIGH); delayMicroseconds(11); digitalWrite(ULTRASONIC_TRIGGER, LOW); // LISTEN FOR PULSE // Set as input : pinMode(ULTRASONIC_TRIGGER,INPUT); // Listen for HIGH pulse : unsigned long microseconds = pulseIn(ULTRASONIC_TRIGGER, HIGH); // SERIAL OUTPUT RESULTS // Serial print microseconds : Serial.print("microseconds "); Serial.println(microseconds); // Serial print centimeters : Serial.print("cm "); Serial.println(microseconds/58); // PAUSE // Wait at least 50 ms : delay(50); }
Code Arduino:
// Connect the ultrasonic sensor to digital pin 7 #define SUS 7 void setup() { Serial.begin(57600); } void loop() { pinMode(SUS,OUTPUT); digitalWrite(SUS, HIGH); delayMicroseconds(10); digitalWrite(SUS, LOW); // Wait for HIGH pinMode(SUS,INPUT); unsigned long duration = pulseIn(SUS, HIGH); Serial.println(microsecondsToCentimeters(duration)); delay(50); } long microsecondsToCentimeters(long microseconds) { return microseconds / 58; }
«To have the circuit continuously loop so the chain is always giving an analog voltage output, connect pin 5 of the last sensor in the sequence to pin 4 of the first sensor in sequence with a 1K resistor in sequence between the pin 5 output and pin 4 input. [...] With these sensor chaining methods, once pin 4 is pulled high for 20uS on the first sensor, all sensors will chain sequencially. After the micro controller brings pin 4 high, the micro controller will have to return it’s pin to a high impedance state so that after the sequence is complete the TX signal output from the last sensor will trigger the RX of the first sensor.»
Plus d'informations : Multiple_MaxSonar_Sensors.pdf