Introduction: Arduino + HC-SR04
Arduino + HC-SR04
assembling
Step 1: Video
This projects consists to use the HC-SR04 sensor with Arduino.
This sensor can be used to measure distances or overcome obstacles.
How this happend?
Well, one transducer send a ultrasonic signal , an the other waits for captured the signal when return.
Let's get started!
Step 2: Required Hardware
- Arduino
- Sensor HC-SR04
- Wires
- Breadboard
- Rule
Step 3: Circuit
This same diagram created with Fritzing was as connect all circuit.
The sensor consists of 4 pins:
- "VCC" connected to the output of 5V.
- "Trig" connected to digital pin board responsible for sending the ultrasonic pulse.
- "Echo" to the digital input pin to receive the echo said pulse.
- "GND" grounded.
Step 4: Code
The code has the explanation how the signal is obtained.
4 Comments
7 years ago
Where is the code in this instructable? I see only two photos below Step 4: Code with one containing only a part of the sketch.
Reply 7 years ago
Hi This is the code:
/*
Descripción -Description:
Programa que permite leer la distacia entre un objeto y el sensor HC-SR04
Program allow read the distance between an object an sensor HC-SR04
Version: 1.0
*/
int echoPin = 9;//Pin pulso ultrasonido - pin ultrasonic pulse
int triggerPin = 8;//Pin para tiempo rebote del ultrasonido - pin reboot time
long distancia;
long tiempo;
void setup(){
Serial.begin(9600);
pinMode(echoPin, OUTPUT);
pinMode(triggerPin, INPUT);
}
void loop(){
digitalWrite(echoPin,LOW); //Inicia en bajo - start in Low
delayMicroseconds(5);
digitalWrite(echoPin, HIGH); //Enviar el pulso Send the pulse
delayMicroseconds(10);
tiempo=pulseIn(triggerPin, HIGH);
/* Usando la formula para calcular la distancia como un valor entero
Se divide la distancia entre 2 porque solo necesitamos la de ida (0,034 cm/microseg)/2
Using the formula to calculate the distance as an integer
The distance between 2 is divided because we only need the round (0.034 cm / microseconds) / 2
*/
distancia= int(0.017*tiempo);
//Imprimir valor - print value
Serial.print("Distancia ");
Serial.print(distancia);
Serial.print(" cm");
Serial.println("");
delay(1000);
}
I Hope help you, see you!!!
7 years ago on Introduction
This project excellently some one i made just view day before
you can copy code from mine https://www.instructables.com/id/Distance-measurement-using-Ultrasonic-sensor-and-A/
Reply 7 years ago on Introduction
Thanks, I promise prove your code