On the back of the MatrixPortal is a triple-axis accelerometer that you can use to detect motion. (That's how our cool digital sand demo works!)
You can use this sensor in both CircuitPython and Arduino. After the library support is installed, you can then use the example code provided to get X, Y and Z acceleration values.
Arduino Usage
In Arduino, make sure to install our LIS3DH library, our guide for the individual sensor covers all that here
You can then load up this example
However, before you upload it - change this line:
if (! lis.begin(0x18)) { // change this to 0x19 for alternative i2c address
to:
if (! lis.begin(0x19)) { // change this to 0x19 for alternative i2c address
This will change the library to use the alternate address 0x19 of the accelerometer instead of the default 0x18!
// Basic demo for accelerometer readings from Adafruit LIS3DH #include <Wire.h> #include <SPI.h> #include <Adafruit_LIS3DH.h> #include <Adafruit_Sensor.h> // Used for software SPI #define LIS3DH_CLK 13 #define LIS3DH_MISO 12 #define LIS3DH_MOSI 11 // Used for hardware & software SPI #define LIS3DH_CS 10 // software SPI //Adafruit_LIS3DH lis = Adafruit_LIS3DH(LIS3DH_CS, LIS3DH_MOSI, LIS3DH_MISO, LIS3DH_CLK); // hardware SPI //Adafruit_LIS3DH lis = Adafruit_LIS3DH(LIS3DH_CS); // I2C Adafruit_LIS3DH lis = Adafruit_LIS3DH(); void setup(void) { Serial.begin(115200); while (!Serial) delay(10); // will pause Zero, Leonardo, etc until serial console opens Serial.println("LIS3DH test!"); if (! lis.begin(0x18)) { // change this to 0x19 for alternative i2c address Serial.println("Couldnt start"); while (1) yield(); } Serial.println("LIS3DH found!"); // lis.setRange(LIS3DH_RANGE_4_G); // 2, 4, 8 or 16 G! Serial.print("Range = "); Serial.print(2 << lis.getRange()); Serial.println("G"); // lis.setDataRate(LIS3DH_DATARATE_50_HZ); Serial.print("Data rate set to: "); switch (lis.getDataRate()) { case LIS3DH_DATARATE_1_HZ: Serial.println("1 Hz"); break; case LIS3DH_DATARATE_10_HZ: Serial.println("10 Hz"); break; case LIS3DH_DATARATE_25_HZ: Serial.println("25 Hz"); break; case LIS3DH_DATARATE_50_HZ: Serial.println("50 Hz"); break; case LIS3DH_DATARATE_100_HZ: Serial.println("100 Hz"); break; case LIS3DH_DATARATE_200_HZ: Serial.println("200 Hz"); break; case LIS3DH_DATARATE_400_HZ: Serial.println("400 Hz"); break; case LIS3DH_DATARATE_POWERDOWN: Serial.println("Powered Down"); break; case LIS3DH_DATARATE_LOWPOWER_5KHZ: Serial.println("5 Khz Low Power"); break; case LIS3DH_DATARATE_LOWPOWER_1K6HZ: Serial.println("16 Khz Low Power"); break; } } void loop() { lis.read(); // get X Y and Z data at once // Then print out the raw data Serial.print("X: "); Serial.print(lis.x); Serial.print(" \tY: "); Serial.print(lis.y); Serial.print(" \tZ: "); Serial.print(lis.z); /* Or....get a new sensor event, normalized */ sensors_event_t event; lis.getEvent(&event); /* Display the results (acceleration is measured in m/s^2) */ Serial.print("\t\tX: "); Serial.print(event.acceleration.x); Serial.print(" \tY: "); Serial.print(event.acceleration.y); Serial.print(" \tZ: "); Serial.print(event.acceleration.z); Serial.println(" m/s^2 "); Serial.println(); delay(200); }
CircuitPython Usage
In CircuitPython, ditto - check the guide for how to install the library. Once you've dragged the files over, you can use this example code and paste it into your code.py
file
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT import time import board import busio import adafruit_lis3dh # Hardware I2C setup. Use the CircuitPlayground built-in accelerometer if available; # otherwise check I2C pins. if hasattr(board, "ACCELEROMETER_SCL"): i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA) lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19) else: i2c = board.I2C() # uses board.SCL and board.SDA lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c) # Hardware SPI setup: # spi = board.SPI() # cs = digitalio.DigitalInOut(board.D5) # Set to correct CS pin! # lis3dh = adafruit_lis3dh.LIS3DH_SPI(spi, cs) # PyGamer or MatrixPortal I2C Setup: # i2c = board.I2C() # uses board.SCL and board.SDA # lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19) # Set range of accelerometer (can be RANGE_2_G, RANGE_4_G, RANGE_8_G or RANGE_16_G). lis3dh.range = adafruit_lis3dh.RANGE_2_G # Loop forever printing accelerometer values while True: # Read accelerometer values (in m / s ^ 2). Returns a 3-tuple of x, y, # z axis values. Divide them by 9.806 to convert to Gs. x, y, z = [ value / adafruit_lis3dh.STANDARD_GRAVITY for value in lis3dh.acceleration ] print("x = %0.3f G, y = %0.3f G, z = %0.3f G" % (x, y, z)) # Small delay to keep things responsive but give time for interrupt processing. time.sleep(0.1)
Before you save, however, you must tell the example where to find the sensor!
Remove these lines:
# Hardware I2C setup. Use the CircuitPlayground built-in accelerometer if available; # otherwise check I2C pins. if hasattr(board, "ACCELEROMETER_SCL"): i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA) int1 = digitalio.DigitalInOut(board.ACCELEROMETER_INTERRUPT) lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19, int1=int1) else: i2c = busio.I2C(board.SCL, board.SDA) int1 = digitalio.DigitalInOut(board.D6) # Set to correct pin for interrupt! lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, int1=int1)
And 'uncomment' these lines:
# PyGamer OR MatrixPortal I2C Setup: # i2c = busio.I2C(board.SCL, board.SDA) # int1 = digitalio.DigitalInOut(board.ACCELEROMETER_INTERRUPT) # lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19, int1=int1)
So they look like this:
# PyGamer OR MatrixPortal I2C Setup: i2c = busio.I2C(board.SCL, board.SDA) int1 = digitalio.DigitalInOut(board.ACCELEROMETER_INTERRUPT) lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19, int1=int1)
Now you can save, and check the REPL for acceleration data!