Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

How To Save A Sensor Value In The EEPROM Of The Arduino- (Part 19/49)

By Ajish Alfred July 9, 2013

The EEPROM stands for Electrically Erasable Programmable Read Only Memory. In an EEPROM the data can be written with the help of electrically programming the chip. EEPROM memory is widely used in microcontroller systems where some particular data need to be retained each time the system is turned on and to save particular data before the system is powered off.The EEPROM memory chips can be interfaced in a microcontroller chip usually with the help of serial communication protocols. In case of small applications where only a few bytes needs to be stored the extra EEPROM chips are not preferred since it add extra complexity in the hardware, coding and increase the cost of the system. Hence most of the microcontrollers are provided with small sized built-in EEPROM chip which can be used to store data in small applications.

The Arduino board has an AVR microcontroller in it which also has a built-in EEPROM memory. The memory size varies with the Arduino boards and the microcontroller used in them. A microcontroller might need to store its data like sensor value, or a particular count or image data for a long period of time uses the EEPROM memory. The EEPROM memory is also used to save the data before the system switches itself off so that the same data can be retained next time when the system is turned on.  This particular project demonstrates how to save the last read value from a senor into the built-in EEPROM of Arduino before it is powered off, so that the next time it is powered on the same data can be read.


 

The microcontroller can read the analog input voltage by sampling it and converting it to their digital values with the help of Analog to Digital Converter (ADC). The microcontroller can also generate an analog voltage on any external device with the help of Pulse Width Modulated (PWM) waves. Most of the microcontrollers have built-in PWM module and ADC modules which helps them in reading analog voltage inputs and generating analog voltage outputs on an external device.In an Arduino board some of the digital pins can be configured as analog output pins and there are also dedicated analog input pins which can be used for voltage sensing applications. In this project the Arduino pro-mini board is used which has a maximum of eight pins which can be used as analog input pins. The pins are marked in the board as A0, A1, A2, … A7. They are actually the input channels to the built-in ADC which can read the analog value and convert them to the digital equivalent.The Arduino pro-mini board has ATMEGA328 microcontroller inside it which has an internal EEPROM memory of 1Kb.

In this project the Arduino pro-mini board is programmed with the help of Arduino IDE version 1.0.3 on windows operating system. The image of the Arduino pro-mini board and the Arduino IDE is shown in the following;

 Typical Arduino Pro-Mini Board

Fig. 2: Typical Arduino Pro-Mini Board

 

 Arduino IDE Software Window

Fig. 3: Arduino IDE Software Window

Another hardware which can perform the USB to TTL conversion is used to upload the program into the arduino board.

 External USB To TTL Converter Board For Programming Arduino And Serial Communication

Fig. 4: External USB to TTL converter board for programming Arduino and serial communication

It is assumed that the reader has gone through the project how to get started with the arduino and done all the things discussed in it. In this particular project a simple potentiometer is used to provide variable voltage to the analog pin as a sensor does and which can be replaced with an actual sensor in future projects. The variable pin of a potentiometer is connected to the analog pin; in this project the pin A0. The other two pins of the potentiometer is connected to the VCC and GND so that as the variable moves it can divide the entire supply voltage and provide it as the analog input voltage for the Arduino board.

Once a value is read from the sensor it is immediately mapped and written to a pin configured as the analog output and where an LED is connected in the circuit. The brightness of the LED indicates the voltage output from the sensor. The value read is also converted to its voltage equivalent and displayed in an LCD and at the same time stored in an EEPROM. The functions analogRead(),analogWrite() and map() which can be used to read, write and map the values respectively and also the method to convert the value to the actual voltage are already explained in previous projects on how to use analog input and output of an Arduino board,  how to display the sensor values using Arduino, and how to make dynamic sensor display using arduino.

The last read value, the present value and the other data are displayed on a 16*2 LCD using the functions provided by the library <LiquidCrystal.h>. Few functions from the library <LiquidCrystal.h> including those which are used in this particular project are already discussed in the previous projects onhow to interface an LCD, how to display sensor value on LCD, how to connect the LCD with the PC and how to make an LCD scrolling display.

The Arduino pro-mini has ATMEGA328 microcontroller which has 1Kb of EEPROM memory. The EEPROM memory is accessed in the code with the help of functions provided by the library <EEPROM.h>.  The projects on how to access the EEPROM of the Arduino and how to perform the EEPROM test of the Arduino explains the method of reading and writing the EEPROM memory of the Arduino.

There are basically two functions namely EEPROM.write() and EEPROM.read() which helps in writing data to the EEPROM memory and reading data from the EEPROM memory respectively. The details of the functions are explained in the following section.

EEPROM.write()

The function EEPROM.write() is used to write a data byte into a particular address of the EEPROM memory mentioned by the parameters passed to the function. The function has two parameters where the first one should be provided with the address of the EEPROM location into which the data need to be written into and the second parameter should be provided with actual data byte. For example if the data ‘A’ need to be written into the address mentioned by the variable ‘addr’ the following statement can be used.

EEPROM.write(addr, ‘A’);

EEPROM.read()

The function EEPROM.read() is used to read a particular data byte from the internal EEPROM of the Arduino’s microcontroller. The function has a single parameter which is the address from which the data should be read from. The function has a return value which is the actual data byte which it read from the address mentioned by the parameter passed into it. For example if the data byte is to be read from the location mentioned by the variable ‘addr’ the following statement can be used.

EEPROM.read(addr);

THE CODE

The Arduino board reads the analog value at the analog input channel using the function analogRead() and then maps the value to the range 0 to 255 so that it can be written into the PWM module. The function map() is used to map the value from the range 0 to 1023 to the range 0 to 255. The mapped value is immediately written to the pin which is selected as the analog output pin using the function analogWrite() and hence varying the brightness of an LED indicator connected to that pin.

