I'm having some issues writing sensor data to an SD card from 2 VEML6075 UV sensors. These are connected to a UNO R3 with data logging shield for arduino via an Adafruit TCA9548A MUX (I will be adding more UV sensors once I can get 2 to log). I have confirmed that the two sensors are working correctly through the serial monitor.
The sensors are recognised and correctly log sensor 1 values, but log the sensor 1 values for sensor 2. I can't figure out where I've programmed it incorrectly and was hoping somebody could identify the issue.
Here is my code:
#include <SparkFun_VEML6075_Arduino_Library.h>
#include "Wire.h" //include Wire library
#include <SD.h>
#include <RTClib.h>
#define TCAADDR 0x70 //define then select multiplexor port address
#define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
#define LOG_INTERVAL 1000 // mills between entries (reduce to take more/faster data)
uint32_t syncTime = 0; // time of last sync()
#define ECHO_TO_SERIAL 1 // echo data to serial port
#define WAIT_TO_START 0 // Wait for serial input in setup()
/* Assign a unique ID to this sensor at the same time - do this for MCP9808 later – may have to change orders?!*/
VEML6075 uv1 = VEML6075();
VEML6075 uv2 = VEML6075();
// chip select for SD card
const int SD_CS_PIN = 10;
/* Set up the 8 ports on multiplexor */
void tcaselect(uint8_t i) {
if (i > 7) return;
Wire.beginTransmission(TCAADDR);
Wire.write(1 << i);
Wire.endTransmission();
}
// variables
float UVa1 = 0;
float UVb1 = 0;
float UVa2 = 0;
float UVb2 = 0;
// the logging file
File logfile;
//start setup
void setup(void)
{
Wire.begin();
Serial.begin(9600);
/* Initialise the 1st sensor */
tcaselect(5);
if(! uv1.begin())
{
Serial.println("Failed to communicate with VEML6075 sensor1, check wiring?");
while(1);
}
/* Initialise the 2nd sensor */
tcaselect(6);
if(! uv2.begin())
{
Serial.println("Failed to communicate with VEML6075 sensor1, check wiring?");
while(1);
}
// initialize the SD card
pinMode(10, OUTPUT);
// create a new file
char filename[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i/10 + '0';
filename[7] = i%10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
logfile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
/**************** Initialise the sensors ******************/
logfile.println("millis,UVA1,UVB1,UVA2,UVB2");
#if ECHO_TO_SERIAL
Serial.println("millis,UVA1,UVB1,UVA2,UVB2");
#endif //ECHO_TO_SERIAL
}
//Now for the loop:
void loop() {
// delay for the amount of time we want between readings
delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
// log milliseconds since starting
uint32_t m = millis();
logfile.print(m); // milliseconds since start
logfile.print(", ");
#if ECHO_TO_SERIAL
Serial.print(m); // milliseconds since start
Serial.print(", ");
#endif
// read the sensors values
UVa1 = uv1.rawUva();
UVb1 = uv1.rawUvb();
UVa2 = uv2.rawUva();
UVb2 = uv2.rawUvb();
logfile.print(", ");
logfile.print(String(UVa1));
logfile.print(", ");
logfile.print(String(UVb1));
logfile.print(", ");
logfile.print(String(UVa2)); //problem - it's writing the same as UVa1
logfile.print(", ");
logfile.print(String(UVb2)); //problem - it's writing the same as UVb2
#if ECHO_TO_SERIAL
Serial.print(", ");
Serial.print(UVa1);
Serial.print(", ");
Serial.print(UVb1);
Serial.print(", ");
Serial.print(UVa2); //it's printing unique value to UVa1
Serial.print(", ");
Serial.print(UVb2); //it's printing unique value to UVb1
#endif //ECHO_TO_SERIAL
logfile.println();
#if ECHO_TO_SERIAL
Serial.println();
#endif // ECHO_TO_SERIAL
// Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
// which uses a bunch of power and takes time
if ((millis() - syncTime) < SYNC_INTERVAL) return;
syncTime = millis();
logfile.flush();
/* Display the results */
tcaselect(5);
Serial.print("Sensor #1 - ");
Serial.print("Raw UVA reading: "); Serial.println(uv1.rawUva());
Serial.print("Sensor #1 - ");
Serial.print("Raw UVB reading: "); Serial.println(uv1.rawUvb());
tcaselect(6);
Serial.print("Sensor #2 - ");
Serial.print("Raw UVA reading: "); Serial.println(uv2.rawUva());
Serial.print("Sensor #2 - ");
Serial.print("Raw UVB reading: "); Serial.println(uv2.rawUvb());
delay(1000);
}
Here is the output from the serial monitor at a given time point:
12:18:52.176 -> millis,UVA1,UVB1,UVA2,UVB2
12:18:53.165 -> 999, , 88.00, 114.00, 88.00, 114.00
12:18:53.200 -> Sensor #1 - Raw UVA reading: 7
12:18:53.235 -> Sensor #1 - Raw UVB reading: 11
12:18:53.270 -> Sensor #2 - Raw UVA reading: 88
12:18:53.305 -> Sensor #2 - Raw UVB reading: 114
You can see that the sensor information is correct on lines 3-6, however the values being written to the SD card for the second sensor mirror that of the first sensor.
Thanks