issue179:micro-ci_micro-la
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue179:micro-ci_micro-la [2022/03/27 14:59] – créée auntiee | issue179:micro-ci_micro-la [2022/04/01 16:53] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | 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. | + | **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. | 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. | ||
Ligne 5: | Ligne 5: | ||
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. | 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. | + | 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), | + | Ce mois-ci, nous allons nous intéresser à l' |
+ | |||
+ | J'ai trouvé trois différentes bibliothèques de pilotes qui étaient censées être pour Micropython et qui étaient censées supporter les trois microcontrôleurs sur lesquels nous nous concentrons. Après avoir testé les trois pilotes, seul un d' | ||
+ | |||
+ | Tout d' | ||
+ | |||
+ | J'ai utilisé le bus I2C standard 0, qui utilise la broche GPIO 8 pour SDA et la broche GPIO 9 pour SCL. | ||
+ | |||
+ | |||
+ | **Running the i2cscan program for the Pico, I verified that the addresses were 0x19 (for the accelerometer) and 0x1e (for the magnetometer), | ||
That driver library can be found at https:// | That driver library can be found at https:// | ||
Ligne 15: | Ligne 24: | ||
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. | 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. | + | 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. | + | En exécutant le programme i2cscan pour le Pico, j'ai vérifié que les adresses étaient 0x19 (pour l' |
+ | |||
+ | Cette bibliothèque de pilotes peut être trouvée sur https:// | ||
+ | |||
+ | Nous ne pouvons pas avancer sans les modifications du pilote, alors regardons les changements. Je ne vais pas essayer de fournir le code source complet ici. Copiez le fichier source sur votre Pico et utilisez Thonny pour le modifier. | ||
+ | |||
+ | La première chose à modifier est une ligne dans la section d' | ||
+ | |||
+ | Nous devons maintenant créer un nouveau fichier vierge pour notre programme de test. Comme nous allons nous concentrer sur la fonction boussole du LSM303, le programme est assez 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 machine import I2C,Pin | ||
Ligne 30: | Ligne 50: | ||
loop=True | loop=True | ||
- | In our driver library is the function that returns a tuple providing the x, y and z axis. | + | 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). | + | La première chose, comme vous le savez déjà, est de mettre en place les importations. Nous devons avoir accès aux fonctions I2C. Puisque nous utilisons le bus i2c 0, nous n' |
+ | |||
+ | from machine import I2C,Pin | ||
+ | from lsm303a import LSM303 | ||
+ | from time import sleep | ||
+ | from math import atan2,pi | ||
+ | |||
+ | Ensuite, nous devons définir l' | ||
+ | |||
+ | i2c=I2C(0) | ||
+ | compass = LSM303(i2c) | ||
+ | loop=True | ||
+ | |||
+ | Dans la bibliothèque de notre pilote se trouve la fonction qui renvoie un tuple fournissant les axes x, y et z. | ||
+ | |||
+ | |||
+ | **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. | Thankfully, we don’t have to type this into our code. Anyway, back to the code of our test program. | ||
Ligne 40: | Ligne 76: | ||
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. | 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. | + | 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. | + | Voici à quoi ressemble la fonction (en haut à droite de la page suivante). |
+ | |||
+ | Heureusement, | ||
+ | |||
+ | Maintenant, nous créons notre boucle « éternelle » (en bas à gauche) qui interroge l' | ||
+ | |||
+ | Je vais emprunter une partie du texte du site Web d' | ||
+ | |||
+ | Une fois que nous avons la valeur de l' | ||
+ | |||
+ | |||
+ | **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. | 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. | ||
Ligne 50: | Ligne 97: | ||
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. | 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! | + | Until next time, as always; stay safe, healthy, positive and creative!** |
+ | |||
+ | Maintenant, lorsque nous lançons notre programme, nous obtenons une valeur qui indique notre cap. Ma carte a un petit indicateur sérigraphié sur elle qui montre les axes x, y et z. Lorsque l'axe x est aligné sur le nord, le dispositif renvoie 0. | ||
+ | |||
+ | Enregistrez votre fichier sous le nom de lsm303aTest.py et exécutez-le. Déplacez votre plaque d' | ||
+ | |||
+ | J'ai mis le code de notre projet sur mon dépôt à https:// | ||
+ | |||
+ | Dans un prochain article (le mois prochain, j' | ||
+ | |||
+ | Jusqu' | ||
+ | |||
+ | //Encadré de la p 35 en haut à droite, lignes noires :// | ||
+ | |||
+ | **Next, the RPi Pico Micropython port doesn’t include a method called deviceAddrList, | ||
+ | |||
+ | Ensuite, le portage du Micropython vers le RPi Pico n' |
issue179/micro-ci_micro-la.1648385994.txt.gz · Dernière modification : 2022/03/27 14:59 de auntiee