The mapped value is then converted to the actual voltage value which is then stored in the internal EEPROM with the help of functions from the library <EEPROM.h>. The function EEPROM.write() is used for writing the value into a particular memory location. The value is also displayed in a 16*2 LCD.

Whenever the Arduino is powered up the code first reads the previously stored value from the same memory location and displays it on a 16*2 LCD. The function from the library <EEPROM.h> namely EEPROM.read() is used to read the value from the memory location.

Both the previous value read from the location is and the current value reads from the analog pin are displayed in a 16*2 LCD with the help of functions from the library <LiquidCrystal.h>. Few functions from the library <LiquidCrystal.h> including those which are used in this particular project are already discussed in the previous projects on how to interface an LCD, how to display sensor value on LCD, how to connect the LCD with the PC and how to make an LCD scrolling display.

When the coding is finished one can verify and upload the code to the Arduino board as explained in the project how to get started with the Arduino. First time when the Arduino is powere up the last value displayed will be 0 and from the next time onwards it will display the last value sensed before the Arduino is powered off.

 

Project Source Code

###



/*================================= EG LABS =======================================

Read a sensor value and store it in EEPROM before power off

 The circuit:

 * LED attached from pin 6 to ground through a 1K resistor

 * Potentiometer attached to analog input A0

 * one side pin (either one) to ground

 * the other side pin to +5V

 LCD:

 * LCD RS pin to digital pin 12

 * LCD Enable pin to digital pin 11

 * LCD D4 pin to digital pin 5

 * LCD D5 pin to digital pin 4

 * LCD D6 pin to digital pin 3

 * LCD D7 pin to digital pin 2

 * LCD R/W pin to ground

 * 10K resistor:

 * ends to +5V and ground

 * wiper to LCD pin 3

 * LED anode attached to digital output 6

 * LED cathode attached to ground through a 1K resistor

//================================= EG LABS =======================================*/


#include <EEPROM.h>

#include <LiquidCrystal.h>


LiquidCrystal lcd(12, 11, 5, 4, 3, 2);               // initialize the LCD library with the numbers of the interface pins


const int analogInPin = A0;                          // Analog input pin that the potentiometer is attached to

const int analogOutPin = 6;                          // Analog output pin that the LED is attached to

int potvalue = 0;

int outputvalue = 0;


int address = 50;                                    // the variable which holds the address in the eeprom

int read_value = 0;                                  // the variable which holds the data which is read from the eeprom


int led = 6;                                         // variable which holds the pin number at which the LED is connected


void setup()

{

  pinMode(led, OUTPUT);                               // initialize the led pin as an output.


  lcd.begin(16, 2);                                   // set up the LCD's number of columns and rows: 

  lcd.print("ENGINEERS GARAGE");

  delay(2000);

  lcd.clear();

  lcd.print("LAST VALUE : ");

  read_value = EEPROM.read(address);                  // read the value from the address

  lcd.print(read_value);  

  lcd.print('v');  

  delay(2000);

  lcd.clear();    

}

void loop()

{

  potvalue = analogRead(analogInPin);        

  outputvalue = map(potvalue, 0, 1023, 0, 255); 

  analogWrite(analogOutPin, outputvalue);    

  lcd.setCursor(0, 0);

  lcd.print("  SENSOR VALUE");

  lcd.setCursor(7, 2);

  lcd.print((outputvalue * 5)/255);

  lcd.print("V");

  EEPROM.write(address, ((outputvalue * 5)/255));                 // write the value to the EEPROM address

}

###

 


Circuit Diagrams

Circuit-Diagram-Saving-Sensor-Value-EEPROM-Arduino

Project Components

  • Arduino Pro Mini
  • LCD
  • LED
  • Resistor

Project Video


Filed Under: Arduino Projects
Tagged With: Arduino, eeprom, sensor
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: Internet of Things
Explore practical strategies for minimizing attack surfaces, managing memory efficiently, and securing firmware. Download now to ensure your IoT implementations remain secure, efficient, and future-ready.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Multiple DC/DC converters and a single input source
  • Voltage mode pushpull is a nonsense SMPS?
  • High Side current sensing
  • Reducing "shoot-through" in offline Full Bridge SMPS?
  • MOSFET thermal noise in Weak vs Strong inversion

RSS Electro-Tech-Online.com Discussions

  • Failure of polypropylene motor-run capacitors
  • Siemens large industrial PLC parts
  • Wideband matching an electrically short bowtie antenna; 50 ohm, 434 MHz
  • Curved lines in PCB design
  • using a RTC in SF basic

Featured – RPi Python Programming (27 Part)

  • RPi Python Programming 21: The SIM900A AT commands
  • RPi Python Programming 22: Calls & SMS using a SIM900A GSM-GPRS modem
  • RPi Python Programming 23: Interfacing a NEO-6MV2 GPS module with Raspberry Pi
  • RPi Python Programming 24: I2C explained
  • RPi Python Programming 25 – Synchronous serial communication in Raspberry Pi using I2C protocol
  • RPi Python Programming 26 – Interfacing ADXL345 accelerometer sensor with Raspberry Pi

Recent Articles

  • What is AWS IoT Core and when should you use it?
  • AC-DC power supply extends voltage range to 800 V DC
  • Infineon’s inductive sensor integrates coil system driver, signal conditioning circuits and DSP
  • Arm Cortex-M23 MCU delivers 87.5 µA/MHz active mode
  • STMicroelectronics releases automotive amplifiers with in-play open-load detection

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe