Board and part

Arduino Mega BMP280 Simulator: Wiring, Code and a Live Circuit

This is a BMP280 barometric pressure and temperature sensor wired to an Arduino Mega 2560 over I2C, read live with the Adafruit BMP280 library: temperature, pressure and altitude once a second.

  • 5 V logic
  • Library: Adafruit_BMP280.h
  • I2C address 0x76
  • SDA 20, SCL 21
Arduino Mega · BMP280live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.
Connections

Wiring the BMP280 to the Arduino Mega

Four wires: 3.3 V, GND, SDA to pin 20 and SCL to pin 21. In the editor, drag the sensor’s pressure slider and the altitude moves with it.

Every connection in the circuit above, with the board’s own pin names.

BMP280 pinArduino Mega pinWhy
VIN3.3VThe BMP280 chip is a 3.3 V part; many breakouts have no regulator.
GNDGNDCommon ground.
SCLpin 21I2C clock.
SDApin 20I2C data.

Power

On the Mega the I2C bus is pin 20 (SDA) and pin 21 (SCL), not A4 and A5 as on an Uno. The SDA and SCL pins by AREF are the same two nets. The common purple BMP280 breakout has no regulator, so it runs from the Arduino Mega’s 3.3V pin, not 5V. Its I2C pull-ups then go to 3.3 V, which the ATmega2560 reads as high.

Pins to leave alone on the Arduino Mega

Pins 14 to 19 are the three extra serial ports and 20 and 21 are I2C, so keep them free if you will need those. An Uno shield’s I2C on A4 and A5 is not connected to anything on a Mega.

On a real bench

The Mega is long, so put the breadboard beside its double row of pins 22 to 53 and leave the left-hand headers free. The BMP280 uses only a few of its 54 pins; a Mega earns its size when one sketch drives a keypad, a display and several sensors at once.

The sketch

The code, and what it does.

The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.

BMP280 on Arduino Mega I2C
#include <Wire.h>
#include <Adafruit_BMP280.h>

Adafruit_BMP280 bmp;   // on I2C: SDA pin 20, SCL pin 21

void setup() {
  Serial.begin(9600);
  if (!bmp.begin(0x76)) {
    Serial.println("No BMP280 at 0x76");
    for (;;) {}
  }
  Serial.println("BMP280 found");
}

void loop() {
  Serial.print("Temperature: ");
  Serial.print(bmp.readTemperature(), 1);
  Serial.print(" C  Pressure: ");
  Serial.print(bmp.readPressure() / 100.0F, 1);
  Serial.print(" hPa  Altitude: ");
  Serial.print(bmp.readAltitude(1013.25), 0);
  Serial.println(" m");
  delay(1000);
}

bmp.begin(0x76) reads the chip’s ID and its factory calibration, which every reading needs. It returns false when nothing answers at that address; boards with SDO tied high answer at 0x77 instead.

readTemperature() is degrees Celsius and readPressure() is pascals, so dividing by 100 gives hectopascals, the unit weather reports use. readAltitude(1013.25) turns pressure into meters above the level where it would be 1013.25 hPa; give it your local sea-level pressure for a true altitude.

Serial.begin(9600) opens the serial monitor. Serial is the USB port; Serial1 to Serial3 are on pins 18 and 19, 16 and 17, and 14 and 15.

Pitfalls

Common mistakes with the BMP280 on the Arduino Mega

The wrong address

Adafruit’s own board answers at 0x77, the cheap purple ones at 0x76, and bmp.begin() with no argument tries 0x77. If it fails, try the other address before suspecting the wiring.

Powering a bare breakout from 5V

The six-pin purple board has no regulator, so 5V goes straight to a chip rated for 3.6 V. Use the Arduino Mega’s 3.3V pin, as in the circuit above.

Confusing it with a BME280

The BME280 looks the same and adds humidity. A BMP280 reports a chip ID of 0x58, a BME280 0x60, and each library refuses the other. If begin() fails at both addresses, check which chip you have.

FAQ

Questions and answers

Can I simulate a BMP280 with an Arduino Mega 2560?

Yes. The circuit at the top of this page is an Arduino Mega 2560 with a BMP280, running in your browser on a simulated ATmega2560. The temperature and pressure sliders set what the simulated chip measures, and the library applies the real calibration math to it. Press Run it in the editor to change the wiring or the code; it is free and needs no account.

Where does the BMP280 connect on the Arduino Mega?

On the Mega the I2C bus is pin 20 (SDA) and pin 21 (SCL), not A4 and A5 as on an Uno. The SDA and SCL pins by AREF are the same two nets.

How accurate is the altitude?

Pressure changes with the weather by more than it changes over a few floors, so readAltitude() is only as good as the sea-level pressure you give it. Relative changes over a few minutes are accurate to about a meter.

Can I use the BMP280 over SPI?

The chip supports SPI, but this simulated part speaks I2C. Most breakouts are wired for I2C out of the box anyway.

Build your own BMP280 project

Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.