ARDUINO EEPROM INTERFACE WITH LED



 In this tutorial we are going to interface EEPROM to save the LED status, and to retrieve the previous status during micro controller starts.

CODE EXPLANATION:

The EEPROM is used to store the data from the serial input.

The microcontroller checks the EEPROM address whether it having '0' or '1'

If '0' is present then the LED is turned OFF

If '1' is present then the LED is turned ON.

The state can be changed from serial input and the last state change is updated to EEPROM address.

CODE

#include <EEPROM.h>
char val = '0';
void setup() {
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  digitalWrite(2, LOW);
}
void loop() {
  while (Serial.available() > 0)
  {
    val = Serial.read();
    Serial.println(val);
    EEPROM.write(0, val);
  }
  val = EEPROM.read(0);
  Serial.println(val);
  if (val == '1')
    digitalWrite(2, HIGH);
  else  if (val == '0')
    digitalWrite(2, LOW);
delay(1000);
}

Comments