🛠 Connecter un capteur externe en I²C et SPI
Objectives
Wire and read an external sensor (example: BMP280/BME680) on the I²C and SPI buses of the nRF52840. The SoC routes SPI, UART, I²C, and I²S to any pin (PSEL), but watch out for Feather-specific pitfalls: the 3 slow SPI masters share their instances with the I²C masters, and the fast SPIM3 is non-functional under battery power.
Hardware
- 1× Adafruit Feather nRF52840 Express
- 1× I²C sensor module (BMP280, BME680…) + 1× SPI module of your choice
- Adafruit BMP280 Unified + Adafruit BusIO libraries
Full code
// nrf52840-spi-i2c-capteur-externe.ino
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>
Adafruit_BMP280 bmp; // par defaut: I2C sur Wire
void setup {
Serial.begin(115200);
Wire.begin; // TWI sur les broches Feather SDA/SCL
// AVERTISSEMENT: board.SPI/SPI par defaut peut allouer SPIM3 (<=32 MHz),
// qui NE FONCTIONNE PAS sur batterie (bug materiel documente par Adafruit,
// la ligne ne devient correcte que lorsque VUSB est alimente). Sur batterie,
// forcez un SPIM lent: SPIClass spi(SPI1); ou workaround CircuitPython.
if (!bmp.begin_I2C(0x76, &Wire)) {
Serial.println("BMP280 absent sur 0x76 (essayer 0x77)");
while (1) delay(100);
}
Serial.println("BMP280 OK");
}
void loop {
Serial.print(bmp.readTemperature, 2); Serial.print(" C ");
Serial.print(bmp.readPressure / 100.0f, 2); Serial.println(" hPa");
delay(1000);
}
Expected verification
Temperature and pressure values scroll every second. An I²C Wire.scan lists the module's address (0x76 or 0x77). The test is reproducible on USB and on battery only if you did not use SPIM3 — test both power sources before validating a portable project.
Common errors
- SPI works on USB but not on battery: this is the documented SPIM3 bug; allocate and ignore the first SPI object (CircuitPython) or force a slow SPIM on the Arduino side.
- SPI/I²C conflict: the 3 TWIM instances share instances with SPIM0-2 — "not all SPI devices are always available" if you stack multiple buses.
- Missing I²C pull-ups: most Adafruit modules include them; a bare module without pull-ups will not respond.
- Confusing addresses 0x76/0x77: the J1 jumper on the BMP280 module selects one or the other.
Other tutorials for this board
Code examples for this board
neopixel_pulsation.pyPythonled_bouton.pyPythonentrees_num.pyPythonanalogique_lumiere.pyPythonservo_pwm.pyPythonscanner_i2c.pyPython