analogique_lumiere.py

Adafruit Feather nRF52840 Express · File : analogique_lumiere.py · Language : Python · Use case : Mesurer la lumière avec une entrée analogique

The Feather A0 input reads 0-3.3 V over 16 bits: with a photoresistor / 10 kΩ divider, the script computes voltage and decides day vs night. analogio returns a 16-bit value directly, handy for thresholding without math.

Hardware

An Adafruit Feather nRF52840 Express (nRF52840 Cortex-M4F SoC, BLE 5 / 802.15.4) with on-board NeoPixel, red LED, 2 MB QSPI flash for the CircuitPython filesystem and a LiPo battery connector. One photoresistor (LDR) and a 10 kΩ resistor for the divider; any other 0-3.3 V source works for testing.

Wiring

3V3 → photoresistor → A0 pin → 10 kΩ → GND. The midpoint goes to A0. Never above 3.3 V on A0, the ADC is 3.3 V.

How it works

AnalogIn(board.A0).value samples and scales to 0-65535; converting to volts is trivial, and the threshold compares the raw value directly to trigger night logic.

Running it

CircuitPython (feather_nrf52840_express build) installed via the UF2 bootloader (double reset); edit code.py on the CIRCUITPY drive, console in Mu/Thonny.

Variants

Swap the LDR for a flex or ambient-light sensor, average over N reads, or drive the NeoPixel from the light level.

analogique_lumiere.py

# Photorésistance sur A0 : lecture 16 bits et seuil crépusculaire.
# Circuit : 3V3 -- [photorésistance] -- A0 -- [10k] -- GND.
import time
import board
import analogio

entree = analogio.AnalogIn(board.A0)   # 0..65535 pour 0..3,3 V

SEUIL = 20000   # à ajuster : nuit tombante vers 20000 ici

while True:
    brut = entree.value                # déjà mis à l'échelle 16 bits
    volts = brut * 3.3 / 65535
    nuit = brut < SEUIL
    print(f"A0={brut} ({volts:.2f} V) ->",
          "NUIT" if nuit else "jour")
    time.sleep(0.5)

← Back to the board