CPB & HCSR04 Distance Sensor

Play with it! Please tell us which board you're using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
gallaugher
 
Posts: 267
Joined: Tue Aug 14, 2018 12:42 pm

CPB & HCSR04 Distance Sensor

Post by gallaugher »

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

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 373 times
cpb plus breadboard.png
cpb plus breadboard.png (936.97 KiB) Viewed 373 times
back-of-breadboard.png
back-of-breadboard.png (920.09 KiB) Viewed 373 times

User avatar
gallaugher
 
Posts: 267
Joined: Tue Aug 14, 2018 12:42 pm

Re: CPB & HCSR04 Distance Sensor

Post by gallaugher »

Doh! Made a silly error. This wiring works.
correct-fritzing.png
correct-fritzing.png (240.59 KiB) Viewed 368 times

Locked
Please be positive and constructive with your questions and comments.

Return to “Circuit Playground Classic, Circuit Playground Express, Circuit Playground Bluefruit”