STM32 Blue Pill I2C BMP280 with Wire: Wiring, Code and Simulator

To build this STM32 Blue Pill I2C BMP280 with Wire project you need an STM32 Blue Pill (STM32F103C8) and a pressure sensor BMP280. Wire on I2C1 (SDA PB7, SCL PB6) talks to a BMP280 through the STM32’s I2C peripheral and reads its chip ID (0x58). Make the 4 connections in the wiring table, upload the Arduino C++ code, and change the address to 0x77 and run again to see the error path, or read the temperature registers 0xFA–0xFC.

Open this circuit in the free STM32 Blue Pill (STM32F103C8) simulator

Parts list

QtyPartWhat it does
1STM32 Blue Pill (STM32F103C8)the microcontroller board
1Pressure Sensor BMP280Temperature and air pressure on I²C (0x76; 0x77 with SDO high): bmp.begin(0x76), readTemperature(), readPressure(), readAltitude(1013.25).

Wiring

#FromTo
1STM32 Blue Pill (STM32F103C8) 3V3Pressure Sensor BMP280 VCC
2STM32 Blue Pill (STM32F103C8) GNDPressure Sensor BMP280 GND
3STM32 Blue Pill (STM32F103C8) PB7Pressure Sensor BMP280 SDA
4STM32 Blue Pill (STM32F103C8) PB6Pressure Sensor BMP280 SCL

Step by step

  1. Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the STM32 Blue Pill (STM32F103C8). No install or account is needed.
  2. Add the parts. Add a pressure sensor BMP280 from the parts palette.
  3. Wire 1. Connect STM32 Blue Pill (STM32F103C8) 3V3 to Pressure Sensor BMP280 VCC.
  4. Wire 2. Connect STM32 Blue Pill (STM32F103C8) GND to Pressure Sensor BMP280 GND.
  5. Wire 3. Connect STM32 Blue Pill (STM32F103C8) PB7 to Pressure Sensor BMP280 SDA.
  6. Wire 4. Connect STM32 Blue Pill (STM32F103C8) PB6 to Pressure Sensor BMP280 SCL.
  7. Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
  8. Run it. Press Run. Change the address to 0x77 and run again to see the error path, or read the temperature registers 0xFA–0xFC.

Arduino C++ code

// I2C with Wire on I2C1 (SDA PB7, SCL PB6): read a BMP280's chip ID
// (register 0xD0 answers 0x58), then its raw temperature.
#include <Wire.h>

void setup() {
  Serial.begin(115200);
  Wire.begin();
}

uint8_t readReg(uint8_t reg) {
  Wire.beginTransmission(0x76);
  Wire.write(reg);
  Wire.endTransmission(false);
  Wire.requestFrom(0x76, 1);
  return Wire.read();
}

void loop() {
  Wire.beginTransmission(0x76);
  if (Wire.endTransmission() != 0) {
    Serial.println("No answer from 0x76");
  } else {
    Serial.print("BMP280 chip ID: 0x");
    Serial.println(readReg(0xD0), HEX);
  }
  delay(1000);
}

What to try

Change the address to 0x77 and run again to see the error path, or read the temperature registers 0xFA–0xFC.

Frequently asked questions

How do I wire the I2C BMP280 with Wire project on an STM32 Blue Pill (STM32F103C8)?

Make 4 connections: STM32 Blue Pill (STM32F103C8) 3V3 to Pressure Sensor BMP280 VCC; STM32 Blue Pill (STM32F103C8) GND to Pressure Sensor BMP280 GND; STM32 Blue Pill (STM32F103C8) PB7 to Pressure Sensor BMP280 SDA; STM32 Blue Pill (STM32F103C8) PB6 to Pressure Sensor BMP280 SCL.

What parts do I need for this STM32 Blue Pill project?

An STM32 Blue Pill (STM32F103C8) and a pressure sensor BMP280.

Can I simulate this STM32 Blue Pill project online for free?

Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is built with the STM32duino core and runs on an STM32F103 emulated instruction by instruction: its ARM Cortex-M3 core, NVIC and peripherals (GPIO, EXTI, USART, timers, ADC and I²C).

What does the code do?

Wire on I2C1 (SDA PB7, SCL PB6) talks to a BMP280 through the STM32’s I2C peripheral and reads its chip ID (0x58).

More STM32 Blue Pill (STM32F103C8) projects