Reading Sensors with LoPy: Accelerometer
Adapted from Frack Albinet's exercises here.
Learning outcomes
You will learn how to:
- access Pysense sensors for you application of interest using Pycom high-level Python modules
- dive deep into the Pysense sensor chips registers to address your custom use or to fix existing library [advanced and optional]
Required Components
For this example you will need:
- a LoPy module on a Pysense board
- a microUSB cables
- a development PC
The source code is here.
Always update the firmware to the most recent version.
Using Pycom-provided high-level modules
Pycom provides a library (a set of Python modules) abstracting the implementation details of sensor chips (as you will see below). This library is already included in labs source code under the lib
folder of each example.
Let's take a look a the src/pysense/acceloremeter
first:
from pysense import Pysense from LIS2HH12 import LIS2HH12 import pycom import micropython import machine import time py = Pysense() acc = LIS2HH12(py) while True: print('----------------------------------') print('X, Y, Z:', acc.read()) print('Roll:', acc.roll()) print('Pitch:', acc.pitch()) print('Yaw:', acc.yaw()) time.sleep(1)
You notice that it looks particularly simple and straightforward! That's the power of code abstraction and Python.
Let's go through each step:
- first create a
Pysense
object which will be used to communicate over the I2C serial bus between the microcontroller and sensor devices (in our specific case the LIS2HH12 accelerometer - refer to thesrc/pysense/datasheet
folder for sensors' documentation).
py = Pysense()
For further information on I2C: wikipedia entry Pycom I2C
class * I2C briefing by Marco Rainone, ICTP
- then create a
LIS2HH12
object passing the previously created Pysense object as argument. While thePysense
object provides general purpose methods to communicate over the I2C bus, this object will further specify the address of the device (LIS2HH12
sensor) and register addresses of interest to be used to calculate and give access to the 3-axis accelerometer measurements.
acc = LIS2HH12(py)
Now simply loop over measurements and readings every second:
while True: print('----------------------------------') print('X, Y, Z:', acc.read()) print('Roll:', acc.roll()) print('Pitch:', acc.pitch()) print('Yaw:', acc.yaw()) time.sleep(1)
It's that easy!
But less us be clear, in many real implementation, as soon as you'd like to finetune the setups of your sensor, calibrate or sometimes even fix the firmware provided, you will have to immerse yourself in all the technical details of the chip sensor.
Exercises
- Change the colour of the LED based on accelerometer measurements (green, orange, red, blue).
- Make the LED blink blue when there's no movement
- Make the LED blink orange when the greatest movement is Roll
- Make the LED blink red when the greatest movement is Pitch
- Make the LED blink green when the greatest movement is Yaw
- When you push the button, start logging measurements to
/flash/log.
 When you push again logging is stopped.