Accelerometer

The StudioBot includes an accelerometer that can be used to detect movement.

The following example will write out the current acceleration. What value do you think Z would be when the StudioBot is sitting still on the desk?

import time
import board
import digitalio
import adafruit_lis3dh

i2c = board.I2C()

int1 = digitalio.DigitalInOut(board.ACCELEROMETER_INTERRUPT)  

lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, int1=int1)

while True:
    x, y, z = lis3dh.acceleration
    print(x, y, z)
    time.sleep(0.1)

The StudioBot can Shakes:

import time
import board
import digitalio
import adafruit_lis3dh

i2c = board.I2C()

int1 = digitalio.DigitalInOut(board.ACCELEROMETER_INTERRUPT)  

lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, int1=int1)

while True:
    if lis3dh.shake(shake_threshold=15):
        print("Shaken!")
    time.sleep(0.1)

We can detect Taps on the board:

import time
import board
import digitalio
import adafruit_lis3dh

i2c = board.I2C()

int1 = digitalio.DigitalInOut(board.ACCELEROMETER_INTERRUPT)  

lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, int1=int1)
    
lis3dh.set_tap(2, 60)
while True:
    if lis3dh.tapped:
        print("Tapped!")
        time.sleep(0.01)

To read more about this accelerometer and CircuitPython, have a read of this Adafruit article.

Last updated