CircuitPython

It's easy to use the SPI FRAM Breakout with Python or CircuitPython and the Adafruit CircuitPython FRAM module.  This module allows you to easily write Python code that reads the humidity, temperature, pressure, and more from the sensor.

CircuitPython Microcontroller Wiring

First we'll wire up a SPI FRAM Breakout to a microcontroller, as was shown in the Arduino Test page.

Here is an example of wiring the breakout to a Feather M0 Basic:

  • Board 3V to sensor VIN
  • Board GND to sensor GND
  • Board SCK to sensor SCK
  • Board MOSI to sensor MOSI
  • Board MISO to sensor MISO
  • Board D5 to sensor CS

If you'd like to use the hardware write protection, connect another GPIO to the sensor's WP pad, like so:

  • Board 3V to sensor VIN
  • Board GND to sensor GND
  • Board SCK to sensor SCK
  • Board MOSI to sensor MOSI
  • Board MISO to sensor MISO
  • Board D5 to sensor CS
  • Board D6 to sensor WP
The CircuitPython library takes advantage of the software level write protection on the SPI FRAM chip, so using the hardware write protection isn't necessary. However, the hardware write protection could be useful with an external source of control, like a separate microcontroller.

CircuitPython Installation of FRAM Library

You'll need to install the Adafruit CircuitPython FRAM library on your CircuitPython board.

First make sure you are running the latest version of Adafruit CircuitPython for your board.

Next you'll need to install the necessary libraries to use the hardware--carefully follow the steps to find and install these libraries from Adafruit's CircuitPython library bundle.  Our CircuitPython starter guide has a great page on how to install the library bundle.

For non-express boards like the Trinket M0 or Gemma M0, you'll need to manually install the necessary libraries from the bundle:

  • adafruit_fram.mpy
  • adafruit_bus_device

Before continuing make sure your board's lib folder or root filesystem has the adafruit_fram.mpy, and adafruit_bus_device files and folders copied over.

Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

CircuitPython Usage

To demonstrate the usage of the breakout we'll initialize it, write data to the FRAM, and read that data from the board's Python REPL.

Run the following code to import the necessary modules and initialize the SPI connection with the breakout:

Download: file
import board
import busio
import digitalio
import adafruit_fram
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
cs = digitalio.DigitalInOut(board.D5)
fram = adafruit_fram.FRAM_SPI(spi, cs)

Or, if you're using the hardware write protection:

Download: file
import board
import busio
import digitalio
import adafruit_fram
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
cs = digitalio.DigitalInOut(board.D5)
wp = digitalio.DigitalInOut(board.D6)
fram = adafruit_fram.FRAM_SPI(spi, cs, wp_pin=wp)

Now you can write or read to any address locations:

Download: file
fram[0] = 1

fram[0]

Reading the FRAM returns a bytearray. To get a "raw" value, use the index of the value's location. Some various ways to get values are as such:

Full Example Code

## Simple Example For CircuitPython/Python SPI FRAM Library

import board
import busio
import digitalio
import adafruit_fram

## Create a FRAM object.
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
cs = digitalio.DigitalInOut(board.D5)
fram = adafruit_fram.FRAM_SPI(spi, cs)

## Write a single-byte value to register address '0'

fram[0] = 1

## Read that byte to ensure a proper write.
## Note: 'read()' returns a bytearray

print(fram[0])

## Or write a sequential value, then read the values back.
## Note: 'read()' returns a bytearray. It also allocates
##       a buffer the size of 'length', which may cause
##       problems on memory-constrained platforms.

#values = list(range(100)) # or bytearray or tuple
#fram[0] = values
#print(fram[0:99])
This guide was first published on May 23, 2014. It was last updated on May 23, 2014. This page (CircuitPython) was last updated on Jul 08, 2019.