Archives for 

hcsr04

HC-SR04 Distance Sensor Arduino Source Code

hc-sr04 distance sensor and arduino wire

hc-sr04 distance sensor and arduino wire

HC-SR04 is a proximity sensor that is very widely used. This sensor is very easy to use because it only needs a simple syntax for programming it. You can view the datasheet and specification of the sensor’s hc-sr04 here.

Sensor HC-SR04 is widely used as a sensor on the robot to detect objects around the robot. In this tutorial, use the Arduino Uno as the processor. While its pin configuration can be seen in the image above.

VCC and GND pins on the sensor is connected to the VCC and GND pins on the Arduino UNO. While Trig pin is connected to digital pin 11 on the Arduino UNO. And echo pin is connected to digital pin 12 on the Arduino UNO.

 

HC-SR04 Source code :

#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define LEDPin 13 // Onboard LED

int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}

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){
/* Send a negative number to computer and Turn LED ON
to indicate “out of range” */
Serial.println(“-1”);
digitalWrite(LEDPin, HIGH);
}
else {
/* Send the distance to the computer using Serial protocol, and
turn LED OFF to indicate successful reading. */
Serial.println(distance);
digitalWrite(LEDPin, LOW);
}

//Delay 50ms before next reading.
delay(50);
}

HC-SR04 Ultrasonic Distance Sensor

hc-sr04-distance-sensor

hc-sr04-distance-sensor

HC-SR04 is one of a proximity sensor. HC-SR04 is very cheap, so many developers that use these sensors to the system they developed. HC-SR04 can measure distances with ultrasonic wave method. HC-SR04 has a two-part sensor which is important, namely the transmitter and receiver. The transmitter will throw ultrasonic waves and will be reflected by a particular object. This reflected wave will be captured by the receiver. The delivery time can be determined from the calculation of the transmitting and catching time.

hc-sr04 distance sensor for arduino

hc-sr04 distance sensor for arduino

HC-SR04 has a 4 pin connector: VCC, Trig (T), echo (R), GND. VCC is connected to the voltage of 5V, GND connected to Ground, Trig (T) is connected to the data pin microcontroller, and echo (R) is connected to the data microcontroller (different pin).

– Model: HC-SR04
– Color: Blue + Silver
– Working voltage : 5V(DC)
– Static current: Less than 2mA.
– Output signal: Electric frequency signal, high level 5V, low level 0V.
– Sensor angle: Not more than 15 degrees.
– Detection distance: 2cm~450cm.
– High precision: Up to 3mm
– Mode of connection: VCC / trig(T) / echo(R) / GND
– Module Working Principle:
– Adopt IO trigger through supplying at least 10us sequence of high level signal
– The module automatically send eight 40khz square wave and automatically detect whether receive the returning pulse signal
– If there is signals returning, through outputting high level and the time of high level continuing is the time of that from the ultrasonic transmitting to receiving

 

hc-sr04 distance sensor and arduino wire

hc-sr04 distance sensor and arduino wire

For example, you can connect Trig (T) pin and echo (R) pin on the Arduino Uno as shown above. In the picture can be seen that Trig is connected to the pin number 11 and the echo is connected to the pin number 12. For source code and the programming tutorial you can read here.