Log in

View Full Version : I2C Color Sensor not working


TrentMcBignerd
06-02-2016, 15:34
My team recently purchased a Modern Robotics Color Sensor, so we are trying to test it out before implementing it on our robot. However whenever we try to use the read method on the color sensor, it return true, meaning the transfer was aborted. However we tried communicating with it and we can, because we are able to turn on and off the led on the sensor. Can someone please explain?
The problem is at line 150.
https://github.com/CougarRobotics1982/2014NewCode/blob/master/src/org/usfirst/frc1982/Robot2014new/Robot.java

pblankenbaker
09-02-2016, 14:59
Are you using the color sensor found at: http://www.modernroboticsinc.com/color-sensor

If so, it looks like product page indicates that the address of the sensor is 0x3C. Could it be that you just need to change:


ColorSensor = new I2C(I2C.Port.kOnboard, 0x1E);


To:


ColorSensor = new I2C(I2C.Port.kOnboard, 0x3C);

mikets
09-02-2016, 15:31
Are you using the color sensor found at: http://www.modernroboticsinc.com/color-sensor

If so, it looks like product page indicates that the address of the sensor is 0x3C. Could it be that you just need to change:


ColorSensor = new I2C(I2C.Port.kOnboard, 0x1E);


To:


ColorSensor = new I2C(I2C.Port.kOnboard, 0x3C);

I am not familiar with the I2C class in WPILib but it depends on whether the regAddress parameter in the I2C constructor is expecting a 7-bit address or 8-bit address. If it is 8-bit, then 0x3C is the correct address. But if it is expecting a 7-bit address, then it should be 0x1E. In the I2C spec, register address is actually 7 bits, the LSB of the 8-bit address is used to indicate whether it is a READ or WRITE transaction. So a 7-bit address must shift left one-bit to accommodate the READ/WRITE bit. The Modern Robotics Color Sensor spec specifies an 8-bit address. So it's already shifted one bit to the left. But if the WPILib I2C constructor is expecting a 7-bit address (i.e. it will internally shift one bit to the left and OR-in the READ/WRITE bit), then passing 0x1E to it is correct.
I recommend you try both and see which way it is.

Joe Ross
09-02-2016, 15:38
I am not familiar with the I2C class in WPILib but it depends on whether the regAddress parameter in the I2C constructor is expecting a 7-bit address or 8-bit address. If it is 8-bit, then 0x3C is the correct address. But if it is expecting a 7-bit address, then it should be 0x1E. In the I2C spec, register address is actually 7 bits, the LSB of the 8-bit address is used to indicate whether it is a READ or WRITE transaction. So a 7-bit address must shift left one-bit to accommodate the READ/WRITE bit. The Modern Robotics Color Sensor spec specifies an 8-bit address. So it's already shifted one bit to the left. But if the WPILib I2C constructor is expecting a 7-bit address (i.e. it will internally shift one bit to the left and OR-in the READ/WRITE bit), then passing 0x1E to it is correct.
I recommend you try both and see which way it is.

The cRIO expects 8 bit, the roboRIO expects 7 bit.

bkeeney
09-02-2016, 21:17
So what we've experienced is that our addressing appears to be correct. We are able to turn the LED on and off.

What's not working is the I2C read. Code below:

byteAr = new byte[1];
boolean success = !ColorSensor.read(0x04, 1, byteAr);

The read is always returning true, which means the read has failed. If we ignore that and try to use the value in byteAr we get an index out of bounds exception.

Not sure where to go from here since it does not appear to be address related.

FWIW, we were able to connect the sensor to a Raspberry Pi and read from the various address locations. RoboRio no success.

Any ideas?

bkeeney
11-02-2016, 19:40
bump? Anyone?

pblankenbaker
12-02-2016, 12:02
That is strange that the write operation would work while the read operation would not.

Is it possible that the voltage levels would make a difference? I'm not an electronics person, but I think I have noticed that some I2C devices are 3.3V and some are 5V and some will dynamically adjust.

The roboRIO is labeled for 3.3V on the I2C power and from the website (http://www.modernroboticsinc.com/color-sensor) it looks like the sensor is expecting 5V on the power line (the website doesn't say that it won't work on 3.3V, but I didn't see anything saying that it would either).

Does the Raspberry PI that the sensor worked on have a 3.3V I2C bus like the roboRIO or a 5V I2C bus?

Not sure how this might impact things or if it could explain why you can send data to the sensor but not get anything back, but maybe it is something to consider or to ask the manufacturer of the sensor.

I have seen ads for items designed to convert between 3.3V and 5V I2C lines (https://www.adafruit.com/products/757). Have never tried using one though.

Good Luck.