0

CPB & HCSR04 Distance Sensor
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

CPB & HCSR04 Distance Sensor

by gallaugher on Thu Jun 10, 2021 6:03 pm

Adafriends,
I'm trying to get a basic HCSR04 distance sensor working with CircuitPython and a CircuitPlayground Bluefruit. My code runs, but keeps printing out Retrying! so I don't think I'm getting a proper reading from the HCSR04. I've adapted code and (hopefully correctly) the wiring without the theremin-part from Sophie Wong's Pumpkin Theramin learn guide (https://learn.adafruit.com/jack-o-there ... he-circuit), so my code is simple read & print (shown below).

Following this is a schematic then photos of my wiring - which I'm guessing is the problem since the code runs. I did try this sensor using an Arduino sketch and different wiring (wired into 5v power so didn't need the double resistors), and the sensor worked. I've checked the alignment & pushed the pins in holes so I think that's OK.

Any ideas what I might be doing wrong? Thanks!
John

Code: Select all | TOGGLE FULL SIZE
import board
import time
from adafruit_circuitplayground import cp
import adafruit_hcsr04
sonar = adafruit_hcsr04.HCSR04(trigger_pin=board.A2, echo_pin=board.A1)

while True:
    try:
        handDistance = int(sonar.distance)
        print("Distance:", handDistance)
    except RuntimeError:
        print("Retrying!")
    time.sleep(0.1)


Fritzing Ultrasonic Sensor.png
Fritzing Ultrasonic Sensor.png (236.84 KiB) Viewed 263 times

cpb plus breadboard.png
cpb plus breadboard.png (936.97 KiB) Viewed 263 times

back-of-breadboard.png
back-of-breadboard.png (920.09 KiB) Viewed 263 times

gallaugher
 
Posts: 217
Joined: Tue Aug 14, 2018 12:42 pm

Re: CPB & HCSR04 Distance Sensor

by gallaugher on Thu Jun 10, 2021 6:55 pm

Doh! Made a silly error. This wiring works.

correct-fritzing.png
correct-fritzing.png (240.59 KiB) Viewed 258 times

gallaugher
 
Posts: 217
Joined: Tue Aug 14, 2018 12:42 pm

Please be positive and constructive with your questions and comments.