I’ve recently started experimenting with the PiicoDev Colour Sensor. I’ve noticed that when I sample RGB colour values, the sensor gives readings much larger than 255. I understand 255 to be the maximum RGB colour value, so these readings are confusing me. It also happens in the official tutorial video. Can anyone explain what is happening please?
I am using the provided PiicoDev Python module on a Raspberry Pi 4B.
The VEML6040 outputs 16-bit data; what led you to believe it was 8-bit?
Values could be scaled down with math if you needed less resolution
255 is the maximum value for an 8-bit number
65535 is the maximum value for a 16-bit number - this is the data returned by the sensor.
If you must work with (255) 8-bit, then values can be easily scaled down by dividing by 8 eg.
newValue = oldValue // 8 # use the // (floor division) operator to get an integer result
Floor can lead to a large errors the closer the dividend is to the divisor. Some unexpected outcomes might be:
While floor is fast, it might be best to use round() when sensor readings are involved (it’s also a built-in function of Python):
newValue = round(oldValue / 8);
Which has more-accurate outcomes:
- round(9 / 8) = 1
- round(15 / 8) = 2
Thanks everyone. That makes a lot of sense.