issue194: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 | ||
issue194:micro-ci_micro-la [2023/07/01 06:52] – créée d52fr | issue194:micro-ci_micro-la [2023/07/03 17:15] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Before we get started on this month’s subject, I want to give you a little bit of an update on MicroPython 1.20. | + | **Before we get started on this month’s subject, I want to give you a little bit of an update on MicroPython 1.20. |
As of the first of June 2023, the I2C issue still somewhat exists. You CAN use I2C, but, at this time, you can’t use the old defaults. I say old defaults because it looks like the people at RPi asked that the defaults be changed to what they are now. So, to be sure that your I2C projects will work under 1.20, use the table below to set your I2C pins. | As of the first of June 2023, the I2C issue still somewhat exists. You CAN use I2C, but, at this time, you can’t use the old defaults. I say old defaults because it looks like the people at RPi asked that the defaults be changed to what they are now. So, to be sure that your I2C projects will work under 1.20, use the table below to set your I2C pins. | ||
- | As to the NeoPixel driver, you CAN use the one you used previously, but should rename it, just to avoid any confusion between you and MicroPython. I’ve tested this using the Pico-W and the Pico with three sets of 1-meter strings of 30 devices each. Everything works fine. | + | As to the NeoPixel driver, you CAN use the one you used previously, but should rename it, just to avoid any confusion between you and MicroPython. I’ve tested this using the Pico-W and the Pico with three sets of 1-meter strings of 30 devices each. Everything works fine.** |
- | Also, as of June 2023, the BLE bluetooth drivers were still being worked on, but were showing great progress. When done, you should be able to use most code from ESP-32 boards. | + | Avant de commencer à parler du sujet du mois, je voudrais faire une petite mise à jour sur MicroPython 1.20. |
+ | |||
+ | Depuis le 1er juin 2023, le problème d'I2C est toujours d' | ||
+ | |||
+ | En ce qui concerne le pilote NeoPixel, vous POUVEZ utiliser celui que vous utilisiez précédemment, | ||
+ | |||
+ | |||
+ | **Also, as of June 2023, the BLE bluetooth drivers were still being worked on, but were showing great progress. When done, you should be able to use most code from ESP-32 boards. | ||
I’ll give you a hint about the subject of the next MTMT, which should be controlling motors using the Pico-W. This is a great way to break into robotics. | I’ll give you a hint about the subject of the next MTMT, which should be controlling motors using the Pico-W. This is a great way to break into robotics. | ||
Ligne 13: | Ligne 20: | ||
As far as I know, this sensor comes only in a 5Vdc version. However, not all is lost. I was able to get a small 4-channel I2C-safe Bi-directional Logic Level Converter that costs about $4.00 USD. It works well. | As far as I know, this sensor comes only in a 5Vdc version. However, not all is lost. I was able to get a small 4-channel I2C-safe Bi-directional Logic Level Converter that costs about $4.00 USD. It works well. | ||
- | You can see it in the wiring image above. | + | You can see it in the wiring image above.** |
- | The pinout is pretty straightforward, | + | Par ailleurs, en juin 2023, les pilotes Bluetooth BLE étaient toujours en cours de développement, |
+ | |||
+ | Je vous donne un indice sur le sujet du prochain Micro-ci Micro-là, qui devrait être le contrôle de moteurs à l'aide du Pico-W. C'est un excellent moyen de s' | ||
+ | |||
+ | Passons maintenant au sujet du mois, le capteur d' | ||
+ | |||
+ | A ma connaissance, | ||
+ | |||
+ | Vous pouvez le voir dans l' | ||
+ | |||
+ | |||
+ | **The pinout is pretty straightforward, | ||
I’ve tested the project with and without the logic level converter, and I haven’t noticed any major difference between the two setups, so if you don’t have a converter, you should be able to use it without any problems. | I’ve tested the project with and without the logic level converter, and I haven’t noticed any major difference between the two setups, so if you don’t have a converter, you should be able to use it without any problems. | ||
Ligne 21: | Ligne 39: | ||
How it works | How it works | ||
- | There is a lot of science behind this little sensor and I would be remiss if I didn’t address it here. So I’m going to put on my science geek hat before we get into the code. | + | There is a lot of science behind this little sensor and I would be remiss if I didn’t address it here. So I’m going to put on my science geek hat before we get into the code.** |
- | Looking at the datasheet, whenever the Pico/ | + | Le brochage est assez simple, même si vous utilisez le convertisseur de niveau logique. J'ai créé un petit tableau (ci-dessous) pour vous aider. |
- | Our program can then take the information from the driver and simply display it, or use it to determine how much closer to the detected object the application wants to get. | + | J'ai testé le projet avec et sans convertisseur de niveau logique et je n'ai pas remarqué de différence majeure entre les deux configurations, donc si vous n'avez pas de convertisseur, |
- | The sensor has a range somewhere between about 2 cm and 4 meters, but the max distance could be anywhere between 2 and 4 meters. | + | Comment cela fonctionne-t-il ? |
+ | |||
+ | Il y a beaucoup de science derrière ce petit capteur et je m'en voudrais de ne pas en parler ici. Je vais donc mettre mon chapeau de scientifique avant d' | ||
+ | |||
+ | |||
+ | **Looking at the datasheet, whenever the Pico/ | ||
+ | |||
+ | Our program can then take the information from the driver and simply display it, or use it to determine how much closer to the detected object the application wants to get.** | ||
+ | |||
+ | |||
+ | En regardant la fiche technique, chaque fois que le Pico/ | ||
+ | |||
+ | Notre programme peut alors prendre l' | ||
+ | |||
+ | |||
+ | **The sensor has a range somewhere between about 2 cm and 4 meters, but the max distance could be anywhere between 2 and 4 meters. | ||
All of that having been said, we have to remember to consider what kind of material we are going to be encountering as we try to use this project. | All of that having been said, we have to remember to consider what kind of material we are going to be encountering as we try to use this project. | ||
- | Because we are relying on an echo coming back to the sensor, we have to take into account what type of object the ultrasonic signal is bouncing off of. If it’s a hard surface like wood or drywall, there won’t be much absorption. However, if the object is like a curtain, a fair amount of the signal will be absorbed and might give you false data. | + | Because we are relying on an echo coming back to the sensor, we have to take into account what type of object the ultrasonic signal is bouncing off of. If it’s a hard surface like wood or drywall, there won’t be much absorption. However, if the object is like a curtain, a fair amount of the signal will be absorbed and might give you false data.** |
+ | |||
+ | Le capteur a une portée comprise entre 2 cm et 4 mètres, mais la distance maximale peut être comprise entre 2 et 4 mètres. | ||
+ | |||
+ | Cela étant dit, nous devons nous rappeler de prendre en compte le type de matériau que nous allons rencontrer en essayant d' | ||
+ | |||
+ | Comme nous comptons sur un écho revenant au capteur, nous devons tenir compte du type d' | ||
+ | |||
+ | |||
+ | **Another thing you want to take into consideration – if you are going to mount the sensor on, let’s say, a rover or small robot – make sure that the sensor is mounted as straight as possible at the front of the device. This is due to the signal’s reflection not returning. The law of physics says that the angle of incidence is equal to the angle of reflection. This gets into some pretty deep science, so I’ll avoid a discussion here. If you are curious, you can look at http:// | ||
+ | |||
+ | The Code** | ||
+ | |||
+ | Une autre chose à prendre en considération - si vous vous apprêtez à monter le capteur sur, disons, un rover ou un petit robot - assurez-vous que le capteur est monté aussi droit que possible à l' | ||
- | Another thing you want to take into consideration – if you are going to mount the sensor on, let’s say, a rover or small robot – make sure that the sensor is mounted as straight as possible at the front of the device. This is due to the signal’s reflection not returning. The law of physics says that the angle of incidence is equal to the angle of reflection. This gets into some pretty deep science, so I’ll avoid a discussion here. If you are curious, you can look at http:// | + | Le code |
- | The Code | ||
- | There are a few drivers out there and you can certainly “roll your own”. However, I’m going to suggest you start with one that works pretty well for me. You can find it, as well as their code to run the driver on the RPi Pico/ | + | **There are a few drivers out there and you can certainly “roll your own”. However, I’m going to suggest you start with one that works pretty well for me. You can find it, as well as their code to run the driver on the RPi Pico/ |
They have a much deeper explanation of how the whole thing works, so you might want to take a look at their article as well as getting the code. I’ll also put it in a repository for your convenience. | They have a much deeper explanation of how the whole thing works, so you might want to take a look at their article as well as getting the code. I’ll also put it in a repository for your convenience. | ||
Ligne 45: | Ligne 90: | ||
import machine, time | import machine, time | ||
- | from machine import Pin | + | from machine import Pin** |
- | First, we have the imports. The driver simply uses machine, machine.Pin, and time. | + | Il existe plusieurs pilotes et vous pouvez certainement « faire votre propre choix ». Cependant, je vais vous suggérer de commencer par celui qui fonctionne très bien pour moi. Vous pouvez le trouver, ainsi que son code pour exécuter le pilote sur le RPi Pico/ |
- | Next, we have the actual class. I’m going to omit most of the comments. | + | Vous y trouverez une explication beaucoup plus détaillée de la façon dont tout cela fonctionne et vous pouvez donc jeter un coup d'œil à cet article et obtenir le code. Je le mettrai également dans un dépôt pour vous faciliter la tâche. |
+ | D' | ||
+ | import machine, time | ||
+ | |||
+ | from machine import Pin | ||
+ | |||
+ | |||
+ | **First, we have the imports. The driver simply uses machine, machine.Pin, | ||
+ | |||
+ | Next, we have the actual class. I’m going to omit most of the comments. | ||
The __init__ function (above) sets up the trigger and echo pins and the echo timeout value (in microseconds). | The __init__ function (above) sets up the trigger and echo pins and the echo timeout value (in microseconds). | ||
Ligne 57: | Ligne 111: | ||
The _send_pulse_and_wait() function sends a pulse of 10 microseconds to the trigger pin, then starts looking for a return pulse on the echo pin, checking the elapsed time (bottom left). | The _send_pulse_and_wait() function sends a pulse of 10 microseconds to the trigger pin, then starts looking for a return pulse on the echo pin, checking the elapsed time (bottom left). | ||
- | The distance_mm() function is not actually called from the test program, but is there if you want to get return data that is more granular than the distance_cm() function (below). | + | The distance_mm() function is not actually called from the test program, but is there if you want to get return data that is more granular than the distance_cm() function (below).** |
- | The distance_cm() function (below) is the actual function that gets called from the test program. It first calls the _send_pulse_and_wait() function, and gets back the pulse time. Then that value is returned as the number of centimeters that represents the time between the trigger and the echo return. | + | Tout d' |
+ | |||
+ | Ensuite, nous avons la classe proprement dite. Je vais omettre la plupart des commentaires. | ||
+ | |||
+ | La fonction __init__ (ci-dessus) définit les broches de déclenchement et d' | ||
+ | |||
+ | La fonction _send_pulse_and_wait() envoie une impulsion de 10 microsecondes à la broche de déclenchement, | ||
+ | |||
+ | La fonction distance_mm() n'est pas réellement appelée par le programme de test, mais elle est là si vous souhaitez obtenir des données de retour plus fines qu' | ||
+ | |||
+ | |||
+ | **The distance_cm() function (below) is the actual function that gets called from the test program. It first calls the _send_pulse_and_wait() function, and gets back the pulse time. Then that value is returned as the number of centimeters that represents the time between the trigger and the echo return. | ||
Ligne 67: | Ligne 132: | ||
We import the driver class and time.sleep. | We import the driver class and time.sleep. | ||
+ | |||
+ | from hcsr04 import HCSR04 | ||
+ | |||
+ | from time import sleep** | ||
+ | |||
+ | La fonction distance_cm() (ci-dessous) est la fonction qui est appelée par le programme de test. Elle appelle d' | ||
+ | |||
+ | |||
+ | Si vous souhaitez connaître le raisonnement qui sous-tend les formules, vous pouvez consulter les commentaires dans le fichier du pilote. | ||
+ | |||
+ | Le programme de test est, quant à lui, beaucoup plus simple que le pilote lui-même. | ||
+ | |||
+ | Nous importons la classe du pilote et time.sleep. | ||
from hcsr04 import HCSR04 | from hcsr04 import HCSR04 | ||
Ligne 72: | Ligne 150: | ||
from time import sleep | from time import sleep | ||
- | Then, we instantiate the class from the driver, assigning the trigger and echo pins, and providing a timeout value in microseconds. | ||
- | I added an optional line of code to assign a button on GPIO 20 as an input. This is so we can poll pin 20 for a low to let the program know that the button has been pressed. If the button has been pressed, it will allow the program to exit the forever loop that we will create. There is no issue if you don’t include a button in your setup. The code will still look for the falling signal, but it really won’t make any difference if it doesn’t see it. You can always just use the stop button in Thonny to end the program. | + | **Then, we instantiate the class from the driver, assigning the trigger and echo pins, and providing a timeout value in microseconds. |
+ | |||
+ | I added an optional line of code to assign a button on GPIO 20 as an input. This is so we can poll pin 20 for a low to let the program know that the button has been pressed. If the button has been pressed, it will allow the program to exit the forever loop that we will create. There is no issue if you don’t include a button in your setup. The code will still look for the falling signal, but it really won’t make any difference if it doesn’t see it. You can always just use the stop button in Thonny to end the program.** | ||
+ | |||
+ | Ensuite, nous instancions la classe du pilote, en assignant les broches de déclenchement et d' | ||
+ | |||
+ | J'ai ajouté une ligne de code optionnelle pour assigner un bouton sur le GPIO 20 en tant qu' | ||
- | We then enter the ‘forever’ loop (right). We poll the sensor and get a distance value. Then I include a calculation to convert centimeters to inches, display them both on the REPL terminal, check to see if the button has been pressed (if there is one), break the loop if we get a 0 from the button pin, and finally sleep for 1 second before starting the loop all over again. | + | **We then enter the ‘forever’ loop (right). We poll the sensor and get a distance value. Then I include a calculation to convert centimeters to inches, display them both on the REPL terminal, check to see if the button has been pressed (if there is one), break the loop if we get a 0 from the button pin, and finally sleep for 1 second before starting the loop all over again. |
Testing the program | Testing the program | ||
I’m going to assume that you are using a breadboard to hold your project. Place it on a table and place a ruler between the sensor and something fairly big like a box of tissues. This will give you a known reference. When you run the test program, you should see, about every second, something like this… | I’m going to assume that you are using a breadboard to hold your project. Place it on a table and place a ruler between the sensor and something fairly big like a box of tissues. This will give you a known reference. When you run the test program, you should see, about every second, something like this… | ||
+ | |||
+ | Distance 28.95189 cm - 11.39838 in** | ||
+ | |||
+ | Nous entrons ensuite dans la boucle « infinie » (à droite). Nous interrogeons le capteur et obtenons une valeur de distance. Ensuite, j' | ||
+ | |||
+ | Test du programme | ||
+ | |||
+ | Je vais supposer que vous utilisez une planque d' | ||
Distance 28.95189 cm - 11.39838 in | Distance 28.95189 cm - 11.39838 in | ||
- | If it works, and the distance returned is somewhat reasonable, you can test the sensor by pointing at something else. If not, you will most likely be getting a return that looks something like this… | + | |
+ | **If it works, and the distance returned is somewhat reasonable, you can test the sensor by pointing at something else. If not, you will most likely be getting a return that looks something like this… | ||
Distance 225.0 cm - 88.58268 in | Distance 225.0 cm - 88.58268 in | ||
- | This is the “default” return that means that the sensor didn’t receive a clear echo back before the timeout value. Given the sensor has a range between about 2 cm and 4 meters, as long as your target is within about 3 meters, this could mean that you have the trigger and echo pins swapped. The other possibility could be that the breadboard you are using has a bad spot or two. Try reseating the Pico, the sensor, and your jumper wires, and try again. I’ve seen it happen that even a brand new breadboard might have a bad spot, or 5, and the normal “tech support” answer is if you are using a breadboard, try different positions for your components and/or check your wires. | + | This is the “default” return that means that the sensor didn’t receive a clear echo back before the timeout value. Given the sensor has a range between about 2 cm and 4 meters, as long as your target is within about 3 meters, this could mean that you have the trigger and echo pins swapped. The other possibility could be that the breadboard you are using has a bad spot or two. Try reseating the Pico, the sensor, and your jumper wires, and try again. I’ve seen it happen that even a brand new breadboard might have a bad spot, or 5, and the normal “tech support” answer is if you are using a breadboard, try different positions for your components and/or check your wires.** |
+ | |||
+ | Si le programme fonctionne et que la distance affichée est raisonnable, | ||
+ | |||
+ | Distance 225.0 cm - 88.58268 in | ||
+ | |||
+ | Il s'agit du retour « par défaut » qui signifie que le capteur n'a pas reçu d' | ||
+ | |||
+ | |||
+ | **Well, that’s about it for this month. As I said, I’ve put the code for both the driver and the demo program in my repository at https:// | ||
+ | |||
+ | Until then, as always; stay safe, healthy, positive and creative!** | ||
- | Well, that’s about it for this month. As I said, I’ve put the code for both the driver and the demo program in my repository at https:// | + | Bon, ce sera tout pour ce mois-ci. Comme je l'ai dit, j'ai mis le code pour le pilote et pour le programme de démo dans mon dépôt à https:// |
- | Until then, as always; stay safe, healthy, positive and creative! | + | Jusque-là et comme d' |
issue194/micro-ci_micro-la.1688187178.txt.gz · Dernière modification : 2023/07/01 06:52 de d52fr