Input Devices
Week 9 Assignment
Assignment Overview
Week 9 — Input Devices. The assignment board is inputoutput (shared with Week 10 Output Devices). It is not the final-project board. Stack: Seeed XIAO ESP32-C3, Sipeed MaixCAM, ultrasonic ranging, 0.96 in SSD1306 OLED (Zave), and three colour LEDs.
- Group: Probe analog levels and digital signals of an input device (minimum: multimeter and oscilloscope). Document on the group page; reflect here.
- Individual: Measure something — add a sensor to a microcontroller board I designed and read it. Breadboards are not allowed for the final interface.
AI assistance: I used AI tools to help structure this page and debug firmware ideas. All hardware choices and measurements are my own work.
1. Group Work
Lab group documentation:
- UNNC FabLab — Group Assignments
- Input Devices on the group hub
- Unit docs: https://fabacademy.org/2026/labs/unnc/docs/input-devices/
What I learned from the group work
Shared lab tests and settings are on the UNNC group page; I use those notes for my individual build on this page.
2. Individual Work
Individual Work — New Input Board
For this assignment I replaced the previous board with a new input board built around the components below. The board must host the MCU and connect the sensors without a breadboard.
0 Board design — schematic and layout
New inputoutput carrier in Fusion Electronics for the Seeed XIAO ESP32-C3 (U$1). Headers break out the sensor / I2C / power nets; three onboard LEDs are for colour feedback (outputs, used with Week 10).
- U$3 (1×4) — 5 V, D0, D1, GND → ultrasonic TRIG / ECHO.
- U$2 (1×4) — 5 V, D4 (SDA), D5 (SCL), GND → I2C (OLED).
- U$4 (1×2) — 5 V, GND → spare power.
- U$5–U$7 — LEDs on D6 / D7 / D8 (outputs).
MaixCAM UART is wired to the board RX path in the firmware bring-up (see pin plan). 3D model: inputoutput.step.
1 Components
- Seeed XIAO ESP32-C3 — microcontroller on the designed board.
- Sipeed MaixCAM — camera / vision input (red / green / blue).
- Ultrasonic ranging sensor — distance measurement (TRIG / ECHO).
- 0.96 in SSD1306 OLED (Zave) — shows distance and colour on I2C.
- Red / green / blue LEDs — one LED lit for the detected colour.
Use for the final project: ultrasonic for proximity; MaixCAM for colour; OLED for local readout; RGB LEDs as colour feedback; ESP32-C3 as the local MCU.
2 Pin plan
For Input Devices, the sensors and camera are: ultrasonic ranging + Sipeed MaixCAM. (OLED and RGB LEDs are outputs — see Week 10.)
Wiring used by firmware v1.3 on the Seeed XIAO ESP32-C3 (full board pin table includes outputs for the shared demo):
| Role | Signal | XIAO ESP32-C3 |
|---|---|---|
| Ultrasonic | TRIG | GPIO2 (D0) |
| Ultrasonic | ECHO (level divider) | GPIO3 (D1) |
| MaixCAM | TX → Serial1 RX @ 115200 | GPIO20 (D7) |
| OLED (SSD1306) — output | I2C SDA / SCL | GPIO6 (D4) / GPIO7 (D5) |
| LEDs — output | Red / Green / Blue | GPIO8 (D8) / GPIO9 (D9) / GPIO10 (D10) |
Camera TX/A16 → D7 RX; TRIG D0; ECHO divider → D1; common GND. OLED / LEDs documented on Week 10.
3 Design and fabrication
This week’s input board is a redesigned milled carrier for the XIAO ESP32-C3. Ultrasonic TRIG/ECHO and MaixCAM UART are wired on the board (ECHO through a level divider). 3D board model: inputoutput.step. Related earlier electronics: Week 6, Week 8.
Reading the sensors
1 ESP32-C3 firmware — ultrasonic, OLED, MaixCAM, RGB LEDs
Firmware v1.3 on the XIAO ESP32-C3: ranges with the ultrasonic sensor, shows distance and colour on a 0.96 in SSD1306 OLED (Zave), reads MaixCAM colour codes on Serial1, and lights the matching red / green / blue LED. Select board Seeed Studio XIAO ESP32-C3.
Download: xiao_esp32c3_input.ino
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#ifndef CONFIG_IDF_TARGET_ESP32C3
#error "Select XIAO ESP32-C3"
#endif
// XIAO ESP32-C3 pin map
constexpr uint8_t TRIG_PIN = 2; // D0
constexpr uint8_t ECHO_PIN = 3; // D1 — use a voltage divider from HC-SR04 Echo
constexpr uint8_t OLED_SDA_PIN = 6; // D4
constexpr uint8_t OLED_SCL_PIN = 7; // D5
constexpr uint8_t MAIXCAM_RX_PIN = 20; // D7, connected to MaixCAM TX
constexpr uint8_t RED_LED_PIN = 8; // D8
constexpr uint8_t GREEN_LED_PIN = 9; // D9
constexpr uint8_t BLUE_LED_PIN = 10; // D10
constexpr uint8_t OLED_WIDTH = 128;
constexpr uint8_t OLED_HEIGHT = 64;
constexpr unsigned long MEASURE_INTERVAL_MS = 180;
constexpr unsigned long ECHO_TIMEOUT_US = 30000;
constexpr float DISTANCE_BAR_MAX_CM = 200.0f;
Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT, &Wire, -1);
bool oledReady = false;
float distanceCm = -1;
char cameraColour = 'n';
unsigned long lastMeasureMs = 0, lastCameraMs = 0, lastLogMs = 0;
bool cameraOnline = false;
String cameraLine;
bool cameraOverflow = false;
uint8_t findOledAddress() {
for (uint8_t address : {0x3C, 0x3D}) {
Wire.beginTransmission(address);
if (Wire.endTransmission() == 0) return address;
}
return 0;
}
float readDistanceCm() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
unsigned long duration = pulseIn(ECHO_PIN, HIGH, ECHO_TIMEOUT_US);
if (duration == 0) return -1;
return duration * 0.0343f / 2.0f;
}
const char *colourName(char colour) {
switch (colour) {
case 'r': return "RED";
case 'g': return "GREEN";
case 'b': return "BLUE";
default: return "NONE";
}
}
void setColourLeds(char colour) {
bool red = false;
bool green = false;
bool blue = false;
if (colour == 'r') {
red = true;
} else if (colour == 'g') {
green = true;
} else if (colour == 'b') {
blue = true;
}
digitalWrite(RED_LED_PIN, red);
digitalWrite(GREEN_LED_PIN, green);
digitalWrite(BLUE_LED_PIN, blue);
}
void drawScreen() {
if (!oledReady) return;
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.print("DISTANCE");
display.setTextSize(2);
display.setCursor(0, 11);
if (distanceCm < 0) {
display.print("NO ECHO");
} else {
display.print(distanceCm, 1);
display.print(" cm");
}
display.setTextSize(1);
display.drawRect(0, 34, 128, 10, SSD1306_WHITE);
if (distanceCm >= 0) {
int width = constrain((int)(distanceCm / DISTANCE_BAR_MAX_CM * 126), 0, 126);
display.fillRect(1, 35, width, 8, SSD1306_WHITE);
}
display.setCursor(0, 46);
display.print("COLOR: ");
display.print(colourName(cameraColour));
display.setCursor(0, 56);
display.print(cameraOnline ? "CAM: ONLINE" : "CAM: NO DATA");
display.display();
}
void readMaixCam() {
for (int i = 0; i < 128 && Serial1.available(); ++i) {
char value = Serial1.read();
if (value == '\n') {
cameraLine.trim();
cameraLine.toLowerCase();
if (!cameraOverflow && cameraLine.length() == 1 &&
String("rgbn").indexOf(cameraLine) >= 0) {
cameraColour = cameraLine[0];
lastCameraMs = millis();
cameraOnline = true;
setColourLeds(cameraColour);
}
cameraLine = "";
cameraOverflow = false;
} else if (value != '\r') {
if (cameraLine.length() < 16) cameraLine += value;
else cameraOverflow = true;
}
}
if (cameraOnline && millis() - lastCameraMs > 3000) {
cameraOnline = false;
cameraColour = 'n';
setColourLeds('n');
}
}
void setup() {
Serial.begin(115200);
Serial1.begin(115200, SERIAL_8N1, MAIXCAM_RX_PIN, -1);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(BLUE_LED_PIN, OUTPUT);
setColourLeds('n');
Wire.begin(OLED_SDA_PIN, OLED_SCL_PIN, 100000);
uint8_t oledAddress = findOledAddress();
oledReady = oledAddress && display.begin(SSD1306_SWITCHCAPVCC, oledAddress);
Serial.println("C3 input/output v1.3 started: Camera TX/A16 -> D7/GPIO20 RX; 115200");
if (oledReady) {
Serial.printf("OLED found at 0x%02X\n", oledAddress);
} else {
Serial.println("OLED not found at 0x3C or 0x3D");
}
}
void loop() {
readMaixCam();
if (millis() - lastMeasureMs >= MEASURE_INTERVAL_MS) {
lastMeasureMs = millis();
distanceCm = readDistanceCm();
drawScreen();
}
if (millis() - lastLogMs >= 1000) {
lastLogMs = millis();
Serial.print(cameraOnline ? "CAM ONLINE | " : "CAM NO DATA: check camera TX -> C3 D7 and GND | ");
Serial.print(oledReady ? "OLED OK | " : "OLED NOT FOUND | ");
Serial.print("Distance: ");
Serial.print(distanceCm);
Serial.print(" cm, MaixCAM: ");
Serial.println(colourName(cameraColour));
}
}
Results: see the OLED + LED demo in section 3.3.
2 MaixCAM RGB detection (Python)
On MaixCAM, a MaixPy script finds the largest red / green / blue blob in LAB colour space (320×240), stabilises the label for a few frames, then sends one character per line over UART0 TX on A16 at 115200 baud to the ESP32-C3 D7 (GPIO20). Codes: r / g / b, or n when lost.
Download: maixcam_rgb_detect.py
"""MaixCAM RGB detection for the XIAO ESP32-C3 Input / Output assignment."""
from maix import app, camera, display, err, image, pinmap, time, uart
FRAME_WIDTH = 320
FRAME_HEIGHT = 240
UART_BAUD = 115200
MIN_PIXELS = 800
MIN_AREA = 1200
STABLE_FRAMES = 3
LOST_FRAMES = 3
# Tune these LAB thresholds in MaixVision if lighting changes.
COLORS = (
("r", (0, 80, 40, 100, 10, 90), image.COLOR_RED),
("g", (0, 85, -128, -10, -20, 90), image.COLOR_GREEN),
("b", (0, 80, 10, 100, -128, -35), image.COLOR_BLUE),
)
def open_uart():
err.check_raise(pinmap.set_pin_function("A16", "UART0_TX"), "Cannot map A16 to UART0_TX")
return uart.UART("/dev/ttyS0", UART_BAUD)
def find_largest_colour(img):
largest = None
largest_letter = "n"
largest_draw_colour = image.COLOR_WHITE
largest_area = 0
for letter, threshold, draw_colour in COLORS:
for blob in img.find_blobs([threshold], pixels_threshold=MIN_PIXELS, area_threshold=MIN_AREA):
area = blob[2] * blob[3]
if area > largest_area:
largest = blob
largest_letter = letter
largest_draw_colour = draw_colour
largest_area = area
return largest_letter, largest, largest_draw_colour
serial = open_uart()
cam = camera.Camera(FRAME_WIDTH, FRAME_HEIGHT)
screen = display.Display()
candidate = "n"
candidate_frames = 0
lost_frames = 0
state = "n"
sent_state = None
last_send_ms = 0
print("RGB detector started: A16 TX -> C3 D7/GPIO20, 115200 baud")
while not app.need_exit():
img = cam.read()
colour, blob, draw_colour = find_largest_colour(img)
if blob:
x, y, width, height = blob[0], blob[1], blob[2], blob[3]
img.draw_rect(x, y, width, height, draw_colour, thickness=3)
img.draw_string(x, max(0, y - 18), colour.upper(), draw_colour)
lost_frames = 0
if colour == candidate:
candidate_frames += 1
else:
candidate = colour
candidate_frames = 1
if candidate_frames >= STABLE_FRAMES:
state = colour
else:
candidate = "n"
candidate_frames = 0
lost_frames += 1
if lost_frames >= LOST_FRAMES:
state = "n"
now = time.ticks_ms()
if state != sent_state or now - last_send_ms >= 200:
serial.write_str(state + "\n")
print(state)
sent_state = state
last_send_ms = now
screen.show(img)
time.sleep_ms(10)
3 Demo — OLED readout and RGB LEDs
Updated demo: colour and distance are shown on the 0.96 in Zave SSD1306 OLED. Detected red, green, or blue lights the matching LED. Ultrasonic distance updates on the screen (and bar graph).
Earlier bring-up clip (colour + distance without OLED): maixcam-color-distance.mp4
4 How the code works
- MaixCAM: LAB blob detection for red / green / blue; UART one-character lines on A16 → C3 D7.
- Ultrasonic: TRIG D0 / ECHO D1; convert echo time to centimetres about every 180 ms.
- OLED: I2C on D4/D5 (address 0x3C or 0x3D); draws distance, bar, colour name, and CAM status.
- RGB LEDs: D8 / D9 / D10 — only the LED for the current colour is on; all off for NONE / timeout.
- Serial: USB log once per second for camera, OLED, distance, and colour.
Hero shot and files
Hero shot
Finished input board: XIAO ESP32-C3 on the milled carrier, HC-SR04 ultrasonic, and MaixCAM linked over UART.
Original design files and source code
- Pictorial wiring: inputoutput-wiring.svg
- Board schematic: inputoutput-schematic.png
- Board layout: inputoutput-layout.png
- Board design (STEP): inputoutput.step (~1.2 MB)
- ESP32-C3 sketch: xiao_esp32c3_input.ino
- MaixCAM RGB script: maixcam_rgb_detect.py
- Demo (OLED + RGB LEDs): zave-screen-color-distance-leds.mp4 (~3.0 MB)
- Earlier clip: maixcam-color-distance.mp4
Hero shot and files
Hero shot
Finished input board: XIAO ESP32-C3 on the milled carrier, HC-SR04 ultrasonic, and MaixCAM linked over UART.
Original design files and source code
- Pictorial wiring: inputoutput-wiring.svg
- Board schematic: inputoutput-schematic.png
- Board layout: inputoutput-layout.png
- Board design (STEP): inputoutput.step (~1.2 MB)
- ESP32-C3 sketch: xiao_esp32c3_input.ino
- MaixCAM RGB script: maixcam_rgb_detect.py
- Demo (OLED + RGB LEDs): zave-screen-color-distance-leds.mp4 (~3.0 MB)
- Earlier clip: maixcam-color-distance.mp4