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);
}
}