For this week’s project, I focused on embedded programming to detect whether a door is open or closed using a sensor. I’m relatively new to microcontrollers, so this project has been an exciting learning experience. I began by researching suitable microcontrollers and sensors for the task.
My initial idea was to use a **magnetic reed switch sensor**, which detects the presence of a magnetic field. When the door is closed, the magnet keeps the switch in one state, and when the door opens, the switch changes state as the magnetic field is removed.
For the microcontroller, I opted to use the **Xiao RP2040** because of its small size, powerful dual-core processor, and flexibility in handling various types of sensors and peripherals. I plan to use the GPIO pins on the Xiao to read the sensor input and process it to determine the door’s state. Additionally, I’m learning how to debounce the input to prevent false signals.
This week, I experimented with three different ways to use the button and LED connected to the Xiao RP2040:
Here are some videos demonstrating each experiment:
Below is the code I used to implement the first experiment, where the LED stays on while the button is held:
#define BUTTON_PIN 14 // Define button pin
#define LED_PIN 13 // Define LED pin
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLDOWN); // Set button pin as input with pull-down resistor
pinMode(LED_PIN, OUTPUT); // Set LED pin as output
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
int buttonState = digitalRead(BUTTON_PIN); // Read the state of the button
if (buttonState == HIGH) {
digitalWrite(LED_PIN, HIGH); // Turn on LED if button is pressed
Serial.println("LED is ON"); // Print message to Serial Monitor
} else {
digitalWrite(LED_PIN, LOW); // Turn off LED if button is not pressed
}
delay(300); // Add a small delay to debounce the button
}
Each of these experiments helped me understand how to handle different types of input from the button and control the LED accordingly.
This week, in addition to learning more about embedded programming, I also gained hands-on experience with soldering. I soldered the components onto a small perfboard to create a more permanent version of the circuit. This was my first time soldering, and I learned how important it is to control the temperature and ensure clean connections to avoid cold joints.
I now feel much more confident in both programming the microcontroller and working with hardware. Moving forward, I hope to integrate the magnetic reed switch sensor into this project to detect door open/close states and expand the functionality to include remote notifications using Wi-Fi.