Home
Taekwondo AI Coach Panbox MK I Panbox MK II Ultrasonic Badge ML Medical Data Analysis Halloween projects D&D Campaign
$ cat ultrasonic_badge.md
Click here to return to the main page View the code and PCB files on Github This project is designed to be a small badge that can be placed on furniture or the end of a white cane to aid the visually impaired It uses an ultrasonic sensor wired to a Raspberry Pi Pico to measure distances. When the measured distance is less than a certain threshold, 10 cm, it sounds a buzzer and an LED. First I designed the schematic of the PCB. It included the microcontroller, the ultrasonic sensor, the buzzer, and the LED. Schematic of PCB Model of PCB before ordering I modeled an enclosure for the badge that could be 3D printed, so that the entire device could be attached to a surface 3D model of enclosure and PCB After a few days, I received the PCBs and the enclosures. PCBs upon arrival 3D printed in resin, upon arrival 3D printed in resin, upon arrival After soldering and assembly. All parts joined together, soldered and glued All parts joined together, soldered and glued The following is the full Circuitpython program running on the Raspberry Pi Pico.
        
import machine
import utime
from machine import Pin, PWM

# Define the pins
trig_pin = machine.Pin(3, machine.Pin.OUT)
echo_pin = machine.Pin(2, machine.Pin.IN)
buzzer = PWM(Pin(4))
led = Pin(5, Pin.OUT)

def get_distance():
    # Trigger pulse
    trig_pin.low()
    utime.sleep_us(2)
    trig_pin.high()
    utime.sleep_us(5)
    trig_pin.low()

    # Measure the pulse duration on the Echo pin
    pulse_duration = machine.time_pulse_us(echo_pin, 1, 60000)

    # Calculate distance in centimeters
    distance_cm = pulse_duration / 58.0

    return distance_cm

# Main loop
while True:
    distance = get_distance()
    print("Distance: {:.2f} cm".format(distance)) # Print to console
    utime.sleep_ms(100)
    
    if distance < 10:
        buzzer.freq(500) 
        buzzer.duty_u16(1000)
        led.toggle()
        utime.sleep_ms(50)  # Sound the buzzer and LED for 1 second
        buzzer.duty_u16(0)  
        led.toggle()		# Stop the buzzer and LED
        
    
First we must import the modules needed, the machine library and utime. From machine we must also import Pin for pin communication and PWM since the buzzers use Pulse Width Modulation to make a tone.
        
import machine
import utime
from machine import Pin, PWM
        
    
Then we must define the pins that the different components will be connected to. Trig and Echo from the ultrasonic sensor will be connected to GPIO pins 3 (output) and 2 (input) respectively. The buzzer will be on GPIO 4, capable of outputting PWM, and the LED will be on GPIO 5.
        
# Define the pins
trig_pin = machine.Pin(3, machine.Pin.OUT)
echo_pin = machine.Pin(2, machine.Pin.IN)
buzzer = PWM(Pin(4))
led = Pin(5, Pin.OUT)
        
    
For abstraction, we must define a function to calculate the distance from the sensor. This will make running the buzzer and LED much simpler.
        
def get_distance():
    # Trigger pulse
    trig_pin.low()
    utime.sleep_us(2)
    trig_pin.high()
    utime.sleep_us(5)
    trig_pin.low()

    # Measure the pulse duration on the Echo pin
    pulse_duration = machine.time_pulse_us(echo_pin, 1, 60000)

    # Calculate distance in centimeters
    distance_cm = pulse_duration / 58.0

    return distance_cm
        
    
The function takes no parameters an returns the distance in centimeters. It begins by sending a pulse from the trigger pin. Its voltage is set to low then after 2 seconds it is set to high. After 5 seconds, it returns to low. The pulse duration variable finds the time it takes for the echo pin to pick up a response from the soundwave emitted by the trigger pin to be reflected off of a surface. The distance in centimeters can then be found by dividing this value by 58. This value is then returned
        
# Main loop
while True:
    distance = get_distance()
    print("Distance: {:.2f} cm".format(distance)) # Print to console
    utime.sleep_ms(100)
    
    if distance < 10:
        buzzer.freq(500) 
        buzzer.duty_u16(1000)
        led.toggle()
        utime.sleep_ms(50)  # Sound the buzzer and LED for 1 second
        buzzer.duty_u16(0)  
        led.toggle()		# Stop the buzzer and LED
        
    
The main loop of the program utilizes the get_distance() function we defined earlier. Infinitely, the loop first stores the distance to a variable 'distance' and prints this distance to the console. It measures this distance every 100 milliseconds. Every iteration, the loop tests this distance variable and checks if it is less than 10 centimeters. If so, it sounds the buzzer and LED by setting the buzzer frequency to 500 Hz and toggling the LED for 1000 milliseconds (1 second), then powering them off. It will continue to do this until the distance is no longer less than 10 centimeters.
$ sudo rm -rf /