Color Sensor Arduino Code

Wanted to play with the color sensor without having to use roborio so i put together this sketch. Be aware it requires a 3v3 arduino to use otherwise you might damage the color sensor. It has code to detect the closest color of the pre-defined selection

https://pastebin.com/AKAgdqnR

Reddit X-post: https://www.reddit.com/r/FRC/comments/elnuiq/color_sensor_arduino_code/

3 Likes

I forked this to display the results on an attached LCD instead of the serial monitor. It seems that the system responds for about three seconds but then freezes and I need to hit the reset button to fix it. I hooked up the sensor to corresponding pins and used this code. I was wondering if you could discern what could be causing this to lock up.

1 Like

what arduino are you using? I may have a similar one I can try. I used a teensy 3.6 and ran for hours just fine. Might be the i2c speed or pullups. What happens when you just run it with serial monitor without the lcd code? The code i put together was quick and dirty and doesn’t do any error handling and assumes things will work well so if there are any i2c errors it could lockup.

I’m using an Arduino uno. How did you wire the pull up resistors? I tried to use the serial monitor yesterday but I set the baud rate wrong. I’ll try again today.

EDIT:
Even with the baud rate set incorrectly, the arduino stopped outputting data in a similar amount of time.

the uno is a 5v microcontroller. per my initial posting: Be aware it requires a 3v3 arduino to use otherwise you might damage the color sensor.

the device doesn’t support running on 5v supply or logic.

I connected the sensor to the 3.3v rail. Somehow I’ve fixed the freezing problems. If I use 1k pullup resistors for SDA and SCL, the arduino never reads data from the sensor.

it’s not just the rail but also the logic lines. i2c is open collector output where master or slave can pull the lines low. but when left to go high it will try to pull up the logic lines to 5v with the internal bias resistors(assuming it has them and they are enabled). been a long time since i played with an actual uno but the quick and dirty is try to put ~2k resistors from each line to the 3v3 rail and disable the internal pullups. I can get back to you tomorrow once i get my hands back on the sensor and i’ll hook it up to an uno.

I scrounged up some 2k ohm resistors and disabled the internal resistors using

pinMode(SDA, INPUT);
pinMode(SCL, INPUT);

It seems that it works now. Another thing is that somethings read over 255.

its your lcd code. if it displays a 3 digit number ie. 250. then it displays a 2 digit number ie. 50. it will show as 500 since it didn’t clear out the previous printed digit in that position. with the code you have you need to print fixed length numbers otherwise you will get visual glitches like this.

if you look at the code you will see it finds the max of all 3 channels, then divides each channel by the max which will always be 0-1 and that is multiplied by 255 so it shouldn’t be possible for the value to actually go bigger.

Ah ok. I’ll put in a clear.

Is that really the correct “blue” in your picture? The color on the wheel is actually cyan, not a dark blue.

it’s what came in the KOP. they are the ones who stated they are color samples of what is on the field. Check your KOP and see what the colors are in your kit. Even if the hue is off with the code provided it makes no difference. the code skews colors to the closest match of pure red, green, blue otherwise it’s yellow.

Thanks. I have not seen our KOP yet (we do have it). I assumed that “cyan” would not be that dark. Certainly does not look like what is printed in the manual.

Look at https://www.youtube.com/watch?v=4dAqyPWyTmw&t=0m19s
from that video you can see all the colors look less saturated than in my image. So my camera is adding saturation or it’s just a darker scene so the colors don’t look as flat with everything being brighter in the field video. Maybe they sent color swatches that aren’t the same as the real field that could be the case too or maybe the video and docs were done with samples not final products so real fields may have what the swatches are. Who knows.

I get a bunch of giberish in the serial monitor with the baud rate set correctly. Should I wire up pull-up resistors on the SDA and SCL rails as suggested earlier?

I believe pull up resistors are required for i2c communication. I used 2k ohm resistors connected to the 3.3v rail.

It gives me a couple lines of values in the serial monitor than begins printing a bunch of random characters. I have the two 2k resistors, any idea what could be wrong?

You could try turning down the polling rate. Change the delay function as the end of the loop block.

I tried 9600 for the baud rate and it began working.

Team 6166 got this working on an Arduino Mega 2560. It runs pretty close to an Uno, I believe, so it performs just the same.

What we did:
2k Ohm resistor on the 3.3V rail.
1k Ohm resistor on the SDA rail.
1k Ohm resisitor on the SCL rail.
Changed the baud rate to 9600.