
BME280 and DS18B20 Temperature Sensing with Arduino
Two sensors cover almost every temperature job on an Arduino. The BME280 reads air temperature, humidity, and barometric pressure from one small board, which is what you want for a room, a greenhouse, or a weather station. The DS18B20 reads temperature from a sealed steel probe on a meter of cable, which is what you want for water, soil, or anything wet. This guide wires both, reads both in one sketch, and shows which one to trust for each job.
The air sensor is the BME280 Temperature, Humidity and Barometric Pressure Sensor Module, 5V, I2C and SPI ($8.99). It measures temperature, humidity, and pressure on a single breakout, works over I2C or SPI, and its pressure readings are good enough to use as an altimeter. Temperature accuracy is plus or minus 1.0 degrees C, pressure accuracy is plus or minus 1 hPa, and altimeter accuracy is plus or minus 1 meter. The header strip comes loose in the box, so soldering is required.
The probe is the DS18B20 Waterproof Temperature Sensor Probe, 1M Cable, with Resistor ($2.99). The sensor sits inside a sealed stainless steel probe on a 1 meter cable, so it works in water, soil, and damp places. It reads minus 55 to plus 125 degrees C at plus or minus 0.5 degrees C, with 9 to 12 bit resolution, and one 4.7k ohm pull-up resistor is included. The cable is pre-wired: red VCC, yellow data, black GND.
Quick facts
| Fact | Value |
|---|---|
| BME280 reads | Temperature, humidity, barometric pressure |
| BME280 accuracy | Temp plus or minus 1.0 C, pressure plus or minus 1 hPa |
| BME280 bus | I2C or SPI, 5V module |
| DS18B20 reads | Temperature only, digital OneWire |
| DS18B20 range | Minus 55 to plus 125 C, plus or minus 0.5 C |
| DS18B20 probe | Sealed steel, 50 mm long, 6 mm diameter, 1 m cable |
| DS18B20 supply | 3.0 to 5.5V, 4.7k pull-up resistor included |
Wiring
Wire the BME280 over I2C: VCC to 5V, GND to ground, SDA to A4, SCL to A5 on a Nano or Uno. On an ESP32 DevKit, SDA is GPIO 21 and SCL is GPIO 22. The listing says to use I2C for simple wiring, and SPI only when several sensors share a bus and I2C address collisions are a concern.
Wire the DS18B20 on OneWire: red to 5V, black to ground, yellow data to digital pin 2. Put the included 4.7k ohm resistor between the data line and 5V. Without that pull-up resistor, the probe will not answer. The probe and the BME280 share the same board with no conflict, because OneWire and I2C are different buses.
| Sensor pin | Arduino Nano / Uno |
|---|---|
| BME280 VCC | 5V |
| BME280 GND | GND |
| BME280 SDA | A4 |
| BME280 SCL | A5 |
| DS18B20 red (VCC) | 5V |
| DS18B20 black (GND) | GND |
| DS18B20 yellow (data) | Digital pin 2, with 4.7k resistor to 5V |
Arduino code: read both sensors
Install three libraries from the Arduino Library Manager: Adafruit BME280 Library, OneWire, and DallasTemperature. The sketch below reads the BME280 over I2C and the DS18B20 over OneWire, then prints both temperatures so you can compare them side by side.
#include <Wire.h>
#include <Adafruit_BME280.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_PIN 2
Adafruit_BME280 bme;
OneWire oneWire(ONE_WIRE_PIN);
DallasTemperature probe(&oneWire);
void setup() {
Serial.begin(9600);
if (!bme.begin(0x76)) {
Serial.println("BME280 not found, check wiring");
while (1);
}
probe.begin();
}
void loop() {
probe.requestTemperatures();
float probeTemp = probe.getTempCByIndex(0);
Serial.print("Air temp (BME280): ");
Serial.print(bme.readTemperature());
Serial.print(" C, humidity: ");
Serial.print(bme.readHumidity());
Serial.print(" %, pressure: ");
Serial.print(bme.readPressure() / 100.0);
Serial.print(" hPa, probe (DS18B20): ");
Serial.print(probeTemp);
Serial.println(" C");
delay(2000);
}
If the sketch prints “BME280 not found”, the module is probably at I2C address 0x77 instead of 0x76. Change the address in bme.begin to 0x77 and try again. If the probe reads minus 127, the OneWire wire is wrong or the pull-up resistor is missing. Minus 127 is the DallasTemperature error value, not a real temperature.
Which sensor to trust for what
For air temperature in a room, trust the BME280, and note its humidity and pressure readings come free with it. For water, soil, or a fridge, trust the DS18B20, because the BME280 is not waterproof and its board cannot go in liquid. For altitude, use the BME280 pressure reading, which the listing rates good enough for plus or minus 1 meter. For a greenhouse, use both: the BME280 for air and humidity, the DS18B20 in the soil.
The DHT22 / AM2302 Digital Temperature and Humidity Sensor Module ($6.99) is the third option in this lane. It reads temperature and humidity only, no pressure, and it is slower and less accurate than the BME280. Pick the DHT22 when you need a cheap humidity reading and nothing else. Pick the BME280 when you want pressure and altitude too. The store guide DHT11 vs DHT22 Temperature Humidity Sensor Choice covers the DHT family in full.
Showing the readings
Serial output is fine for testing, but a small display makes the build usable. The 0.96 Inch OLED Display Module, SSD1306, I2C, 128×64, for Arduino / ESP32 / ESP8266 ($7.49) shares the same I2C bus as the BME280: SDA to A4, SCL to A5, and the display address is selectable at 0x3C or 0x3D on the back of the board. Run it with the Adafruit SSD1306 or U8g2 library. The store guide I2C OLED Display Guide: Wire the SSD1306 to Arduino or ESP32 covers the wiring, and Build a Weather Station with DHT22, BMP280, and OLED shows the same idea as a finished station.
Compatibility and limits
Both sensors work with Arduino, ESP32, ESP8266, and Raspberry Pi. The BME280 module needs its header soldered before it fits a breadboard. The DS18B20 probe is waterproof, but the cable joint where the wires enter the probe should not sit under water for days at a time. The BME280 temperature reading runs a little warm if the board sits next to a hot regulator or a warm enclosure, so keep the sensor in moving air and away from heat sources. The DS18B20 reads one temperature per probe, and several probes can share one OneWire pin, each with its own address.
Troubleshooting
If the BME280 is not found, try address 0x77, then check SDA and SCL are not swapped. If the probe reads minus 127, the data wire or the pull-up resistor is the problem: red to 5V, black to ground, yellow to pin 2, 4.7k resistor from data to 5V. If readings drift slowly upward over an hour, the sensor is warming up from a nearby heat source; move it into airflow and away from the board.
If humidity reads 100 percent all the time, the sensor is wet or sitting in a sealed box with no air exchange. Dry it out and give it a vent. If the two temperatures disagree by a degree or two, that is normal: the BME280 is rated plus or minus 1.0 C and the DS18B20 plus or minus 0.5 C, so a small gap between them is within spec.
Frequently asked questions
Do I need the pull-up resistor for the DS18B20?
Yes. The OneWire data line needs a 4.7k ohm resistor to 5V. One is included with the probe. Without it, the probe will not answer and the sketch reads minus 127.
Can the BME280 go in water?
No. The BME280 board is not waterproof. Use the DS18B20 probe for water, soil, and damp places, and keep the BME280 in dry air.
What I2C address is the BME280?
Usually 0x76 or 0x77, depending on the board. Try 0x76 first, then 0x77. An I2C scanner sketch will show which one answers.
Can I run the BME280 and an OLED on the same I2C bus?
Yes. Both share SDA and SCL. The OLED address is selectable at 0x3C or 0x3D, so pick the one that does not collide with the BME280.
Is the DS18B20 more accurate than the BME280?
For temperature alone, yes: plus or minus 0.5 C against plus or minus 1.0 C. The BME280 wins when you also need humidity and pressure.
How many DS18B20 probes can share one pin?
Many. OneWire gives each probe its own address, so you can put several probes on one data pin and read each by index or by address.
Parts for this build
Shipped fast and securely from our secure facility in New York, providing fast, high quality USA shipping.
BME280 Temperature, Humidity and Barometric Pressure Sensor Module, 5V, I2C and SPI ($8.99)
The air sensor for this guide. Temperature, humidity, and pressure on one board, I2C or SPI, pressure good enough for altitude.
DS18B20 Waterproof Temperature Sensor Probe, 1M Cable, with Resistor ($2.99)
The waterproof probe for water and soil. Minus 55 to plus 125 C, 1 meter cable, 4.7k resistor included.
DHT22 / AM2302 Digital Temperature and Humidity Sensor Module ($6.99)
The budget humidity option when you do not need pressure or altitude.
0.96 Inch OLED Display Module, SSD1306, I2C, 128×64, for Arduino / ESP32 / ESP8266 ($7.49)
The display for the readings. Shares the I2C bus with the BME280, address selectable 0x3C or 0x3D.
