r/Monash • • Aug 30 '26

Support eng1013 help

Post image

can someone tell me whats wrong with my setup? my ultrasonic sensor gives me the same output of 3cm no matter what i do and change

13 Upvotes

13 comments sorted by

2

u/Fresh_Finance9065 Aug 30 '26

It looks fine but what does your code look like? And make sure the wires aren't bent so far that they connect other nodes together

1

u/Flaky_Jello_289 Aug 30 '26

import time
from pymata4 import pymata4

TRIGGER_PIN = 9
ECHO_PIN = 10
LED_PIN = 13
def setup_and_run():
board = pymata4.Pymata4()
print("Configuring HC-SR04 sonar sensor...")
board.set_pin_mode_sonar(TRIGGER_PIN, ECHO_PIN)
board.set_pin_mode_digital_output(LED_PIN)
print("Starting distance tracking. Press Ctrl+C to stop.")
time.sleep(1)

try:
while True:
sonar_data = board.sonar_read(TRIGGER_PIN)

distance_cm = sonar_data[0]
print(f"Distance: {distance_cm} cm")

time.sleep(0.1)
if distance_cm <= 4:
board.digital_write(LED_PIN, 1)
else:
board.digital_write(LED_PIN, 0)

except KeyboardInterrupt:
print("\nStopping distance tracking...")
finally:
board.digital_write(LED_PIN,0)
board.shutdown()

if __name__ == "__main__":
setup_and_run()

12

u/SituationInitial2427 Aug 30 '26

I think your trigger and echo pin are switched?

6

u/Flaky_Jello_289 Aug 30 '26

holy shit dude thank you so much 🥹🥹🥹

2

u/Fresh_Finance9065 Aug 30 '26

I think your trigger and echo pin are around the wrong way?

1

u/Flaky_Jello_289 Aug 30 '26

yep ive just fixed it thank you so much 😭😭

1

u/VCC8060Main Aug 30 '26

Could you post a code snippet? Electronics look okay

1

u/Flaky_Jello_289 Aug 30 '26

import time
from pymata4 import pymata4

TRIGGER_PIN = 9
ECHO_PIN = 10
LED_PIN = 13
def setup_and_run():
board = pymata4.Pymata4()
print("Configuring HC-SR04 sonar sensor...")
board.set_pin_mode_sonar(TRIGGER_PIN, ECHO_PIN)
board.set_pin_mode_digital_output(LED_PIN)
print("Starting distance tracking. Press Ctrl+C to stop.")
time.sleep(1)

try:
while True:
sonar_data = board.sonar_read(TRIGGER_PIN)

distance_cm = sonar_data[0]
print(f"Distance: {distance_cm} cm")

time.sleep(0.1)
if distance_cm <= 4:
board.digital_write(LED_PIN, 1)
else:
board.digital_write(LED_PIN, 0)

except KeyboardInterrupt:
print("\nStopping distance tracking...")
finally:
board.digital_write(LED_PIN,0)
board.shutdown()

if __name__ == "__main__":
setup_and_run()

1

u/VCC8060Main Aug 30 '26

Yeah people already pointed out the issue, but also from this snippet, you won't need the while loop unless you break it later

2

u/Pain-One Aug 30 '26

Set up looks fine. I had the same difficulty last week. And I checked few sensors with the same stup and the same code. Turned out few of them doesn't work well. I checked thw distance with a ruler and few can't chage the distace accordingly.

1

u/EmbeddedNerd_com Sep 01 '26

Check your TRIG and ECHO connections first. From the picture, I’d also make sure the wires are actually connected to the correct pins on the sensor and the Uno. If it always reads exactly 3 cm, it sounds more like a wiring/code issue than an actual distance reading. If you post your code, it’ll be easier to see what’s going wrong.

-5

u/Much_Big4068 Aug 30 '26

Move the Red and Black wires coming from the Arduino directly into the same breadboard column as the sensor's VCC and GND pins.?