This circuit is designed to control a fan and a set of LEDs (red and green) based on input from a capacitive touch sensor and an HC-SR04 ultrasonic sensor. The Arduino Nano serves as the central microcontroller, processing sensor inputs and controlling the output devices accordingly. The circuit includes a power supply system that utilizes a 12v power supply and an MP1584EN power regulator board to provide the necessary voltages to the components. An NPN transistor is used to switch the fan on and off, and a resistor is included to limit current to the LEDs.
LED (Red)
LED (Green)
Capacitive Touch Breakout
HC-SR04 Ultrasonic Sensor
12v Power Supply
MP1584EN Power Regulator Board
#include <Wire.h>
const int trigPin = 5;
const int echoPin = 4;
const int fanSignal = 9;
const int button = 2;
float distance , duration;
const int greenLED = 10;
const int redLED = 11;
bool startSeq = false;
bool buttonState = false;
bool prevButtonState = false;
unsigned int lastDebounceTime;
const int debounceDelay = 30;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(fanSignal, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(button, INPUT);
digitalWrite(redLED, HIGH); // Assuming redLED is the yellow LED (ledy)
delay(2000);
}
void loop() {
// Read the state of the button
int currentButtonState = digitalRead(button);
// Check if the button state has changed
if (currentButtonState != prevButtonState) {
// Reset the debouncing timer
lastDebounceTime = millis();
}
// If the current time is greater than the last debounce time plus the debounce delay, proceed
if ((millis() - lastDebounceTime) > debounceDelay) {
// If the button state has changed and is LOW (pressed)
if (currentButtonState == LOW && prevButtonState == HIGH) {
Serial.println("Button pressed");
digitalWrite(greenLED, LOW);
startSeq = !startSeq;
analogWrite(fanSignal, 0);
}
}
// Save the current state as the last state, for next time through the loop
prevButtonState = currentButtonState;
if (startSeq) {
digitalWrite(redLED, LOW);
delay(100);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration * .0343) / 2;
Serial.print("Distance: ");
Serial.println(distance);
Control(distance);
fanstatus();
delay(500);
}
}
// Fan Operating controller
void Control(float distance) {
float x;
if (distance >= 50.00) {
x = 50;
} else {
x = distance;
}
float fanSpeed = map(x, 0, 50, 135, 255);
// map variable y from 0-50 to 127-255 pwm range values
analogWrite(fanSignal, fanSpeed);
Serial.println(fanSpeed);
unsigned int fanSpeedPercent = map(fanSpeed, 140, 255, 1, 99);
Serial.println(fanSpeedPercent);
delay(100);
}
// Function for Green LED "ON"
void fanstatus() {
Serial.println("Fan: On");
digitalWrite(greenLED, HIGH);
}
This code is designed to run on an Arduino Nano and controls the fan speed and LED states based on the distance measured by the ultrasonic sensor and the state of the capacitive touch sensor. The red LED is turned on at the start and then controlled by the touch sensor. The fan's speed is adjusted according to the distance measured, and the green LED indicates when the fan is on. The button's state is debounced to ensure stable operation.