Random Resets

I have made a project display with lights, a servo, 2 buttons, and a continuous motor.There appears to be a random bug, and nothing is touching; everything is plugged in correctly, and the project works most of the time, but every once in a while the board (ATMega 2500 on an Arduino Mega) randomly resets when I push the blue button (labeled in the code). The other code runs fine all the time. Please help explain why this issue is happening.

#include <Servo.h>

Servo servo;
Servo motor;

int motorpin = 11;
int servopin = 8;

int blueBtnPin = 53;
int greenBtnPin = 9;

bool shake = true;

int red1 = 21;
int red2 = 14;

int blue_streak[10] = {27, 31, 35, 39, 43, 47, 48, 49, 50, 51};

void setup() {
  for(int i = 0; i < 9; i++)
    pinMode(blue_streak[i], OUTPUT);
  pinMode(red1, OUTPUT);
  pinMode(red2, OUTPUT);
  pinMode(blueBtnPin, INPUT);
  pinMode(greenBtnPin, INPUT);

void loop() {
  int blueBtn = digitalRead(blueBtnPin);
  if(blueBtn) {
    for(int idx = 0; idx < 4; idx++) {  
      for(int i = 0; i < 12; i++) {
        if(shake) {
          shake = false;
        } else {
          shake = true;
        if(i > 1)
          digitalWrite(blue_streak[i - 2], LOW);
        if(i < 10)
          digitalWrite(blue_streak[i], HIGH);
    digitalWrite(red1, HIGH);
    digitalWrite(red1, LOW);
  int greenBtn = digitalRead(greenBtnPin);
  if(greenBtn) {
    digitalWrite(red2, HIGH);
    digitalWrite(red2, LOW);

once in a while the board (ATMega 2500 on an Arduino Mega) randomly resets

How are the motor and the servo powered ? Not directly from the Arduino I hope .....

Random resets are usually because of attempting to power the Arduino and the servos (and any motors) from the same inadequate power supply. There should be separate supplies and a common ground. Bypass capacitors and filter capacitors to handle noisy loads like servos and motors are also highly recommended.

By the way, delay(..) is dangerous. Nothing else can happen for two seconds during a delay(2000), for example.

By the way, it would appear that digitalRead(..) returns 0 or 1, so code like


is generally safe. However, the documentation for digitalRead(..) only guarantees a return value of LOW or HIGH, whatever those may be.

Thank you for the information. Yes, I have been powering the motors and servos from the Arduino. If this is the issue, I will need to fix this where I can power the Arduino and the motor both from wall power. The project will be sitting out for people to engage in, and I need it to work every time. The motor seems to have no problem because it only runs for 2 short periods each time, but the servo has to move back and forth rapidly, so it is the one with the problem. What is the easiest way to fix this issue?

"Easiest" depends upon what you have available. It also depends upon the specs of the motor and the servo, which I know nothing about.

Suggestion: Do NOT power the motors and the servos from the Arduino. Use a power supply for the Arduino (as you are doing now), and a separate power supply for the motors and the servos. This separate supply must have the right voltage to work with your motors and servos, and provide sufficient current (amps or milliamps) to handle the worst case of all of your motors and servos simultaneously. The grounds of the two supplies should be connected together.

What if I have no separate power supply? Is there another way?

Maybe. You still haven't told us anything about the motors or the servos. Knowing how you have it wired might be helpful as well.

The Arduino is not intended for powering motors or servos.

By the way, the separate supply could be batteries. Maybe not good for a permanent setup, but it could be a demonstration that the power is an issue.

I am going away for at least 18 hours, but perhaps someone else wants to join in.

I am using one of the general 180 degree servos that come in the MAKE:Arduino kits, and my motor is a high-speed continuous servomotor from parallax.

I don't know about MAKE:Arduino kits, but I notice that the web page for a high-speed continuous servomotor from Parallax says:

Note: Servo current draw can spike while under load. Be sure that your application's power supply and voltage regulator is prepared to supply adequate current for all servos used. Do not try to power this servo directly from a BASIC Stamp module's Vdd or Vin pins; do not connect the servo's Vss line directly to the BASIC Stamp module's Vss pin.

If you substitute "Arduino" for "BASIC Stamp", you will understand what NOT to do.

I was really looking for electrical specifications for your servo and for your motor: Supply Voltage, Current

It appears that the Parallax continuous rotation servomotor (Product ID 900-00008) is specified for 4-6 volts at 15 to 200 mA.

I have no idea about the specifications for your servo.

Here are my suggestions:

1) Power the servo and the motor from a battery (three 1.5 Volt D cells wired in series should do it) with the battery ground tied to the Arduino ground. This may not work as a permanent solution but it will prove the concept.

2) Wire 0.1 microfarad capacitors (rated for at least 10 volts) across the supply leads of the servo, and across the supply leads of the motor. The leads of the capacitors should be short, and the connections should be as close to the servo and as close to the motor as possible. These capacitors will help IF AND ONLY IF it is noise from the motors that is causing the reset.

2 may help if the issue is caused by electrical noise. #2 will NOT help if the resets are caused by excessive current.

Large valued capacitors (thousands of microfarads) might help if the issue is excessive current, but could easily do damage to your Arduino.

vaj4088: By the way, the separate supply could be batteries. Maybe not good for a permanent setup, but it could be a demonstration that the power is an issue.

Have a home automation project where the arduino is plugged into the wall, but the motor is powered with a pair of 6v lantern batteries. It only needs to run twice a day for about a minute (open/close), so a couple of ten-dollar batteries is acceptable - they'll last for a while.

This thread should probably be moved to http://forum.arduino.cc/index.php?board=9.0 . It's not a programming problem.

As @vaj4088 says, use separate power supplies! If you want to fix your problem, follow his advice!

If it happens only when you push the blue button, then it is not "random"

It doesn’t happen EVERY time you press the blue button; it randomly glitches. The thing is it only happens on the blue button not the green button.

I got it working now by supplying a sort of “backup” 9v battery, so it can draw extra power if it needs, but will allow the battery to last the whole day.

Definitely a power supply problem then. Use a separate supply!