Components and supplies
Resistor 10k ohm
Breadboard - 830 contacts
LCD TFT ILI 9341
TCS230 Color Sensor
Push Button
Arduino UNO
Apps and platforms
Arduino IDE
Project description
Code
The RGB Revealer: Unveiling Color with Arduino and TFT (Code)
cpp
TCS3200 reads color, estimates RGB, shows on TFT!
1#include <SPI.h> 2#include <Adafruit_GFX.h> 3#include <Adafruit_ILI9341.h> // This assumes you are using the ILI9341-based TFT display 4 5// Define the pin connections for the TFT display 6#define TFT_CS 10 // Chip select pin 7#define TFT_RST 9 // Reset pin 8#define TFT_DC 8 // Data/Command pin 9#define TFT_MOSI 11 // SPI MOSI (Master Out Slave In) pin 10#define TFT_SCK 13 // SPI SCK (Clock) pin 11#define TFT_MISO 12 // SPI MISO (Master In Slave Out) pin 12 13// Create an instance of the Adafruit_ILI9341 class with specified pin connections 14Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCK, TFT_RST, TFT_MISO); 15 16//push button variables 17int push_button_pin = 4; 18bool button_released = true; 19 20// Define pin numbers for the color sensor and output 21const int out = 7; 22const int s0 = 3; 23const int s1 = 2; 24const int s2 = 6; 25const int s3 = 5; 26 27// Variables to store the frequency / pulse duration for Red, Blue, and Green color 28int pulse_duration_red, pulse_duration_blue, pulse_duration_green; 29 30//Variables to store value of Red, Blue, and Green color 31int red_color, blue_color, green_color; 32 33//Variables to store the lowest and highest pulse duration for Red, Blue, and Green color. 34int min_duration_red, min_duration_blue, min_duration_green; 35int max_duration_red, max_duration_blue, max_duration_green; 36 37 38void setup() 39{ 40 // Initialize the TFT display 41 tft.begin(); 42 // Set display rotation (1 for 90 degrees clockwise) 43 tft.setRotation(1); 44 // Set text size 45 tft.setTextSize(2); 46 47 48 49 //Setting up push button pin 50 pinMode(push_button_pin, INPUT); 51 digitalWrite(push_button_pin, HIGH); 52 53 54 //Setting lowest and highest pulse duration for Red, Blue, and Green color as noted during the test run 55 min_duration_red = 9; 56 max_duration_red = 53; 57 min_duration_blue = 5; 58 max_duration_blue = 40; 59 min_duration_green = 6; 60 max_duration_green = 50; 61 62 // Start serial communication at 9600 bps 63 Serial.begin(9600); 64 65 // Set pin modes for the color sensor and output 66 pinMode(s0, OUTPUT); 67 pinMode(s1, OUTPUT); 68 pinMode(s2, OUTPUT); 69 pinMode(s3, OUTPUT); 70 pinMode(out, INPUT); 71 72 // Set initial conditions for frequency scaling 73 digitalWrite(s0, HIGH); 74 digitalWrite(s1, HIGH); 75} 76 77 78void loop() 79{ 80 //Reading push button state 81 int PinState = digitalRead(push_button_pin); 82 //Detecting the color when push button is pressed. Avoid recalling the color function until push button 83 //released and pressed again. 84 if((PinState == 0) && (button_released == true)){ 85 button_released = false; 86 color(); 87 } 88 if((button_released == false) && (PinState == 1)){ 89 button_released = true; 90 } 91 92} 93 94 95// Function to detect and print the dominant color 96void color() 97{ 98 99 // Set the sensor to detect the red color 100 digitalWrite(s2, LOW); 101 digitalWrite(s3, LOW); 102 //small pause 103 delay(50); 104 // Measure the pulse duration (frequency) for the red color 105 pulse_duration_red = pulseIn(out, LOW); 106 //updating the min pulse duration for red if smaller than previous min pulse duration 107 if(pulse_duration_red < min_duration_red){ 108 min_duration_red = pulse_duration_red; 109 } 110 //updating the max pulse duration for red if smaller than previous max pulse duration 111 if(pulse_duration_red > max_duration_red){ 112 max_duration_red = pulse_duration_red; 113 } 114 115 116 // Set the sensor to detect the blue color 117 digitalWrite(s2, LOW); 118 digitalWrite(s3, HIGH); 119 //small pause 120 delay(50); 121 // Measure the pulse duration (frequency) for the blue color 122 pulse_duration_blue = pulseIn(out, LOW); 123 //updating the min pulse duration for blue if smaller than previous min pulse duration 124 if(pulse_duration_blue < min_duration_blue){ 125 min_duration_blue = pulse_duration_blue; 126 } 127 //updating the max pulse duration for blue if smaller than previous max pulse duration 128 if(pulse_duration_blue > max_duration_blue){ 129 max_duration_blue = pulse_duration_blue; 130 } 131 132 133 // Set the sensor to detect the Green color 134 digitalWrite(s2, HIGH); 135 digitalWrite(s3, HIGH); 136 //small pause 137 delay(50); 138 // Measure the pulse duration (frequency) for the green color 139 pulse_duration_green = pulseIn(out, LOW); 140 //updating the min pulse duration for green if smaller than previous min pulse duration 141 if(pulse_duration_green < min_duration_green){ 142 min_duration_green = pulse_duration_green; 143 } 144 //updating the max pulse duration for green if smaller than previous max pulse duration 145 if(pulse_duration_green > max_duration_green){ 146 max_duration_green = pulse_duration_green; 147 } 148 149 //using the map function to get Red, Blue, Green color values, between 0 and 255, from pulse duration 150 red_color = map(pulse_duration_red, min_duration_red, max_duration_red, 255, 0); 151 green_color = map(pulse_duration_green, min_duration_green, max_duration_green, 255, 0); 152 blue_color = map(pulse_duration_blue, min_duration_blue, max_duration_blue, 255, 0); 153 154 //Using the RGB value as a background color of the TFT screen 155 uint16_t rgbColor = tft.color565(red_color, green_color, blue_color); 156 tft.fillScreen(rgbColor); 157 158 //printing RGB value on tft screen. 159 // Set the cursor position for text drawing (coordinates 50, 50) 160 tft.setCursor(30, 50); 161 //If background is dark, using light font for the text. 162 //If background is light, using dark font for the text. 163 //using luminance formula, R, G, and B are the RGB values normalized to the range [0, 1] 164 if(get_luminance(float(red_color)/255.0, float(green_color)/255.0, float(blue_color)/255.0) > 0.5){ //background is light color 165 //Dark font color for light background 166 // Set text color to black 167 tft.setTextColor(ILI9341_BLACK); 168 } 169 else{//background is dark color 170 //Light font color for dark background 171 // Set text color to white 172 tft.setTextColor(ILI9341_WHITE); 173 } 174 //printing on the TFT 175 tft.print("RGB ( "+String(red_color)+" , "+String(green_color)+" , "+String(blue_color)+" ) "); 176 177} 178 179 180 181float get_luminance(float R, float G, float B){ 182 float luminance = 0.2126 * R + 0.7152 * G + 0.0722 * B; 183 return luminance; 184 }
Downloadable files
The RGB Revealer: Circuit
TCS3200 reads color, estimates RGB, shows on TFT!
circuit.png
Documentation
The RGB Revealer: Circuit Diagram
TCS3200 reads color, estimates RGB, shows on TFT!
Color Sensor.png
Comments
Only logged in users can leave comments