hero-image
HOME
hero-image
project-highlight-image

Backup light and sound module

hero-image
Sophia Myint

Project Timeline

Sep 2025 - Oct-2025

OVERVIEW

Created a module which turns on a light and continuous beeping sound while the module is moved backwards.

HighlightS

Successfully completed the backup lights and sound modules

SKILLS

Python ProgrammingRasberry Pi PicoElectrical WiringSolderingDesign and ImplementationCircuitry

Additional Details

Description:

Utilized a breadboard, Rasberry Pi Pico, amplifier, accelerometer, LEDs, and a speaker in the project. In addition, coded logic in python into the Rasberry Pi to sense movement and actuate the sound and lights. The project is made to be integrated into my electric skateboard.


Code for the project:

import time
import math
import array
import board
import busio
import digitalio
import adafruit_mpu6050
import audiopwmio
import audiocore



i2c = busio.I2C(board.GP11, board.GP10, frequency=100000)

#reading my finicky accelorometer
def make_mpu():
    time.sleep(0.3)  
    for addr in (0x68, 0x69):
        try:
            mpu_local = adafruit_mpu6050.MPU6050(i2c, address=addr)
            print("MPU-6050 detected at I2C address 0x{:02X}".format(addr))
            time.sleep(0.1)
            mpu_local.accelerometer_range = adafruit_mpu6050.Range.RANGE_2_G
            return mpu_local
        except Exception:
            pass
    raise RuntimeError("MPU-6050 not found. Check wiring and use 3.3V power.")

mpu = make_mpu()

def read_accel_with_retry(max_tries=3):
    """Safely read acceleration with retry and auto reinit if sensor times out."""
    global mpu
    last_err = None
    for _ in range(max_tries):
        try:
            return mpu.acceleration
        except OSError as e:
            last_err = e
            time.sleep(0.02)
    print("I2C error ({}). Reinitializing MPU…".format(last_err))
    mpu = make_mpu()
    return mpu.acceleration


greenled = digitalio.DigitalInOut(board.GP14)
greenled.direction = digitalio.Direction.OUTPUT


audio = audiopwmio.PWMAudioOut(board.GP0)  

SR = 22050        
TONE_FREQ = 800    
length = SR // TONE_FREQ
buf = array.array("h", [0] * length)
for i in range(length):
    buf[i] = int(0.45 * 32767 * math.sin(2 * math.pi * i / length))
tone = audiocore.RawSample(buf, sample_rate=SR)

#configure accelorometer
AXIS = 0              
BACKWARD_THRESH = 1.5  

#calibrate accelorometer
print("Calibrating... keep the board still.")
N = 100
baseline = 0.0
for _ in range(N):
    ax, ay, az = read_accel_with_retry()
    baseline += (ax, ay, az)[AXIS]
    time.sleep(0.01)
baseline /= N
print("Baseline on axis", AXIS, "=", baseline, "m/s^2")

#accelorometer read loop
was_backward = False

while True:
    ax, ay, az = read_accel_with_retry()
    a = (ax, ay, az)[AXIS]
    delta = a - baseline

    going_backward = delta > BACKWARD_THRESH
    greenled.value = going_backward

    #beeping
    if going_backward:
        if not audio.playing:
            audio.play(tone, loop=True)
    else:
        audio.stop()

    was_backward = going_backward

    print(
        "ax={:5.2f} ay={:5.2f} az={:5.2f} | dA={:5.2f} | back={}".format(
            ax, ay, az, delta, going_backward
        )
    )
    time.sleep(0.02)
lowinertia
Portfolio Builder for Engineers
Created by Aram Lee
© 2025 Low Inertia. All rights reserved.