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

Arduino UNO Controlled Solenoid Valve with RGB LED and Spectral Sensor

View and Edit Project In Cirkit DesignerImage of Arduino UNO Controlled Solenoid Valve with RGB LED and Spectral Sensor

Circuit Documentation

Summary

This circuit involves an Arduino UNO microcontroller controlling an RGB LED, a solenoid, and an NPN transistor. The circuit also includes a diode, a LifePo4 battery, a 3.3V battery, and a SEN0427 sensor. The Arduino UNO is programmed to control the solenoid valve based on timing and user input, and to measure and log spectral data using the SEN0427 sensor.

Component List

NPN Transistor (EBC)

Diode

LifePo4 3.2V 6Ah

Solenoid

Arduino UNO

RGB LED (Wokwi compatible)

SEN0427

3.3V Battery

Wiring Details

NPN Transistor (EBC)

Diode

LifePo4 3.2V 6Ah

Solenoid

Arduino UNO

RGB LED (Wokwi compatible)

SEN0427

3.3V Battery

Code Documentation

#include "DFRobot_AS7341.h"
#include <math.h>  // For log10()

DFRobot_AS7341 as7341;
int redPin = 4;
int greenPin = 6;
int bluePin = 7;
int valvePin = 8;  // Pin controlling the valve

// Declare the variables here
DFRobot_AS7341::sModeOneData_t data1;
DFRobot_AS7341::sModeTwoData_t data2;
float absorbance;
float transmittance;

// Timing variables for valve control
unsigned long previousMillis = 0;
const long totalInterval = 45000;  // Total interval of 45 seconds
const long openDuration = 1000;   // Valve open duration of 1 second

// Purge mode variables
bool purgeMode = false;
unsigned long purgeStartTime = 0;
const long purgeDuration = 120000;  // 2 minutes (120,000 milliseconds)

void setup(void) {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(valvePin, OUTPUT);  // Set the valve pin as an output
  digitalWrite(valvePin, LOW);  // Ensure the valve is initially closed

  Serial.begin(9600);
  // Detect if IIC can communicate properly
  while (as7341.begin() != 0) {
    Serial.println("IIC init failed, please check if the wire connection is correct");
    delay(1000);
  }

  Serial.println("System ready. Type 'purge' to start purging for 2 minutes.");
}

void setColor(int redValue, int greenValue, int blueValue) {
  analogWrite(redPin, redValue);
  analogWrite(greenPin, greenValue);
  analogWrite(bluePin, blueValue);
}

void loop() {
  // Check for serial input
  if (Serial.available() > 0) {
    String command = Serial.readStringUntil('\n');
    command.trim(); // Remove any leading/trailing whitespace

    if (command.equalsIgnoreCase("purge")) {
      purgeMode = true;
      purgeStartTime = millis();
      Serial.println("Purge mode activated. Solenoid will remain open for 2 minutes.");
      digitalWrite(valvePin, HIGH); // Open the solenoid
    }
  }

  // Handle purge mode
  if (purgeMode) {
    if (millis() - purgeStartTime >= purgeDuration) {
      // End purge mode after 2 minutes
      purgeMode = false;
      digitalWrite(valvePin, LOW); // Close the solenoid
      Serial.println("Purge mode complete. Returning to normal operation.");
    } else {
      // Keep the solenoid open during purge mode
      return;
    }
  }

  // Timed valve control logic (normal operation)
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= total