CircuitPython Code

It's easy to use the analog accelerometer breakouts with CircuitPython and its built-in analog I/O module.  You can read the X, Y, Z accelerometer axis values as simple analog inputs that are converted to acceleration values with simple Python code.

This page will show how to use the ADXL335 with a CircuitPython board like the Feather M0.  Remember your CircuitPython board must support and have three analog inputs, so the ESP8266 and small boards like Gemma M0 won't work.  Stick with a Feather or Metro M0.

Wire up the ADXL335 to your board as follows:

  • Board 3V to sensor VIN
  • Board GND to sensor GND
  • Board A1 to sensor Xout
  • Board A2 to sensor Yout
  • Board A3 to sensor Zout

Remember you can use any available analog inputs for the X, Y, Z outputs.  Be sure that these analog inputs are tolerant of up to 3.3 volt input (the Feather and Metro M0 powered by 3.3-5V as shown will be tolerant)!

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

If you haven't read it yet it will be helpful to read the analog I/O guide as it explains the basics of reading analog inputs with the board's analog to digital converter.  We will read the X, Y, Z axis values as analog inputs.  First import the necessary board and analogio modules and create three inputs, one for each axis:

Download: file
import board
import analogio
x_axis = analogio.AnalogIn(board.A1)
y_axis = analogio.AnalogIn(board.A2)
z_axis = analogio.AnalogIn(board.A3)

You can read the raw value of an axis with the value property:

Download: file
print('Raw XYZ: ({0}, {1}, {2})'.format(x_axis.value, y_axis.value, z_axis.value))

Raw values aren't that interesting though, you really want the acceleration.  Luckily it's easy to convert raw values to acceleration with a simple formula.  Remember these sensors output a radiometric analog voltage proportional to the acceleration between -3G and 3G.  Let's define a function to do this conversion: 

Download: file
def accel_value(axis):
    # Convert axis value to float within 0...1 range.
    val = axis.value / 65535
    # Shift values to true center (0.5).
    val -= 0.5
    # Convert to gravities.
    return val * 3.0

Now try reading the axis values again using the function to convert to gravities:

Download: file
x = accel_value(x_axis)
y = accel_value(y_axis)
z = accel_value(z_axis)
print('Acceleration (G): ({0}, {1}, {2})'.format(x, y, z))

That's all there is to using the ADXL335 with CircuitPython!  You can use the built-in analog input capabilities to convert the accelerometer's X, Y, Z axis acceleration into a gravity value for your own use!

Here's a complete example of printing the X, Y, Z axis acceleration every second.  Save this as main.py on your board and examine the REPL output:

Download: file
import analogio
import board
import time


# Create analog inputs for each ADXL335 axis.
x_axis = analogio.AnalogIn(board.A1)
y_axis = analogio.AnalogIn(board.A2)
z_axis = analogio.AnalogIn(board.A3)

# Define function to convert raw analog values to gravities.
def accel_value(axis):
    # Convert axis value to float within 0...1 range.
    val = axis.value / 65535
    # Shift values to true center (0.5).
    val -= 0.5
    # Convert to gravities.
    return val * 3.0

# Main loop prints acceleration every second.
while True:
    x = accel_value(x_axis)
    y = accel_value(y_axis)
    z = accel_value(z_axis)
    print('Acceleration (G): ({0}, {1}, {2})'.format(x, y, z))
    time.sleep(1.0)
This guide was first published on Nov 05, 2012. It was last updated on Nov 05, 2012. This page (CircuitPython Code) was last updated on May 19, 2019.