Week 2: Embedded Programming

Project Overview

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.

Research & Initial Ideas

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.

Xiao RP2040 Microcontroller

Experimenting with the Button and LED

This week, I experimented with three different ways to use the button and LED connected to the Xiao RP2040:

  1. Holding the Button: When I held the button down, the LED stayed on as long as the button was pressed. This was the simplest form of interaction and helped me confirm that the button and LED were wired and functioning correctly.
  2. Clicking the Button (with Debounce): I then implemented a debounce technique to ensure that when I clicked the button once, the LED stayed on without any unintended flickering. This involved adding a small delay to account for mechanical noise in the button.
  3. Clicking the Button to Blink the LED: Finally, I wrote code that made the LED start blinking after I clicked the button. This involved toggling the LED’s state and using the `delay()` function to make it blink at a controlled rate.

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.

Code Screenshot 1 Code Screenshot 2

Reflection

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.