Electronic – arduino – On the Arduino, why is this LED always on, even though I told it to turn off

arduinocmicrocontroller

I am a software developer trying to understand how computers work at a lower level.

I have purchased the Arduino Uno Microcontroller and I have followed all of the tutorials from LadyAda.

I have noticed that the LED (not the power LED) is always ON unless it is flashing even if I supply the following program, which clears the memory and should switch off the LED:

#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 the LED on when we're done
  digitalWrite(13, LOW);
}

void loop()
{
}

Why is the LED always on? I have Googled this and have read a few similar questions on another forum like this, but I have not yet found an answer.

Best Answer

I believe your problem is that your not setting that pin as an output. Use pinMode(13, OUTPUT) to configure the pin 13 to be used as a digital output. Since GPIO pins can be used as a input or outputs on/off, you need to let the micro controller know what mode that pin needs to be set to.

#include <EEPROM.h>
int led = 13;

// the setup routine runs once when you press reset:
void setup() 
{                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);

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

  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
}

void loop() 
{
}

You can see the basic Hello World example here, that goes over blinking the pin 13 LED.

Also, you should be careful about writing to the EEPROM, as pointed out in the comments, it only has ~100k cycle lifetime.