Champion! Great advice thank you very much. It worked on the first write of a standalone 0x90 command. I think it doesn't like the address being changed and sending of the 0x90 command without a delay between.
Steps I took to change the address for anybody else wanting to do the same thing. Obviously could be made a lot neater, but this was my amateurish approach to get it working:
In tsd305.h, comment out the 'private:' line, allowing you to call the read_EE_coeff function from your code. Added a new 'write_EE_coeff' function as shown below
//private:
/**
* \brief Reads the tsd305 EEPROM coefficient stored at address provided.
*
* \param[in] uint8_t : Address of coefficient in EEPROM
* \param[out] uint16_t* : Value read in EEPROM
*
* \return tsd305_status : status of TSD305
* - tsd305_status_ok : I2C transfer completed successfully
* - tsd305_status_i2c_transfer_error : Problem with i2c transfer
* - tsd305_status_busy : Sensor is busy
* - tsd305_status_memory_error : Sensor EEPROM memory error
*/
enum tsd305_status read_EE_coeff(uint8_t address, uint16_t *coeff);
/**
* \brief Reads the tsd305 EEPROM coefficient stored at address provided.
*
* \param[in] uint8_t : Address of coefficient in EEPROM
* \param[out] float* : IEEE-745 Value read in EEPROM
*
* \return tsd305_status : status of TSD305
* - tsd305_status_ok : I2C transfer completed successfully
* - tsd305_status_i2c_transfer_error : Problem with i2c transfer
* - tsd305_status_busy : Sensor is busy
* - tsd305_status_memory_error : Sensor EEPROM memory error
*/
//new one I added here:
enum tsd305_status write_EE_coeff(uint8_t address, uint16_t *coeff);
Note that the address of the sensor is stored in tsd305.cpp. The default is 0x00 NOT 0x1E. Once you update the address, this has to be changed to suit here.
//TSD305 device address
#define TSD305_ADDR 0x00
//NOT 1E!
If you want to change the address to 0x01. include the following in your new 'write_EE_coeff' function
enum tsd305_status tsd305::write_EE_coeff(uint8_t address, uint16_t *coeff) {
Wire.beginTransmission((uint8_t)TSD305_ADDR);//the default for the TSD304 is 0x00
Wire.write(address);//this is 0x42 to write into EEPROM address 0x02
Wire.write(0x00);// write high byte
Wire.write(0x01);//write low byte. Setting to address 0x01
Wire.endTransmission();
delay(1);
}
My main arduino code below writes the new address to 0x42, corresponding to EEPROM address 0x02, calling our new function above. I connected the power pin of the sensor to pin 4, so I can turn it on and off easily.
#include <tsd305.h>
tsd305 m_tsd305;
void setup() {
uint16_t test;//variable to store contents of address returned from sensor
bool connected;//1 if sensor connected, 0 if not
tsd305_status status;//store contents of status byte returned from sensor
pinMode(4, OUTPUT);//TSD305 power pin
digitalWrite(4, HIGH);//turn on sensor
delay(2000);//some time for sensor to turn on
Serial.begin(9600);//setup serial
Serial.println(F("==== TE Connectivity ===="));//standard printout from lightbulb example
Serial.println(F("==== TSD305 ====="));
Serial.println(F("==== Measure ===="));
m_tsd305.begin();
connected = m_tsd305.is_connected();//check if sensor connected
if ( connected ) {//if so do this
m_tsd305.write_EE_coeff(0x42, (uint16_t *)&test );//Write to 0x42 to write a new address to EEPROM address 0x02. If writing address succeeds, sensor won't respond below as has new address
//change to 0x90 command to reset memory bit
Serial.println("written to 0x90!");//ignore this, should be 0x42
}
Serial.println("turning off sensor!");
digitalWrite(4, LOW);//turn off sensor
delay(2000);//delay to power off sensor
digitalWrite(4, HIGH);//turn on sensor
Serial.println("turning on sensor!");
delay(2000);//some time for sensor to turn on
connected = m_tsd305.is_connected();//check if sensor connected
if ( connected ) {//if so do this
status = m_tsd305.read_EE_coeff(0x02, (uint16_t *)&test );//read address two containing the sensor address
Serial.println("read address 0x02! Contents are:");
Serial.println(test);//print the contents of the address
Serial.println("status byte is:");
Serial.println(status, HEX);//print the status byte. 4 = memory error. 0 = fine. Returns 0!
}
}
Note that if successful, the last part of this code won't work, as we changed the address to 0x01.
Go back into tsd305.cpp and set the address to 0x01
//TSD305 device address
#define TSD305_ADDR 0x01
Change our new write_EE_coeff function in tsd305.cpp to the following:
enum tsd305_status tsd305::write_EE_coeff(uint8_t address, uint16_t *coeff) {
Wire.beginTransmission((uint8_t)TSD305_ADDR);//the default for the TSD304 is 0x00
Wire.write(address);//this is 0x42 to write into EEPROM address 0x02
//note that we will now pass in the command/address 0x90 from the main code
//Wire.write(0x00);// write high byte
//Wire.write(0x01);//write low byte. Setting to address 0x01
Wire.endTransmission();
delay(1);
}
Change this section of the code below in our main arduino function to this to write the 0x90 command. Running the whole program will auto power cycle the sensor and then read the address 0x02, which should give you the result 0x01 and give you a status byte result of 0. A result of 4 means it hasn't worked. Until this gives you 0, the lightbulb library TSD305 temperature result gives you non sensical readings. This worked for me on the first try.
connected = m_tsd305.is_connected();//check if sensor connected
if ( connected ) {//if so do this
m_tsd305.write_EE_coeff(0x90, (uint16_t *)&test );//Write 0x90 command which resets memory bit
Serial.println("written to 0x90!");
}
Good luck if you are trying the same thing!