I have an Arduino uno WiFi rev 2 and I'm trying to run a BME280 sensor on it.
I have it connected as follows:
VCC = 5V
GND = GND
SCL = A5
SDA = A4
The problem is that the sensor can never be found. So I tried to find the I2C address to which the given sensor is connected.
#include <Wire.h>
void setup() {
Serial.begin(9600);
Serial.println("\nI2C Scanner");
}
void loop() {
byte error, address;
int devices;
devices = 0;
for (address = 1; address < 127; 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(" !");
devices++;
}
}
if (devices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000);
}
the output is that a device is connected to all addresses:
19:35:09.816 -> I2C device found at address 0x66 !
19:35:09.848 -> I2C device found at address 0x67 !
19:35:09.880 -> I2C device found at address 0x68 !
19:35:09.914 -> I2C device found at address 0x69 !
19:35:09.945 -> I2C device found at address 0x6A !
19:35:10.009 -> I2C device found at address 0x6B !
19:35:10.046 -> I2C device found at address 0x6C !
19:35:10.077 -> I2C device found at address 0x6D !
19:35:10.108 -> I2C device found at address 0x6E !
19:35:10.140 -> I2C device found at address 0x6F !
19:35:10.173 -> I2C device found at address 0x70 !
19:35:10.237 -> I2C device found at address 0x71 !
19:35:10.269 -> I2C device found at address 0x72 !
19:35:10.300 -> I2C device found at address 0x73 !
19:35:10.333 -> I2C device found at address 0x74 !
19:35:10.365 -> I2C device found at address 0x75 !
19:35:10.432 -> I2C device found at address 0x76 !
19:35:10.464 -> I2C device found at address 0x77 !
19:35:10.498 -> I2C device found at address 0x78 !
19:35:10.528 -> I2C device found at address 0x79 !
19:35:10.559 -> I2C device found at address 0x7A !
19:35:10.623 -> I2C device found at address 0x7B !
19:35:10.656 -> I2C device found at address 0x7C !
19:35:10.688 -> I2C device found at address 0x7D !
19:35:10.719 -> I2C device found at address 0x7E !
19:35:10.753 -> done
19:35:10.785 ->
Thank you for all the information and the sketch between code tags
That helps a lot to give a short answer: Wire.begin() is missing in the first sketch.
The second sketch has Wire.begin() hidden in Scanner.Init() here.
/***************************************************************************
This is a library for the BME280 humidity, temperature & pressure sensor
Designed specifically to work with the Adafruit BME280 Breakout
----> http://www.adafruit.com/products/2650
These sensors use I2C or SPI to communicate, 2 or 4 pins are required
to interface. The device's I2C address is either 0x76 or 0x77.
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
See the LICENSE file for details.
***************************************************************************/
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
unsigned long delayTime;
void setup() {
Serial.begin(9600);
while(!Serial); // time to get serial running
Serial.println(F("BME280 test"));
unsigned status;
// default settings
status = bme.begin();
// You can also pass in a Wire library object like &Wire2
// status = bme.begin(0x76, &Wire2)
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
Serial.print("SensorID was: 0x"); Serial.println(bme.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("-- Default Test --");
delayTime = 1000;
Serial.println();
}
void loop() {
printValues();
delay(delayTime);
}
void printValues() {
Serial.print("Temperature = ");
Serial.print(bme.readTemperature());
Serial.println(" °C");
Serial.print("Pressure = ");
Serial.print(bme.readPressure() / 100.0F);
Serial.println(" hPa");
Serial.print("Approx. Altitude = ");
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println(" m");
Serial.print("Humidity = ");
Serial.print(bme.readHumidity());
Serial.println(" %");
Serial.println();
}
i recive this response
20:45:35.619 -> BME280 test
20:45:35.625 -> Could not find a valid BME280 sensor, check wiring, address, sensor ID!
20:45:35.689 -> SensorID was: 0x0
20:45:35.722 -> ID of 0xFF probably means a bad address, a BMP 180 or BMP 085
20:45:35.785 -> ID of 0x56-0x58 represents a BMP 280,
20:45:35.817 -> ID of 0x60 represents a BME 280.
20:45:35.850 -> ID of 0x61 represents a BME 680.
But my question is still why I2C can fund some sensor, even though nothing is connected to the arduino
I noticed that the arduino I used - Arduino uno Wifi rev 2 have SDA and SCL pins not on A4 and A5 but it has specially designed pins for that, see picture: (right up PA3 and PA2)
So i connect that BPE280 sensor to that pins and also on +5 and GND and use this code:
/***************************************************************************
This is a library for the BME280 humidity, temperature & pressure sensor
Designed specifically to work with the Adafruit BME280 Breakout
----> http://www.adafruit.com/products/2650
These sensors use I2C or SPI to communicate, 2 or 4 pins are required
to interface. The device's I2C address is either 0x76 or 0x77.
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
See the LICENSE file for details.
***************************************************************************/
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
unsigned long delayTime;
void setup() {
Serial.begin(9600);
while(!Serial); // time to get serial running
Serial.println(F("BME280 test"));
unsigned status;
// default settings
status = bme.begin(0x76);
// You can also pass in a Wire library object like &Wire2
// status = bme.begin(0x76, &Wire2)
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
Serial.print("SensorID was: 0x"); Serial.println(bme.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("-- Default Test --");
delayTime = 1000;
Serial.println();
}
void loop() {
printValues();
delay(delayTime);
}
void printValues() {
Serial.print("Temperature = ");
Serial.print(bme.readTemperature());
Serial.println(" °C");
Serial.print("Pressure = ");
Serial.print(bme.readPressure() / 100.0F);
Serial.println(" hPa");
Serial.print("Approx. Altitude = ");
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println(" m");
Serial.print("Humidity = ");
Serial.print(bme.readHumidity());
Serial.println(" %");
Serial.println();
}