...
- VCC = +5VDC (GPIO)
- Trig = Trigger input of Sensor (5V Digital GPIO)
- Echo = Echo output of Sensor (5V Digital GPIO)
Important: Outputs 5V. If the microcontroller can only handle 3.3V inputs, we need to build an easy voltage divider. - GND = GND (GPIO)
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
/*
HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 13 Trig to Arduino pin 12
Red POS to Arduino pin 11
Green POS to Arduino pin 10
560 ohm resistor to both LED NEG and GRD power rail
More info at: http://goo.gl/kJ8Gl
Original code improvements to the Ping sketch sourced from Trollmaker.com
Some code and wiring inspired by http://en.wikiversity.org/wiki/User:Dstaub/robotcar
*/
#define trigPin 13
#define echoPin 12
#define led 11
#define led2 10
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 4) { // This is where the LED On/Off happens
digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
digitalWrite(led2,LOW);
}
else {
digitalWrite(led,LOW);
digitalWrite(led2,HIGH);
}
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
} |
Expand | ||
---|---|---|
| ||
/*
HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 13 Trig to Arduino pin 12
Red POS to Arduino pin 11
Green POS to Arduino pin 10
560 ohm resistor to both LED NEG and GRD power rail
More info at: http://goo.gl/kJ8Gl
Original code improvements to the Ping sketch sourced from Trollmaker.com
Some code and wiring inspired by http://en.wikiversity.org/wiki/User:Dstaub/robotcar
*/
#define trigPin 13
#define echoPin 12
#define led 11
#define led2 10
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 4) { // This is where the LED On/Off happens
digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
digitalWrite(led2,LOW);
}
else {
digitalWrite(led,LOW);
digitalWrite(led2,HIGH);
}
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
} |
Expand | ||
---|---|---|
| ||
Minimum sensor recognition distance:> Sensor’s minimum-distance does not have to be much smaller than 20cm (8“). Infrared:
Pro: - Quality products comparatively cheap - In some cases very high resolution (up to 1 mm) Contra: - Problems with sun light - Problems with recognition of dark or black objects - Small range sizes
Recommendation after some research: Sharp GP2Y 0A02 (ca. $11, 20cm – 150cm Range, Analogue Output) http://acroname.com/products/SHARP-GP2Y0A02YK0F-IR-PACKAGE (see also: http://acroname.com/articles/sharp-infrared-ranger-comparison for comparison)
UltrasonicPro: - Not depending on optical properties of an object -> more reliable in common usage - Larger range sizes Contra: - Usually higher prize - Lower resolution (2-3 cm) - Problems detecting acoustic quieting objects
Recommendation after some research: Parallax PING Ultrasonic Sensor (ca. $29, 2cm – 300cm Range) https://www.parallax.com/product/28015 (maybe cheap replacement: http://www.dx.com/p/hc-sr04-ultrasonic-sensor-distance-measuring-module-133696?Utm_rid=37202148&Utm_source=affiliate#.VTsyqa3tmko) |
...