Arduino crashing at random

Hi there,

I have two Arduino boards now (one using ATMega8 and one using ATMega168). The boards are both collecting infromation from 3 sensors connected to a bike and sent down the USB cable to a computer. The newer one (ATMega168) seems to be resetting, almost at random, under normal use. I have tried switching everything but the board to the stuff which I know works on the other board but it still messes up.

Basically it's sending data fine and then just stops, and the lights stop flashing for a few seconds, and then it starts sending data again. Occasionally it has spat out a load of random characters, but this only happened once.

Any ideas?!

An urgent reply would be much appreciated

My top suspects for such behavior: - Overloading the voltage regulator. Touch the regulator. Is it warm? Is it hot? A cool regulator does not have this problem. One that is overloaded will be a bit on the uncomfortable side to hold your finger against it. This is reasonably unscientific, but a good first test. Try reducing the load... you mention lights... maybe use less? - Glitch in your power supply. - Intermittent short circuit somewhere that suddenly draws too much power. - Ran out of SRAM. When you exhaust your SRAM you usually end up with a corrupted stack and a PC that jumps into some random space which hopefully eventually culminates in something like a reset. - And the unlikely, but I've done it... you have the watchdog timer fuses set. Arduino's don't come this way, but if you built your own hardware from parts it could happen. It would probably reset at exactly the same time after boot each time.

The lights I referred to were the TX/RX lights. What value resistor should I be using? Currently I am using one of these http://www.maplin.co.uk/Search.aspx?criteria=h8r2&source=15&SD=Y

The 5V from the board is connected to a number of switches/sensors, which then come back to the board to ground (through a resistor like the one I linked to above) and into the analog pins.

What causes the board to run out of SRAM? The code I'm using is as follows;

// Cycopath bike interface code for running on Arduino USB interface board
// Code created by Ben Organ of Piccadilly

// Set up variables
int on = 0;
int i = 1;
int angle = 0;
int value = 0;
int button = 0;
int buttonon = 0;
int time = 0;
int count = 0;

// Set up serial connection and digital pin input
void setup()
{
  beginSerial(9600);
  pinMode(7, INPUT);
}

// Main loop of execution
void loop()
{
  // Read from the pins 
  value = analogRead(0);
  angle = analogRead(1);
  button = digitalRead(7);

  // If button is down
  if (button != 0)
  {
    time++;
    // Set the button to be on
    buttonon = 1;
  }
  else
  {
    // If the button was pressed
    if (buttonon == 1)
    {
      // If the button was pressed for more than a second
      if (time > 100)
      {
        // Long button press
        Serial.print("Y,");
        time = 0;
        buttonon = 0;
      }
      else
      {
        // Short button press
        Serial.print("Z,");
        time = 0;
        buttonon = 0;
      }
    }
  }
  
  // If reed switch is closed
  if (value > 500)
  {
    // If value wasn't high previously count a new magnet
    if (on == 0)
    {
      count ++;
    }

    on = 1;
  }
  else
  {
    on = 0;
  }

  // Every 20 times round the loop
  if (i % 20 == 0)
  {
    Serial.print("A");
    Serial.print(angle);
    Serial.print(",");
  }

  // Every 100 times round the loop
  if (i % 100 == 0)
  {
    Serial.print("S");
    Serial.print(count);
    Serial.print(",");
    count = 0;
    i = 1;
  }

  i++;

  // Wait
  delay(1);
}

I bought the board preassembled.

Nothing in your code is expanding SRAM use as it runs and you aren't allocating enough to run out, so that is not the problem.

Might those resistors be drawing too much current and causing the voltage regulator to overload?

It depends on their resistance. The one in the picture is a 4.5k ohm resistor which won't be a problem, but frequently resistors in that package are much smaller values that could be a problem. For reference a 50 ohm resistor would send 100mA through the switch. Far more than is needed for a pullup or pulldown, but still not so much as to cause a problem. Hopefully they are much larger. 10k is a nice value for a switch pullup or pulldown on an Arduino.

I have 3 strong magnets which pass a reed switch to close the circuit and sense the wheel rotation. Might this be inducing a current and causing the board to reset?

The resistor has 10W 8 (Ohm) 2 J written on it... does this mean anything?

A quick response would be much appreciated (this is for my University project which is due next week!)

We have a winner! An 8 ohm resistor will pass 625mA when you connect it from 5v to ground with a switch. You want something like a 200 to 10k ohm resistor in that application.

Excellent, I'll get another few resistors tomorrow and let you know how it goes!

Thanks for your help.

If it's a switch pullup resistor it could be a much smaller resistor couldn't it?

The resistor has 10W 8 (Ohm) 2 J written on it... does this mean anything?

A quick response would be much appreciated (this is for my University project which is due next week!)

@mrmeval

the Atmega input pins need tiny amounts of current to work properly.. so a pullup resistor only needs to provide tiny amounts of current. The 8 ohm (!) resistor mentioned above is providing (or perhaps the better word is "wasting") huge amounts of current, and destabilizing the power supply as it does so. You really only need a 10K or 4.7K resistor for a pullup.

D