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

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;

Fig. 2: Typical Arduino Pro-Mini Board

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.

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.

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.

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.