change I2C address

I am trying to get a Gy-21P sensor working with this sketch:

/* .. hook up sensor .... SCL - A5 .... SDA - A4 ... */

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include "Adafruit_Si7021.h"

Adafruit_BMP280 bme; // I2C
Adafruit_Si7021 sensor = Adafruit_Si7021();

void setup()
{
Serial.begin(9600);
Serial.println("BMP280 and SI7021 (GY-21p) test");

if (!bme.begin())
{
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while (1);
}

if (!sensor.begin())
{
Serial.println("Did not find Si7021 sensor!");
while (true);
}
}

void loop()
{
Serial.println("BMP280 results");
Serial.print("Temperature = ");
Serial.print(bme.readTemperature());
Serial.println(" *C");
Serial.print("Pressure = ");
Serial.print(bme.readPressure());
Serial.println(" Pa");
Serial.print("Approx altitude = ");
Serial.print(bme.readAltitude(1013.25)); // this should be adjusted to your local forcase
Serial.println(" m");
Serial.println();

Serial.println("SI7021 results");
Serial.print("Humidity: ");
Serial.println(sensor.readHumidity(), 2);
Serial.print("Temperature: ");
Serial.println(sensor.readTemperature(), 2);
Serial.println();
delay(2000);
}

It uploads ok but the output is just:

Could not find a valid BMP280 sensor, check wiring!

The I2C address finder finds (0x40) ..... I think that's the Si7021
and .......................................(0x76) ...... that must be the BMP280

I read somewhere that <Adafruit_BMP280.h> is looking for 0x77 ..... but I don't know how (or where) to change it.
If I change the address it's looking for should I rename the file xxxxx.h and then #include xxxxx.h so I leave the original
<Adafruit_BMP280> alone?

BTW Si7021.c (attached) works. I don't know as much as I'd like about the mechanics of how these include things work

SI7021.c (1.49 KB)

Show us the wiring, please. A wiring diagram and some photos would be ideal. Did you use pullup resistors?

Read "How To Use This Forum"

in particular, 7. If you are posting code or error messages, use "code" tags

This is what happens when you do not

if you have a module that is not working, do not say "A GPS" or "the fingerprint sensor". Show us a link to the particular sensor, and a link to the datasheet if available

ok ....... code tags ...... here's a link to the sensors I bought on ebay ... DIYMALL Gy-21p Atmospheric Humidity Temperature Sensor Breakout Bmp280 Si7021 for sale online | eBay

the GY-21P has both BMP280 and Si7021 sensors .... The code (attached) for the Si7021 works without pullup resitors.
So I thought the code tagged below should work too.

I also noticed that GY-21P is 3.3 volts ..... ooops, I had it hooked up to +5. I tried 3.3v same result .. the code below
doesn't work but the attached code for Si7021 works 3.3 or 5 volts.

I really think the code below would work if I knew how to change the I2C address in <BMP280.h> .... I have some
bi-directional level shifters on order so I will wait for them before I go much further but I would still like to know how
I might change an I2C address in the header file

Sensor Nano
The wiring is simple Vin ......... + 3.3 or 5 volts
Gnd ...... Gnd
SCL ...... A5
SDA ...... A4

/* .. hook up sensor .... SCL - A5 .... SDA - A4 ... */

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include "Adafruit_Si7021.h"
 
Adafruit_BMP280 bme; // I2C
Adafruit_Si7021 sensor = Adafruit_Si7021();
 
void setup() 
{
Serial.begin(9600);
Serial.println("BMP280 and SI7021 (GY-21p) test");
 
if (!bme.begin()) 
{ 
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while (1);
}
 
if (!sensor.begin()) 
{
Serial.println("Did not find Si7021 sensor!");
while (true);
}
}
 
void loop() 
{
Serial.println("BMP280 results");
Serial.print("Temperature = ");
Serial.print(bme.readTemperature());
Serial.println(" *C");
Serial.print("Pressure = ");
Serial.print(bme.readPressure());
Serial.println(" Pa");
Serial.print("Approx altitude = ");
Serial.print(bme.readAltitude(1013.25)); // this should be adjusted to your local forcase
Serial.println(" m");
Serial.println();
 
Serial.println("SI7021 results");
Serial.print("Humidity: "); 
Serial.println(sensor.readHumidity(), 2);
Serial.print("Temperature: "); 
Serial.println(sensor.readTemperature(), 2);
Serial.println();
delay(2000);
}

