Crashing a Duemilinove with noisy input power

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!

Hi,

We need the circuit diagram that shows your relays, power supplies and solenoid.
Do you have a flywheel diode across the solenoid to prevent problems with back EMF from the coil?
Have you kept the solenoid and its power supply separate to all the other wiring?

Can you please post a picture of your project?

Thanks.. Tom... :slight_smile:

No flywheel diode (to prevent back-EMF) across the solenoid because the polarity of the voltage across solenoid is changed depending on whether it's opening or closing or doing nothing, wired up like this:

It's night time here in Australia, I'll post a pic in the coming days. I haven't drawn schematics on a computer before, what software can I try to use, OS X preferably?

I have approximately 1 meter of 14 or 16GA wiring from the battery to the project, via a 5 amp blade fuse. The power splits at the end of this wire at the Arduino DC jack and to the relays.

Hi,

Hand draw the circuit and photograph it.
Use REPLY rather than QUICK REPLY and use the ATTACHMENT facility.

Tom.... :slight_smile:
There are components to fix that with bidirectional solenoids, its dark here too, g'day mate.

Thanks for taking your time to assist. Here's my fourth attempt at drawing the circuit diagram.

Yep, G'Day from another 'Strayan, down the Mexican corner for me.

Thank you again.

Hi,
I have added a diode to your circuit, try and place it as close as possible to the terminals on the relay board.
It will prevent any BACK-EMF that gets on the supply.

You may have to decouple the 12V supply wire to the DUO.

Tom.... :slight_smile: Central Vic

The Arduino should have no trouble detecting a 3.3v HIGH on its digital pins.

...R

TomGeorge:
Hi,
I have added a diode to your circuit, try and place it as close as possible to the terminals on the relay board.
It will prevent any BACK-EMF that gets on the supply.

You may have to decouple the 12V supply wire to the DUO.

Tom.... :slight_smile: Central Vic

I've added a power diode (I only had a 1n5408) where you've drawn it, across the terminals of the relay contacts.

So far it's not crashing, after your assistance, it's so obvious that's the right place to put it. I just had it fixated in my mind that it should be across the solenoid, as back EMF protection typically are in a single polarity solenoid, motor or inductive load.

Thank you for sharing your knowledge and helping me along.

Robin2 - my thought was to use the CMOS logic levels of
Low <= 1/3rd Vdd
High >= 2/3rds Vdd
With a 5v Vdd, the 3.3v trigger would be somewhere in the nether regions so I used an analog value for certainty. Does the Arduino detect differently?

nooby:
Does the Arduino detect differently?

RTFM

The Atmega 328 datasheet say 0.6 Vcc

...R