Color Detection with TCS34725 Sensor and Arduino: Fun Projects & Easy Coding

Поділитися
Вставка
  • Опубліковано 26 січ 2025

КОМЕНТАРІ • 4

  • @pedrinho1393
    @pedrinho1393 2 дні тому

    In this line" g > b && g > r" the sensor will detect only the color green, or he Will detect other colors like black and white?

    • @BMonsterLaboratory
      @BMonsterLaboratory  День тому +1

      The condition only detects green as the dominant color. It does not detect black, white, or grayscale shades where all values are equal or close to equal.
      Try something like this:
      const int BLACK_THRESHOLD = 50;
      const int WHITE_THRESHOLD = 200;
      const int GRAYSCALE_DIFF_THRESHOLD = 10;
      // detect black
      if (r < BLACK_THRESHOLD && g < BLACK_THRESHOLD && b < BLACK_THRESHOLD) {
      return "BLACK";
      }
      // detect white or grayyscale
      if (abs(r - g) < GRAYSCALE_DIFF_THRESHOLD && abs(g - b) < GRAYSCALE_DIFF_THRESHOLD && r > WHITE_THRESHOLD) {
      return "WHITE";
      }
      This may be a good starting point to experiment with adjustments to the scale. 👍

  • @turin3271
    @turin3271 6 місяців тому +1

    Cho mình xin code ạ

    • @BMonsterLaboratory
      @BMonsterLaboratory  6 місяців тому +1

      here is project 2 from the video:
      đây là dự án 2 từ video:
      //project 2 RGB sensor w/ IR
      #include
      #include "Adafruit_TCS34725.h"
      const int IR_PIN = 10; // Pin connected to the HW201 IR module OUT
      const int LED_PIN = 9; // Pin connected to the onboard LED control pin of the TCS34725
      // Create an instance of the TCS34725 sensor
      Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_614MS, TCS34725_GAIN_1X);
      bool objectDetected = false;
      void setup() {
      Serial.begin(9600);
      pinMode(IR_PIN, INPUT);
      pinMode(LED_PIN, OUTPUT);
      digitalWrite(LED_PIN, LOW); // Turn off LED initially
      if (tcs.begin()) {
      Serial.println("Found TCS34725 sensor");
      // Ensure the LED is off by default
      tcs.setInterrupt(true);
      } else {
      Serial.println("No TCS34725 found ... check your connections");
      while (1); // halt the program
      }
      }
      void loop() {
      // Read the IR sensor
      int irValue = digitalRead(IR_PIN);
      if (irValue == LOW) { // Assuming LOW means object detected
      if (!objectDetected) {
      objectDetected = true;
      Serial.println("Object detected, taking color reading...");
      delay(1000); //delay for 1sec before turn on LED
      // Turn on the LED
      digitalWrite(LED_PIN, HIGH);
      delay(500); // Give some time for the LED to stabilize
      uint16_t r, g, b, c;
      tcs.getRawData(&r, &g, &b, &c);
      // Normalize the values
      float sum = c;
      float red = r / sum;
      float green = g / sum;
      float blue = b / sum;
      Serial.print("R: "); Serial.print(r); Serial.print(" ");
      Serial.print("G: "); Serial.print(g); Serial.print(" ");
      Serial.print("B: "); Serial.print(b); Serial.print(" ");
      Serial.print("C: "); Serial.print(c); Serial.println(" ");
      // Calculate the color temperature and lux
      uint16_t colorTemp = tcs.calculateColorTemperature(r, g, b);
      uint16_t lux = tcs.calculateLux(r, g, b);
      Serial.print("Color Temp: "); Serial.print(colorTemp, DEC); Serial.print(" K - ");
      Serial.print("Lux: "); Serial.println(lux, DEC);
      // Determine the color
      if (red > green && red > blue) {
      Serial.println("Detected Color: RED");
      } else if (green > red && green > blue) {
      Serial.println("Detected Color: GREEN");
      } else if (blue > red && blue > green) {
      Serial.println("Detected Color: BLUE");
      } else {
      Serial.println("Detected Color: UNKNOWN");
      }
      // Turn off the LED
      digitalWrite(LED_PIN, LOW);
      delay(1000); // Wait for some time before the next reading
      }
      } else {
      if (objectDetected) {
      objectDetected = false; // Reset object detected flag
      }
      delay(100); // Small delay to prevent rapid loop execution
      }
      }
      You can also find this post on Facebook by searching "colordetect" in Facebook.
      Bạn cũng có thể tìm thấy bài đăng này trên Facebook bằng cách tìm kiếm "colorDetect" trên Facebook.