Saturday, January 10, 2015

Persistence

The Arduino Sensorium that I described in previous posts is limited by the amount of RAM on the Arduino. Recall that it stores readings from its sensors periodically in a circular buffer, discarding old entries when it runs out of space. The example version I described earlier used a DHT11 sensor, and the reading was stored as temperature, humidity and team, each as a 16 bit integers. This only gives you around 170 entries before you get close to the RAM limits of the Arduino Uno.

A simple way of adding memory is to use a FRAM chip. Adafruit has a 32k byte one accessed through the I2C interface, and an 8k byte SPI version as well. The 32k byte chip would allow around 5,000 sensor readings of three integers. In addition, it is non-volatile memory, meaning you can turn the power off and still retain the data.

The main issue with using this chip is that you can't address it in a simple way, that is through pointers. Instead, you have to read or write it byte by byte using functions in the library provided by Adafruit. I wrote a class which makes this a little easier. The interface is meant to give roughly the same functionality as a native C fixed size array. The specific methods are different, and in particular, I decided not to overload the [] operator. Instead, there are Get and Set methods which take pointers to bytes to specify the location of the objects to be read or written, with templated GetItem and SetItem methods to allow writing of other data types. The code is here. It's written as a base class (StoredArray), and two subclasses. One of them (FRAMArray) stores the data in FRAM. The other (MemoryArray) stores the data in ordinary RAM, allowing you to easily switch code between the two kinds of storage.

I have a new version of the Sensorium code which illustrates this here. You select between FRAM or normal RAM with a #define. When you select the FRAM version it will use the existing data in FRAM, so that you can power down the Arduino, then restart it and add more sensor reading later (though the timestamps will not make sense if you do so). To clear the data, press down the button on input 3 within 3 seconds of resetting the Arduino. This is the same button used in the earlier version for initiating upload.

A quick note on the hardware configuration for this and for the original Sensorium.
  • Arduino pin 3 connects to a push button switch. This side of the switch has a 10k resistor to ground, the other side goes to 5V.
  • Arduino pin 4 goes to pin 2 of a DHT11 temperature/humidity sensor. This pin has a 10k resistor to 5V. Pin 1 (leftmost with the "grid" side facing you) goes to 5V, pin 4 to ground.
  • The new hardware for this version of a FRAM breakout board described above. It has 5V and ground connections as shown on the board (pins 1 and 2). SCL (pin 4 of the board) goes to the Arduino's A5 pin, and SDA (pin 5 of the board) goes to Arduino A4.
Next, to try some different sensors.

No comments: