digitalWrite causes Arduino Pro Mini to Freeze or Reboot

I am using digital output Pin 9 of an Arduino Pro Mini, 5V, 16 MHZ to drive a relay, which periodically turns on and off a small motor. The relay uses 28 mA of current. The motor is powered from the VCC and its current does not go through the digital pins. The motor's starting current is about 1.2 A then quickly reduces to about 0.5A.

The problem is that the Arduino always freezes after a few on/off cycles, or rebooting itself then freezes after a couple of cycles. The Arduino runs again after the power is turned off then on, but it freezes again quickly. The sketch is below.

If the digitalWrite(RELAY, HIGH) line is commented out, the codes runs for a long time without problem.

If I extend the wait time between digitalWrite to HIGH to 600 seconds, it runs more cycles but still freezes within about 20 cycles.

I have also tried to reduce the current per pin, by defining 5 RELAY pins (pins RELAY_A for pin 9, RELAY_B for pin 8 etc), and replicate the digitalWrite(PIN, HIGH) line to write the 5 pins at the same time. This did not prevent the Arduino from freezing or rebooting.

What would be the cause for the random freezing/rebooting?

Thanks very much.

Sketch:

#define RELAY 9 

int run = 2;
int cycle = 10;  //sec
unsigned int count = 0;

void setup() {
  pinMode(RELAY, OUTPUT);
  Serial.begin(19200);
  Serial.println("\n\nBegin ******\n\n");
}

void loop() {
  count++;
  Serial.print("No. "); Serial.println(count);
  digitalWrite(RELAY, HIGH);                           //line that causes freezing or restarting
  for (int i = 0; i < run; ++i) {
    Serial.print("  HIGH ");     Serial.println(i+1);
    delay(1000);
  }
  digitalWrite(RELAY, LOW);

  for (int i = 0; i < cycle - run; ++i) {
    Serial.print(" LOW "); Serial.println(i);
    delay(1000);
  }
}

Are you driving a relay coil directly from an Arduino output pin? That's all kinds of bad.

Flyback diodes?

The motor is powered from the VCC

Totally inadequate power supply. For motors and servos, use a separate power supply.

Very much appreciate the replies! It seems that the transients from the relay operation would be the cause for the freezing/rebooting issue, since when I increased the holding time (the "run" variable in the sketch) after digitalWrite to HIGH the Arduino runs for a long time without problem. I will test with a MOSFET controlled by the digital pin, and a flyback diode around the relay.

On the motor, should a flyback diode also be added between the motor terminals?

The power for the motor is from the same 5V header as for the VCC, but via a separate copper trace on the PCB. The power supply is 5V, 2A. With the PCB already made the power to the Arduino and the motor can only be from the same power supply. The Arduino ground and the negative terminal of the motor are connected.

Always, and capacitors.

The power for the motor is from the same 5V header as for the VCC

Not a good idea. Power supply decoupling is required for reliable operation of shared motor and digital circuit power.

I added a separate power supply to the motor, bypassed the relays and used an H-Bridge, which has flyback diodes included. Now the system is working well, no freezing or restarting issues. Many thanks to all for the help!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.