ESP32 Project #4: BMP280 Sensor
Hello! I’m back. On this project I’ll show you how external sensor: BMP280 sensor works. BMP280 is a humidity, temperature, and pressure sensor.
Let’s try!
Parts and components
- Arduino IDE
- ESP32 Board
- Breadboard
- Jumper wires
- BMP280 sensor
- USB cable
Follow the schematic above then setup the BMP280 package. Go to Tools > Manage Libraries then search for “BMP 280”. Install the Adafruit BMP280 Library.
To test the sketch, go to File > Examples > Adafruit BMP280 Library > bmp280test. Connect the ESP32 with the PC then verify and upload the sketch.
This is the bmp280test.
/***************************************************************************
This is a library for the BMP280 humidity, temperature & pressure sensorDesigned specifically to work with the Adafruit BMP280 Breakout
----> http://www.adafruit.com/products/2651These sensors use I2C or SPI to communicate, 2 or 4 pins are required
to interface.Adafruit invests time and resources providing this open source code,
please support Adafruit andopen-source hardware by purchasing products
from Adafruit!Written by Limor Fried & Kevin Townsend for Adafruit Industries.
BSD license, all text above must be included in any redistribution
***************************************************************************/#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>#define BMP_SCK (13)
#define BMP_MISO (12)
#define BMP_MOSI (11)
#define BMP_CS (10)Adafruit_BMP280 bmp; // I2C
//Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
//Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);void setup() {
Serial.begin(9600);
Serial.println(F("BMP280 test"));if (!bmp.begin()) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
while (1);
}/* Default settings from datasheet. */
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */
Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
Adafruit_BMP280::FILTER_X16, /* Filtering. */
Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
}void loop() {
Serial.print(F("Temperature = "));
Serial.print(bmp.readTemperature());
Serial.println(" *C");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");Serial.println();
delay(2000);
}
I uploaded the sketch then opened Serial Monitor. What? Nothing showed. Hmm, what’s wrong? I tried to solve the problem and found this code to find the I2C address for BMP280 sensor.
#include <Wire.h> //include Wire.h library
void setup()
{
Wire.begin(); // Wire communication begin
Serial.begin(9600); // The baudrate of Serial monitor is set in 9600
while (!Serial); // Waiting for Serial Monitor
Serial.println("\nI2C address Scanner CircuitSchools.com");
}
void loop()
{
byte error, address; //variable for error and I2C address
int devicecount;
Serial.println("Scanning...");
devicecount = 0;
for (address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address < 16)
Serial.print("0");
Serial.print(address, HEX);
Serial.println(" !");
devicecount++;
}
else if (error == 4)
{
Serial.print("Unknown error at address 0x");
if (address < 16)
Serial.print("0");
Serial.println(address, HEX);
}
}
if (devicecount == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000); // wait 5 seconds for the next I2C scan
}
I uploaded the sketch, but…
No I2C devices found
I found the discussion on my class that there are some reasons for this. Either the sensor was broken, not soldered well, or the code. Then I found a comment by my course assistant to change the Wire.begin() to Wire.begin(21,22) as SDA was on 21 and SCL on 22. But still…
No I2C devices found
I tried to change the pin to 23 and 19 but still not working. I guess something was wrong with the sensor. Maybe it was not soldered well, or broken(?). I ordered the soldered one tho… :(
Final say
Sadly I couldn’t finish this project. But, I learned about BMP280, what supposed to be shown on the serial monitor, and the problem of my experiment. I will try a bit more to solve this problem. Let’s hope for the best!