Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
8 years, 6 months ago.
I2C testing problem with tcs34725
I watch the video: https://www.youtube.com/watch?v=ljh5xENAR5Q and write a similar code to f334r8 board for testing. About the result, LED should turn on about 4 seconds, but LED is turn on forever. what is the problem? Thank you
- include "mbed.h"
I2C i2c(PB_9,PB_8); int color_sensor_address = 41 << 1; DigitalOut green(LED1);
int main() { green = 1; i2c.frequency(200000); char id_registor_value[1] = {146}; i2c.write(color_sensor_address,id_registor_value,1, true); char data[1] = {0}; i2c.read(color_sensor_address,data,1, false);
if (data[0]==68) { green = 0; wait (4); green = 1; } else { green = 1; }
}
1 Answer
8 years, 6 months ago.
Please use <<code>> "Your Program" <</code>>
to format listings here.
so your code is -
include "mbed.h" I2C i2c(PB_9,PB_8); int color_sensor_address = 41 << 1; DigitalOut green(LED1); int main() { green = 1; i2c.frequency(200000); char id_registor_value[1] = {146}; i2c.write(color_sensor_address,id_registor_value,1, true); char data[1] = {0}; i2c.read(color_sensor_address,data,1, false); if (data[0]==68) { green = 0; wait (4); green = 1; } else { green = 1;} }
So the basic problem is line 9 you use a register address 146 decimal, there is no such address this should be 0x12 or 18 decimal. It would be better if you use hexidecimal numbers, as per the datasheet, in your code and less opportunity for errors, it is easily done. Also the LED will not respond as you intended from from question. OFF for 4 seconds then on for a very short time before Going OFF again for 4 seconds if condition is met, else the LED is always ON, which is what you are seeing. here is a revised code.... Note the LED will Blink in One Second intervals if the device correctly Identifies itself, and also using Hexidecimal numbering as per the Datasheet for the Sensor.
include "mbed.h" I2C i2c(PB_9,PB_8); int color_sensor_address = 0x29 << 1;// was 41 decimal. 0x29 is the i2c address for the TCS34725FN Datasheet page 3 DigitalOut green(LED1); int main() { i2c.frequency(200000); char id_registor_value[1] = {0x12};//was 146 decimal????? no such register. Datasheet page 13 i2c.write(color_sensor_address,id_registor_value,1, true); char data[1] = {0}; while(true){ i2c.read(color_sensor_address,data,1, false); if (data[0]==0x44) { //was 68 decimal. 0x44 is ID for TCS34725FN Datasheet page 18 green = 1; wait (0.5); green = 0; wait (0.5); } else { green = 1; } } }