I'm trying to use 5 color sensors at the same time, but I'm not sure about the wiring / setup. My wiring looks like this:
All S2 pins from the sensors multiplexed and connectes to PIN 12
All S3 pins from the sensors multiplexed and connectes to PIN 13
All OUT pins from the sensors multiplexed and connectes to PIN 5
All S0-S3 of the multiplexers are connected to PIN 1-4
With my code / setup the values of the sensors seem to depend on each other - if I hold a white card in front of reader0, the values of all other readers change accordingly (and somewhat randomly).
This is the code that I'm trying to use:
#include <MD_TCS230.h>
#include <FreqCount.h>
// Pin definitions
#define COLOR_S2 12 // S2 Pins from ColorSensors bundled here
#define COLOR_S3 13 // S3 Pins from ColorSensors bundled here
#define COLOR_OE 8 // OE Pins from ColorSensors bundled here
#define COLOR_OUT 5 // Out-Pins from ColorSensors bundled here
#define M_OUT_0 1 // Controll Pins S0 from Multiplexers
#define M_OUT_1 2 // Controll Pins S1 from Multiplexers
#define M_OUT_2 3 // Controll Pins S2 from Multiplexers
#define M_OUT_3 4 // Controll Pins S3 from Multiplexers
const int nrSensors = 5;
MD_TCS230 CS(COLOR_S2, COLOR_S3, COLOR_OE);
void setup() {
Serial.begin(9600);
pinMode(M_OUT_0, OUTPUT);
pinMode(M_OUT_1, OUTPUT);
pinMode(M_OUT_2, OUTPUT);
pinMode(M_OUT_3, OUTPUT);
CS.begin();
}
void loop() {
readSensors();
}
void readSensors()
{
for (int i = 0; i < nrSensors; i++) {
digitalWrite(M_OUT_0, bitRead(i, 0));
digitalWrite(M_OUT_1, bitRead(i, 1));
digitalWrite(M_OUT_2, bitRead(i, 2));
digitalWrite(M_OUT_3, bitRead(i, 3));
static bool waiting;
if (!waiting)
{
CS.read();
waiting = true;
}
else
{
if (CS.available())
{
colorData rgb;
CS.getRGB(&rgb);
Serial.print("Sensor ");
Serial.print(i);
Serial.print("RGB [");
Serial.print(rgb.value[TCS230_RGB_R]);
Serial.print(",");
Serial.print(rgb.value[TCS230_RGB_G]);
Serial.print(",");
Serial.print(rgb.value[TCS230_RGB_B]);
Serial.println("]");
waiting = false;
delay(50);
}
}
}
}
I am not sure what to do with the OE pins of the sensors and would appreciate any help.
I tried connecting all OE pins to GND and used MD_TCS230 CS(COLOR_S2, COLOR_S3). I also tried connecting all OE pins to PIN 8, using MD_TCS230 CS(COLOR_S2, COLOR_S3, COLOR_OE); or even multiplexing them - nothing worked. I guess this only shows, how little I understand, what the OE pins are there for.
If anyone could point me in the right direction, I'd appreciate that very much.
Thanks in advance!
The first thing is to test that the devices work on their own. This verifies the setup with respect to pins and frequency detection. If that does not work then the mux setup will not work also.
The OE is an active low input to the sensor that enables the output from the sensor. From the datasheet "Output enable (OE) places the output in the high-impedance state for multiple-unit sharing of a microcontroller input line." In other words, the OE will enable the frequency output of the sensor so that many sensors can be connected to the same microcontroller input. The high impedance state allows the outputs to be connected together without being damaged.
Your code can turn off the current OE (set the digital output to HIGH) and then turn on the output from the next sensor (digital output to LOW), take a reading, and flow on down the line. This implies there is no need for mux at all.
Agreed, according to my reading of the data sheet.
In the absence of the mux, you need one Arduino output pin for each sensor, to control the sensor's /OE line. The connections to S0-3, and OUT can be shared among the sensors.
(I've been planning for some time on experimenting with this sensor -- note that in order to avoid confusion due to 700-1100nm IR radiation, you need an external filter. The data sheet suggests using a Hoya CM500.)
Thanks a bunch for the input, guys! I'm actually relieved to hear that it should be possible without MUX.
I wired everything up as jremington suggested: S2, S3 and OUT are shared among the sensors (PIN12, PIN13 and PIN 5). I gave every reader its own output pin (PIN7 and PIN8 in my case).
I then wrote some very crappy code and tested it with 2 sensors.
When I check the LEDs of the sensors, I can see that they are clearly switched on and off alternately, which looks like the setup is working. However, as far as the values go, the sensors are still active. For instance, when I switch sensor0 off via OE, and sensor1 on, I still only get readings from sensor0.
When I only cover sensor1, nothing happens.
When I cover sensor0, the values drop down halfway.
When I cover sensor0 and then also sensor1, the values drop down all the way to black (0,0,0).
I thoght I cut the tracks on the PCB correctly, but they are very hard to spot. I have access to a multimeter tomorrow so I'll go check the connection between OE and GND and see if maybe that's the problem.
It turns out, I didn't cut the track at all. When I used the multimeter, it still showed 0 between GND and OE. It's so hard to see where the tracks are, so I'll just wait for a friend of mine who's better with electronics - maybe he can find the right track.
So I checked my sensors with a mutlimeter. OE and GND showed 0 at first. After I cut the right track, I got some resistance, so I'm guessing it was the right one. cut track
I checkd my sensors individually and as long as I only use one, the setup works okay. I'm not able to disable the sensor via OE set to HIGH though.
With two sensors I get the same result as before: I cannot enable / disable any of the two sensors and the values depend on each other. It's like I have one big sensor consisting of two smaller ones.
I think you need to be careful to understand why you needed to cut the track. This was explained in the PDF for the library but I'll repeat it here.
Some of the breakouts connect the sensor's /OE pin directly to ground. This is to ensure that the sensor is enabled ON when it is powered. What this means, however, is that if you try and control the pin from the breakout edge connector (ie, set to HIGH), you effectively short the Arduino output pin (set HIGH) to ground, which shorts the power supply and shuts it down. The solution is to cut the sensor \OE to ground connection.
Other sensor breakout board properly pull the pin to ground using a resistor. In this case there should be no problem leaving it as it is - the resistor prevents the power supply short.
To decide which you have, you need to measure the resistance from the ground pin edge connector to the /OE pin edge connector. If there is no resistance, then the track needs to be cut between \OE on the sensor and ground on the breakout board. If there is a resistance, it should work ok
In any case, the first time you try and use the board and control \OE, you will know if the power supply shorts out.
It worth stating the obvious and understanding why things are done in these forums as the skill level of participants varies considerably. There will be others using this as a reference for this in future.