Keyboard shortcuts

Press ← or β†’ to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Examples

Arduino/C++ Examples

The following examples demonstrate various features of this development board.

⚑ light_sensor

#include <Arduino.h>

const int SENSOR_PIN = 12;  // GPIO12 connected to the module’s digital output

void setup() {
  // Initialize Serial at 115200 baud
  Serial.begin(115200);
  while (!Serial) { }  // Wait for Serial (on some boards)

  // Configure GPIO12 as digital input with internal pull-down
  pinMode(SENSOR_PIN, INPUT_PULLDOWN);

  Serial.println("Reading Qwiic module in digital mode (GPIO12)...");
}

void loop() {
  int state = digitalRead(SENSOR_PIN);  // Read 0 (LOW) or 1 (HIGH)

  if (state == HIGH) {
    Serial.println("πŸ”† Light detected (HIGH)");

πŸ“„ See complete code on GitHub

MicroPython Examples

The following MicroPython examples demonstrate usage with microcontrollers.

🐍 light_sensor_adc

from machine import Pin, ADC
import time

# Configure GPIO6 as ADC input (ESP32C6)
light_sensor = ADC(Pin(6))

# Depending on your board, you may need to set attenuation for higher voltage range
# For ESP32: attenuation can be 0db (0–1V), 2.5db (0–1.34V), 6db (0–2V), 11db (0–3.6V)
try:
    light_sensor.atten(ADC.ATTN_11DB)   # Full range ~3.6V
except:
    pass  # Some ports (like RP2040) don't need this

# Optionally set resolution (ESP32 defaults to 12 bits β†’ values 0–4095)
try:
    light_sensor.width(ADC.WIDTH_12BIT)
except:
    pass

while True:

πŸ“„ See complete code on GitHub

🐍 light_sensor

from machine import Pin
import time

# --- DIGITAL INPUT CONFIGURATION ---
# Use GPIO12 as a digital input with internal pull-down
sensor_digital = Pin(12, Pin.IN, Pin.PULL_DOWN)

print("Reading Qwiic module in digital mode (GPIO12)...")

while True:
    state = sensor_digital.value()  # 0 = LOW, 1 = HIGH
    if state:
        print("πŸ”† Light detected (HIGH)")
    else:
        print("πŸŒ‘ No light (LOW)")
    time.sleep(0.5)


πŸ“„ See complete code on GitHub