Using the HC-SR04 sensor, turn on 4 LEDs on
waterfall, this is as we vary the distance
between an object and the sensor, they will light up more or
less LEDs. On Arduino Uno
The numbers do jump around a lot even when it's "looking" at a stationary object. It's not a precision device (IMO).
What kind of distance are you talking about?
I would first get an HC-SR04 sensor working on your UNO. With one of the example programs have the distance read by the sensor printed to the IDE serial monitor. Move things around and see is the sensor results follow what you are looking for. As @runaway_pancake said, the readings can seem to be very random at times and depending on the moving "target"
Why did you not just copy the code from the video? (see Post #4)
ino
#define PIN_TRIG 3
#define PIN_ECHO 2
int led;
void setup() {
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
for (int i = 4; i < 7; i++) {
pinMode(i, OUTPUT);
}
}
void loop() {
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
led = pulseIn(PIN_ECHO, HIGH) / 6000 + 5;
for (int i = 4; i < led; i++) {
digitalWrite(i, HIGH);
}
for (int i = led; i < 8; i++) {
digitalWrite(i, LOW);
}
}
json
{
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-nano", "id": "nano", "top": -4.8, "left": -0.5, "attrs": {} },
{
"type": "wokwi-hc-sr04",
"id": "ultrasonic1",
"top": -142.5,
"left": 101.5,
"attrs": { "distance": "400" }
},
{ "type": "wokwi-led", "id": "led3", "top": -109.2, "left": -73, "attrs": { "color": "red" } },
{
"type": "wokwi-led",
"id": "led4",
"top": -109.2,
"left": -101.8,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led",
"id": "led5",
"top": -109.2,
"left": -15.4,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led",
"id": "led6",
"top": -109.2,
"left": -44.2,
"attrs": { "color": "red" }
}
],
"connections": [
[ "nano:5V", "ultrasonic1:VCC", "red", [ "v14.4", "h66.7" ] ],
[ "ultrasonic1:TRIG", "nano:3", "green", [ "v9.6", "h-38.8" ] ],
[ "ultrasonic1:ECHO", "nano:2", "green", [ "v19.2", "h-68" ] ],
[ "nano:GND.1", "ultrasonic1:GND", "black", [ "v28.8", "h57.6", "v-9.6" ] ],
[ "nano:4", "led5:A", "green", [ "v-19.2", "h-86.4" ] ],
[ "nano:5", "led6:A", "green", [ "v-19.2", "h-96" ] ],
[ "nano:6", "led3:A", "green", [ "v-19.2", "h-115.2" ] ],
[ "nano:7", "led4:A", "green", [ "v-19.2", "h-134.4" ] ],
[ "nano:GND.2", "led5:C", "black", [ "v-9.6", "h-124.8" ] ],
[ "nano:GND.2", "led6:C", "black", [ "v-9.6", "h-144" ] ],
[ "nano:GND.2", "led3:C", "black", [ "v-9.6", "h-182.4", "v-57.6" ] ],
[ "nano:GND.2", "led4:C", "black", [ "v-9.6", "h-211.2", "v-57.6" ] ]
],
"dependencies": {}
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.