09

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.

1. VL53L0X ToF and 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"

// ToF on a custom I2C bus (not the default ESP32 pins)
#define I2C_SDA 14
#define I2C_SCL 15

// Camera pin map for this ESP32 module
#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); // start I2C on the pins above
  if (!lox.begin()) {
    Serial.println("ToF Failed");
    while (1); // stop here if the sensor does not answer
  }
}

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;      // 20 MHz clock to the camera
  config.pixel_format = PIXFORMAT_JPEG;
  config.frame_size = FRAMESIZE_QVGA;   // 320 x 240
  config.jpeg_quality = 12;             // lower number = larger file
  config.fb_count = 1;                  // one frame buffer

  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();     // distance sensor first
  initCamera();  // then the camera
  Serial.println("System Ready");
}

void loop() {
  // Read one range. Status 4 means the reading is not valid.
  VL53L0X_RangingMeasurementData_t measure;
  lox.rangingTest(&measure, false);
  
  if (measure.RangeStatus != 4) {
    Serial.printf("Distance: %d mm\n", measure.RangeMilliMeter);
  }

  // Grab one JPEG, print its size, then give the buffer back
  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); // half a second between samples
}

2. 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.

3. 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

4. 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.