Program Arduino for Safe Circuit Changes

Previous Topic Subject: Clear EEPROM for Safe Circuit Changes
Hi. I have been getting started with the Arduino and have had previous experience with the Basic Stamp 2(BS2). One of the suggestions I had while working with the BS2 was the clear the EEPROM of the device so that when you changed the circuit and powered the board back up again, you would not end up frying something or even hurting somebody (if your project had the potential; think motors).

I looked for something in the forum using some of the words in the Subject heading above. I started to search the tutorials page and came across the EEPROM Clear link on:

http://arduino.cc/en/Tutorial/HomePage.

This program is for anybody who would like to use it but if you have any ideas about it, please comment.


/*
Program: EEPROM Clear.pde
Purpose: to set all of the bytes of the EEPROM to 0. The LED attached to pin 13 is made to blink to indicate that the program has done its job. The original
version of the program that simply sets the EEPROM to 0 can be found at http://arduino.cc/en/Tutorial/EEPROMClear. The while instruction at the end of the
program puts the board in an endless loop of doing nothing. It also stops the LED from blinking. That idea was found on the Arduino Forum. Try commenting
out the instruction and see how the LED blinks in an infinite loop.

Check out the Pictou County Electronics Club website for fun: http://www.pcecconnect.com
*/

#include <EEPROM.h>

void setup() {
// write a 0 to all 512 bytes of the EEPROM
for (int i = 0; i < 512; i++)
EEPROM.write(i, 0);

// turn on the LED when we're done - the instruction below has been commented after it was tested so that all pins can be connected safely with no
// possibility of connecting a damaging circuit.
// digitalWrite(13, HIGH);

}

void loop() {
/* After some thought, having the LED as an indicator that the program has accomplished the task of clearing all the EEPROM, the LED should be cycled and
then turned off. */
for (int j = 0; j < 20; j++){
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
}

while(1) {}
}

Two things...

  1. 0xFF is probably a better value to use for "clearing" the EEPROM. This is the value that will be in EEPROM from the factory (but not guaranteed).

  2. If you use (E2END+1) instead of 512, your Sketch will work on any board / processor.

One of the suggestions I had while working with the BS2 was the clear the EEPROM of the device so that when you changed the circuit and powered the board back up again, you would not end up frying something or even hurting somebody (if your project had the potential; think motors).

Well in context of the Arduino platform, clearing eeprom data does not accomplish what you are suggesting. The Arduino uses flash memory to hold the running program, eeprom memory is just used optionally for users data that needs to persist between power on/off cycles. I believe the stamp products used a different setup where the basic interrupter which never changed was in flash memory and the user program was stored in eeprom memory. So quite the different usage of memory.

If you do wish to clear flash memory of prior programs on an Arduino, one could just load the example program in the IDE called BareMinimum:

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly: 
  
}

I generally just load the blink program back into a board which I won't be using for awhile.

Lefty

Just to add a little to retrolefty's explanation and suggestion.

When the Arduino chip starts running from power up or reset, all of the I/O pins are set 'off', a high impedance input state. The pins will only change if there is a sketch (program) loaded which changes pin mode, either explicitly using pinMode, or implicitly, for example using analogWrite.

The BareMinimum sketch doesn't change any pins, or use any pins, so pins will stay 'off'. Uploading BareMinimum will overwrite any previous sketch.

The way I like to work is start with a simple sketch, wire up a little bit, then test it. Then do a bit more once I understand that part is working. This tends to reduce problems.

When driving motors, I try to arrange the electronics with pull-down resistors on motor-driver enables to reduce the chance of motors starting up randomly. I don't always remember the pull-downs, so I do understand the point you are making.

Thanks for the comments and letting me know more about what really happens with the Arduino. While I expected my original program to work, I suspected there would a lot of I do not know about the process of accomplishing placing the Arduino into a safe state for adding new circuits. Eventually, I would like to learn a lot more about the internal workings of the ATMEL products.

Here is my latest program for the purpose of connecting new circuits safely:

/*
Program: Clear_IO_Pins.pde

Purpose: To clear the I/O pins of their states from the previous program so that new circuits can be connected safely.

This program was found as a reply to my posting the Clear_EEPROM.pde program on the Arduino Forum.

Check out the Pictou County Electronics Club: http://www.pcecconnect.com

*/

void setup() {
}

void loop() {
}


Does anybody else think that this should be the second program taught to Arduino users? The first one would be the Hello World program or Blinky program.