I've got a simple circuit to control a solenoid when motion is detected from a BISS0001 PIR sensor.
Here's the code for the sake of completeness
int pirPin = A1;
int relay1 = 6;
int relay2 = 7;
int sensorValue = 0;
long lastMotionDetect = 0;
bool isOpen = false;
long minOpenTime = 300; // seconds
long minClosedTime = 180; // seconds
void setup() {
// put your setup code here, to run once:
pinMode(pirPin, INPUT);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
Serial.begin(115200);
CloseSesame();
}
void loop() {
// put your main code here, to run repeatedly:
sensorValue = analogRead(pirPin);
Serial.println(lastMotionDetect);
delay(200);
if ((lastMotionDetect + (1000 * minOpenTime)) < millis() && isOpen == true)
{
// close the gate
Serial.println("Closing");
CloseSesame();
isOpen = false;
delay(minClosedTime * 1000);
}
if (sensorValue > 100)
{
lastMotionDetect = millis();
if (isOpen == false)
{
Serial.println("Opening");
OpenSesame();
isOpen = true;
}
}
}
void OpenSesame()
{
digitalWrite(relay1, LOW);
digitalWrite(relay2, HIGH);
delay(80);
digitalWrite(relay1, HIGH);
}
void CloseSesame()
{
digitalWrite(relay1, HIGH);
digitalWrite(relay2, LOW);
delay(80);
digitalWrite(relay2, HIGH);
}
The PIR sensor output is logical high at 3.3v when motion is detected, it, so I am using an analog input to detect when the PIR is triggered.
The Arduino is connected to two 5v Songle relays from the Arduino 5v output, the relays drive the solenoid. The solenoid is attached to both relay Common contacts, 12v on the NO and Gnd on the NC contacts.
The code works as expected, however since the solenoid draws approximately 6 amps (for 80 milliseconds when triggered), the input power is noisy and occasionally the Arduino crashes when opening or closing the solenoid.
The whole lot is powered from a 12v 100AH sealed lead acid battery that's kept float charged. I haven't got a spare power supply to separately power the Arudino, so my options are limited to improving the power supply and conditioning.
I've tried an Arduino Uno as well, it's not a faulty board causing the restarts.
I've had a browse of the schematic, I'm wondering if it would help to add a couple of electrolytic capacitors before and after the voltage regulator (eg 1000uF) to help stabilize the voltage?
I can increase the frequency of crashes by reducing the timeouts and cycling the solenoids more often, but the crashes are intermittent so it's tough to troubleshoot.
Help!


