Text Editor
Adafruit recommends using the Mu editor for editing your CircuitPython code. You can get more info in this guide.
Alternatively, you can use any text editor that saves simple text files.
Download the Project Bundle
Your project will use a specific set of CircuitPython libraries and the code.py file. To get everything you need, click on the Download Project Bundle link below, and uncompress the .zip file.
Drag the contents of the uncompressed bundle directory onto your board's CIRCUITPY drive, replacing any existing files or directories with the same names, and adding any new ones that are necessary.
# SPDX-FileCopyrightText: 2024 John Park for Adafruit Industries
#
# SPDX-License-Identifier: MIT
'''
Air Blaster
Feather RP2040 Prop-Maker with Power Relay FeatherWing and VL53L1X distance sensor
'''
import time
import board
import digitalio
import adafruit_vl53l1x
TRIGGER_DISTANCE = 50.0
triggered = False
i2c = board.STEMMA_I2C()
vl53 = adafruit_vl53l1x.VL53L1X(i2c)
vl53.distance_mode = 2
vl53.timing_budget = 100
print("VL53L1X Simple Test.")
print("--------------------")
model_id, module_type, mask_rev = vl53.model_info
print("Model ID: 0x{:0X}".format(model_id))
print("Module Type: 0x{:0X}".format(module_type))
print("Mask Revision: 0x{:0X}".format(mask_rev))
print("Distance Mode: ", end="")
if vl53.distance_mode == 1:
print("SHORT")
elif vl53.distance_mode == 2:
print("LONG")
else:
print("UNKNOWN")
print("Timing Budget: {}".format(vl53.timing_budget))
print("--------------------")
vl53.start_ranging()
relay_pin = digitalio.DigitalInOut(board.D10)
relay_pin.direction = digitalio.Direction.OUTPUT
relay_pin.value = False
def blast(repeat, duration, rate):
for _ in range(repeat):
relay_pin.value = True
print("bang")
time.sleep(duration)
relay_pin.value = False
time.sleep(rate)
distance = None
while True:
if vl53.data_ready:
distance = vl53.distance
print("Distance: {} cm".format(vl53.distance))
vl53.clear_interrupt()
time.sleep(0.1)
if distance:
if distance <= TRIGGER_DISTANCE:
if not triggered :
blast(3, 0.01, 0.1) # adjust repeat, duration, rate here
time.sleep(0.4)
blast(2, 0.01, 0.2) # adjust repeat, duration, rate here
triggered = True
else:
triggered = False
How It Works
The distance sensor continuously measures how far objects are from it. When something moves within 50 cm of the sensor, it triggers the air blaster to fire a few quick blasts. You can control the number of blasts, how long they last, and the rate at which they happen in the code.
Libraries
We start by importing the necessary libraries. board gives us access to the Feather’s pins, digitalio helps us control the relay pin, and adafruit_vl53l1x is the library for the distance sensor.
import time import board import digitalio import adafruit_vl53l1x
Variables
-
TRIGGER_DISTANCEis set to50cm, meaning that when something is closer than this distance, it will activate the air blaster. - The
triggeredvariable keeps track of whether the air blaster has already been activated, preventing it from firing repeatedly when the object stays close.
TRIGGER_DISTANCE = 50.0 triggered = False
Setup
- We initialize the I2C bus for communication with the VL53L1X sensor and set it to long-distance mode (
distance_mode = 2), perfect for larger detection ranges. - The timing budget is set to 100 ms, which controls how quickly the sensor takes measurements. Lower values mean faster readings but slightly reduced accuracy.
- We set up the relay pin (connected to pin D10) that will control the air blaster. By default, the relay is off (
False).
i2c = board.STEMMA_I2C() vl53 = adafruit_vl53l1x.VL53L1X(i2c) vl53.distance_mode = 2 vl53.timing_budget = 100 relay_pin = digitalio.DigitalInOut(board.D10) relay_pin.direction = digitalio.Direction.OUTPUT relay_pin.value = False
blast Function
This function controls how many times the air blaster will fire, how long each blast lasts, and the delay between blasts. You can adjust repeat, duration, and rate to create different blast patterns.
def blast(repeat, duration, rate):
for _ in range(repeat):
relay_pin.value = True # Turn on the relay (blasts air)
print("bang")
time.sleep(duration) # Wait for the duration of the blast
relay_pin.value = False # Turn off the relay
time.sleep(rate) # Wait for the interval before the next blast
Main Loop
In the main loop, the sensor checks if it’s ready to send a distance reading. If it is, it prints the distance in centimeters and then clears the sensor’s interrupt (which tells it to get ready for the next reading).
while True:
if vl53.data_ready:
distance = vl53.distance
print("Distance: {} cm".format(vl53.distance))
vl53.clear_interrupt()
time.sleep(0.1)
Triggering the Air Blaster
- When the distance is less than or equal to
50cm, and the air blaster hasn’t already been triggered (triggered == False), theblastfunction fires the air cannon. - In this example, it blasts three quick shots, pauses for
0.4seconds, and then blasts two more. - The
triggeredvariable is set toTrueto ensure that the blaster doesn’t keep firing while the object remains close. - If the object moves farther than
50cm, the system resets for another trigger.
if distance <= TRIGGER_DISTANCE:
if not triggered:
blast(3, 0.01, 0.1)
time.sleep(0.4)
blast(2, 0.01, 0.2)
triggered = True
else:
triggered = False
Page last edited January 22, 2025
Text editor powered by tinymce.