Startup Checks

When you’re making a microcontroller circuit that drives a high current load like a motor or an incandescent light, it’s not uncommon to make a mistake and create a circuit that draws too much electrical energy on startup and causes the microcontroller to reset itself constantly. To debug this, it helps to put a routine in your startup() function so you can see when the microcontroller has started up. For example, I often blink an LED three times in the startup. If the LED blinks three times then stops, I know the microcontroller has successfully run the startup and gone into its main loop. If it blinks constantly, I know the microcontroller is continually resetting, so there’s a problem.

Hans Steiner recently showed me his trick for checking for the startup routine on the Arduino: he writes to the microcontroller’s EEPROM, or permanent memory, and reads back the result. Every time the Arduino resets, it’ll increment the EEPROM value. Since this value is stored even when the Arduino is not powered, you’re going to get a new number each time it’s run.

Thanks to Hans for the code.

Technorati Tags: ,


/*
  EEPROM reset checker
  By Hans Steiner

  Reads a value from the EEPROM on startup, prints it out,
  and increments the value then stores it back in EEPROM.
  This is a handy way to check for resetting programs, because the 
  EEPROM value will only be incremented and stored in memory on startup.

  Created 25 Oct. 2007
*/
  
#include <EEPROM.h>

int potPin = 5;       // select the input pin for the potentiometer
int motorPin = 9;     // select the pin for the LED
int value = 0;        // variable to store the value coming from the sensor
int resetCountByte = 36;

void setup() {
  // open the serial port
  Serial.begin(9600);
  // read the last value stored in the EEPROM:
  value = EEPROM.read(resetCountByte);
  // send it out the serial port:
  Serial.println(value);
  // increment it and write it back to the EEOPROM:
  EEPROM.write(resetCountByte, value + 1);

  pinMode(motorPin, OUTPUT);  // declare the motor as an OUTPUT
  pinMode(13, OUTPUT);        // declare the LED pin as an output
  digitalWrite(13,HIGH);      // turn on the LED
}

void loop() {
  // you can do anything you want to here.
}