This project involves creating a photoresistor-based actuator control system using an Arduino. The system calibrates the baseline light level for 5 seconds upon startup. It continuously monitors light levels using a photoresistor, and when a significant change in light level (exceeding a predefined threshold) is detected, it triggers an actuator to extend for 4 seconds. After extending, the actuator enters a 2-minute cooldown period before retracting for another 4 seconds. The process then resets, ready to react to new changes in light levels. The control of the actuator is managed via relays. The issue I am having is that the actuator does not move once the photoresistor detects light change and the relays trigger in the sequence described above (e.g. relay 1 = extend actuator for 4 seconds when photoresistor detects light change, relay 2 = after 2 minutes retrack the actuator for 4 seconds). Using a multimeter I have examined readings between the different ports on the relays as the trigger and they seem to be changing the polarity across these as intended. I cannot figure out why the actuator will not move and need some guidance. Below I list the items used, a wiring description, and the code for the project. Any suggestions would be greatly appreciated.
Item List:
- Linear actuator: Linear Motion Actuators DC 12V 100mm Internal Limit Switch Low Noise Electric Linear Actuator for Industrial Agriculture(Stroke 100mm-15mm/s-50N)
- 2 channel relay: AEDIKO DC 5V Relay Module 2 Channel Relay Board with Optocoupler Support High or Low Level Trigger
- Photoresistor
- 12v battery
- Arduino board
Wiring:
- Power the Relay Module:
o Connect the DC+ on the relay module to the 5V output on the Arduino board.
o Connect the DC- on the relay module to the GND on the Arduino board. - Connect the Relay Control Pins:
o Connect IN1 on the relay module to the digital pin 6 on the Arduino board.
o Connect IN2 on the relay module to the digital pin 7 on the Arduino board. - Connect the Actuator to the Relays:
o Connect COM1 on Relay 1 to the positive lead of the actuator.
o Connect COM2 on Relay 2 to the negative lead of the actuator. - Connect the Power Supply to the Relays:
o Connect NO1 on Relay 1 to the positive terminal of the 12V power supply.
o Connect NC1 on Relay 1 to the negative terminal of the 12V power supply.
o Connect NO2 on Relay 2 to the negative terminal of the 12V power supply.
o Connect NC2 on Relay 2 to the positive terminal of the 12V power supply.
IDE Code:
` // Define change threshold (adjust as needed based on readings)
const int changeThreshold = 50; // Adjust this based on photoresistor readings
// Variables to track the actuator state
bool actuatorExtended = false;
// Time in milliseconds to extend the actuator and cooldown period
const unsigned long extendTime = 4000; // 4 seconds
const unsigned long cooldownTime = 120000; // 2 minutes
// Variable to store the time when the relay was activated
unsigned long activationTime = 0;
// Variables for baseline light level calculation
long accumulatedLightLevel = 0;
int calibrationReadings = 0;
int baselineLightLevel = 0;
// Calibration time
const unsigned long calibrationTime = 5000; // 5 seconds
unsigned long startTime;
// Pin definitions
const int photoresistorPin = A0; // Adjust as needed
const int relay1 = 6; // Adjust as needed for IN1
const int relay2 = 7; // Adjust as needed for IN2
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Set up the photoresistor pin
pinMode(photoresistorPin, INPUT);
// Set up the relay pins
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
// Ensure all relays are off at the start
digitalWrite(relay1, LOW); // Active HIGH, so LOW means off
digitalWrite(relay2, LOW); // Active HIGH, so LOW means off
Serial.println("Relays initialized to OFF state.");
// Initialize calibration
startTime = millis();
}
void loop() {
unsigned long currentTime = millis();
// Calibration phase
if (currentTime - startTime <= calibrationTime) {
// During calibration, accumulate the baseline light level readings
int reading = analogRead(photoresistorPin);
accumulatedLightLevel += reading;
calibrationReadings++;
Serial.print("Calibration reading: ");
Serial.println(reading);
return; // Skip the rest of the loop during calibration
} else if (baselineLightLevel == 0 && calibrationReadings > 0) {
// Calculate the average baseline light level after calibration
baselineLightLevel = accumulatedLightLevel / calibrationReadings;
Serial.print("Baseline Light Level: ");
Serial.println(baselineLightLevel);
}
// Read the photoresistor value
int sensorValue = analogRead(photoresistorPin);
int difference = sensorValue - baselineLightLevel;
// Print the sensor value and baseline to the serial monitor
Serial.print("Sensor Value: ");
Serial.println(sensorValue);
Serial.print("Baseline Light Level: ");
Serial.println(baselineLightLevel);
Serial.print("Difference: ");
Serial.println(difference);
// Additional debug information
Serial.print("Actuator state (extended): ");
Serial.println(actuatorExtended);
// Check for actuator extension
if (!actuatorExtended && abs(difference) >= changeThreshold) {
Serial.println("Condition met to extend actuator."); // Add this line
// Extend actuator
digitalWrite(relay1, HIGH); // Turn on relay 1 to extend actuator (active HIGH)
digitalWrite(relay2, LOW); // Ensure relay 2 is off
Serial.println("Relay 1 set to HIGH (ON), Relay 2 set to LOW (OFF)");
activationTime = currentTime; // Record the time the relay was activated
actuatorExtended = true; // Update the state to indicate actuator is extending
Serial.println("Actuator extending... Relay 1 ON, Relay 2 OFF");
// Wait for the actuator to extend
delay(extendTime);
digitalWrite(relay1, LOW); // Turn off the relay
Serial.println("Actuator fully extended. Relay 1 OFF.");
} else {
Serial.println("Condition not met to extend actuator.");
}
// Check if the cooldown period has ended and retract the actuator
if (actuatorExtended && currentTime - activationTime >= cooldownTime) {
Serial.println("Condition met to retract actuator."); // Add this line
// Retract actuator
digitalWrite(relay1, LOW); // Ensure relay 1 is off
digitalWrite(relay2, HIGH); // Turn on relay 2 to retract actuator (active HIGH)
Serial.println("Relay 2 set to HIGH (ON), Relay 1 set to LOW (OFF)");
Serial.println("Retracting actuator... Relay 2 ON, Relay 1 OFF");
delay(extendTime); // Wait for the actuator to retract
digitalWrite(relay2, LOW); // Turn off the retracting relay
actuatorExtended = false; // Reset the actuator state for next activation
Serial.println("Actuator retracted and ready for next cycle. Relay 2 OFF.");
} else if (actuatorExtended) {
Serial.print("Cooldown time remaining: ");
Serial.println(cooldownTime - (currentTime - activationTime));
}
// Small delay to avoid excessive sensor reading
delay(100);
}
`