RAISE YOUR HEAD UP!
Original project: https://www.instructables.com/Simple-Arduino-and-H...
Hi everyone, I’m Doris, and I’m going to teach you how to make a distance sensor today. This device will light up green when the distance is greater than 500, and light up in red when the distance is less than 500, and will beep for a long time until the distance is greater than 500 again. The device is designed to keep a distance from the paper when writing for young children, so that it will not hurt the eyes.
Supplies
1. 10 * wires
2. 1 * 1W speaker
3. 2 * LED (Green, Red)
4. 2 * Resistor 220 Ohm
5. 1* HC-SR04
6. Arduino Leonardo
7. Breadboard
Connect the Circuits
1. Connect the red wire of your 1W speaker to ~9
2. Connect the black wire of your 1W speaker to GND
3. Use a wire to connect the positive power trail to 5V
4. Use a wire to connect the negative power trail to another GND
5. Use a wire to connect A50 to ~10; A60 to ~11
6. Connect one side of the 220 ohm resistor to the right side of each wire in step 4 (A49 & A59)
7. Connect the other side of the resistor to the negative power trail
8. Connect the longer pin of the red LED to one of the wire in step four (B60 or B50)
9. Connect the shorter pin of the red LED to one of the resistor in step four (B49 or B59)
* Remember the short and long pin must be on the same trail (B60 ←→ B49 ; B50 ←→B59)
10. Use a wire to connect the UCC of your HC-SR04 to the positive power trail
11. Use a wire to connect the Trig of your HC-SR04 to 13
12. Use a wire to connect the Echo of your HC-SR04 to 12
10. Use a wire to connect the GND of your HC-SR04 to the negative power trail
11. YOU ARE FINISH ~!
* If any of the steps confuse you, you can follow the circuit diagram to connect your circuit
Upload the Code
My Code: Click the below link to view my code or copy below code or https://create.arduino.cc/editor/dorfina_1415/31bc...
void setup() { // put your setup code here, to run once: } void loop() { long duration, distance; digitalWrite(13, LOW); delayMicroseconds(2); digitalWrite(13, HIGH); delayMicroseconds(10); digitalWrite(13, LOW); duration = pulseIn(12, HIGH); distance = (duration/2) / 29.1; if (distance < 4) { digitalWrite(11,HIGH); digitalWrite(10,LOW); tone(9, 660, 1000); // 改 } else { digitalWrite(11,LOW); digitalWrite(10,HIGH); } if (distance >= 500 || distance <= 0){ // 改 Serial.println("Out of range"); } else { Serial.print(distance); Serial.println(" cm"); } delay(1000); // 改,為了讓它能夠正常運作 }
Downloads
Decoration (optional Step)
The decorative components make the device look more beautiful, but you can skip this step.
THANK YOU!
Final Production
Link: https://youtu.be/HIb_2euPbJI