🛠 Connecter un capteur externe en I²C et SPI

Adafruit Feather nRF52840 Express · intermediaire · Schéma : IMG-nrf52840-feather-intermediaire-3-nrf52840-spi-i2c-capteur-externe.svg — Feather nRF52840 reliée à un module BMP280 en I²C (SDA/SCL) et à un module SPI, pull-up sur le bus, broches 3V3 et GND communes.

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials