#define BLYNK_TEMPLATE_ID "TMPL32EY8iTuB"
#define BLYNK_TEMPLATE_NAME "ColorSense2"
#define BLYNK_AUTH_TOKEN "8y2fa7A3pDDLfmywpG-ghz9phWChMRqs"
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
WidgetLED ledr(V0);
WidgetLED ledg(V1);
WidgetLED ledb(V2);
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "VIVO";
char pass[] = "123456789";
#define S0 D1
#define S1 D2
#define S2 D3
#define S3 D4
#define sensorOut D5
int redFreq, greenFreq, blueFreq;
String detectedColor = "";
BlynkTimer timer;
void readColours() {
// Set frequency scaling to 20%
digitalWrite(S0, HIGH);
digitalWrite(S1, LOW);
// Red filter
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
redFreq = pulseIn(sensorOut, LOW);
// Green filter
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
greenFreq = pulseIn(sensorOut, LOW);
// Blue filter
digitalWrite(S2, LOW);
digitalWrite(S3, HIGH);
blueFreq = pulseIn(sensorOut, LOW);
Serial.print("Red: "); Serial.print(redFreq);
Serial.print(" Green: "); Serial.print(greenFreq);
Serial.print(" Blue: "); Serial.println(blueFreq);
// Detect color by finding lowest frequency (highest light intensity)
if (redFreq < greenFreq && redFreq < blueFreq && redFreq < 20) {
Serial.println("Red color detected");
detectedColor = "RED";
}
else if (greenFreq < redFreq && greenFreq < blueFreq) {
Serial.println("Green color detected");
detectedColor = "GREEN";
}
else if (blueFreq < redFreq && blueFreq < greenFreq) {
Serial.println("Blue color detected");
detectedColor = "BLUE";
} else {
detectedColor = "UNKNOWN";
}
Blynk.virtualWrite(V3, detectedColor);
// Turn LEDs on/off accordingly
ledr.off();
ledg.off();
ledb.off();
if (detectedColor == "RED") {
ledr.on();
}
else if (detectedColor == "GREEN") {
ledg.on();
}
else if (detectedColor == "BLUE") {
ledb.on();
}
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi Connected!");
Blynk.begin(auth, ssid, pass);
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(sensorOut, INPUT);
digitalWrite(S0, HIGH);
digitalWrite(S1, LOW);
Serial.println("TCS3200 Color Sensor - Detect lowest frequency color");
timer.setInterval(1000L, readColours);
}
void loop() {
Blynk.run();
timer.run();
}