Thanks so much for your comments.
So I bought the multiplexer TCA9548a
(Overview | Adafruit TCA9548A 1-to-8 I2C Multiplexer Breakout | Adafruit Learning System)
I ran a TCA script and the 2 LPS25H were detected.
So now I have this example code which they use for 2 acceleration sensors (same address).
Can someone help me understand what I need to change and how? Should I change the example code or can I maybe just add some pieces of the code to my LPS25H sensor code to make the multiplexer work?
I understand that in the Example Code I dont Need the adafruit_sensor and adafruit_HMC5883_U library. But how do I assign a unique address to each sensor for example…
Here is the LPS library (GitHub - pololu/lps-arduino: Arduino library for Pololu LPS25H and LPS331AP boards)
Here is the Adafruit_HMC5883_U.h library (https://github.com/adafruit/Adafruit_HMC5883_Unified/blob/master/Adafruit_HMC5883_U.h)
In the adafruit Forum I got this reply but dont know where I Need to put it. :
You do not need Adafruit_Sensor.h for the multiplexer. You just need to include the tcaselect function in your code. You can call that function in you loop() to select the channel for the sensor you want to read.
Code: Select all | TOGGLE FULL SIZE
void tcaselect(uint8_t i) {
if (i > 7) return;
Wire.beginTransmission(TCAADDR);
Wire.write(1 << i);
Wire.endTransmission();
}
I am so happy for any reply. Or help…thanks guys!
Example Code:
#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);
}
LPS25H Code:
#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();
}
void tcaselect(uint8_t i) {
if (i > 7) return;
Wire.beginTransmission(TCAADDR);
Wire.write(1 << i);
Wire.endTransmission();
}
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);
}