friendly93
Posts: 11
Joined: Thu Nov 30, 2023 4:36 pm

How to wire 2 sensors to a raspberry pico w?

Sun Dec 10, 2023 3:43 am

How would I wire 2 hcsr04 5V sensors to a pico w? Currently im putting one of it to the 3V3 output but when I try to put both, the sensors only work for the first second and thonny stops printing the distance after the first time

How can I fix this? Can I do it such that only wires are needed? I also have an actuator attached via a h bridge to it so some pins are also not available

User avatar
neilgl
Posts: 7473
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near The National Museum of Computing

Re: How to wire 2 sensors to a raspberry pico w?

Sun Dec 10, 2023 11:36 am

Can you post the python script you are using in Thonny so we can look at it. Thanks.

friendly93
Posts: 11
Joined: Thu Nov 30, 2023 4:36 pm

Re: How to wire 2 sensors to a raspberry pico w?

Sun Dec 10, 2023 12:27 pm

from machine import Pin
import time

led = Pin('LED',Pin.OUT)
trig_1 = Pin(3, Pin.OUT)
echo_1 = Pin(6, Pin.IN)
trig_2 = Pin(9, Pin.OUT)
echo_2 = Pin(8, Pin.IN)

def measure_distance(trig_pin, echo_pin):
# Trigger pulse
trig_pin.value(1)
time.sleep_us(10)
trig_pin.value(0)

# Measure pulse duration
pulse_start = time.ticks_us()
while echo_pin.value() == 0:
pulse_start = time.ticks_us()

pulse_end = time.ticks_us()
while echo_pin.value() == 1:
pulse_end = time.ticks_us()

pulse_duration = time.ticks_diff(pulse_end, pulse_start)

# Calculate distance in centimeters
distance = (pulse_duration * 0.0343) / 2
return distance

# Main program
try:
# Main loop
while True:
distance_1 = measure_distance(trig_1, echo_1)
distance_2 = measure_distance(trig_2, echo_2)
if distance_1 < 10 and distance_2 > 10:
led(1)
print("Distance Sensor 1:", distance_1, "cm")
print("Distance Sensor 2:", distance_2, "cm")

else:
led(0)
print("Distance Sensor 1:", distance_1, "cm")
print("Distance Sensor 2:", distance_2, "cm")


time.sleep(1) # Pause for 1 second

except KeyboardInterrupt:

friendly93
Posts: 11
Joined: Thu Nov 30, 2023 4:36 pm

Re: How to wire 2 sensors to a raspberry pico w?

Sun Dec 10, 2023 12:28 pm

how would the wiring look like?

User avatar
neilgl
Posts: 7473
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near The National Museum of Computing

Re: How to wire 2 sensors to a raspberry pico w?

Sun Dec 10, 2023 2:04 pm

This code is working for me (with no sensors attached!) on a pico W and gives Thonny output:

Code: Select all

Distance Sensor 1: 10.8388 cm
Distance Sensor 2: 173.7638 cm
Distance Sensor 1: 9.5011 cm
Distance Sensor 2: 173.8324 cm

Code: Select all

from machine import Pin
import time

led = Pin('LED',Pin.OUT)
trig_1 = Pin(3, Pin.OUT)
echo_1 = Pin(6, Pin.IN)
trig_2 = Pin(9, Pin.OUT)
echo_2 = Pin(8, Pin.IN)

def measure_distance(trig_pin, echo_pin):
  # Trigger pulse
  trig_pin.value(1)
  time.sleep_us(10)
  trig_pin.value(0)
  # Measure pulse duration
  pulse_start = time.ticks_us()
  while echo_pin.value() == 0:
    pulse_start = time.ticks_us()
    pulse_end = time.ticks_us()
  while echo_pin.value() == 1:
    pulse_end = time.ticks_us()
    pulse_duration = time.ticks_diff(pulse_end, pulse_start)
  # Calculate distance in centimeters
  distance = (pulse_duration * 0.0343) / 2
  return distance

# Main program
try:
  # Main loop
  while True:
    distance_1 = measure_distance(trig_1, echo_1)
    distance_2 = measure_distance(trig_2, echo_2)
    if distance_1 < 10 and distance_2 > 10:
      led(1)
      print("Distance Sensor 1:", distance_1, "cm")
      print("Distance Sensor 2:", distance_2, "cm")
    else:
      led(0)
      print("Distance Sensor 1:", distance_1, "cm")
      print("Distance Sensor 2:", distance_2, "cm")
    time.sleep(1) # Pause for 1 second

except KeyboardInterrupt:
    print ("stop now")

Return to “General”