{"id":125,"date":"2007-10-30T14:09:05","date_gmt":"2007-10-30T19:09:05","guid":{"rendered":"http:\/\/www.tigoe.net\/pcomp\/code\/category\/code\/arduinowiring\/125"},"modified":"2008-01-15T21:33:10","modified_gmt":"2008-01-16T02:33:10","slug":"startup-checks","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/arduinowiring\/125\/","title":{"rendered":"Startup Checks"},"content":{"rendered":"<p>When you&#8217;re making a microcontroller circuit that drives a high current load like a motor or an incandescent light, it&#8217;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&#8217;s a problem.<\/p>\n<p>Hans Steiner recently showed me his trick for checking for the startup routine on the Arduino: he writes to the microcontroller&#8217;s EEPROM, or permanent memory, and reads back the result.  Every time the Arduino resets, it&#8217;ll increment the EEPROM value.  Since this value is stored even when the Arduino is not powered, you&#8217;re going to get a new number each time it&#8217;s run.<\/p>\n<p>Thanks to Hans for the code.<\/p>\n<p><!-- technorati tags start --><\/p>\n<p style=\"text-align:right;font-size:10px;\">Technorati Tags: <a href=\"http:\/\/www.technorati.com\/tag\/pcomp\" rel=\"tag\">pcomp<\/a>, <a href=\"http:\/\/www.technorati.com\/tag\/physical computing\" rel=\"tag\">physical computing<\/a><\/p>\n<p><!-- technorati tags end --><br \/>\n<!--more--><\/p>\n<pre>\/*\n  EEPROM reset checker\n  By Hans Steiner\n\n  Reads a value from the EEPROM on startup, prints it out,\n  and increments the value then stores it back in EEPROM.\n  This is a handy way to check for resetting programs, because the \n  EEPROM value will only be incremented and stored in memory on startup.\n\n  Created 25 Oct. 2007\n*\/\n  \n#include &lt;EEPROM.h&gt;\n\nint potPin = 5;       \/\/ select the input pin for the potentiometer\nint motorPin = 9;     \/\/ select the pin for the LED\nint value = 0;        \/\/ variable to store the value coming from the sensor\nint resetCountByte = 36;\n\nvoid setup() {\n  \/\/ open the serial port\n  Serial.begin(9600);\n  \/\/ read the last value stored in the EEPROM:\n  value = EEPROM.read(resetCountByte);\n  \/\/ send it out the serial port:\n  Serial.println(value);\n  \/\/ increment it and write it back to the EEOPROM:\n  EEPROM.write(resetCountByte, value + 1);\n\n  pinMode(motorPin, OUTPUT);  \/\/ declare the motor as an OUTPUT\n  pinMode(13, OUTPUT);        \/\/ declare the LED pin as an output\n  digitalWrite(13,HIGH);      \/\/ turn on the LED\n}\n\nvoid loop() {\n  \/\/ you can do anything you want to here.\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Since this value is stored even when the Arduino is not powered, you&#8217;re going to get a new number each time it&#8217;s run.Thanks to Hans for the code.\/* 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&#8230;.  Created 25 Oct. 2007*\/ #include &lt;EEPROM.h&gt;int potPin = 5; \/\/ select the input pin for the potentiometerint motorPin = 9; \/\/ select the pin for the LEDint value = 0; \/\/ variable to store the value coming from the sensorint 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.}<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,24],"tags":[],"class_list":["post-125","post","type-post","status-publish","format-standard","hentry","category-arduinowiring","category-circuits"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=125"}],"version-history":[{"count":0,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/125\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}