PyBMA400/examples/basic_usage.py
2025-05-24 12:23:08 +01:00

43 lines
1.3 KiB
Python

#!/usr/bin/env python3
"""
Basic example of using the PyBMA400 library to read
acceleration and temperature data from the BMA400 sensor.
"""
import time
from pybma400 import BMA400
def main():
# Initialize the sensor
try:
sensor = BMA400() # Default I2C bus=1, address=0x14
print("BMA400 sensor initialized successfully!")
# Configure the sensor
sensor.power_mode = BMA400.NORMAL_MODE
sensor.output_data_rate = BMA400.ACCEL_100HZ
sensor.acc_range = BMA400.ACC_RANGE_4 # ±4g range
print(f"Power mode: {sensor.power_mode}")
print(f"Output data rate: {sensor.output_data_rate}")
print(f"Acceleration range: {sensor.acc_range}")
print(f"Filter bandwidth: {sensor.filter_bandwidth}")
print(f"Oversampling rate: {sensor.oversampling_rate}")
# Read and display sensor data
for i in range(10):
# Read acceleration
x, y, z = sensor.acceleration
print(f"Acceleration: X={x:.2f}, Y={y:.2f}, Z={z:.2f} m/s²")
# Read temperature
temp = sensor.temperature
print(f"Temperature: {temp:.1f}°C")
time.sleep(0.5)
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
main()