Conexión:
GND - tierra
ECHO - digital pin 2 de arduino
TRIG - digital pin 3 de arduino
Vcc - 5v de arduino
Código:
/*
* Define the pins you want to use as trigger and echo.
*/
#define ECHOPIN 2 // Pin to receive echo pulse
#define TRIGPIN 3 // Pin to send trigger pulse
/*
* setup function
* Initialize the serial line (D0 & D1) at 115200.
* Then set the pin defined to receive echo in INPUT
* and the pin to trigger to OUTPUT.
*/
void setup()
{
// Serial.begin(115200);
Serial.begin(9600);
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
}
/*
* loop function.
*
*/
void loop()
{
// Start Ranging
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW);
// Compute distance
float distance = pulseIn(ECHOPIN, HIGH);
distance= distance/58;
Serial.println(distance);
delay(200);
}
fuente: http://www.xappsoftware.com/wordpress/2012/03/15/how-to-interface-the-hc-sr04-ultrasonic-ranging-module-to-arduino/
Ejemplo de Paking assistant:
http://electronicavm.wordpress.com/2011/07/07/sensor-de-aparcamiento-con-arduino/
No hay comentarios:
Publicar un comentario