Outils pour utilisateurs

Outils du site


issue179:micro-ci_micro-la

Ceci est une ancienne révision du document !


This month, we will look at interfacing the LSM303DLHC Triple Axis Accelerometer and Magnetometer (Compass) breakout board to the Raspberry Pi Pico Microcontroller. There are many websites that talk about interfacing the board with an Arduino and a few that discuss how to interface it with the PyBoard, but very few discuss how to deal with the ESP32, ESP8622 or the Raspberry Pi Pico using Micropython. I found three different driver libraries that were supposed to be for Micropython and supposedly supported the three Microcontrollers that we focus on. After testing the three drivers, only one actually worked properly on any of the three boards and that one only worked on the RPi Pico. First, let’s talk about the wiring. Thankfully, this sensor breakout board supports I2C, so the hookup is pretty standard, as you can see from the breadboard drawing. I used the standard I2C bus 0, which uses GPIO pin 8 for SDA and GPIO pin 9 for SCL.

Running the i2cscan program for the Pico, I verified that the addresses were 0x19 (for the accelerometer) and 0x1e (for the magnetometer), which matches the driver information. That driver library can be found at https://gitlab.iue.fh-kiel.de/hassan.karo/robotling-m3-labor/-/blob/feature/config_2020/robotling_lib/driver/lsm303.py but still requires a couple of changes to work correctly on the Pico. I will also provide the modified driver on my repository. You can find the address of the repository near the end of this article. We can’t move forward without the driver modifications, so let’s look at the changes. I won’t try to provide the full source code here. Copy the source file onto your Pico and use Thonny to modify it. The first thing that needs to be changed is one line in the import section (top right). I verified that nothing in the driver code calls the timed_function routine, so we can safely comment it out. Now we need to create a new blank file for our test program. Since we are going to concentrate on the compass feature of the LSM303, the program is pretty simple.

The first thing, as you already know, is to set up the imports. We need to have access to the I2C functions. Since we are using the i2c bus 0, we don’t have to import the Pin functions, but I’m going to pull it in as well. We also need to import the LSM303 class from the library we just fixed up, as well as sleep from time, and the atan2 and pi functions from the math library. from machine import I2C,Pin from lsm303a import LSM303 from time import sleep from math import atan2,pi Next, we need to define the i2c object, instantiate the LSM303 class to the compass object, and define a variable for the “forever” loop that we will use to continuously poll the magnetometer. i2c=I2C(0) compass = LSM303(i2c) loop=True In our driver library is the function that returns a tuple providing the x, y and z axis.

Here is what the function looks like (next page, top right). Thankfully, we don’t have to type this into our code. Anyway, back to the code of our test program. Now, we create our “forever” loop (bottom left) that polls the compass object to get our heading in relation to magnetic north. I’m going to borrow some of the text from the Adafruit website to explain what is going on in the code: In the absence of any strong local magnetic fields, the sensor readings should reflect the magnetic field of the earth (between 20 and 60 micro-Teslas). When the sensor is held level, by calculating the angle of the magnetic field with respect to the X and Y axis, the device can be used as a compass. To convert the microTesla readings into a 0-360 degree compass heading, we can use the atan2() function to compute the angle of the vector defined by the Y and X axis readings. The result will be in radians, so we multiply by 180 degrees and divide by Pi to convert that to degrees. After we have the value of where the sensor is pointing, the data coming back will be from -180 to +180. We need to normalize the value to be from 0 to 359. So we simply check to see if the value is less than 0 and if so we add 360 to the value and return that.

Now when we run our program, we’ll get a value that shows our heading. My board has a small indicator silk-screened on it that shows the x, y and z axis. When the x axis aligns to north, the device returns 0. Save your file as lsm303aTest.py and run it. Move your breadboard around so that the x axis is pointing close to north and watch the values change and try to get the heading value to 0. I’ve put the code for our project on my repository at https://github.com/gregwa1953/FCM-179_MicroThisMicroThat . In a future article (hopefully next month), we’ll add a NeoPixel style RGB LED ring to our project to provide a visual indication that points to north – no matter what direction we are facing, emulating a real compass. Until next time, as always; stay safe, healthy, positive and creative!

issue179/micro-ci_micro-la.1648493361.txt.gz · Dernière modification : 2022/03/28 20:49 de d52fr