Introduction: ITTT: Color Mixer Arduino Project
Project made for If This Then That, a school subject! Match a color as closely as you can to a random one using RGB values.
Showcase: https://drive.google.com/file/d/17yaXKNZx72h_qEzzXrqS54VpI6VeYWYe/view?usp=sharing (video. 1:51)
Supplies
- Arduino UNO
- Breadboard
- Potentiometer
- Button x2
- Colored LED x3 (red, green, blue)
- RGB LED
- Resistor x6
- Jumper wires
Step 1: The Concept
The idea of this project was inspired by one of the assignments for one of our art classes - to digitally paint several color studies with principles of color theory (complimentary, monochrome color palette, quantity of a certain color, warm/cool colors etc.) without using any sort of color picker so we would understand how the colors worked together.
For If This Then That I have attempted to make something similar - using three LEDs, each for a red, green, and blue color value, you are tasked with matching the values of randomly generated color as closely as possible.
The way the project works is:
- When the Arduino is plugged in, three values from 0-255 are randomly generated; red, green and blue.
- When the button on the side of the red colored LED is pressed, it cycles through: 1) red, 2) green, 3) blue, 4) the generated color.
- When each LED is selected, its brightness can be adjusted with the potentiometer. (More on the potentiometer in the Reflection section.)
- When the user is satisfied with the color they have made, they can click the other button. This makes the created and generated color flash after each other in comparison.
- If each color is within the defined range, the green LED flashes, indicating the user succeeded.
If at least one color is outside the range, the red LED flashes instead, indicating the user-made color is not close enough.
Step 2: The Code
Credits: ezButton library created by ArduinoGetStarted.com
#include <ezButton.h>
// define the pins for the potentiometer and the amount of colored LEDs
#define potmeter A0
#define amntleds 3 // red, green, blue
// define pins for the colored LEDs (led) and each color output of the rgb LED (rgb)
const int led_red = 11;
const int led_green = 10;
const int led_blue = 9;
const int rgb_red = 6;
const int rgb_green = 5;
const int rgb_blue = 3;
ezButton button8(8); // create ezButton objects - I named mine after the pin they are connected to
ezButton button7(7);
// let the amount of times the switch button was pressed and last recorded brightness of an LED be remembered
unsigned long last_count = -1;
unsigned long last_brightness = 0;
int current_led = 0;
int current_rgb = 0;
int current_brightness = 0;
int brightness_red = 0;
int brightness_green = 0;
int brightness_blue = 0;
int random_red = 0;
int random_green = 0;
int random_blue = 0;
void setup() {
randomSeed(analogRead(5)); // leave A5 empty! each time the sketch runs, it'll generate white noise and thus the random values will truly be random
random_red = random(256); // generate a random brightness from 0-255 for each color; this'll be the color you will have to match
random_green = random(256);
random_blue = random(256);
Serial.begin(9600); // connect serial monitor to print text
button8.setDebounceTime(50); // set debounce time to 50 ms
button8.setCountMode(COUNT_FALLING);
button7.setDebounceTime(50);
button7.setCountMode(COUNT_FALLING);
pinMode(led_red, OUTPUT);
pinMode(led_green, OUTPUT);
pinMode(led_blue, OUTPUT);
pinMode(rgb_red, OUTPUT);
pinMode(rgb_green, OUTPUT);
pinMode(rgb_blue, OUTPUT);
}
void loop() {
// define upper and lower range for the accuracy of each color
int upper_range_red = random_red + 50;
int upper_range_green = random_green + 50;
int upper_range_blue = random_blue + 50;
int lower_range_red = random_red - 50;
int lower_range_green = random_green - 50;
int lower_range_blue = random_blue - 50;
button8.loop();
button7.loop();
if (button7.isPressed()) { // press this button when you are done setting up your color
Serial.println("done");
analogWrite(rgb_red, brightness_red); // RGB LED will flash between your color and the generated color
analogWrite(rgb_green, brightness_green);
analogWrite(rgb_blue, brightness_blue);
delay(500);
analogWrite(rgb_red, random_red);
analogWrite(rgb_green, random_green);
analogWrite(rgb_blue, random_blue);
delay(500);
analogWrite(rgb_red, brightness_red);
analogWrite(rgb_green, brightness_green);
analogWrite(rgb_blue, brightness_blue);
delay(500);
analogWrite(rgb_red, random_red);
analogWrite(rgb_green, random_green);
analogWrite(rgb_blue, random_blue);
// if each color is close enough to (within the range of) the generated color, you succeed and a green light flashes
if (brightness_red > lower_range_red && brightness_red < upper_range_red &&
brightness_green > lower_range_green && brightness_green < upper_range_green &&
brightness_blue > lower_range_blue && brightness_blue < upper_range_blue){
digitalWrite(led_red, LOW);
digitalWrite(led_blue, LOW);
digitalWrite(led_green, HIGH);
delay(500);
digitalWrite(led_green, LOW);
delay(500);
digitalWrite(led_green, HIGH);
delay(500);
digitalWrite(led_green, LOW);
delay(500);
}
else{
// if a color is outside the range, you fail and a red light flashes
digitalWrite(led_green, LOW);
digitalWrite(led_blue, LOW);
digitalWrite(led_red, HIGH);
delay(500);
digitalWrite(led_red, LOW);
delay(500);
digitalWrite(led_red, HIGH);
delay(500);
digitalWrite(led_red, LOW);
delay(500);
}
}
// this part of the code checks if the brightness has been adjusted - if it has, the new value is saved 'in' that color.
int potmeter_value = analogRead(potmeter);
int brightness = potmeter_value / 4;
if (brightness != last_brightness) {
switch (current_led) {
case led_red:
brightness_red = brightness;
break;
case led_green:
brightness_green = brightness;
break;
case led_blue:
brightness_blue = brightness;
break;
}
// current LED and its value in the RGB LED are adjusted accordingly
analogWrite(current_led, brightness);
analogWrite(rgb_red, brightness_red);
analogWrite(rgb_green, brightness_green);
analogWrite(rgb_blue, brightness_blue);
last_brightness = brightness;
}
int count = button8.getCount();
if (count != last_count) {
Serial.println(count);
//based on the amount of times the button has been pressed the sketch cycles through:
//adjust red/green/blue led, show the color you made, show the generated color
int countingFunction = count % 4 + 1;
switch (countingFunction) {
case 1:
Serial.println("red");
current_led = led_red;
analogWrite(led_red, brightness_red);
break;
case 2:
Serial.println("green");
current_led = led_green;
analogWrite(led_green, brightness_green);
break;
case 3:
Serial.println("blue");
current_led = led_blue;
analogWrite(led_blue, brightness_blue);
break;
case 4:
Serial.println("example");
current_led = 0;
analogWrite(rgb_red, random_red);
analogWrite(rgb_green, random_green);
analogWrite(rgb_blue, random_blue);
break;
}
last_count = count;
}
}
Step 3: The Wiring
The wiring is as follows:
Note:
Tinkercad did not have visually the same model of the potentiometer I used - the spacing on mine was holes 13, 15 and 17.
Step 4: Iterations and Progress
Step 5: The Box
Supplied I used for the box:
- Existing small wooden box
- Sandpaper
- Gouache (black, red, green)
- Dark blue POSCA pen (could also be gouache - I did not have any on hand)
- Unneeded newspaper (to protect the surface you will be painting the box on)
For the box, I found one we had lying around - it contained some shells, pieces of (sea)glass and other beach trinkets, I’m pretty sure I made it as a part of some workshop some years ago.
It looked rough and would definitely leave some splinters if I were to work with it as it was; so my next steps were to sand it down and paint it.
I went for a plain black look, with the sides being more colorful in order to fit the theme of the project.
The dimensions of the box were approx. 10x8x8 cm, which was just big enough to fit the Arduino and breadboard.
I’d planned to have the Arduino with all the wires at the bottom and use the lid as a ‘holder’ for the breadboard, with all the interactive parts on a top layer. Then, on top of that, I would add another layer of black paper to make the breadboard less noticeable.
The lid did not have enough space to comfortably house the breadboard, so I had to cut small bits out in order to position it right.
Step 6: The Solderiing
Having been completely new to Arduino work and soldering, I ordered the YwRobot breadboard, which looks exactly like the one I used for prototyping. This made converting the prototyped schematic to a properly soldered one much more intuitive and easy
Soldering in itself was surprisingly fun! I got the hang of it pretty quickly and everything had gone quite well. There was one instance of a poorly soldered wire that ended up quite wiggly and a short-circuiting resistor, but those were both fairly easy to fix.
Step 7: The Reflection
One thing that I hadn’t accounted for was the fact that the potentiometer I used for this project is not a model that can turn all the way around. This led (hehe, led) to the value of every next LED being momentarily reset to the previous one, as it referenced the current position of the potentiometer. Although it makes setting up the color harder than initially planned, it could also act as an extra challenge.
An idea I considered at first was adding a 4-part segment display and letting it show the generated and input RGB values so the user would be able to compare them. This idea did not work out in the end, as one such display would need a total of 12 free pins, which I did not have available.
This project as a whole was quite a challenge - I, as an artist, find coding by itself quite a task already. Looking at the wiring behind a piece of tech is something I had never really done, much less actually trying to understand what goes on and how everything works.
The hardest part, though, was coming up with an idea: I had ended up buying a keypad and LCD for a completely different concept that I ended up not using at all. After having decided on this idea, the project had gone pretty smoothly, with the only delays being due to the RGB LEDs and breadboard not having arrived in the mail yet.
Despite the challenges, it turned out to be pretty fun and I’ve learned a lot! From looking into how you code and put together an Arduino project from scratch with all the unique pins and libraries, to setting myself reminders and milestones to document my progress, to double and triple-checking my wiring (no, I didn’t manage to mix up my blue and red LEDs several times, why’d you ask?).
One last thing I leave you with - something that has helped me a lot during the process and I would recommend to anyone else who’s completely new to setting up the parts and wiring of an Arduino project.
Write down comments about what goes where, with which color wire, and into which pin as a comment in your code! Somewhere where it doesn’t get in the way, like at the very bottom. You can always remove it later, and you’ll always have it nearby as a reference when you’re working on your project.


