Help with 2 LPS25H sensors and coding (arduino uno)

**Hi guys :slight_smile: **
**I need help. I am working with Arduino uno. **
**and I bought 2 LPS25H sensors (https://www.pololu.com/product/2724). **
The first sensor was so easy to read out, so I was so happy.
**But when I wanted to read both out at the same time, I had a Problem because they have the same address. So I bought the multiplexer (TCA9548A) it seemed straight forward. **
There is even a very nice guide in whih they also used 2 sensors with the same address. That seemed to solve my Problem (https://learn.adafruit.com/adafruit-tca9548a-1-to-8-i2c-multiplexer-breakout/wiring-and-test) but of Course…they are using no pressure sensors.

So now I am stuck. And really need someones help. Because I have no clue about the LPS.h library which the LPS25H is using. I will post the two codes I have.
So the Code they used to read out the acceleration sensors is this:

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>
 
#define TCAADDR 0x70
 
/* Assign a unique ID to this sensor at the same time */
Adafruit_HMC5883_Unified mag1 = Adafruit_HMC5883_Unified(1);
Adafruit_HMC5883_Unified mag2 = Adafruit_HMC5883_Unified(2);
 
void displaySensorDetails(Adafruit_HMC5883_Unified *mag)
{
  sensor_t sensor;
  mag->getSensor(&sensor);
  Serial.println("------------------------------------");
  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" uT");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" uT");
  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" uT");  
  Serial.println("------------------------------------");
  Serial.println("");
  delay(500);
}
 
void tcaselect(uint8_t i) {
  if (i > 7) return;
 
  Wire.beginTransmission(TCAADDR);
  Wire.write(1 << i);
  Wire.endTransmission();  
}
 
 
void setup(void) 
{
  Serial.begin(9600);
  Serial.println("HMC5883 Magnetometer Test"); Serial.println("");
  
  /* Initialise the 1st sensor */
  tcaselect(2);
  if(!mag1.begin())
  {
    /* There was a problem detecting the HMC5883 ... check your connections */
    Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
    while(1);
  }
  
  /* Initialise the 2nd sensor */
  tcaselect(6);
  if(!mag2.begin())
  {
    /* There was a problem detecting the HMC5883 ... check your connections */
    Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
    while(1);
  }
  
  /* Display some basic information on this sensor */
  tcaselect(2);
  displaySensorDetails(&mag1);
  tcaselect(6);
  displaySensorDetails(&mag2);
}
 
void loop(void) 
{
  /* Get a new sensor event */ 
  sensors_event_t event; 
  
  tcaselect(2);
  mag1.getEvent(&event);
 
  /* Display the results (magnetic vector values are in micro-Tesla (uT)) */
  Serial.print("Sensor #1 - ");
  Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print("  ");
  Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print("  ");
  Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print("  ");Serial.println("uT");
  
  tcaselect(6);
  mag2.getEvent(&event);
  /* Display the results (magnetic vector values are in micro-Tesla (uT)) */
  Serial.print("Sensor #2 - ");
  Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print("  ");
  Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print("  ");
  Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print("  ");Serial.println("uT");
  
  delay(500);
}

**And from the Adafruit Forum one person replied and told me I need to include the tcaselect function in my code. **
–>You can call that function in you loop() to select the channel for the sensor you want to read.

        void tcaselect(uint8_t i) {
          if (i > 7) return;
         
          Wire.beginTransmission(TCAADDR);
          Wire.write(1 << i);
          Wire.endTransmission();  

The Code which is used for the LPS25H sensors is the following:

#include <Wire.h>
#include <LPS.h>

LPS ps;

void setup()
{
  Serial.begin(9600);
  Wire.begin();

  if (!ps.init())
  {
    Serial.println("Failed to autodetect pressure sensor!");
    while (1);
  }

  ps.enableDefault();
}
SerialMetric
void loop()
{
  float pressure = ps.readPressureMillibars();
  float altitude = ps.pressureToAltitudeMeters(pressure);
  float temperature = ps.readTemperatureC();
 
  Serial.print("p: ");
  Serial.print(pressure);
  Serial.print(" mbar\ta: ");
  Serial.print(altitude);
  Serial.print(" m\tt: ");
  Serial.print(temperature);
  Serial.println(" deg C");

  delay(100);
}

I really dont know how to Combine them. Can please someone help. I would be so happy!!!
Thanks so much for all your help.

Hello.

We cannot help you use your Adafruit I2C multiplexer. However, you should not need it, because you can use two LPS25H sensors on the same I2C bus. To do so, you will have to change the device ID on one of the sensors, which can be done by driving the SDO/SA0 pin on one of your LPS25H low (i.e. connecting it to ground). Then, using our LPS Arduino library, you can create two LPS objects, one for each of the two configurations of the SA0 pin. The code to set that up looks like:

#include <Wire.h>
#include <LPS.h>

LPS ps1;
LPS ps2;

void setup()
{
 Serial.begin(9600);
 Wire.begin();
 ps1.init(LPS::device_25H, LPS::sa0_high);
 ps2.init(LPS::device_25H, LPS::sa0_low);
 ps1.enableDefault();
 ps2.enableDefault();
}

void loop()
{
 float pressure1 = ps1.readPressureMillibars();
 float altitude1 = ps1.pressureToAltitudeMeters(pressure1);
 float temperature1 = ps1.readTemperatureC();
 
 float pressure2 = ps2.readPressureMillibars();
 float altitude2 = ps2.pressureToAltitudeMeters(pressure2);
 float temperature2 = ps2.readTemperatureC();

 /*
 your own code to deal with the data goes here
 */
}

-Jon

Wow thanks Jonathan,

I already drove the sdo pin low and got the 2 different addresses but just didnt know what to do with the code.
What should I do if I need more than 2 sensors with the addresses?

Thanks so much for all your help already

The LPS25H only allows for two different slave addresses, so to communicate with more than two of those sensors over I2C, you would need to use something like that Adafruit I2C multiplexer that you mentioned in your original post (or use a microcontroller with more than one I2C interface).

-Jon