Simple test¶
Ensure your device works with this simple test.
import time
from machine import Pin, I2C
from micropython_mma8452q import mma8452q
i2c = I2C(1, sda=Pin(2), scl=Pin(3)) # Correct I2C pins for RP2040
mma = mma8452q.MMA8452Q(i2c)
while True:
x, y, z = mma.acceleration
print(f"Acceleration: X={x:0.1f}m/s^2 y={y:0.1f}m/s^2 z={z:0.1f}m/s^2")
print()
time.sleep(0.5)
Operation mode settings¶
Example showing the Operation mode setting
import time
from machine import Pin, I2C
from micropython_mma8452q import mma8452q
i2c = I2C(1, sda=Pin(2), scl=Pin(3)) # Correct I2C pins for RP2040
mma = mma8452q.MMA8452Q(i2c)
mma.operation_mode = mma8452q.STANDBY_MODE
while True:
for operation_mode in mma8452q.operation_mode_values:
print("Current Operation mode setting: ", mma.operation_mode)
for _ in range(10):
accx, accy, accz = mma.acceleration
print(
f"Acceleration: X={accx:0.1f}m/s^2 y={accy:0.1f}m/s^2 z={accz:0.1f}m/s^2"
)
print()
time.sleep(0.5)
mma.operation_mode = operation_mode
Scale range settings¶
Example showing the Scale range setting
import time
from machine import Pin, I2C
from micropython_mma8452q import mma8452q
i2c = I2C(1, sda=Pin(2), scl=Pin(3)) # Correct I2C pins for RP2040
mma = mma8452q.MMA8452Q(i2c)
mma.scale_range = mma8452q.RANGE_8G
while True:
for scale_range in mma8452q.scale_range_values:
print("Current Scale range setting: ", mma.scale_range)
for _ in range(3):
accx, accy, accz = mma.acceleration
print(
f"Acceleration: X={accx:0.1f}m/s^2 y={accy:0.1f}m/s^2 z={accz:0.1f}m/s^2"
)
print()
time.sleep(0.5)
mma.scale_range = scale_range
Data rate settings¶
Example showing the Data rate setting
import time
from machine import Pin, I2C
from micropython_mma8452q import mma8452q
i2c = I2C(1, sda=Pin(2), scl=Pin(3)) # Correct I2C pins for RP2040
mma = mma8452q.MMA8452Q(i2c)
mma.data_rate = mma8452q.DATARATE_800HZ
while True:
for data_rate in mma8452q.data_rate_values:
print("Current Data rate setting: ", mma.data_rate)
for _ in range(10):
accx, accy, accz = mma.acceleration
print(
f"Acceleration: X={accx:0.1f}m/s^2 y={accy:0.1f}m/s^2 z={accz:0.1f}m/s^2"
)
print()
time.sleep(0.5)
mma.data_rate = data_rate
High pass filter cutoff settings¶
Example showing the High pass filter cutoff setting
import time
from machine import Pin, I2C
from micropython_mma8452q import mma8452q
i2c = I2C(1, sda=Pin(2), scl=Pin(3)) # Correct I2C pins for RP2040
mma = mma8452q.MMA8452Q(i2c)
mma.high_pass_filter = mma8452q.HPF_ENABLED
mma.high_pass_filter_cutoff = mma8452q.CUTOFF_8HZ
while True:
for high_pass_filter_cutoff in mma8452q.high_pass_filter_cutoff_values:
print("Current High pass filter cutoff setting: ", mma.high_pass_filter_cutoff)
for _ in range(10):
accx, accy, accz = mma.acceleration
print(
f"Acceleration: X={accx:0.1f}m/s^2 y={accy:0.1f}m/s^2 z={accz:0.1f}m/s^2"
)
print()
time.sleep(0.5)
mma.high_pass_filter_cutoff = high_pass_filter_cutoff