Have you ever wondered how our phones know when we flip them, or how a drone keeps its balance while soaring through the air? Imagine if we could give our own projects the same ability to sense motion—reacting to tilts, shakes, and spins just like the smartest devices around us.
With the PSOC™ 6 AI Kit and its integrated accelerometer and gyroscope, we get to do exactly that. As curious makers, we’re always looking for ways to bridge the gap between code and the real world. Now, we have the perfect playground to explore how motion sensing works on advanced hardware—without the headache of complicated programming.
In this protip, we’ll team up to read and visualize real-time movement using both the accelerometer and gyroscope right on our PSOC™ 6 board, all with MicroPython.
HardwareFor this we only need our PSOC™ 6 AI Kit connected to power since it contains already a BMI270 motion sensor with a 6-axis accelerometer and gyroscope :)
For the software, we start by flashing MicroPython on our PSOC™ AI kit as previously explained here and then import the necessary libraries :
import time
from machine import I2C
import bmi270
For the bmi270, you will have to go to Tools > Manage Packages > search bmi270 and then click install like it is shown in the pictures bellow:
Next, we set up the I2C connection using the correct pins for the PSOC™ AI Kit that we can found in the documentaton here, and then create an instance of the BMI270 sensor.
i2c = I2C(scl='P0_2', sda='P0_3') # Correct I2C pins for PSOC6 AI Kit
bmi = bmi270.BMI270(i2c)
We now set up an infinite loop (while True:
) to continuously read and display real-time acceleration data from the sensor:
while True:
accx, accy, accz = bmi.accel()
gyrox, gyroy, gyroz = bmi.gyro()
print("-" * 40)
print("ACCELERATION (m/s²) :")
print(f" X: {accx:6.2f} Y: {accy:6.2f} Z: {accz:6.2f}")
print("GYROSCOPE (°/s) :")
print(f" X: {gyrox:6.2f} Y: {gyroy:6.2f} Z: {gyroz:6.2f}")
print("-" * 40)
print()
time.sleep(0.5)
bmi.accel()
: Reads linear acceleration from the sensor on all three axes (X, Y, Z), measured in meters per second squared (m/s²). This tells us how quickly the device is moving in a straight line or if it is being tilted.bmi.gyro()
: Reads the rotation rate (how fast the device is turning) around each axis, in degrees per second (°/s). This is useful for detecting spins, twists, or quick board movements.
Next, to make our sensor data much easier to read, we combine acceleration and rotation values in a neatly formatted block.
In the serial monitor, we see both sets of sensor data grouped together. When you tilt, shake, or rotate your board, you can immediately notice which values change and by how much.
Now if you want to visulize your output with a plotter we can just adjust the code to this :
import time
from machine import I2C
import bmi270
i2c = I2C(scl='P0_2', sda='P0_3')
bmi = bmi270.BMI270(i2c)
# Plotter configuration
# Options: 'acc' -> plot acceleration XYZ
# 'gyro' -> plot gyroscope XYZ
# 'both' -> plot acceleration XYZ and gyroscope XYZ (6 series)
plot_mode = 'both'
sample_hz = 25
_sleep_s = 1.0 / sample_hz
time.sleep(0.2)
try:
while True:
ax, ay, az = bmi.accel()
gx, gy, gz = bmi.gyro()
if plot_mode == 'acc':
print(f"{ax:.3f} {ay:.3f} {az:.3f}")
elif plot_mode == 'gyro':
print(f"{gx:.3f} {gy:.3f} {gz:.3f}")
else: # 'both'
print(f"{ax:.3f} {ay:.3f} {az:.3f} {gx:.3f} {gy:.3f} {gz:.3f}")
time.sleep(_sleep_s)
except KeyboardInterrupt:
pass bmi270.BMI270(i2c)
# Plotter configuration
# Options: 'acc' -> plot acceleration XYZ
# 'gyro' -> plot gyroscope XYZ
# 'both' -> plot acceleration XYZ and gyroscope XYZ (6 series)
plot_mode = 'both'
sample_hz = 25
_sleep_s = 1.0 / sample_hz
time.sleep(0.2)
try:
while True:
ax, ay, az = bmi.accel()
gx, gy, gz = bmi.gyro()
if plot_mode == 'acc':
print(f"{ax:.3f} {ay:.3f} {az:.3f}")
elif plot_mode == 'gyro':
print(f"{gx:.3f} {gy:.3f} {gz:.3f}")
else: # 'both'
print(f"{ax:.3f} {ay:.3f} {az:.3f} {gx:.3f} {gy:.3f} {gz:.3f}")
time.sleep(_sleep_s)
except KeyboardInterrupt:
pass
Next, you need to open the plotter in Thonny (go view > tick Plotter)
After choosing in the code what you want to plot excatly (acc/gyro/both) you can get a result similar to this :
In conclusion, this article has demonstrated how to use the PSOC™ 6 AI Kit and MicroPython to read and visualize real-time motion data from the accelerometer and gyroscope. We hope this has inspired you to explore the possibilities of motion sensing in your own projects and stay tuned we might have somthing quite cool coming up sooon 😉🍻
Comments