In this lesson, you'll build a swing force logger using your Micro:bit's accelerometer to measure acceleration as a proxy for force during swings or kicks, like in sports such as golf or football. You'll store readings in a list, calculate the maximum and average force using functions and loops, and display the results with string formatting. This project reinforces variables for thresholds, control structures for detection, and data handling for real-world applications.
By the end, your Micro:bit will log acceleration during a detected swing, compute stats, and show them on the display. Open the Micro:bit Python editor at python.microbit.org and start a new project.
Start by importing the necessary modules. We'll need the microbit module to access the hardware, including the accelerometer and display. We'll also import the log module to handle data logging for our swing readings later in the project.
Add the following complete code to your editor:
from microbit import *
import log
Let's read the acceleration from the accelerometer and display it. The accelerometer measures acceleration in three directions: x (left-right), y (forward-backward), and z (up-down).
The functions accelerometer.get_x()
, get_y()
, and get_z()
return these values in milli-g (around -2000 to 2000), where positive and negative indicate direction.
We'll calculate a simple magnitude, which is a single value representing the overall strength of the acceleration regardless of direction, by using abs(x) + abs(y) + abs(z)
as a proxy for force, and scroll it in a loop.
Update your code to this complete version:
from microbit import *
import log
while True:
x = accelerometer.get_x()
y = accelerometer.get_y()
z = accelerometer.get_z()
magnitude = abs(x) + abs(y) + abs(z)
display.scroll(str(magnitude))
sleep(500)
abs()
to get positive values and sum them for a simple magnitude proxy. The while True
loop makes it continuous.Now, set a threshold (e.g., 2000) to detect a swing. Use an if statement inside the loop to check if the magnitude exceeds the threshold, and if so, show an image like a happy face to indicate detection.
Update your code to this complete version:
from microbit import *
import log
threshold = 2000 # Adjust based on testing
while True:
x = accelerometer.get_x()
y = accelerometer.get_y()
z = accelerometer.get_z()
magnitude = abs(x) + abs(y) + abs(z)
if magnitude > threshold:
display.show(Image.HAPPY)
else:
display.clear()
sleep(100)
To log the data during a detected swing, we'll use the log module. First, set up the logging labels outside the loop. When a swing is detected, clear any previous log, then take 10 quick readings (every 50ms) and log the x
, y
, z
, and magnitude
if still above threshold. After logging, scroll "Logged".
Update your code to this complete version:
from microbit import *
import log
threshold = 2000
# Set up data logging with columns for x, y, z, and magnitude
log.set_labels('x', 'y', 'z', 'magnitude')
while True:
x = accelerometer.get_x()
y = accelerometer.get_y()
z = accelerometer.get_z()
magnitude = abs(x) + abs(y) + abs(z)
if magnitude > threshold:
display.show(Image.HAPPY)
log.delete() # Clear previous log for new swing
for i in range(10): # Log 10 readings during swing
x = accelerometer.get_x()
y = accelerometer.get_y()
z = accelerometer.get_z()
magnitude = abs(x) + abs(y) + abs(z)
if magnitude > threshold:
log.add(x=x, y=y, z=z, magnitude=magnitude) # Log all values
sleep(50)
display.scroll("Logged") # Indicate logging is complete
else:
display.clear()
sleep(100)