For this week's embedded programming assignment, our lovely architecture group struggled a bit. Luckily we all gathered as a group after recitation to brain storm the group assgnment prompt and got help from Neil's not TA and fellow classmate Kat.
For us to approach the work, created a large group document with our final project interests, a microprocessor chip we were interested in, and their capacities.
My final project (at the moment) involves motion sensored light depending on the distance, and after looking at the three options we decided in class I landed on the RP2040 and Raspberry Pi Pico.
Here is a link to the shared Google Doc we all worked on, but for ease of access I've copied over my notes below.
Use Case / Application 9:
Dimmable motion sensor LED with tracking abilities
Chip: Raspberry Pi RP2040
Capabilities
Programming Workflow
Performance Considerations
Note: I do want to add a moving component to the final project, but because embedded programming is very new to me, I started focusing on one aspect of the project.
Now on the Wowkzi simulator, I wanted to put my notes to use. I decided to use Python intead of C++ because I have very very limited knowledge of Python and thought it would be best to grow my skills further with it. I first read through the datasheet of the microcontroller, Rasberry Pi Pico to get myself familiarized with the board. I also read through the sensor datasheet I was thinking of using, Hc-SR04. With the help of openAI and my partner, we cobbled together code to have the LED react to the distance.
Code for reference below:
from machine import Pin, PWM, time_pulse_us
import time
# Set global variables
max_PWM = 65535
max_distance = 200
speed_of_sound = 29.1 # Speed of sound is ~343 m/s or 29.1 µs per cm
# LED on GPIO 15
led = PWM(Pin(15))
led.freq(1000)
# Ultrasonic sensor setup (HC-SR04)
trig = Pin(28, Pin.OUT)
echo = Pin(27, Pin.IN)
def get_distance():
# Send a 10µs pulse to TRIG pin
trig.low()
time.sleep_us(2)
trig.high()
time.sleep_us(10)
trig.low()
# Measure the time for the echo to return
duration = time_pulse_us(echo, 1, 1000000) # Wait for the echo pin to go high (max 1 second)
# Convert duration to distance (in cm)
distance = (duration / 2) / speed_of_sound
return distance
while True:
distance = get_distance()
brightness = int((max_distance-distance) / max_distance * max_PWM)
brightness = min(max_PWM, max(0, brightness)) # Ensure it stays within bounds
led.duty_u16(brightness)
time.sleep(0.1)
click here to download the simulation file to test for yourself!