Week 3

Embedded Programming

ESP32C3 Security Door

I have programmed microcontrollers many times before. I thought it would be funny if AI wrote the code, and for “AI safety” we do “AI security” - by letting the AI write code for my home security system.

I first hooked up the ESP32C3 XIAO board via USB C to my laptop (XPS 15). Then I flashed a blinky script. No serial output was found. After poking around the internet I found out that “CDC” must be enabled. Turns out “CDC” is for USB comms, and required for serial output. Once I enabled that in Arduino studio, I got hello world serial output.

Then I hooked up a magnet sensor to the door of my office. I first asked GPT4 how this sensor works:

I am hooking up a magnet door close detector... how do they work? It just has 2 wires coming off of it
                    ChatGPT
                    Brief Answer:
                    Magnet door close detectors work on the principle of a reed switch. When a magnet comes close, the switch closes, completing the circuit. The two wires are for connecting it to a monitoring system.

                    Bonus Section:
                    How It Works:
                    The reed switch inside the detector is a small glass tube with two metal contacts. When a magnet is near, the contacts touch, closing the circuit. When the magnet moves away, the circuit opens. This change in circuit state is monitored by a connected system, like a home security setup, to detect if a door is open or closed.

                    Wiring:
                    The two wires usually serve as follows:

                    Common (COM): Connects to the common ground or voltage in your system.
                    Normally Open (NO) or Normally Closed (NC): Connects to the input pin of your monitoring system.
                    What You Might Ask Next:
                    How to wire it to an Arduino?: Connect one wire to a digital input pin and the other to the ground (GND). Use digitalRead() to monitor the pin state.
                    Is polarity important?: Generally, no. Reed switches are not polarized.
                    Can it work with a Raspberry Pi?: Yes, similar to Arduino, but you'll need a pull-up or pull-down resistor.
                    Feel free to experiment, given your background in hacking and inventing!

Now that I knew how it worked, I wanted to write code for it:

Brief Answer:
                    Here's a simple Arduino sketch to monitor a magnetic door sensor:

                    arduino
                    Copy code
                    const int doorPin = 2;  // Pin connected to the door sensor
                    int doorState = 0;      // Variable to hold the door state

                    void setup() {
                      pinMode(doorPin, INPUT_PULLUP);  // Initialize the doorPin as an INPUT with pull-up resistor
                      Serial.begin(9600);              // Initialize serial communication
                    }

                    void loop() {
                      doorState = digitalRead(doorPin);  // Read the state of the door sensor

                      if (doorState == LOW) {
                        Serial.println("Door Open");
                      } else {
                        Serial.println("Door Closed");
                      }

                      delay(1000);  // Wait for a second
                    }
                    Bonus Section:
                    Explanation:
                    INPUT_PULLUP is used to enable the internal pull-up resistor, so you don't need an external one.
                    doorState holds the current state of the door. LOW means the door is open, and HIGH means it's closed.
                    The Serial.println() statements output the door state to the Serial Monitor for debugging.
                    What You Might Ask Next:
                    Can I add an LED indicator?: Absolutely, just define another pin for the LED and use digitalWrite() to turn it on/off based on the door state.
                    How about debouncing?: You can implement a simple debounce by adding a small delay or by checking the door state multiple times before confirming a change.
                    Given your knack for building the future, you could even integrate this into a more complex home automation system!

Ok cool… upload… and it doesn’t work… it always says closed?

I’m used to boards not actually having the right markings of the GPIO, so I googled the pinout of the ESP32C3 XIAO board and sure enought that’s the case, so I changed the pin to align in the code:

Then I added an LED, and ended up with this script:

const int doorPin = 5;  // Pin connected to the door sensor
                    const int ledPin = 2;  // Pin connected to the LED indicator
                    int doorState = 0;      // Variable to hold the door state

                    void setup() {
                      pinMode(doorPin, INPUT_PULLUP);  // Initialize the doorPin as an INPUT with pull-up resistor
                      pinMode(ledPin, OUTPUT);  // Initialize the ledPin as an OUTPUT
                      digitalWrite(ledPin, HIGH);
                      Serial.begin(9600);              // Initialize serial communication
                    }

                    void loop() {
                      doorState = digitalRead(doorPin);  // Read the state of the door sensor

                      if (doorState == LOW) {
                        Serial.println("Door Closed");
                        digitalWrite(ledPin, LOW);
                      } else {
                        Serial.println("Door Open");
                        digitalWrite(ledPin, HIGH);
                      }

                      delay(100);  // Wait for a second
                    }

And now my I have a door open/close detector:

Next Steps

A sick next step would be to setup a backend that emails me whenever a get request hits a certain endpoint - then have the ESP32 connect to wifi and hit the backend everytime the door opens, so I can monnitor when people enter my office.

Group - Different Types of Controllers

Compare the performance and dev workflows for other architectures

Microprocessor vs Microcontroller

FPGA

FPGAs actually run logic in hardware - they don’t really run a program. Designers will write a hardware description language, and that is compiled into hardware, which is loaded into the FPGA. This can allow for logic to run very fast and at very low power, but is limiting in terms of complexity and scalability.

Different MCU Families

Main differences to consider