Skip to content

9. Input Devices

This week I worked on sensors for my final project.

Weekly Objectives

  • Group assignment:
  • probe an input device’s analog levels an digital signals
  • Individual assignment:
  • measure something: add a sensor to a microcontroller board that you have designed and read it

Group Assignment

In this week group assignment, I . You can find our full group documentation here.

PIR Sensor

Since I am making an interactive map, I need a sensor to sensor the touch when I put my hands on the map. SO I decided to make good use of this week and explore the sensor I am going to use for my final project.

Wokwi Simulation

The first sensor I though of was the PIR sensor. I first looked over Adrian Torres’s documentation and confirmed that XIAO RP2040 is able to program the PIR sensor. Then I found a Wokwi that has the PIR sensor connecting with Raspberry Pi Pico.

Then I added a switch to the simulation to turn on and turn off the sensor. I put the original code into ChatGPT and it modified the code so that it is able to use the switch to control the sensor. I also asked for the wiring with XIAO RP2040 and got the right wiring.

After I got the switch + PIR Sensor code, I put it into Wokwi and simulated it. The code worked perfectly fine so I started to build the real circuit.

Wiring

I looked at the XIAO RP2040 Pinout and connected PIR sensor with RP2040.

Then I programmed the code I used in Wokwi through Aruino IDE and I successfully made the sensor work.

Then I added a switch and use the modified code to turn on and off the sensor.

In order to see the touch more clearly, I added a LED onto the bread board. When I touch the sensor, the sensor would send signal to the microcontroller and make the LED light up.

This is the complete circuit.

This is the video of the full circuit working.

PIR Sensor Final Code

void setup() {
  pinMode(27, INPUT);        // PIR Sensor
  pinMode(29, INPUT_PULLUP); // Toggle Switch with pull-up resistor
  pinMode(7, OUTPUT);       // LED


  Serial.begin(115200);
  Serial.println("Hello, Raspberry Pi Pico!");
}


void loop() {
  bool switchState = digitalRead(29); // Read toggle switch state


  if (switchState == LOW) { // Switch ON (connected to GND)
    int pir = digitalRead(27);
    if (pir == HIGH) {
      Serial.println("MOVIMIENTO DETECTADO");
      digitalWrite(7, HIGH); // Turn LED ON
    } else {
      Serial.println("NO MOVIMIENTO DETECTADO");
      digitalWrite(7, LOW);  // Turn LED OFF
    }
  } else {
    Serial.println("Sensor OFF");
    digitalWrite(7, LOW); // Ensure LED is OFF when sensor is disabled
  }


  delay(500);
}

Problem Encountered

After I built the circuit in real life, I found out a problem about the PIR sensor: It was very insensitive. Elle Hahn told me that there are two switch that control the sensitivity and the sensor time. I watched this tutorial and tried to adjust the the sensitivity of the PIR sensor, but it did not work very well. So I decided to change to another sensor: the touch capacitive sensor.

Touch Capacitive Sensor

Design in Kicad

Milling

Final Product

Reflection of the Week

AI Usage

I used ChatGPT to help me generate the code and fixing my wiring. You can found the whole conversation here.

File Download

You can download my file here.


Last update: April 7, 2025