Week 9: Input Devices¶
The components: • 1x XIAO RP2040 • 1x GY-521 Accelerometer and Gyroscope • 1x Buzzer • 1x 1206 SMD LED • 1x 0Ω Jumper Resistor • 1x 220Ω Resistor
Step 1: designing¶
Yet againg we return to KiCAD.
First you open the Schematic Editor, and add the predetermined list of components. When adding these onto the sheet you need to make sure the symbols also come with footprints. If not, you either go online and download the footprints corresponding to your pieces, or create them yourself in Symbol/Footprint Editor [depending on your needs].
Once you have layed out a desired schematic, you switch to PCB Editor, and wire the board according to the pieces.
Schematic editor¶

In my example, I did not have a symbol or a footprints for my buzzer. So I googled the name of it online and found the files through this website.
If I were unable to find them online also, my steps would be to measure the distamce between the anode and the cathode and sketch a simple circle with two holes in it. This would serve as the soldering pinholes for the buzzer.
PCB editor¶

Step 2: G-code¶
To generate the toolpath for the milling machine go to mods, a web-based CAM.




Step 3: milling¶
I began by attaching an FR-1 PCB blank with double-sided tape onto the machine bed. Then I clamp it down with a for about 5 minutes


As it did not go according to plan, I taped a piece of plywood onto the scroll saw table to ensure a straign cut.
Step 4: soldering¶

After milling the PCB, I fetched the electronics components to begin soldering.


Picking up from where I left last week… things seemed to be worse than I thought they’d be. I quite liked my fisrt-time soldering reults, but soon after found out that some pins were placed incorrectly, to be precise SDL and SCL.
Also, on my GY-521 [gyroscope and accelerometer], I disregarded the ADO pin, which was supposed to be connected to GND for this purpose. Therefore after I had already milled the FR-1, I was advized by my local instructoir Onik, to bridge the ADO pin with a 10kΩ resistor.

The code¶
To make sure I have life in this PCB, I wrote a simple code to make the SMD LED blink, and the buzzer tweet. But then after, I asked Claude.ai to generate code based on the MPU6050 library, which would also take in account the gyroscope.
I then found this website with some documentation on how to wire the gyroscope with the RP2040. The code did not use the library I had, which was MPU5060.
I asked chatGPT to take the minimal code I had, and connect it to the reference code above, yet using the functions found in my downloaded library. Here was the prompt I gave to it:
C++
#include <Wire.h>
#include <MPU6050.h>
#define LED_PIN D0
#define BUZZ_PIN D2
#define THRESHOLD 5000
MPU6050 mpu;
void setup() {
Serial.begin(115200);
while (!Serial);
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZ_PIN, OUTPUT);
// Startup blink/beep
digitalWrite(LED_PIN, HIGH);
digitalWrite(BUZZ_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZ_PIN, LOW);
Wire.begin();
mpu.initialize();
if (!mpu.testConnection()) {
Serial.println("MPU6050 not found! Check wiring.");
// Rapid blink = error
while (1) {
digitalWrite(LED_PIN, HIGH);
delay(100);
digitalWrite(LED_PIN, LOW);
delay(100);
}
}
Serial.println("MPU6050 Found!");
}
void loop() {
int16_t ax, ay, az;
int16_t gx, gy, gz;
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
Serial.print("Accel X: "); Serial.print(ax);
Serial.print(" Y: "); Serial.print(ay);
Serial.print(" Z: "); Serial.println(az);
Serial.print("Gyro X: "); Serial.print(gx);
Serial.print(" Y: "); Serial.print(gy);
Serial.print(" Z: "); Serial.println(gz);
Serial.println("---");
if (abs(gx) > THRESHOLD ||
abs(gy) > THRESHOLD ||
abs(gz) > THRESHOLD) {
Serial.println(">> MOVING!");
digitalWrite(LED_PIN, HIGH);
digitalWrite(BUZZ_PIN, HIGH);
delay(300);
} else {
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZ_PIN, LOW);
}
delay(200);
}
