I am using the TCA9548A I2C multiplexer with two of the same sensor (MAG3110). I am following this 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);
}
But, I keep getting errors. Mainly, my code doesn't like that I'm putting an argument in MAG3110() and mag.initialize(), and so on. I'm not sure why it's a problem here but it's fine with the example code. Below is my code.
#include <SparkFun_MAG3110.h>
MAG3110 mag = MAG3110(0); //Instantiate MAG3110
MAG3110 mag1 = MAG3110(1);
void tcaselect(uint8_t i) {
if (i > 7) return;
Wire.beginTransmission(0x70);
Wire.write(1 << i);
Wire.endTransmission();
}
void setup() {
Serial.begin(9600);
mag.initialize(0); //Initializes the mag sensor
mag.start(0); //Puts the sensor in active mode
mag1.initialize(1);
mag1.start(1);
}
void loop() {
int x, y, z;
//Only read data when it's ready
tcaselect(0);
if(mag.dataReady(0)) {
//Read the data
mag.readMag(&x, &y, &z);
Serial.print("X: ");
Serial.print(x);
Serial.print(", Y: ");
Serial.print(y);
Serial.print(", Z: ");
Serial.println(z);
Serial.println("--------");
tcaselect(1);
if(mag1.dataReady(1)) {
//Read the data
mag1.readMag(&x, &y, &z);
Serial.print("X: ");
Serial.print(x);
Serial.print(", Y: ");
Serial.print(y);
Serial.print(", Z: ");
Serial.println(z);
Serial.println("--------");
delay(200);
}
}
The library is included. The code I am using is the second code, I am just referencing the top code to set up the multiplexer.
Here are the error messages:
sketch_jul18d:4: error: no matching function for call to 'MAG3110::MAG3110(int)'
MAG3110 mag = MAG3110(0); //Instantiate MAG3110
^
C:\Users\slb0494\Documents\Arduino\BS\sketch_jul18d\sketch_jul18d.ino:4:24: note: candidates are:
In file included from C:\Users\slb0494\Documents\Arduino\BS\sketch_jul18d\sketch_jul18d.ino:1:0:
C:\Users\slb0494\Documents\Arduino\libraries\SparkFun_MAG3110_Breakout_Board_Arduino_Library-master\src/SparkFun_MAG3110.h:124:3: note: MAG3110::MAG3110()
MAG3110();
^
C:\Users\slb0494\Documents\Arduino\libraries\SparkFun_MAG3110_Breakout_Board_Arduino_Library-master\src/SparkFun_MAG3110.h:124:3: note: candidate expects 0 arguments, 1 provided
C:\Users\slb0494\Documents\Arduino\libraries\SparkFun_MAG3110_Breakout_Board_Arduino_Library-master\src/SparkFun_MAG3110.h:121:7: note: constexpr MAG3110::MAG3110(const MAG3110&)
class MAG3110
^
C:\Users\slb0494\Documents\Arduino\libraries\SparkFun_MAG3110_Breakout_Board_Arduino_Library-master\src/SparkFun_MAG3110.h:121:7: note: no known conversion for argument 1 from 'int' to 'const MAG3110&'
C:\Users\slb0494\Documents\Arduino\libraries\SparkFun_MAG3110_Breakout_Board_Arduino_Library-master\src/SparkFun_MAG3110.h:121:7: note: constexpr MAG3110::MAG3110(MAG3110&&)
C:\Users\slb0494\Documents\Arduino\libraries\SparkFun_MAG3110_Breakout_Board_Arduino_Library-master\src/SparkFun_MAG3110.h:121:7: note: no known conversion for argument 1 from 'int' to 'MAG3110&&'
sketch_jul18d:5: error: no matching function for call to 'MAG3110::MAG3110(int)'
MAG3110 mag1 = MAG3110(1);
^
C:\Users\slb0494\Documents\Arduino\BS\sketch_jul18d\sketch_jul18d.ino:5:25: note: candidates are:
In file included from C:\Users\slb0494\Documents\Arduino\BS\sketch_jul18d\sketch_jul18d.ino:1:0:
C:\Users\slb0494\Documents\Arduino\libraries\SparkFun_MAG3110_Breakout_Board_Arduino_Library-master\src/SparkFun_MAG3110.h:124:3: note: MAG3110::MAG3110()
MAG3110();
^
C:\Users\slb0494\Documents\Arduino\libraries\SparkFun_MAG3110_Breakout_Board_Arduino_Library-master\src/SparkFun_MAG3110.h:124:3: note: candidate expects 0 arguments, 1 provided
C:\Users\slb0494\Documents\Arduino\libraries\SparkFun_MAG3110_Breakout_Board_Arduino_Library-master\src/SparkFun_MAG3110.h:121:7: note: constexpr MAG3110::MAG3110(const MAG3110&)
class MAG3110
^
C:\Users\slb0494\Documents\Arduino\libraries\SparkFun_MAG3110_Breakout_Board_Arduino_Library-master\src/SparkFun_MAG3110.h:121:7: note: no known conversion for argument 1 from 'int' to 'const MAG3110&'
C:\Users\slb0494\Documents\Arduino\libraries\SparkFun_MAG3110_Breakout_Board_Arduino_Library-master\src/SparkFun_MAG3110.h:121:7: note: constexpr MAG3110::MAG3110(MAG3110&&)
C:\Users\slb0494\Documents\Arduino\libraries\SparkFun_MAG3110_Breakout_Board_Arduino_Library-master\src/SparkFun_MAG3110.h:121:7: note: no known conversion for argument 1 from 'int' to 'MAG3110&&'
C:\Users\slb0494\Documents\Arduino\BS\sketch_jul18d\sketch_jul18d.ino: In function 'void setup()':
sketch_jul18d:18: error: no matching function for call to 'MAG3110::initialize(int)'
mag.initialize(0); //Initializes the mag sensor
^
C:\Users\slb0494\Documents\Arduino\BS\sketch_jul18d\sketch_jul18d.ino:18:19: note: candidate is:
In file included from C:\Users\slb0494\Documents\Arduino\BS\sketch_jul18d\sketch_jul18d.ino:1:0:
C:\Users\slb0494\Documents\Arduino\libraries\SparkFun_MAG3110_Breakout_Board_Arduino_Library-master\src/SparkFun_MAG3110.h:126:8: note: void MAG3110::initialize()
void initialize();
^
C:\Users\slb0494\Documents\Arduino\libraries\SparkFun_MAG3110_Breakout_Board_Arduino_Library-master\src/SparkFun_MAG3110.h:126:8: note: candidate expects 0 arguments, 1 provided
sketch_jul18d:19: error: no matching function for call to 'MAG3110::start(int)'
mag.start(0); //Puts the sensor in active mode
^
C:\Users\slb0494\Documents\Arduino\BS\sketch_jul18d\sketch_jul18d.ino:19:14: note: candidate is:
In file included from C:\Users\slb0494\Documents\Arduino\BS\sketch_jul18d\sketch_jul18d.ino:1:0:
C:\Users\slb0494\Documents\Arduino\libraries\SparkFun_MAG3110_Breakout_Board_Arduino_Library-master\src/SparkFun_MAG3110.h:145:8: note: void MAG3110::start()
void start();
^
C:\Users\slb0494\Documents\Arduino\libraries\SparkFun_MAG3110_Breakout_Board_Arduino_Library-master\src/SparkFun_MAG3110.h:145:8: note: candidate expects 0 arguments, 1 provided
sketch_jul18d:20: error: no matching function for call to 'MAG3110::initialize(int)'
mag1.initialize(1);
^
C:\Users\slb0494\Documents\Arduino\BS\sketch_jul18d\sketch_jul18d.ino:20:20: note: candidate is:
In file included from C:\Users\slb0494\Documents\Arduino\BS\sketch_jul18d\sketch_jul18d.ino:1:0:
C:\Users\slb0494\Documents\Arduino\libraries\SparkFun_MAG3110_Breakout_Board_Arduino_Library-master\src/SparkFun_MAG3110.h:126:8: note: void MAG3110::initialize()
void initialize();
^
C:\Users\slb0494\Documents\Arduino\libraries\SparkFun_MAG3110_Breakout_Board_Arduino_Library-master\src/SparkFun_MAG3110.h:126:8: note: candidate expects 0 arguments, 1 provided
sketch_jul18d:21: error: no matching function for call to 'MAG3110::start(int)'
mag1.start(1);
^
C:\Users\slb0494\Documents\Arduino\BS\sketch_jul18d\sketch_jul18d.ino:21:15: note: candidate is:
In file included from C:\Users\slb0494\Documents\Arduino\BS\sketch_jul18d\sketch_jul18d.ino:1:0:
C:\Users\slb0494\Documents\Arduino\libraries\SparkFun_MAG3110_Breakout_Board_Arduino_Library-master\src/SparkFun_MAG3110.h:145:8: note: void MAG3110::start()
void start();
^
C:\Users\slb0494\Documents\Arduino\libraries\SparkFun_MAG3110_Breakout_Board_Arduino_Library-master\src/SparkFun_MAG3110.h:145:8: note: candidate expects 0 arguments, 1 provided
C:\Users\slb0494\Documents\Arduino\BS\sketch_jul18d\sketch_jul18d.ino: In function 'void loop()':
sketch_jul18d:29: error: no matching function for call to 'MAG3110::dataReady(int)'
if(mag.dataReady(0)) {
^
C:\Users\slb0494\Documents\Arduino\BS\sketch_jul18d\sketch_jul18d.ino:29:21: note: candidate is:
In file included from C:\Users\slb0494\Documents\Arduino\BS\sketch_jul18d\sketch_jul18d.ino:1:0:
C:\Users\slb0494\Documents\Arduino\libraries\SparkFun_MAG3110_Breakout_Board_Arduino_Library-master\src/SparkFun_MAG3110.h:132:8: note: bool MAG3110::dataReady()
bool dataReady();
^
C:\Users\slb0494\Documents\Arduino\libraries\SparkFun_MAG3110_Breakout_Board_Arduino_Library-master\src/SparkFun_MAG3110.h:132:8: note: candidate expects 0 arguments, 1 provided
sketch_jul18d:43: error: no matching function for call to 'MAG3110::dataReady(int)'
if(mag1.dataReady(1)) {
^
C:\Users\slb0494\Documents\Arduino\BS\sketch_jul18d\sketch_jul18d.ino:43:26: note: candidate is:
In file included from C:\Users\slb0494\Documents\Arduino\BS\sketch_jul18d\sketch_jul18d.ino:1:0:
C:\Users\slb0494\Documents\Arduino\libraries\SparkFun_MAG3110_Breakout_Board_Arduino_Library-master\src/SparkFun_MAG3110.h:132:8: note: bool MAG3110::dataReady()
bool dataReady();
^
C:\Users\slb0494\Documents\Arduino\libraries\SparkFun_MAG3110_Breakout_Board_Arduino_Library-master\src/SparkFun_MAG3110.h:132:8: note: candidate expects 0 arguments, 1 provided
sketch_jul18d:57: error: expected '}' at end of input
}
^
Using library SparkFun_MAG3110_Breakout_Board_Arduino_Library-master at version 1.0.0 in folder: C:\Users\slb0494\Documents\Arduino\libraries\SparkFun_MAG3110_Breakout_Board_Arduino_Library-master
Using library Wire at version 1.0 in folder: C:\Users\slb0494\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.6.19\libraries\Wire
exit status 1
no matching function for call to 'MAG3110::MAG3110(int)'
What are the 0 and 1 supposed to be? The compiler says that constructor doesn't take any arguments. It has no code to do anything with the 0 or the 1. So what are intending to happen with them?
I don't know entirely what their purpose is, I assumed it was just to differentiate between the two sensors. I was essentially just trying to follow exactly what they did in the example code(1st code I posted) and impliment that into my code(2nd code I posted), but I really don't know what a lot of it actually means.
Yes, it is, but I assumed that the multiplexer portion of the code would be applicable regardless of the library being used, because it is an entirely different device. I have scoured the internet and have been unable to find any examples utilizing the exact sensor I'm using. Implementing the multiplexer should be the same regardless, though (as long as the sensor uses I2C communication). Can you offer any help on coding for the multiplexer itself?
septillion:
Multiplexer is separate device and needs separate control. But if you want to control your sensor you can't just mix and match code and libraries...
And your sensor and sensor library no nothing about the I2C multiplexer. That's all up to you
So grab the library you like for the sensor. Make the code for the I2C multiplexer (or grab a library if available) and try again
Making the code for the multiplexer is what I'm struggling with, I'm aware that it has nothing to do with the sensor library. The code and library I have for my sensor work fine if I'm only trying to use one sensor, but I'm struggling with the multiplexer implementation for multiple sensors.