Last active
August 6, 2025 13:46
-
-
Save dieseltravis/3f2a1346ed4738ba0f9a0e42502e23d7 to your computer and use it in GitHub Desktop.
Demo of using BMP280+AHT20 sensor on I2C with a Seeed Xiao nRF52840 Sense, and modifications to files in the Arduino packages to use different pins for SDA/SCL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <Adafruit_AHTX0.h> | |
| #include <Adafruit_BMP280.h> | |
| Adafruit_AHTX0 aht; | |
| Adafruit_BMP280 bmp; | |
| void setup() { | |
| unsigned status; | |
| // Xiao nRF52840 specific pins: | |
| pinMode(LED_RED, OUTPUT); | |
| pinMode(LED_GREEN, OUTPUT); | |
| pinMode(LED_BLUE, OUTPUT); | |
| digitalWrite(LED_RED, LOW); | |
| Serial.begin(9600); | |
| delay(5000); | |
| digitalWrite(LED_RED, HIGH); | |
| // I can't figure out why serial printing doesn't work in setup | |
| Serial.println("AHT20+BMP280 Demo"); | |
| digitalWrite(LED_GREEN, LOW); | |
| status = aht.begin(); | |
| if (!status) { | |
| Serial.println("Could not find AHT? Check wiring"); | |
| while (1) delay(10); | |
| } | |
| Serial.println("AHT10 or AHT20 found."); | |
| digitalWrite(LED_GREEN, HIGH); | |
| digitalWrite(LED_BLUE, LOW); | |
| status = bmp.begin(); | |
| if (!status) { | |
| Serial.println(F("Could not find a valid BMP280 sensor, check wiring or " | |
| "try a different address!")); | |
| Serial.print("SensorID was: 0x"); | |
| Serial.println(bmp.sensorID(), 16); | |
| Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n"); | |
| Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n"); | |
| Serial.print(" ID of 0x60 represents a BME 280.\n"); | |
| Serial.print(" ID of 0x61 represents a BME 680.\n"); | |
| while (1) delay(10); | |
| } | |
| Serial.println("BMP280 found."); | |
| digitalWrite(LED_BLUE, HIGH); | |
| } | |
| void loop() { | |
| digitalWrite(LED_RED, LOW); | |
| delay(500); | |
| digitalWrite(LED_RED, HIGH); | |
| delay(1000); | |
| digitalWrite(LED_GREEN, LOW); | |
| Serial.println("AHT20:"); | |
| sensors_event_t humidity, temp; | |
| aht.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data | |
| Serial.print("Temperature: "); | |
| float aht_c = temp.temperature; | |
| Serial.print(aht_c); | |
| Serial.print("°C\t"); | |
| Serial.print(aht_c * 1.8 + 32); | |
| Serial.println("°F"); | |
| Serial.print("Humidity: "); | |
| Serial.print(humidity.relative_humidity); | |
| Serial.println("% rH"); | |
| delay(1000); | |
| digitalWrite(LED_GREEN, HIGH); | |
| delay(1000); | |
| Serial.println(); | |
| digitalWrite(LED_BLUE, LOW); | |
| Serial.println("BMP280:"); | |
| Serial.print(F("Temperature: ")); | |
| float bmp_c = bmp.readTemperature(); | |
| Serial.print(bmp_c); | |
| Serial.print("°C\t"); | |
| Serial.print(bmp_c * 1.8 + 32); | |
| Serial.println("°F"); | |
| Serial.print(F("Pressure: ")); | |
| Serial.print(bmp.readPressure()); | |
| Serial.println(" Pa"); | |
| Serial.print(F("Approx altitude: ")); | |
| Serial.print(bmp.readAltitude(1013.25)); /* Adjusted to local forecast! */ | |
| Serial.println(" m"); | |
| delay(1000); | |
| digitalWrite(LED_BLUE, HIGH); | |
| Serial.println("\n"); | |
| delay(5000); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # C:\Users\travis\AppData\Local\Arduino15\packages\Seeeduino\hardware\nrf52\1.1.10\boards.txt | |
| # ----------------------------------------^ | |
| # Add the block below to the boards.txt file to customize the SDA and SCL pins | |
| # ---------------------------------- | |
| # Seeed XIAO nRF52840 Sense with SDA D6 & SCL D7 | |
| # ---------------------------------- | |
| xiaonRF52840SenseSDA6SCL7.name=Seeed XIAO nRF52840 Sense SDA6 SCL7 | |
| # VID/PID for bootloader | |
| xiaonRF52840SenseSDA6SCL7.vid.0=0x2886 | |
| xiaonRF52840SenseSDA6SCL7.pid.0=0x8045 | |
| xiaonRF52840SenseSDA6SCL7.vid.1=0x2886 | |
| xiaonRF52840SenseSDA6SCL7.pid.1=0x0045 | |
| xiaonRF52840SenseSDA6SCL7.vid.2=0x2886 | |
| xiaonRF52840SenseSDA6SCL7.pid.2=0x0045 | |
| xiaonRF52840SenseSDA6SCL7.vid.3=0x2886 | |
| xiaonRF52840SenseSDA6SCL7.pid.3=0x8045 | |
| # Upload | |
| xiaonRF52840SenseSDA6SCL7.bootloader.tool=bootburn | |
| xiaonRF52840SenseSDA6SCL7.upload.tool=nrfutil | |
| xiaonRF52840SenseSDA6SCL7.upload.protocol=nrfutil | |
| xiaonRF52840SenseSDA6SCL7.upload.use_1200bps_touch=true | |
| xiaonRF52840SenseSDA6SCL7.upload.wait_for_upload_port=true | |
| xiaonRF52840SenseSDA6SCL7.upload.maximum_size=811008 | |
| xiaonRF52840SenseSDA6SCL7.upload.maximum_data_size=237568 | |
| # Build | |
| xiaonRF52840SenseT.build.mcu=cortex-m4 | |
| xiaonRF52840SenseSDA6SCL7.build.f_cpu=64000000 | |
| xiaonRF52840SenseSDA6SCL7.build.board=Seeed_XIAO_nRF52840_Sense_SDA6SCL7 | |
| xiaonRF52840SenseSDA6SCL7.build.core=nRF5 | |
| xiaonRF52840SenseSDA6SCL7.build.variant=Seeed_XIAO_nRF52840_Sense_SDA6SCL7 | |
| xiaonRF52840SenseSDA6SCL7.build.usb_manufacturer="Seeed" | |
| xiaonRF52840SenseSDA6SCL7.build.usb_product="XIAO nRF52840 Sense SDA6SCL7" | |
| xiaonRF52840SenseSDA6SCL7.build.extra_flags=-DNRF52840_XXAA {build.flags.usb} | |
| xiaonRF52840SenseSDA6SCL7.build.ldscript=nrf52840_s140_v7.ld | |
| xiaonRF52840SenseSDA6SCL7.build.vid=0x2886 | |
| xiaonRF52840SenseSDA6SCL7.build.pid=0x8045 | |
| # SofDevice Menu | |
| xiaonRF52840SenseSDA6SCL7.menu.softdevice.s140v6=S140 7.3.0 | |
| xiaonRF52840SenseSDA6SCL7.menu.softdevice.s140v6.build.sd_name=s140 | |
| xiaonRF52840SenseSDA6SCL7.menu.softdevice.s140v6.build.sd_version=7.3.0 | |
| xiaonRF52840SenseSDA6SCL7.menu.softdevice.s140v6.build.sd_fwid=0x0123 | |
| # Debug Menu | |
| xiaonRF52840SenseSDA6SCL7.menu.debug.l0=Level 0 (Release) | |
| xiaonRF52840SenseSDA6SCL7.menu.debug.l0.build.debug_flags=-DCFG_DEBUG=0 | |
| xiaonRF52840SenseSDA6SCL7.menu.debug.l1=Level 1 (Error Message) | |
| xiaonRF52840SenseSDA6SCL7.menu.debug.l1.build.debug_flags=-DCFG_DEBUG=1 | |
| xiaonRF52840SenseSDA6SCL7.menu.debug.l2=Level 2 (Full Debug) | |
| xiaonRF52840SenseSDA6SCL7.menu.debug.l2.build.debug_flags=-DCFG_DEBUG=2 | |
| xiaonRF52840SenseSDA6SCL7.menu.debug.l3=Level 3 (Segger SystemView) | |
| xiaonRF52840SenseSDA6SCL7.menu.debug.l3.build.debug_flags=-DCFG_DEBUG=3 | |
| xiaonRF52840SenseSDA6SCL7.menu.debug.l3.build.sysview_flags=-DCFG_SYSVIEW=1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // C:\Users\travis\AppData\Local\Arduino15\packages\Seeeduino\hardware\nrf52\1.1.10\cores\nRF5\main.cpp | |
| // ----------------------------------------^ | |
| // edit line # 55: | |
| #if defined(ARDUINO_Seeed_XIAO_nRF52840) || defined(ARDUINO_Seeed_XIAO_nRF52840_Sense) || defined(ARDUINO_Seeed_XIAO_nRF52840_Sense_SDA6SCL7) || defined(ARDUINO_Seeed_XIAO_nRF52840_Plus) || defined(ARDUINO_Seeed_XIAO_nRF52840_Sense_Plus) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // C:\Users\travis\AppData\Local\Arduino15\packages\Seeeduino\hardware\nrf52\1.1.10\variants\Seeed_XIAO_nRF52840_Sense_SDA6SCL7\variant.cpp | |
| // ----------------------------------------^ ^ | |
| // ------------------------------------------------------------------------------------------| New Folder | |
| #include "variant.h" | |
| #include "wiring_constants.h" | |
| #include "wiring_digital.h" | |
| #include "nrf.h" | |
| const uint32_t g_ADigitalPinMap[] = | |
| { | |
| // D0 .. D10 | |
| 2, // D0 is P0.02 (A0) | |
| 3, // D1 is P0.03 (A1) | |
| 28, // D2 is P0.28 (A2) | |
| 29, // D3 is P0.29 (A3) | |
| 4, // D4 is P0.04 (A4) | |
| 5, // D5 is P0.05 (A5) | |
| 43, // D6 is P1.11 (TX,SDA) | |
| 44, // D7 is P1.12 (RX,SCL) | |
| 45, // D8 is P1.13 (SCK) | |
| 46, // D9 is P1.14 (MISO) | |
| 47, // D10 is P1.15 (MOSI) | |
| // LEDs | |
| 26, // D11 is P0.26 (LED RED) | |
| 6, // D12 is P0.06 (LED BLUE) | |
| 30, // D13 is P0.30 (LED GREEN) | |
| 14, // D14 is P0.14 (READ_BAT) | |
| // LSM6DS3TR | |
| 40, // D15 is P1.08 (6D_PWR) | |
| 27, // D16 is P0.27 (6D_I2C_SCL) | |
| 7, // D17 is P0.07 (6D_I2C_SDA) | |
| 11, // D18 is P0.11 (6D_INT1) | |
| // MIC | |
| 42, // D19 is P1.10 (MIC_PWR) | |
| 32, // D20 is P1.00 (PDM_CLK) | |
| 16, // D21 is P0.16 (PDM_DATA) | |
| // BQ25100 | |
| 13, // D22 is P0.13 (HICHG) | |
| 17, // D23 is P0.17 (~CHG) | |
| // | |
| 21, // D24 is P0.21 (QSPI_SCK) | |
| 25, // D25 is P0.25 (QSPI_CSN) | |
| 20, // D26 is P0.20 (QSPI_SIO_0 DI) | |
| 24, // D27 is P0.24 (QSPI_SIO_1 DO) | |
| 22, // D28 is P0.22 (QSPI_SIO_2 WP) | |
| 23, // D29 is P0.23 (QSPI_SIO_3 HOLD) | |
| // NFC | |
| 9, // D30 is P0.09 (NFC1) | |
| 10, // D31 is P0.10 (NFC2) | |
| // VBAT | |
| 31, // D32 is P0.31 (VBAT) | |
| }; | |
| void initVariant() | |
| { | |
| // Disable reading of the BAT voltage. | |
| // https://wiki.seeedstudio.com/XIAO_BLE#q3-what-are-the-considerations-when-using-xiao-nrf52840-sense-for-battery-charging | |
| pinMode(VBAT_ENABLE, OUTPUT); | |
| digitalWrite(VBAT_ENABLE, HIGH); | |
| // Low charging current. | |
| // https://wiki.seeedstudio.com/XIAO_BLE#battery-charging-current | |
| pinMode(PIN_CHARGING_CURRENT, INPUT); | |
| pinMode(PIN_QSPI_CS, OUTPUT); | |
| digitalWrite(PIN_QSPI_CS, HIGH); | |
| pinMode(LED_RED, OUTPUT); | |
| digitalWrite(LED_RED, HIGH); | |
| pinMode(LED_GREEN, OUTPUT); | |
| digitalWrite(LED_GREEN, HIGH); | |
| pinMode(LED_BLUE, OUTPUT); | |
| digitalWrite(LED_BLUE, HIGH); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // C:\Users\travis\AppData\Local\Arduino15\packages\Seeeduino\hardware\nrf52\1.1.10\variants\Seeed_XIAO_nRF52840_Sense_SDA6SCL7\variant.h | |
| // ----------------------------------------^ ^ | |
| // ------------------------------------------------------------------------------------------| New Folder | |
| #ifndef _SEEED_XIAO_NRF52840_SENSE_H_ | |
| #define _SEEED_XIAO_NRF52840_SENSE_H_ | |
| #define TARGET_SEEED_XIAO_NRF52840_SENSE | |
| #define TARGET_SEEED_XIAO_NRF52840_SENSE_SDA6SCL7 | |
| /** Master clock frequency */ | |
| #define VARIANT_MCK (64000000ul) | |
| #define USE_LFXO // Board uses 32khz crystal for LF | |
| //#define USE_LFRC // Board uses RC for LF | |
| /*---------------------------------------------------------------------------- | |
| * Headers | |
| *----------------------------------------------------------------------------*/ | |
| #include "WVariant.h" | |
| #ifdef __cplusplus | |
| extern "C" | |
| { | |
| #endif // __cplusplus | |
| #define PINS_COUNT (33) | |
| #define NUM_DIGITAL_PINS (33) | |
| #define NUM_ANALOG_INPUTS (8) | |
| #define NUM_ANALOG_OUTPUTS (0) | |
| // LEDs | |
| #define PIN_LED (LED_RED) | |
| #define LED_PWR (PINS_COUNT) | |
| #define PIN_NEOPIXEL (PINS_COUNT) | |
| #define NEOPIXEL_NUM (0) | |
| #define LED_BUILTIN (PIN_LED) | |
| #define LED_RED (11) | |
| #define LED_GREEN (13) | |
| #define LED_BLUE (12) | |
| #define LED_STATE_ON (1) // State when LED is litted | |
| // Buttons | |
| #define PIN_BUTTON1 (PINS_COUNT) | |
| // Digital PINs | |
| static const uint8_t D0 = 0 ; | |
| static const uint8_t D1 = 1 ; | |
| static const uint8_t D2 = 2 ; | |
| static const uint8_t D3 = 3 ; | |
| static const uint8_t D4 = 4 ; | |
| static const uint8_t D5 = 5 ; | |
| static const uint8_t D6 = 6 ; | |
| static const uint8_t D7 = 7 ; | |
| static const uint8_t D8 = 8 ; | |
| static const uint8_t D9 = 9 ; | |
| static const uint8_t D10 = 10; | |
| #define VBAT_ENABLE (14) // Output LOW to enable reading of the BAT voltage. | |
| // https://wiki.seeedstudio.com/XIAO_BLE#q3-what-are-the-considerations-when-using-xiao-nrf52840-sense-for-battery-charging | |
| #define PIN_CHARGING_CURRENT (22) // Battery Charging current | |
| // https://wiki.seeedstudio.com/XIAO_BLE#battery-charging-current | |
| // Analog pins | |
| #define PIN_A0 (0) | |
| #define PIN_A1 (1) | |
| #define PIN_A2 (2) | |
| #define PIN_A3 (3) | |
| #define PIN_A4 (4) | |
| #define PIN_A5 (5) | |
| #define PIN_VBAT (32) // Read the BAT voltage. | |
| // https://wiki.seeedstudio.com/XIAO_BLE#q3-what-are-the-considerations-when-using-xiao-nrf52840-sense-for-battery-charging | |
| static const uint8_t A0 = PIN_A0; | |
| static const uint8_t A1 = PIN_A1; | |
| static const uint8_t A2 = PIN_A2; | |
| static const uint8_t A3 = PIN_A3; | |
| static const uint8_t A4 = PIN_A4; | |
| static const uint8_t A5 = PIN_A5; | |
| #define ADC_RESOLUTION (12) | |
| // Other pins | |
| #define PIN_NFC1 (30) | |
| #define PIN_NFC2 (31) | |
| // Serial interfaces | |
| #define PIN_SERIAL1_RX (7) | |
| #define PIN_SERIAL1_TX (6) | |
| // SPI Interfaces | |
| #define SPI_INTERFACES_COUNT (2) | |
| #define PIN_SPI_MISO (9) | |
| #define PIN_SPI_MOSI (10) | |
| #define PIN_SPI_SCK (8) | |
| static const uint8_t SS = 7; | |
| static const uint8_t MOSI = PIN_SPI_MOSI; | |
| static const uint8_t MISO = PIN_SPI_MISO; | |
| static const uint8_t SCK = PIN_SPI_SCK ; | |
| #define PIN_SPI1_MISO (25) | |
| #define PIN_SPI1_MOSI (26) | |
| #define PIN_SPI1_SCK (29) | |
| // Wire Interfaces | |
| #define WIRE_INTERFACES_COUNT (2) | |
| // Redefining SDA and SCL here: | |
| #define PIN_WIRE_SDA (6) | |
| #define PIN_WIRE_SCL (7) | |
| static const uint8_t SDA = PIN_WIRE_SDA; | |
| static const uint8_t SCL = PIN_WIRE_SCL; | |
| #define PIN_WIRE1_SDA (17) | |
| #define PIN_WIRE1_SCL (16) | |
| #define PIN_LSM6DS3TR_C_POWER (15) | |
| #define PIN_LSM6DS3TR_C_INT1 (18) | |
| // PDM Interfaces | |
| #define PIN_PDM_PWR (19) | |
| #define PIN_PDM_CLK (20) | |
| #define PIN_PDM_DIN (21) | |
| // QSPI Pins | |
| #define PIN_QSPI_SCK (24) | |
| #define PIN_QSPI_CS (25) | |
| #define PIN_QSPI_IO0 (26) | |
| #define PIN_QSPI_IO1 (27) | |
| #define PIN_QSPI_IO2 (28) | |
| #define PIN_QSPI_IO3 (29) | |
| // On-board QSPI Flash | |
| #define EXTERNAL_FLASH_DEVICES (P25Q16H) | |
| #define EXTERNAL_FLASH_USE_QSPI | |
| #ifdef __cplusplus | |
| } | |
| #endif | |
| /*---------------------------------------------------------------------------- | |
| * Arduino objects - C++ only | |
| *----------------------------------------------------------------------------*/ | |
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // C:\Users\travis\AppData\Local\Arduino15\packages\Seeeduino\hardware\nrf52\1.1.10\libraries\Wire\Wire_nRF52.cpp | |
| // ----------------------------------------^ | |
| // Edit line # 32 | |
| #if defined(ARDUINO_Seeed_XIAO_nRF52840) || defined(ARDUINO_Seeed_XIAO_nRF52840_Sense) || defined(ARDUINO_Seeed_XIAO_nRF52840_Sense_SDA6SCL7) || defined(ARDUINO_Seeed_XIAO_nRF52840_Plus) || defined(ARDUINO_Seeed_XIAO_nRF52840_Sense_Plus) || defined(ARDUINO_TRACKER_T1000_E_LORAWAN) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment