Easy Arduino
This is page is going to tell you the process of creating an HC-SR04 example. This project doesn't require many materials. It is improved form a work piece. Below is the link of the original work.
Supplies
Arduino Leornado (The original use Arduino UNO)
One (1) HC-SR04 Ultrasonic Sensor
One (1) yellow LED
One (1) White LED
Two (2) (Brown, Black, Brown. Gold) Resistors
Half Breadboard
Eight (8) Male/Male hookup wires
Step 2 Connect the Components
Connect the components and wires as shown in the two pictures. The Led's color can change as you wish.
Step 3: Coding
Copy the code
At first the code is when the distance of led and the object is smaller than 4 it will light up. I change it to when the distance with the object is from 4 to 50 it will light. Else, another led will light up.
#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 && distance <50) { // 改過 This is where the LED On/Off happens
digitalWrite(led,HIGH); // When the Yellow condition is met, the White 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); }
Step 4 Test If the Arduino Work
Put your hand in front of the sensor and get further slowly. The white light should sine at the beginning. Then yellow light will shine for few seconds. Last, the white light will shine and yellow light will not bright
Step 5 Making the Cover
I use card board to cover my Arduino. On the top there are two holes on it for the led to pass through. One of the side had a hole to let the sensor pass through, For the another side, it is just cardboard.