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.

Input test circuit in Circuit Designer

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

← Week 8 Back to Assignments Week 10 →