How to use different analog pins as SDA and SCL?

Hello.
I am making a project using various sensors, two of them using SDA and SCL pins that I have to connect to A4 and A5 pins respectively.
When I upload the code, I guess the data gets mixed up due to them sharing the two pins and thus the information doesn't show up.
I would like to know if there is any way to change the Analog pins of one sensor (for example, sensor1 uses pins A4 and A5 for SDA and SCL; and sensor2 uses pins A3 and A2.

Here is the code I have:

#include <BH1750FVI.h>
#include <Wire.h>
#include <Adafruit_BMP085.h>
#define presionNivelMar_hPa 1013.25
BH1750FVI LightSensor(BH1750FVI::k_DevModeContLowRes);
Adafruit_BMP085 bmp;

void setup() {
Serial.begin(9600);
LightSensor.begin();
}

void loop() {
uint16_t lux = LightSensor.GetLightIntensity();
Serial.print("Light: ");
Serial.print(lux);
Serial.println(" lx");
Serial.print("Barometric pression: ");
Serial.print(bmp.readPressure());
Serial.println(" Pa");
Serial.println();
}

All the I2C sensors can use A4 and A5. You might use a breadboard to expand the number of pins I2C is available on.

Here is an ESP32 plugged into a breadboard with several devices using the SPI buss. To the left of the ESP32 is a bundle of connected wires where the SPI buss is brought out from the ESP32 to a spot on the breadboard, where multiple SPI devices are connected.

The same can be done for I2C.

OK so far. All of the I2C devices are connected to SDA and SCL

Now you have gone wrong

Each I2C device has an address and will only put data onto the bus to be read when it is requested. It is possible that your two I2C devices have the same address. To find their address have you tried running an I2C scanner sketch ? There is one included with the Wire library

The Arduino I2C bus.

The two addresses are different. I have also tried connecting SCL and SDA to the SCL and SDA pins on the UNO board, but the same problem appears:

I have also found that by eliminating this line it functions alright, but of course I don't get the one of the pieces of information I need.

Serial.print(bmp.readPressure());

To do what you want sorta defeats the purpose of the i2c bus orientated scheme.

Did you miss @UKHeliBob’s suggestion?

have you tried running an I2C scanner sketch ? There is one included with the Wire library

Also, where are any pull-up resistors on your bus lines? It may be a source of trouble when attaching multiple devices that may all have their own.

a7

Where is the BMP start thingy?

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

Most of the adafruity thingies do a something.begin.

adafruit/Adafruit-BMP085-Library: A powerful but easy to use BMP085/BMP180 Library (github.com)

From the Adafruit library example:

void setup() {
  Serial.begin(9600);
  if (!bmp.begin()) {
	Serial.println("Could not find a valid BMP085 sensor, check wiring!");
	while (1) {}
  }
}

I tried includeing bmp.begin(); and now it works. I guess that was why it wasn't working. The !bme.begin I have eliminated because I don't need it for the project. Thanks!

1 Like

mark this as solved?

Affirmative.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.