Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO and AS7262 Color Change Detection System with Bluetooth and OLED Display

View and Edit Project In Cirkit DesignerImage of Arduino UNO and AS7262 Color Change Detection System with Bluetooth and OLED Display

Circuit Documentation

Summary

This project is an Automatic Color Change Detection and Timing system. It detects a color change in a solution, times the change, provides a sound cue, and sends the time to a computer via Bluetooth. The timer can be reset, and the sound cue stopped using a Bluetooth keyboard. The system includes an Arduino UNO, a spectral sensor, an OLED display, a NeoPixel ring, a Bluetooth module, a piezo buzzer, and various passive components.

Component List

  1. SparkFun Spectral Sensor Breakout - AS7262 Visible (Qwiic)

    • Description: A spectral sensor used for detecting color changes.
    • Pins: 3.3V, LED_DRV, CTS, DTR, GND, TX/SDA, RX/SCL, !INT, !RST
  2. Arduino UNO

    • Description: The main microcontroller used for processing and control.
    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
  3. 0.96" OLED

    • Description: A small OLED display used for visual output.
    • Pins: GND, VDD, SCK, SDA
  4. 1N4007 Rectifier Diode

    • Description: A diode used for rectification.
    • Pins: Cathode, Anode
  5. NPN Transistor (EBC)

    • Description: A transistor used for switching.
    • Pins: emitter, base, collector
  6. Resistor

    • Description: A resistor with a resistance of 1000 Ohms.
    • Pins: pin1, pin2
  7. Adafruit 24 NeoPixel Ring

    • Description: A ring of 24 NeoPixels used for visual effects.
    • Pins: VDD, OUT, IN, GND
  8. HC-05 Bluetooth Module

    • Description: A Bluetooth module used for wireless communication.
    • Pins: Key, VCC, TXD, RXD, State, GND
  9. Piezo Buzzer

    • Description: A buzzer used for sound cues.
    • Pins: pin 1, pin 2
  10. Electrolytic Capacitor

    • Description: A capacitor with a capacitance of 1000 Farads.
    • Pins: -, +

Wiring Details

SparkFun Spectral Sensor Breakout - AS7262 Visible (Qwiic)

Arduino UNO

0.96" OLED

Adafruit 24 NeoPixel Ring

HC-05 Bluetooth Module

Piezo Buzzer

NPN Transistor (EBC)

Resistor

Electrolytic Capacitor

Code Documentation

/*
 * Project: Automatic Color Change Detection and Timing
 * Description: This project detects a color change in a solution, times the change,
 *              provides a sound cue, and sends the time to a computer via Bluetooth.
 *              The timer can be reset and the sound cue stopped using a Bluetooth
 *              keyboard.
 * Enhancements: Optimized color detection, debouncing logic, enhanced Bluetooth
 *               communication, and logging for diagnostics.
 */

#include <Adafruit_AS726x.h>
#include <Wire.h>
#include <SoftwareSerial.h>

Adafruit_AS726x ams;
SoftwareSerial BTSerial(10, 11); // RX, TX

const int buzzerPin = 9;
const int buttonPin = 2;

unsigned long startTime = 0;
bool colorChanged = false;
bool timing = false;

const int debounceDelay = 50; // milliseconds
unsigned long lastDebounceTime = 0;
int lastButtonState = HIGH;

unsigned long previousMillis = 0;
const long interval = 1000; // 1 second

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(buzzerPin, OUTPUT);
  BTSerial.begin(9600);
  if (!ams.begin()) {
    Serial.println("AS726x not detected. Check wiring.");
    while (1);
  }
  ams.enableIndicatorLED(true);
  ams.setMeasurementMode(AS726x_4CHAN_2);
  ams.setGain(AS726x_GAIN_64X);
  ams.setIntegrationTime(50);
}

void loop() {
  int buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState) {
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (buttonState == LOW) {
      resetTimer();
    }
  }
  lastButtonState = buttonState;

  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    ams.startMeasurement();
    if (ams.dataReady()) {
      uint16_t r = ams.readRed();
      uint16_t g = ams.readGreen();
      uint16_t b = ams.readBlue();
      if (detectColorChange(r, g, b)) {
        if (!timing) {
          startTime = millis();
          timing = true;
        } else if (!colorChanged) {
          unsigned long elapsedTime = millis() - startTime;
          colorChanged = true;
          soundCue();
          sendTime(elapsedTime);
        }
      }
    }
  }
}

bool detectColorChange(uint16_t r, uint16_t g, uint16_t b) {
  static uint16_t prevR = 0, prevG = 0, prevB = 0;
  if (abs(r - prevR) > 50 || abs(g - prevG) > 50 || abs(b - prevB) > 50) {
    prevR = r;
    prevG = g;
    prevB = b;
    return true;
  }
  return false;
}

void soundCue() {
  tone(buzzerPin, 1000);
  delay(1000);
  noTone(buzzerPin);
}

void sendTime(unsigned long time) {
  BTSerial.print