Arduino powered by a capacitor – EEPROM in page mode

atmega_capacitors_pv_7This is the next step forward In my “minimalistic standalone ATmega328 powered by a capacitor”. In this project I was walking from the start through first tests, reducing consumption, optimized tests and adding a RTC including the problems of waking up from sleep via interrupt. Then I added a 24AA256 EEPROM as external memory. EEPROMs are good for data storage as they keep data even when supply voltage has gone down. But they are slow, especially in write mode. This means that a lot of time is spent by simply writing two bytes of data into this memory and thus, energy is consumed during this time. Now with the 24AA256 it is possible to write up to 64 bytes at once. This is called “block mode”. Let’s look if we can reduce energy consumption furthermore by using this feature.

Writing in page mode

Block mode write is described in the datasheet on page 9. After the control byte the two address bytes will follow and then up to 64 bytes of data. After the end of transmission all data bytes are written to memory at once. And the times for a byte write and a page write are the same!!! Sow we probably will get a dramatic decrease of total write time and also total runtime of the controller 🙂

Carefully reading the datasheet shows that these memory pages are physical units on the chip. During a block write you can write only in one block. You cannot write over a block boundary. Here is a link to a very detailed explanation of the pitfalls of page writing to a 24AA256: http://www.hobbytronics.co.uk/eeprom-page-write/. In our tests we will write 16 or 64 bytes at once. When starting at the beginning of the EEPROM we will always stay inside one memory page.

To get this realized we use an array that keeps data until they’re written to EEPROM. Every time the controller wakes up it reads the ADC and writes the data to the array. When the array is full all bytes are written to EEPROM at once. Then the controller goes to sleep again. The drawback is that the array resides in RAM and can get lost when the system runs out of energy. The 16 byte array holds up to 8 ADC measurements. So we will loos a maximum of 8 values. With a 64 byte array a maximum of 32 measurements can get lost at the end.

Why do we start with an array as small as 16 bytes? First of all this is for to see what happens and to have a comparison to writing a full memory page at once. The other point lies in the Wire library that is used to write the EEPROM: The write buffer is set to 32 bytes (see http://playground.arduino.cc/Main/WireLibraryDetailedReference). When writing to the EEPROM the first two bytes are the address bytes. So only 30 bytes are left for data. This is why we take 16. So we will stay inside the page limits.

For these tests we talk to the 24AA256 directly via the Wire library to have full control of what is happening. The same things can also be done with [robtillaart]s I2C_EEPROM library (net tested yet).

16 bytes at once

Now here we go: uptime_step_5over 8,800 seconds of total uptime! This are two hours and 26 minutes 🙂 This is almost 9 times more as when writing only two bytes at once as in the last post! Now it can clearly be seen that a big part of the energy is wasted during the long write cycles of the EEPROM.

Writing full pages

The next step is to write 64 bytes of data at once. This is the size of one complete memory page. For this to do it is necessary to increase the buffers of the Wire library. But beware! There’s not only one buffer but five of them! This means: Increasing the buffers also increases total RAM usage. Always keep this in mind when writing your sketch. If you get strange results during runtime you should check your RAM usage.

This thread in the arduino forum: http://forum.arduino.cc/index.php?topic=54439.0 describes how to increase the buffers of the Wire library. For our tests we take the buffer size to 66 bytes (2 address bytes and 64 data bytes). Keep in mind to change this back to the standard of 32 bytes because you could get some problems with your other sketches when they suddenly run out of RAM 😉

uptime_step_6And this is the result: The total uptime doesn’t rise a lot compared to writing 16 bytes in page mode. The system runs 9400 seconds = 2 hours and 36 minutes. There isn’t a lot more savings potential when going from 16 bytes at once to 64 bytes.. So this isn’t any more the main spot where energy is consumed in the system.

We see that as the EEPROM is rather slow energy consumption of the system is very much determined by the long write cycles to the EEPROM. Now that we decreased this it is only a small part of the total consumption of energy in the system.

And now we have a system driven by two capacitors and running for more than 2.5 hours 🙂 During this time the ADC is read 9400 times and all the data is written to the external EEPROM!

Enjoy

heliosoph

2 thoughts on “Arduino powered by a capacitor – EEPROM in page mode

  1. rob tillaart

    There is one optimization you could do when writing single bytes to EEPROM. In pseudo code

    writeEE(addr, val)
    {
    if (readEE(addr) != val) realWriteEE(addr,val);
    }

    In English, only write when really changed.

    Reply
    1. heliosoph Post author

      Hello rob,

      thanks for the hint. I didn’t think of this because the high-byte of the data from the AD-converter goes only from 0x00 to 0x03. For the low-byte it works as you wrote.

      heliosoph

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

37 + = 46