O sensor ultra-sônico HC-SR04 é utilizado para a medição de distâncias com precisão. Sua utilização é bem simples, e também podemos contar com uma classe pronta para fazer seu tratamento.
O HC-SR04 conta com 4 pinos:
- Vcc (pino de alimentação)
- Trig (entrada de dados)
- Echo (saida de dados)
- Gnd (comum, neutro)
Vamos liga-lo na arduino da seguinte forma:
Vamos escrever o seguinte programa para ele:
const int trigPin = 12; const int echoPin = 11; void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); Serial.begin(9600); } void loop() { long duration, inches, cm; // The PING))) is triggered by a HIGH pulse of 2 or more microseconds. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse: digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(5); digitalWrite(trigPin, LOW); // The same pin is used to read the signal from the PING))): 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. duration = pulseIn(echoPin, HIGH); // convert the time into a distance inches = microsecondsToInches(duration); cm = microsecondsToCentimeters(duration); Serial.print(inches); Serial.print("in, "); Serial.print(cm); Serial.print("cm"); Serial.println(); delay(100); } 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. // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf 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; }
Essa é toda a forma “manual” de se fazer, enviamos os pulsos, calculamos o tempo de resposta e pegamos a distância relativa a formula da velocidade do som.
Agora, descobri que existe uma biblioteca para isso bem simples de ser utilizada veja:
http://code.google.com/p/arduino-new-ping/
Com esse biblioteca não precisamos nos preocupar com os cálculos veja um exemplo de sua utilização:
// --------------------------------------------------------------------------- // Example NewPing library sketch that does a ping about 20 times per second. // --------------------------------------------------------------------------- #include <NewPing.h> // Arduino pin tied to trigger pin on the ultrasonic sensor. #define TRIGGER_PIN 12 // Arduino pin tied to echo pin on the ultrasonic sensor. #define ECHO_PIN 11 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. #define MAX_DISTANCE 200 // NewPing setup of pins and maximum distance. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); void setup() { // Open serial monitor at 115200 baud to see ping results. Serial.begin(115200); } void loop() { // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings. delay(50); // Send ping, get ping time in microseconds (uS). unsigned int uS = sonar.ping(); Serial.print("Ping: "); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo) Serial.print(uS / US_ROUNDTRIP_CM); Serial.println("cm"); }
boa noite gostaria de adaptar um medidor com display em uma guilhotina grafica com a medida maxima de 150 mm e teria que ter uma prcisao de 1 mm ,vc tema para vender?