I have a project where I need to use arduino to turn on/off all 10 parallel connected fans at the same time. I have bought a 24V relay. Is there anything else I need to buy?
I have bought 2 relays. One is 5V and another one is 24V. I have connected the 5V relay and apparently it works. The seller advised me to use a 5V relay as the Arduino can only output maximum 5V to the relay in order to turn it on and off. Is there still a need to change to 24V relay?
#include <LiquidCrystal.h>
const int relayPin = 6; // Relay pin to control the fan
const int buttonPin = 4; // Button pin
const int greenLedPin = 3; // Green LED pin
const int redLedPin = 5; // Red LED pin
const int piezoPin = 2; // Piezo pin
bool deviceOn = false; // State of the device
unsigned long startTime; // Start time for the fan
unsigned long runTime = 10000; // 30 minutes in milliseconds
// Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
void setup() {
Serial.begin(9600); // Start the Serial communication
pinMode(relayPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // Use internal pull-up resistor
pinMode(greenLedPin, OUTPUT);
pinMode(redLedPin, OUTPUT);
pinMode(piezoPin, OUTPUT);
// Set up the LCD's number of columns and rows
lcd.begin(16, 2);
lcd.print("Press button to");
lcd.setCursor(0, 1);
lcd.print("turn on device");
// Ensure the fan and LEDs are off initially
digitalWrite(relayPin, LOW);
digitalWrite(greenLedPin, LOW);
digitalWrite(redLedPin, HIGH);
}
void loop() {
// Check if the button is pressed
if (digitalRead(buttonPin) == LOW) {
delay(50); // Debounce delay
while (digitalRead(buttonPin) == LOW); // Wait for button release
deviceOn = !deviceOn; // Toggle device state
if (deviceOn) {
lcd.clear();
lcd.print("Device is ON");
delay(1000); // Display message for 1 second
lcd.clear();
lcd.print("Fan is running");
// Turn on the fan and green LED, turn off the red LED
digitalWrite(relayPin, HIGH);
digitalWrite(greenLedPin, HIGH);
digitalWrite(redLedPin, LOW);
// Record the start time
startTime = millis();
} else {
// Emergency stop
lcd.clear();
lcd.print("Emergency Stop");
delay(1000); // Display message for 1 second
lcd.clear();
lcd.print("Press button to");
lcd.setCursor(0, 1);
lcd.print("turn on device");
// Turn off the fan and green LED, turn on the red LED
digitalWrite(relayPin, LOW);
digitalWrite(greenLedPin, LOW);
digitalWrite(redLedPin, HIGH);
// Ensure no sound notification
digitalWrite(piezoPin, LOW);
}
}
// If the device is on, calculate the remaining time
if (deviceOn) {
unsigned long elapsedTime = millis() - startTime;
unsigned long remainingTime = runTime - elapsedTime;
// Update the LCD display with the remaining time
lcd.setCursor(0, 1); // Move to the second line
lcd.print("Time left: ");
lcd.print(remainingTime / 60000); // Display minutes
lcd.print(" m");
// Check if the time is up or emergency stop
if (elapsedTime >= runTime || !deviceOn) {
// Turn off the fan and green LED, turn on the red LED
digitalWrite(relayPin, LOW);
digitalWrite(greenLedPin, LOW);
digitalWrite(redLedPin, HIGH);
// Notify with the piezo if the time is up
if (elapsedTime >= runTime) {
lcd.clear();
lcd.print("Process complete");
for (int i = 0; i < 5; i++) {
digitalWrite(piezoPin, HIGH);
delay(500);
digitalWrite(piezoPin, LOW);
delay(500);
}
}
// Update the device state
deviceOn = false;
lcd.clear();
lcd.print("Press button to");
lcd.setCursor(0, 1);
lcd.print("turn on device");
}
}
}