9. Input Devices
This week, I focused on input devices by reading a pushbutton signal from the microcontroller. I built and simulated the circuit in Circuit Designer, then verified the input-reading logic with Arduino code.
Assignment Goal
The goal of this assignment was to connect and read an input device in a microcontroller project. I used a button as the input source and used servo movement to visualize the reading result.
Circuit Design
I designed a simple test circuit with a pushbutton and a servo motor. The button is connected to pin D2 and the servo signal is connected to pin D5 of the XIAO ESP32S3.
- Microcontroller: XIAO ESP32S3
- Input: Pushbutton on D2
- Output: Servo motor on D5
- Power: 5V and GND from the controller
Programming and Test
I wrote a simple Arduino sketch to read the button state and control the servo position. When the button is pressed, the servo rotates to 10 degrees; otherwise, it returns to 0 degrees.
/*
* This Arduino sketch controls a servo motor using a pushbutton.
* When the button on D2 is pressed, the servo on D5 rotates by 10 degrees.
*/
#include <Servo.h>
Servo myServo;
const int buttonPin = D2;
const int servoPin = D5;
int buttonState = 0;
void setup() {
pinMode(buttonPin, INPUT);
myServo.attach(servoPin);
myServo.write(0);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
myServo.write(10);
delay(500);
} else {
myServo.write(0);
}
}
What I Learned
- How to connect and read a pushbutton as a digital input
- How digital input can trigger physical movement as feedback
- How to verify behavior through simulation before fabrication