10

Output Devices

Week 10 Assignment

Assignment Overview

Outputs: MG996R + two LEDs on XIAO ESP32-C3.

AI: Gemini helped with debug and site text.

Two LEDs used for output indication
Two LEDs
MG996R servo motor
MG996R servo
Seeed XIAO ESP32-C3 development board
Seeed XIAO ESP32-C3

📋 Process

Breadboard first. Tested LEDs and servo separately, then wired the rest.

LED test (Gemini)

Red/green on D0/D1, XIAO ESP32-C3. Blink test via Gemini.

Prompt

I need to test whether a red and a green LED blink correctly on D0 and D1. The board is a Seeed XIAO ESP32-C3.

Sketch

// Pin map
const int ledRed = 0;   // D0 -> GPIO0
const int ledGreen = 1; // D1 -> GPIO1

void setup() {
  // Outputs
  pinMode(ledRed, OUTPUT);
  pinMode(ledGreen, OUTPUT);
  
  Serial.begin(115200);
  Serial.println("C3 LED test: D0 (red), D1 (green)");
}

void loop() {
  // Red on, green off
  digitalWrite(ledRed, HIGH);
  digitalWrite(ledGreen, LOW);
  Serial.println("Red on");
  delay(500); // 500 ms

  // Red off, green on
  digitalWrite(ledRed, LOW);
  digitalWrite(ledGreen, HIGH);
  Serial.println("Green on");
  delay(500);
}

Servo test (Gemini)

MG996R on XIAO ESP32-C3. Standalone test first; ToF→angle later.

Prompt

Write a sketch for a Seeed XIAO ESP32-C3 to drive an MG996R servo. This is only for testing; later I will map ToF distance readings to a rotation angle. For now, give me a standalone sketch that only exercises the servo.

Sketch

#include <ESP32Servo.h>

// Servo instance
Servo myServo;

// XIAO ESP32-C3 pin
const int servoPin = 1; // D0 on the XIAO

void setup() {
  Serial.begin(115200);
  
  // Hardware timer
  ESP32PWM::allocateTimer(0);
  myServo.setPeriodHertz(50);           // MG996R: 50 Hz
  myServo.attach(servoPin, 500, 2400);  // pulse: 500 us (0 deg) .. 2400 us (180 deg)

  Serial.println("--- Servo test ready ---");
  Serial.println("Enter angle 0-180 in Serial Monitor, then press Enter:");
}

void loop() {
  // Serial input
  if (Serial.available() > 0) {
    // Read integer
    int targetAngle = Serial.parseInt();

    // Reject invalid values (parseInt may return 0 if no digits)
    if (targetAngle >= 0 && targetAngle <= 180) {
      Serial.print("Moving to: ");
      Serial.print(targetAngle);
      Serial.println(" deg");

      myServo.write(targetAngle);
    }
    
    // Flush leftover bytes (e.g. newline)
    while(Serial.available() > 0) Serial.read();
  }
}

Test video

Servo test

Integration

After LED + servo worked on the breadboard, combined for the final setup.

Final build

The final version uses the VL53L0X ToF from the Week 9 (Input) bring-up. Camera integration is still under test.

Behaviour: The LEDs show whether the sensor chain is wired and responding as expected. ToF range maps to servo angle: the larger the measured distance, the larger the angle sent to the MG996R (within 0–180°).

Final demo

✅ Results

Done

XIAO ESP32-C3 + MG996R + two LEDs; photos above.

Deliverables

Photos, prompts, servo test clip, final demo video.