I used CircuitPython for the example code.  Are you new to using CircuitPython? No worries, there is a full getting started guide here.

Adafruit suggests using the Mu editor to edit your code and have an interactive REPL in CircuitPython. You can learn about Mu and installation in this tutorial.

We're using the adafruit_lsm9ds0 library for CircuitPython to easily read the accelerometer.

You can learn about installing the adafruit_lsm9ds0 library in the CircuitPython Essentials Guide on CircuitPlayground Libraries. It is easiest to install the whole library package.

At this point the feather now has an accelerometer attached to it via I2C, just like in any other situation. Indeed, just like the CPX (other than that the CPX uses a LIS3DH).

As a demo, I hooked up a servo and controlled it using the accelerometer. The result is that the servo horn indicates the direction and magnitude of the Crickit's tilt.

import time
import busio
import board
from adafruit_seesaw.seesaw import Seesaw
from adafruit_seesaw.pwmout import PWMOut
from adafruit_motor import servo
import adafruit_lsm9ds0

# Setup hardware
i2c = busio.I2C(board.SCL, board.SDA)

sensor = adafruit_lsm9ds0.LSM9DS0_I2C(i2c)
seesaw = Seesaw(i2c)

# Create servo objects
pwm1 = PWMOut(seesaw, 17)
pwm1.frequency = 50
servo1 = servo.Servo(pwm1, min_pulse=500, max_pulse=2500)

# Center the servo
servo1.angle = 90

while True:
    # Read the accel
    x, y, z = sensor.acceleration

    # Clip the value
    if y < -10:
        y = -10
    if y > 10:
        y = 10

    # print(((y / 10) + 1) * 90)

    # Set the angle
    servo1.angle = ((-y / 10) + 1) * 90

    time.sleep(0.1)

The code simply reads the accelerometer and computes the servo angle based on the Y value it read. Note that I was using the Crickit build of CircuitPython 3.0.0rc0 which bundles the Crickit support libraries into the runtime to save RAM.

If you use a different Flora accelerometer breakout, change the import and the sensor creation line to reflect the library for that sensor.

This guide was first published on Jun 25, 2018. It was last updated on Jun 25, 2018.

This page (Code) was last updated on Sep 08, 2021.

Text editor powered by tinymce.