Hi,
I would like to use two color sensors together. Unfortunately they have the same I2C address.
-
My first idea was to use the SoftI2C library for one sensor and the hardware I2C for the other.
This would mean duplicating the GroveColorSensor library and adapt it to work with SoftI2C.
I think that the Wire library isn’t completely compatible with the SoftI2C library.
e.g.
How can I adapt GroveColorSensor::readRGB() function with SoftI2C library? -
My second idea was to use a MUX to connect the two color sensors.
Here I used this example from GitHub: mahfuz195/Pressure_sensor_BMP180
(for some reason I can’t post links.)
I used the same wiring, created two instances of GroveColorSensor and toggled between them using the select pin.
This will get me garbage values.
I tried with pull-up resistors on all I2C lines but that didn’t solve anything.
I posted the code bellow.
Neither of these ideas worked for me. What am I doing wrong?
Any support is greatly appreciated!
#include <Wire.h>
#include <GroveColorSensor.h>
#define SELECT_PIN 19
#define LeftColorSensorActive digitalWrite(SELECT_PIN,HIGH); //delay(100);
#define RightColorSensorActive digitalWrite(SELECT_PIN,LOW); //delay(100);
void setup()
{
Serial.begin(9600);
Wire.begin();
}
void loop()
{
GroveColorSensor LeftColorSensor;
GroveColorSensor RightColorSensor;
while(1)
{
Serial.println("StartUp");
RightColorSensorActive
Serial.println("LeftColorSensorActive");
LeftColorSensor.readRGBC();
LeftColorSensor.clearInterrupt();
LeftColorSensor.printRGBC();
delay(1000);
RightColorSensorActive
Serial.println("RightColorSensorActive");
LeftColorSensor.readRGBC();
LeftColorSensor.clearInterrupt();
LeftColorSensor.printRGBC();
delay(1000);
}
}
EDIT:
For the first idea, I managed to use non-I2C pins with reeedstudio/Arduino_SoftI2C (GitHub) and it’s working 80%.
The problem is that I get “-1” for some RGBC channel reads.
This library is almost fully compatible with the Wire library but there is a missing part - there is no corespondent for Wire.available().
This is needed in the function GroveColorSensor::readRGB() at
…
if(8 <= Wire.available())
…
I commented out this line when replacing Wire with SoftI2C.