GitHub Logo

Code Documentation

Overview

The OpenTholoMetri firmware runs on an Adafruit Feather M0 microcontroller and performs:

Key Features

Hardware Architecture

Microcontroller

Pin Configuration

Pin Function Description
10 SD_CS_PIN SD card chip select (SPI)
SPI SCK/MOSI/MISO SD card data interface (hardware SPI pins)
I2C SDA/SCL VCNL4010 (0x13) and PCF8523 (0x68)
5 RTC_INTERRUPT_PIN RTC countdown timer interrupt (SQW/INT from RTC)
9 BATTERY_PIN Battery voltage ADC input (via /2 divider)
12 DS18B20_POWER_PIN DS18B20 VCC power control
A0 DS18B20_DATA_PIN DS18B20 1‑Wire data
13 GROVE_POWER_PIN Grove turbidity sensor VCC power control
A1 GROVE_ANALOG_PIN Grove turbidity sensor analog ADC input

Power Consumption

Mode Current Description
Standby (USB disconnected) ~6µA Deep sleep, waiting for RTC interrupt
Idle (USB connected) ~15mA Light sleep, maintains USB for debugging
Active (sensing/logging) ~12-20mA Sensor reading and SD write operations

Firmware Structure

The code is organized into modular components:

Sensor Data

The system reads the following sensors:

CSV Output Format

Data is logged with the following columns:

Timestamp,Proximity,Ambient Light,Battery Voltage,Temperature (C),Turbidity Raw,Turbidity Voltage
2024-01-15 10:30:00,245,1023,4.15,22.5,512,2.50

Configuration

Changing Logging Interval

Edit Config.h:

const int SLEEP_INTERVAL = 300;  // Change to desired seconds (300 = 5 minutes)

Considerations:

Software Installation

Required Libraries

Install via Arduino Library Manager:

  1. RTClib by Adafruit
  2. Adafruit VCNL4010
  3. SD (built-in)
  4. SPI (built-in)
  5. Wire (built-in)
  6. OneWire
  7. DallasTemperature

Upload Process

  1. Open main.ino in Arduino IDE
  2. Select board: Adafruit Feather M0
  3. Select port: Your COM port
  4. Click Upload
  5. Open Serial Monitor (115200 baud) to verify initialization

First-Time Setup

  1. Verify RTC: Check Serial output shows RTC initialization success
  2. Verify SD Card: Confirm "SD card initialized successfully!" and sensor.csv is created
  3. Test Sensor: Check for reasonable proximity and light readings
  4. Test Logging: Wait for first RTC interrupt and verify LED flash and data written to SD card

Deployment Configuration

For field deployment:

  1. Set desired SLEEP_INTERVAL in Config.h
  2. Ensure CR1220 coin cell battery in RTC socket
  3. Format fresh SD card (FAT32)
  4. Upload final firmware
  5. Connect battery and deploy