9. Input Devices¶
This week I worked on sensors for my final project.
Weekly Objectives¶
- Group assignment:
- probe an input device’s analog levels and digital signals
- Individual assignment:
- measure something: add a sensor to a microcontroller board that you have designed and read it
Useful links¶
Group Assignment¶
In this week group assignment, I measured an input device’s analog level using oscilloscope. 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 breadboard. 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¶
The circuit remained completely unchange, because the pinout I need for the touch capacitive sensor is exactly the same. I did use ChatGPT again to modified the code so that the code would fit for the touch capacitive sensor.
Wiring Diagram¶
Capacitive Touch Sensor Connection¶
Touch Sensor Pin | XIAO RP2040 Pin |
---|---|
VCC | 3.3v/5v |
GND | GND |
SIG | GPIO 26 |
Toggle Switch COnnections¶
Toggle Switch Pin | XIAO RP2040 pin |
---|---|
Common (middle pin) | GND |
Normally Open (NO) | GPIO 29 |
Normally Closed (NC) | Not connected |
Notes: the side pins of the toggle switch do not matter either one works.
LED Connections¶
LED Pin | XIAO RP2040 pin |
---|---|
Positive side | GPIO 6 |
Negative Side | GND |
Touch Sensor Code¶
void setup() {
pinMode(26, INPUT); // Touch Sensor
pinMode(28, INPUT_PULLUP); // Toggle Switch with pull-up resistor
pinMode(6, OUTPUT); // LED
Serial.begin(115200);
Serial.println("Hello, Raspberry Pi Pico!");
}
void loop() {
bool switchState = digitalRead(28); // Read toggle switch state
if (switchState == HIGH) { // Switch ON (NC & C setup, HIGH when connected)
int touch = digitalRead(26); // Read touch sensor state
if (touch == HIGH) {
Serial.println("TOUCH DETECTED!");
digitalWrite(6, HIGH); // Turn LED ON
} else {
Serial.println("NO TOUCH DETECTED");
digitalWrite(6, LOW); // Turn LED OFF
}
} else {
Serial.println("Sensor OFF");
digitalWrite(6, LOW); // Ensure LED is OFF when sensor is disabled
}
delay(500);
}
Then I successfully make the LEd light up when I touched the sensor.
Design in Kicad¶
After I complete the cicuit on breadboard, it is time for me to design a board. I designed the board in Kicad.
This is my schematic diagram.
Then I import the schemetic design into the PCB Editor.
Then I realized there are holes that I don’t want on the switch footprint. My fellow student Noah Smith told me to use Footprint Editor to get rid of the holes on the footprint.
I successfully did it and changed the traces of my PCB design. Then I ended up having my final PCB design.
Here is a 3D view of my PCB
Milling¶
Here’s a video of me milling my PCB.
Here’s the PCb board
Final Product¶
After I soldered all of the components onto my first board, nothing happened. Kathryn Wu helped me figure out that my LED somehow did not work asfter I soldered it on. I tested before I solder it. However, I just got bad luck like I always do.
So I soldered another board and soldered every component carefully onto my PCB board. This time, it worked! Here’s a video of my PCB board working!
Reflection of the Week¶
This week is pretty fun (except the group assignment). It was the first week after spring break (and there’s also half of the week I caould not work because of spring break), so I wasnot being very productive. But I successfully explored a sensor that I would use for my final project. I also designed a board for the PCB and I successfully made that board work. I was really proud of myself for learning so many things about electronics.
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.