122 views|0 replies

183

Posts

12

Resources
The OP
 

【Follow me Season 2 Episode 1】Basic Task 3: Proximity Detection [Copy link]

 This post was last edited by nemon on 2024-8-31 19:07

The requirements of basic task three are: set a safe distance and display it through the onboard LED, and initiate a sound alarm when an intrusion is detected.

At the beginning, I looked at the video training tips, which said that it can be achieved with the infrared sensor library. But when I looked at the IR library, I found that the function is encoding and decoding. I also saw some netizens use the method of infrared emission and then reading, but the effect was not good. So I used ultrasonic wave instead.

The method of using the ultrasonic module is to read the time taken for the return wave, then multiply it by the speed of sound to calculate the distance. This method is also affected by the air temperature and density, but it is much more accurate than infrared.

When in use, just use VOUT to supply 5V power and occupy 2 IOs:

import board,digitalio
import time
from adafruit_circuitplayground import cp 

echo_pin = digitalio.DigitalInOut(board.A1)
trigger_pin = digitalio.DigitalInOut(board.A2)
trigger_pin.switch_to_output()
 
def get_distance():
    trigger_pin.value = True
    time.sleep(0.00001)
    trigger_pin.value = False
    while echo_pin.value == False:
        pass
    start_time = time.monotonic()
    while echo_pin.value == True:
        pass
    end_time = time.monotonic()
    elapsed_time = end_time - start_time
    distance = elapsed_time * 34300
    return distance
 
while True:
    distance_cm = get_distance()
    m = distance_cm / 100
    inches = distance_cm / 254
    print(f"Distance: {m}m | {distance_cm} cm")
    cp.pixels.fill( (0,0,0) )
    if distance_cm <20:
        cp.pixels.fill( (255,0,0) )
        cp.play_file('distance.wav')
    time.sleep(0.5)

When the distance is less than 20cm, the red light will turn on and the message "Keep your distance" will be played.

The play_file method of the standard library is used for playback. According to the introduction on the official website ( https://learn.adafruit.com/circuitpython-made-easy-on-circuit-playground-express/play-file ), the sound file supports PCM 16-bit mono WAV format with a sampling rate of 22KHz. So I used goldwave5 to convert it. By the way, the conversion tool introduced on the official website is not as easy to use as goldwave5.

The final effect is this:


This post is from DigiKey Technology Zone
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list