Answers.Body: <p>To understand how to use the data, it help to understand how the color sensor works. Let's take a step back for a minute and have a look. The sensitive electrical component here is just a photodiode or phototransistor that lets us measure light intensity.</p>
  
  <p>If we turn all of the LEDs off we are just measuring ambient light. If we turn on just one of the LEDs and hold the sensor close to a surface, we are measuring mostly the reflected light from that one LED (and some ambient light).</p>
  
  <p>When we switch to color mode, it looks like all 3 LEDs are on at the same time, but what is actually happening is that they are being switched in sequence very rapidly. off-red-green-blue-off-red-green-blue-... and so on.</p>
  
  <p>This means the mysterious 4th value, INPUT_BLANK, is the off part of the cycle - it is just measuring ambient light.</p>
  
  <p>You said that you would take intensity as a bonus, but actually that is all you are getting. If you hold the sensor close to a white surface, you should get high values for all 3 colors. If you move the sensor even closer (but not too close), the values will increase. This is because the reflected intensity is getting strong, not because the surface is getting whiter.</p>
  
  <p>This means the RGB values are not like the ones on your computer screen. If you do a web search for robotic vision systems, you will find that RGB just doesn't work. The common practice is to convert the values to HSV (hue-saturation-value). The HSV values can more easily deal with changes in ambient light allowing you to more accurately determine colors.</p>
  
  <p>I am not sure how the normalized and scaled values are calculated in <code>ReadSensorColorEx</code>, but you can play around with them and see if they give you better results for whatever it is that you are trying to do.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-14T11:20:48.833Z