SI7021.c (1.49 KB)

I don't know how to change the subject line to solved but it is

I found the file to modify @ /home/sam/Arduino/libraries/Adafruit_BMPLibrary .......

Here is the relevant part of Adafruit_BMP280.h


/*!

  • I2C ADDRESS/BITS/SETTINGS
    */
    #define BMP280_ADDRESS (0x77) /< The default I2C address for the sensor. */
    #define BMP280_ADDRESS_ALT
    (0x76) /
    < Alternative I2C address for the sensor. */
    #define BMP280_CHIPID (0x58) /**< Default chip ID. */

I changed where it says (0x77) to (0x76)
and (0x76) to (Ox77)

I didn't use level shifters 'cause I don't have them yet .... not sure if that will be a problem later but it works as is

I also put in 4K7 pullup resistors to 3v3
so ................

Sensor Nano
Vin 3v3
Gnd Gnd
SCL A5 (4K7 pullup to 3v3)
SDA A4 (4K7 pullup to 3v3)

I think I can figure out how to print the data to a 4 line lcd .... I will also make the GY-21P the only thing on the I2C
just cause I don't know how to mix 3v3 and 5v on that bus

Maybe this will help someone else ... They were cool sensors accurate and cheap. Thanks to all who read my post

When it is a BME280, then please use the BME280 library.
When it is a BMP280, then please use 'bmp' in the sketch. I'm getting confused.

The bme/p.begin() uses the default address when nothing is specified. You can set the alternative address with:

if (!bme.begin(BMP280_ADDRESS_ALT))
{

If you still get an error, then it might be the BME280 or vice versa. The ID of the chip is checked.

On that module is a voltage regulator and level shifters for SDA and SCL and pullup resistors of 10k on both sides of the level shifter. You can use it with 5V to the VIN pin. I think you should apply 5V and let the level shifters do their job. The description is wrong anyway, because it can not be used with SPI.
Can you check the voltages of the chip ? They should not exceed 3.3V. If one of them is 3.5V or higher then something is wrong.

Don't add another extra level shifter module, that weakens the signal.

Sorry, I was not clear on this, I did not write the original sketch. I found it somewhere, I forget now where.

If I understand you correctly .......

1.) The Gy-21P already has level shifters and 10K pullups so I don't need the 4K7 resistors
2.) I can wire it as I originally did, Vin on the sensor can go to 5v
3.) I can change the line ....... if(!bme.begin()) ...... in the sketch to

if (!bme.begin(BMP280_ADDRESS_ALT))

and that will cause the sketch to look for the BMP280 sensor at (0x76) so no need to modify the header file)?

I installed the arduino ide on a different computer and modified the Adafruit_BMP.h file there because changing it on the computer I normally use for this stuff seemed a bad idea and because months from now I could forget I'd done that. I'm more likely to remember I modified the spare computer .h file.

I will try the ...... if (!bme.begin(BMP280_ADDRESS_ALT)) ..... idea when I get a chance, I'd bet it will work

Thanks

When using a library, you can start with the examples that come with that library.
This is an example for the Adafruit BMP280 library: Adafruit_BMP280_Library/bmp280test.ino at master · adafruit/Adafruit_BMP280_Library · GitHub.

This is the for function .begin(): Adafruit_BMP280_Library/Adafruit_BMP280.h at master · adafruit/Adafruit_BMP280_Library · GitHub.

bool begin(uint8_t addr = BMP280_ADDRESS, uint8_t chipid = BMP280_CHIPID);

The parameters 'addr' and 'chipid' are already set to 'BMP280_ADDRESS' and 'BMP280_CHIPID'. If you don't use the parameters, then those default arguments will be used. All the examples for .begin() without parameters have actually two parameters.

I think the official name is "default arguments": Default arguments - cppreference.com.

  1. Correct. The onboard level shifter have 10k on both sides, that makes a combined pullup of 5k.
  2. Yes.
  3. Yes.

Look at the bottom:


The component with "103" are four 10k pullup resistors.
The black smd component with three pins and "662" is the voltage regulator for 3.3V.
The black smd component with 6 pins are mosfets for the level shifting of SDA and SCL.