Input Devices
Week 9 Assignment
Assignment Overview
Week 9 covers input devices: reading sensors and bringing data into a microcontroller. Below, a combined sketch reads distance from a VL53L0X time-of-flight sensor over I2C and captures frames from an ESP32-style camera using the esp_camera driver.
AI assistance: I used Google Gemini to help debug firmware and to write parts of this website.
๐ก VL53L0X ToF + camera firmware
Adafruit_VL53L0X on custom SDA/SCL pins; camera pin map matches a common ESP32 module layout (QVGA JPEG, 20 MHz XCLK). The loop prints range in millimetres when valid and reports each JPEG capture size over Serial.
#include "esp_camera.h"
#include <Wire.h>
#include "Adafruit_VL53L0X.h"
// --- Configuration ---
#define I2C_SDA 14
#define I2C_SCL 15
#define PWDN_GPIO_NUM 32
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 0
#define SIOD_GPIO_NUM 26
#define SIOC_GPIO_NUM 27
#define Y9_GPIO_NUM 35
#define Y8_GPIO_NUM 34
#define Y7_GPIO_NUM 39
#define Y6_GPIO_NUM 36
#define Y5_GPIO_NUM 21
#define Y4_GPIO_NUM 19
#define Y3_GPIO_NUM 18
#define Y2_GPIO_NUM 5
#define VSYNC_GPIO_NUM 25
#define HREF_GPIO_NUM 23
#define PCLK_GPIO_NUM 22
Adafruit_VL53L0X lox = Adafruit_VL53L0X();
// --- Module Initializers ---
void initToF() {
Wire.begin(I2C_SDA, I2C_SCL);
if (!lox.begin()) {
Serial.println("ToF Failed");
while (1);
}
}
void initCamera() {
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = Y2_GPIO_NUM;
config.pin_d1 = Y3_GPIO_NUM;
config.pin_d2 = Y4_GPIO_NUM;
config.pin_d3 = Y5_GPIO_NUM;
config.pin_d4 = Y6_GPIO_NUM;
config.pin_d5 = Y7_GPIO_NUM;
config.pin_d6 = Y8_GPIO_NUM;
config.pin_d7 = Y9_GPIO_NUM;
config.pin_xclk = XCLK_GPIO_NUM;
config.pin_pclk = PCLK_GPIO_NUM;
config.pin_vsync = VSYNC_GPIO_NUM;
config.pin_href = HREF_GPIO_NUM;
config.pin_sscb_sda = SIOD_GPIO_NUM;
config.pin_sscb_scl = SIOC_GPIO_NUM;
config.pin_pwdn = PWDN_GPIO_NUM;
config.pin_reset = RESET_GPIO_NUM;
config.xclk_freq_hz = 20000000;
config.pixel_format = PIXFORMAT_JPEG;
config.frame_size = FRAMESIZE_QVGA;
config.jpeg_quality = 12;
config.fb_count = 1;
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.println("Camera Failed");
while (1);
}
}
// --- Main Logic ---
void setup() {
Serial.begin(115200);
initToF();
initCamera();
Serial.println("System Ready");
}
void loop() {
// 1. Get Distance
VL53L0X_RangingMeasurementData_t measure;
lox.rangingTest(&measure, false);
if (measure.RangeStatus != 4) {
Serial.printf("Distance: %d mm\n", measure.RangeMilliMeter);
}
// 2. Capture Image
camera_fb_t * fb = esp_camera_fb_get();
if (fb) {
Serial.printf("Captured: %zu bytes\n", fb->len);
esp_camera_fb_return(fb);
}
delay(500);
}
๐ Assignment Process
This section documents the step-by-step process of completing this week's assignment.
Process Documentation
Add your process documentation, images, and files here.
โ Assignment Results
This section showcases the final outcomes and deliverables of this week's assignment.
Completed Tasks
Document your completed tasks here.
Final Deliverables
Document your final deliverables here. Include screenshots, files, or links to your work.
Files and Resources
๐ Assignment Notes
This section contains learning notes, reflections, and insights gained during this week's work.
Key Learnings
Document your key learnings here.
Challenges Faced
Document any challenges you encountered and how you overcame them.
Reflection
Share your thoughts on this week's learning experience. What challenges did you face? What did you learn? What would you do differently?
Additional Notes
Any additional observations, tips, or resources that might be useful for future reference.