Answers.Text: Thanks for this enlightening explanation. Do you have any references for this, so we can read deeper if needed? Also, do you know of any example program using the RGB sensor in full mode?
Answers.UserDisplayName: mirabilos
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-14T17:22:21.643Z
Answers.Text: Sure. I learned most of this by reading the source code for the EV3. The timing happens in the [d_analog](https://github.com/mindboards/ev3sources/blob/master/lms2012/d_analog/Linuxmod_AM1808/d_analog.c) device driver. I haven't got to the part where it actually computes the colors though. It's in there somewhere.
Answers.UserDisplayName: David Lechner
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-14T17:25:56.627Z
Answers.Text: For an example, have a look at the [Mindcuber source code](http://mindcuber.com/mindcuber/mindcuber-source.html). It does it's own figuring of colors by converting them to HSV and then sorting them.
Answers.UserDisplayName: David Lechner
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-17T12:31:50.610Z
Answers.Text: Thanks. I will accept this as answer (feel free to move those comments into the answer body), even though the Mindcuber source is not Open Source it’s enough to read it to write a documentation for the sensor.
Answers.UserDisplayName: mirabilos
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-13T03:23:33.273Z
Answers.LastActivityDate: 2014-09-13T03:23:33.273Z
Answers.UserDisplayName: David Lechner
Body: <p>I've got an RGB sensor (<a href="http://rads.stackoverflow.com/amzn/click/B005GXTD2M" rel="nofollow">this one with the three LED thingies</a>) and wonder how to access it using NXC.</p>
  
  <p>I know I can <code>SetSensorColorFull(IN_1);</code> and then access it using <a href="http://bricxcc.sourceforge.net/nbc/nxcdoc/nxcapi/group___input_module_functions_gaf8e01f28be7f3476baface8d4515357f.html" rel="nofollow">ReadSensorColorEx</a> or, for simplicity, <a href="http://bricxcc.sourceforge.net/nbc/nxcdoc/nxcapi/group___input_module_functions_gae29b1117df46bf44aaf25843d61ccbaf.html" rel="nofollow">ReadSensorColorRaw</a>, but I'm a bit stumped wrt. the return values.</p>
  
  <p>I know normal 24-bit (3x8bit) RGB colour codes. I know normal NXC return values (0/1, 0‥100, 0‥255). But I don't know how the values here work.</p>
  
  <p>I've toyed around with <code>i = ReadSensorColorRaw(IN_1, r);</code> where <code>unsigned int r[4];</code>, and got values: <code>i</code> was always <code>1</code> (even if the cable was unplugged), and the <code>r[INPUT_RED]</code> <a href="http://bricxcc.sourceforge.net/nbc/nxcdoc/nxcapi/group___input_color_idx_constants.html" rel="nofollow">aka <code>r[0]</code></a> values were somewhat usable, and about 0‥60 were black in general, and I got up to 1023 (when holding the sensor towards light), but if I hold the RGB sensor towards anything yellow, green or blue, the values are somewhat off. Also, I found <em>no</em> docs whatsoever as to what <code>r[INPUT_BLANK]</code> is supposed to be.</p>
  
  <p>When unplugging the sensor, I got <code>r = { x, 0, x, 0 }</code> (where <em>x</em> was roughly 120) in my test program just using <code>NumOut</code> to debug the sensors, so I assume that the sensor return values are biased somewhat?</p>
  
  <p>Would using <code>ReadSensorColorEx</code> give somewhat better usable values, or anything else I can use? Do I need to calibrate them first, and if yes, how? To clarify, I don't want the functions that just give back “it's red”, “it's green”, etc. but I want somewhat usable R/G/B colour tuples (and would take Intensity as bonus, although I'm using light sensors for them normally, or <code>SetSensorColorRed</code> if must be).</p>
  
  <p>I'm using NXT Firmware 1.29, and exclusively Open Source tools. If there's anything else needed, information wise, just comment.</p>
  
Tags: <programming><nxc><sensor-colour>
Title: RGB sensor HOWTO use?
UserDisplayName: mirabilos
ThreadText: # RGB sensor HOWTO use?
  
  (Asked by: username_0 on 2014-03-08)
  
  I've got an RGB sensor ([this one with the three LED thingies](http://rads.stackoverflow.com/amzn/click/B005GXTD2M)) and wonder how to access it using NXC.
  
  I know I can `SetSensorColorFull(IN_1);` and then access it using [ReadSensorColorEx](http://bricxcc.sourceforge.net/nbc/nxcdoc/nxcapi/group___input_module_functions_gaf8e01f28be7f3476baface8d4515357f.html) or, for simplicity, [ReadSensorColorRaw](http://bricxcc.sourceforge.net/nbc/nxcdoc/nxcapi/group___input_module_functions_gae29b1117df46bf44aaf25843d61ccbaf.html), but I'm a bit stumped wrt. the return values.
  
  I know normal 24-bit (3x8bit) RGB colour codes. I know normal NXC return values (0/1, 0‥100, 0‥255). But I don't know how the values here work.
  
  I've toyed around with `i = ReadSensorColorRaw(IN_1, r);` where `unsigned int r[4];`, and got values: `i` was always `1` (even if the cable was unplugged), and the `r[INPUT_RED]` [aka `r[0]`](http://bricxcc.sourceforge.net/nbc/nxcdoc/nxcapi/group___input_color_idx_constants.html) values were somewhat usable, and about 0‥60 were black in general, and I got up to 1023 (when holding the sensor towards light), but if I hold the RGB sensor towards anything yellow, green or blue, the values are somewhat off. Also, I found *no* docs whatsoever as to what `r[INPUT_BLANK]` is supposed to be.
  
  When unplugging the sensor, I got `r = { x, 0, x, 0 }` (where *x* was roughly 120) in my test program just using `NumOut` to debug the sensors, so I assume that the sensor return values are biased somewhat?
  
  Would using `ReadSensorColorEx` give somewhat better usable values, or anything else I can use? Do I need to calibrate them first, and if yes, how? To clarify, I don't want the functions that just give back “it's red”, “it's green”, etc. but I want somewhat usable R/G/B colour tuples (and would take Intensity as bonus, although I'm using light sensors for them normally, or `SetSensorColorRed` if must be).
  
  I'm using NXT Firmware 1.29, and exclusively Open Source tools. If there's anything else needed, information wise, just comment.
  
  **Tags:** programming, nxc, sensor-colour
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-09-13. Score: 3 | Accepted answer
  
  To understand how to use the data, it help to understand how the color sensor works. Let's take a step back for a minute and have a look. The sensitive electrical component here is just a photodiode or phototransistor that lets us measure light intensity.
  
  If we turn all of the LEDs off we are just measuring ambient light. If we turn on just one of the LEDs and hold the sensor close to a surface, we are measuring mostly the reflected light from that one LED (and some ambient light).
  
  When we switch to color mode, it looks like all 3 LEDs are on at the same time, but what is actually happening is that they are being switched in sequence very rapidly. off-red-green-blue-off-red-green-blue-... and so on.
  
  This means the mysterious 4th value, INPUT\_BLANK, is the off part of the cycle - it is just measuring ambient light.
  
  You said that you would take intensity as a bonus, but actually that is all you are getting. If you hold the sensor close to a white surface, you should get high values for all 3 colors. If you move the sensor even closer (but not too close), the values will increase. This is because the reflected intensity is getting strong, not because the surface is getting whiter.
  
  This means the RGB values are not like the ones on your computer screen. If you do a web search for robotic vision systems, you will find that RGB just doesn't work. The common practice is to convert the values to HSV (hue-saturation-value). The HSV values can more easily deal with changes in ambient light allowing you to more accurately determine colors.
  
  I am not sure how the normalized and scaled values are calculated in `ReadSensorColorEx`, but you can play around with them and see if they give you better results for whatever it is that you are trying to do.
  
  > **Comments:**
  > **username_0**: Thanks for this enlightening explanation. Do you have any references for this, so we can read deeper if needed? Also, do you know of any example program using the RGB sensor in full mode?  
  > **username_1**: Sure. I learned most of this by reading the source code for the EV3. The timing happens in the [d_analog](https://github.com/mindboards/ev3sources/blob/master/lms2012/d_analog/Linuxmod_AM1808/d_analog.c) device driver. I haven't got to the part where it actually computes the colors though. It's in there somewhere.  
  > **username_1**: For an example, have a look at the [Mindcuber source code](http://mindcuber.com/mindcuber/mindcuber-source.html). It does it's own figuring of colors by converting them to HSV and then sorting them.  
  > **username_0**: Thanks. I will accept this as answer (feel free to move those comments into the answer body), even though the Mindcuber source is not Open Source it’s enough to read it to write a documentation for the sensor.  
  
  ---
  
  