issue166:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue166:python [2021/02/27 16:59] – créée auntiee | issue166:python [2021/03/04 14:28] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | I don’t know if you have heard, but on January 21, 2021, the folks at Raspberry Pi announced another new board. It’s called the Raspberry Pi Pico. It’s their first Microcontroller-classed product, and it’s priced at just $4.00 USD (£3.60 GBP). They even created their own chip called the RP2040 for it. I won’t give you all the specs, but I will point out a few: | + | **I don’t know if you have heard, but on January 21, 2021, the folks at Raspberry Pi announced another new board. It’s called the Raspberry Pi Pico. It’s their first Microcontroller-classed product, and it’s priced at just $4.00 USD (£3.60 GBP). They even created their own chip called the RP2040 for it. I won’t give you all the specs, but I will point out a few: |
• A Dual-core ARM Cortex M0+ processor running up to 133 MHz | • A Dual-core ARM Cortex M0+ processor running up to 133 MHz | ||
• 264KB of SRAM and 2MB on-board Flash memory | • 264KB of SRAM and 2MB on-board Flash memory | ||
Ligne 12: | Ligne 12: | ||
• Development for the board supports both a C/C++ SDK, and an official MicroPython port | • Development for the board supports both a C/C++ SDK, and an official MicroPython port | ||
• Low power. All GPIOs use 3.3VDC (NOT 5V) | • Low power. All GPIOs use 3.3VDC (NOT 5V) | ||
- | • And more | + | • And more** |
- | Now, some of these specs might not get your blood pumping faster, but consider the 2 I2c ports, 2 SPI ports and 2 UARTS, that’s a lot of interface possibilities, | + | Je ne sais pas si vous en avez entendu parler, mais le 21 janvier 2021, les gens de Raspberry Pi ont annoncé la création d'une nouvelle carte. Elle s' |
+ | - Un processeur ARM Cortex M0+ à double cœur fonctionnant jusqu' | ||
+ | - 264 Ko de SRAM et 2 Mo de mémoire Flash embarquée | ||
+ | - Programmation par glisser-déposer utilisant le stockage de masse via USB | ||
+ | - 26 broches GPIO (Entrées/ | ||
+ | - 2 x I2c, 2 x SPI, 2 x UARTs et 3 x ports ADC 12 bits | ||
+ | - 16 broches PWM contrôlables | ||
+ | - Port série ARM 3 broches pour le débogage (SWD) | ||
+ | - Capteur de température intégré | ||
+ | - 8 machines d' | ||
+ | - Port Micro-USB B pour l' | ||
+ | - Le développement de la carte supporte à la fois un SDK C/C++ et un portage officiel de MicroPython | ||
+ | - Faible consommation. Tous les GPIO utilisent 3,3 VDC (PAS 5 V) | ||
+ | - Et plus encore... | ||
+ | |||
+ | |||
+ | **Now, some of these specs might not get your blood pumping faster, but consider the 2 I2c ports, 2 SPI ports and 2 UARTS, that’s a lot of interface possibilities, | ||
The board itself is only 51x21mm (that’s just about 2x0.8 inches in American thinking)! | The board itself is only 51x21mm (that’s just about 2x0.8 inches in American thinking)! | ||
- | Of course, I went on the Internet to try to buy one the very first day. I was extremely lucky to find an online retailer that still had some left. It hasn’t arrived yet, so I can’t give you any information on how the board performs quite yet. In addition, I had to find a Micro-USB type B cable before I could use it. Luckily, I have some header pins from an old project that are left over, so I should be good to go as soon as it comes in. | + | Of course, I went on the Internet to try to buy one the very first day. I was extremely lucky to find an online retailer that still had some left. It hasn’t arrived yet, so I can’t give you any information on how the board performs quite yet. In addition, I had to find a Micro-USB type B cable before I could use it. Luckily, I have some header pins from an old project that are left over, so I should be good to go as soon as it comes in.** |
- | You can find more on the Raspberry Pi Pico at https:// | + | Certaines de ces spécifications ne feront peut-être pas battre votre cœur plus rapidement, mais si on considère les 2 ports I2c, les 2 ports SPI et les 2 UARTS, cela fait beaucoup de possibilités d' |
- | In addition to all of that, the very next day (January 22, 2021), the Arduino group announced they also will be releasing a new board based on the 2040 chipset along with some extras like WIFI and Bluetooth as well as 9-axis IMU sensor and microphone. They have stated that they will be updating their IDE to support the new board. It’s expected to be called the Arduino Nano RP2040. Very little is known at this point beyond these details. You can see the announcement at https:// | + | La carte elle-même ne fait que 51x21mm |
- | If you are interested in the Raspberry Pi Pico or the new Arduino Nano RP2040, or if you are still on the fence about it all, check out this link. https://www.raspberrypi.org/ | + | Bien sûr, je suis allé sur Internet pour essayer d'en acheter une dès le premier jour. J'ai eu beaucoup de chance de trouver un détaillant en ligne qui en avait encore. Elle n'est pas encore arrivée, donc je ne peux pas encore vous donner d' |
- | Now that the announcements are over, we’ll get back to Python things. Most of you readers have been with me for a while, and you might remember back to late 2015 and early 2016 where I started talking about the Raspberry Pi and Python programming. I’ve been having to do some small-board computer programming development lately and one project specifically had me creating a prototype test program on the RPi for GPIO use. I am somewhat proud of the fact that I have at least one of every RPi board model – from the original version 1 to the latest version 4, and a RPi zero. There are three boards that are in constant use, one as a media center, one as a network server, and one that is for “current” RPi type projects. | ||
- | One of the downsides of working with the Raspberry Pi is that they are slow. Even the RPi 4 with 8 Gig of Ram is somewhat slow compared to my “big boy” Linux box. But, as they say, needs must. When you need to deal with GPIO programming and sensors, there are very few options out there that don’t include the RPi. IOT (Internet Of Things) and sensors pretty much require either the RPi or an Arduino (which is another story all together). | + | **You can find more on the Raspberry Pi Pico at https://www.raspberrypi.org/ |
- | However, some of that frustration might be gone. I recently found a small device that allows us to do GPIO programming directly on a Linux box, Windows machine, or Mac, without | + | In addition to all of that, the very next day (January 22, 2021), the Arduino group announced they also will be releasing a new board based on the 2040 chipset along with some extras like WIFI and Bluetooth as well as 9-axis IMU sensor and microphone. They have stated that they will be updating their IDE to support |
- | Check out https:// | + | Vous pouvez trouver plus d' |
+ | |||
+ | Outre tout cela, le lendemain (le 22 janvier 2021), le groupe Arduino a annoncé la sortie d'une nouvelle carte basée sur le chipset 2040, avec quelques extras comme le Wifi et le Bluetooth, ainsi qu'un capteur IMU 9 axes et un microphone. Ils ont déclaré qu'ils mettront à jour leur IDE pour supporter la nouvelle carte. Elle devrait s' | ||
+ | |||
+ | |||
+ | **If you are interested in the Raspberry Pi Pico or the new Arduino Nano RP2040, or if you are still on the fence about it all, check out this link. https:// | ||
+ | |||
+ | Si vous êtes intéressé par le Raspberry Pi Pico ou le nouvel Arduino Nano RP2040, ou si vous êtes toujours hésitant à ce sujet, consultez ce lien : https:// | ||
+ | |||
+ | |||
+ | **Now that the announcements are over, we’ll get back to Python things. Most of you readers have been with me for a while, and you might remember back to late 2015 and early 2016 where I started talking about the Raspberry Pi and Python programming. I’ve been having to do some small-board computer programming development lately and one project specifically had me creating a prototype test program on the RPi for GPIO use. I am somewhat proud of the fact that I have at least one of every RPi board model – from the original version 1 to the latest version 4, and a RPi zero. There are three boards that are in constant use, one as a media center, one as a network server, and one that is for “current” RPi type projects. | ||
+ | |||
+ | One of the downsides of working with the Raspberry Pi is that they are slow. Even the RPi 4 with 8 Gig of Ram is somewhat slow compared to my “big boy” Linux box. But, as they say, needs must. When you need to deal with GPIO programming and sensors, there are very few options out there that don’t include the RPi. IOT (Internet Of Things) and sensors pretty much require either the RPi or an Arduino (which is another story all together).** | ||
+ | |||
+ | Maintenant que ces annonces sont faites, nous allons revenir aux choses en Python. La plupart d' | ||
+ | |||
+ | L'un des inconvénients du travail avec les Raspberry Pi, c'est qu'ils sont lents. Même le RPi 4 avec 8 Go de Ram est un peu lent par rapport à mon grand boîtier Linux. Mais, comme on dit, les besoins doivent être satisfaits. Quand vous devez vous occuper de la programmation de GPIO et de capteurs, il y a très peu d' | ||
+ | |||
+ | |||
+ | **However, some of that frustration might be gone. I recently found a small device that allows us to do GPIO programming directly on a Linux box, Windows machine, or Mac, without the use of the remote GPIO and a RPi in the mix. (We’ll talk about remote GPIO another time). In theory, you should be able to create and test Python GPIO programs on your main development machine, and then simply copy the code over to your target RPi, make the connections to your sensor, and, with (possibly) one change to the code, be running on the RPi immediately. Are you thinking “If it sounds too good to be true, it has to be?” We’ll find out. I ordered one just about the same time that I ordered the RPi PICO and it came in just last night.** | ||
+ | |||
+ | Cependant, une partie de cette frustration pourrait bien avoir disparu. J'ai récemment trouvé un petit appareil qui nous permet de faire de la programmation de GPIO directement sur un boîtier Linux, une machine Windows ou un Mac, sans utiliser le GPIO distant et un RPi dans ce mélange. (Nous parlerons des GPIO distants une autre fois). En théorie, vous devriez pouvoir créer et tester des programmes en Python pour les GPIO sur votre machine de développement principale, puis copier le code sur votre RPi cible, établir les connexions avec votre capteur et, avec (éventuellement) une modification du code, les exécuter immédiatement sur le RPi. Vous vous dites : « Si ça semble trop beau pour être vrai, ça doit l' | ||
+ | |||
+ | |||
+ | **Check out https:// | ||
Following the instructions at https:// | Following the instructions at https:// | ||
+ | |||
+ | sudo usermod -a -G dialout $USER** | ||
+ | |||
+ | Consultez le site https:// | ||
+ | |||
+ | En suivant les instructions sur https:// | ||
sudo usermod -a -G dialout $USER | sudo usermod -a -G dialout $USER | ||
- | The reason for this, just in case you are interested, is that the connection to the RTK.GPIO board not only powers the board, but communicates via one of the serial ports on the Linux box. | + | |
+ | **The reason for this, just in case you are interested, is that the connection to the RTK.GPIO board not only powers the board, but communicates via one of the serial ports on the Linux box. | ||
Next, we need to make sure that some software exists on our main machine. You probably already have these packages on your machine, but just in case, we’ll enter the following command into the terminal: | Next, we need to make sure that some software exists on our main machine. You probably already have these packages on your machine, but just in case, we’ll enter the following command into the terminal: | ||
Ligne 45: | Ligne 91: | ||
They suggest that you reboot your machine at this point. Finally we need to install the Python library. | They suggest that you reboot your machine at this point. Finally we need to install the Python library. | ||
+ | |||
+ | sudo -H pip3 install RTk** | ||
+ | |||
+ | La raison en est, au cas où cela vous intéresserait, | ||
+ | |||
+ | Ensuite, nous devons nous assurer que certains logiciels existent sur notre machine principale. Ces logiciels sont probablement déjà sur votre machine, mais juste au cas où, nous entrerons la commande suivante dans le terminal : | ||
+ | |||
+ | sudo apt-get install python3-pip python3-setuptools python3-wheel | ||
+ | |||
+ | Ils vous suggèrent de redémarrer votre machine à ce stade. Enfin, nous devons installer la bibliothèque Python. | ||
sudo -H pip3 install RTk | sudo -H pip3 install RTk | ||
- | Now that that is all done, we can start adding the components to our protoboard. Of course, you’ll need a protoboard, a few jumpers, an LED, a resistor (220 ohm), and a momentary switch. | + | |
+ | **Now that that is all done, we can start adding the components to our protoboard. Of course, you’ll need a protoboard, a few jumpers, an LED, a resistor (220 ohm), and a momentary switch. | ||
Remember that LEDs have a positive and negative leg. The Anode is the longer leg and should be connected to the positive voltage, the Cathode is the negative (shorter) leg. | Remember that LEDs have a positive and negative leg. The Anode is the longer leg and should be connected to the positive voltage, the Cathode is the negative (shorter) leg. | ||
- | In our circuit (next page, top left), we’ll be setting the GPIO pin to a low level in order to turn the LED on, so we’ll connect the 220 ohm resistor directly between the 3.3 volt pin and the Anode of the LED, then connect the Cathode to our GPIO pin to complete the circuit. For the switch, we’ll connect one side of the switch directly to ground and the other directly to a different GPIO pin | + | In our circuit (next page, top left), we’ll be setting the GPIO pin to a low level in order to turn the LED on, so we’ll connect the 220 ohm resistor directly between the 3.3 volt pin and the Anode of the LED, then connect the Cathode to our GPIO pin to complete the circuit. For the switch, we’ll connect one side of the switch directly to ground and the other directly to a different GPIO pin** |
- | Now for the code. I’m using the same code that we did in the original FCM# 104 article, with a few modifications where needed. | + | Maintenant que tout cela est fait, nous pouvons commencer à ajouter les composants à notre carte d' |
+ | |||
+ | N' | ||
+ | |||
+ | Dans notre circuit (page suivante, en haut à gauche), nous allons mettre la broche GPIO à un niveau bas afin d' | ||
+ | |||
+ | |||
+ | **Now for the code. I’m using the same code that we did in the original FCM# 104 article, with a few modifications where needed. | ||
# import RPi.GPIO as GPIO | # import RPi.GPIO as GPIO | ||
Ligne 59: | Ligne 123: | ||
import RTk.GPIO as GPIO | import RTk.GPIO as GPIO | ||
- | First, of course, we need to import the GPIO library. On the RPi, we used the RPi.GPIO library, but for our RTK board, we need to use the RTk library. | + | First, of course, we need to import the GPIO library. On the RPi, we used the RPi.GPIO library, but for our RTK board, we need to use the RTk library.** |
- | In the original | + | Passons maintenant au code. J' |
- | So we have our LED connected to GPIO pin 18 and the button connected to GPIO pin 17. Now we can create a function to do all the setup commands (above). | + | # importer RTk.GPIO en tant que GPIO |
- | Next, we’ll create a loop function (bottom left) to constantly look for the button to be pressed. When we see that, we’ll drive the LED pin to a low state, turning on the LED. | + | import RTk.GPIO as GPIO |
+ | |||
+ | Tout d' | ||
+ | |||
+ | |||
+ | **In the original code, we had the option to use either the BCM GPIO pin numbers or the physical board numbers. At this point, it looks like the RTK board supports only the BCM pin numbering scheme. I have yet to verify this, but for the sake of time, we’ll stick with the BCM numbering scheme. Luckily, the RTK board has the pin numbers silk screened directly on the board right next to the GPIO pins. In the original code, I opted to use the board numbers, so I’ve just commented out those commands and uncommented the code that deals with the BCM numbering (top right). | ||
+ | |||
+ | So we have our LED connected to GPIO pin 18 and the button connected to GPIO pin 17. Now we can create a function to do all the setup commands (above).** | ||
+ | |||
+ | Dans le code d' | ||
+ | |||
+ | Ainsi, nous avons notre LED connectée à la broche GPIO 18 et le bouton connecté à la broche GPIO 17. Nous pouvons maintenant créer une fonction pour effectuer toutes les commandes de configuration (ci-dessus). | ||
+ | |||
+ | |||
+ | **Next, we’ll create a loop function (bottom left) to constantly look for the button to be pressed. When we see that, we’ll drive the LED pin to a low state, turning on the LED. | ||
Next, we will create a destroy function that will reset the LED pin and properly release the GPIO library code. | Next, we will create a destroy function that will reset the LED pin and properly release the GPIO library code. | ||
Ligne 76: | Ligne 154: | ||
- | Finally, we’ll create the entry point code to get things started (next page, top right). | + | Finally, we’ll create the entry point code to get things started (next page, top right).** |
- | When you run the program, you’ll see the LED turn on as long as the button is pressed. | + | Ensuite, nous allons créer une fonction en boucle (en bas à gauche) pour surveiller en permanence le bouton qui sera enfoncé. Lorsque nous verrons cela, nous mettrons la broche de la LED dans un état bas, ce qui allumera la LED. |
- | So, at this point, I can say that, for simple | + | Ensuite, nous créerons une fonction destroy (détruire) qui réinitialisera la broche de la LED et libérera correctement le code de la bibliothèque |
- | I have placed the code on my github repository, as I often do. You can find it at: | + | def destroy() : |
+ | |||
+ | GPIO.output(LedPin, | ||
+ | |||
+ | GPIO.cleanup() | ||
+ | |||
+ | |||
+ | Enfin, nous allons créer le code du point d' | ||
+ | |||
+ | |||
+ | **When you run the program, you’ll see the LED turn on as long as the button is pressed. | ||
+ | |||
+ | So, at this point, I can say that, for simple GPIO work, the RTK.GPIO board works well for development of RPi programs that access the GPIO pins. Will it work with other sensors and modules designed for the RPi? Well, that’s yet to be seen, since my RTK.GPIO board just came last night. I’ll be spending some of my self-imposed quarantine time working with this new board. I do know from the board website, that there is some question about working with SOME i2c devices, and I haven’t heard anything about the support of SPI devices. So my testing will include both of those along with some other simple modules like relays and trying some other “normal” components. I will keep you advised.** | ||
+ | |||
+ | Lorsque vous exécuterez le programme, vous verrez la LED s' | ||
+ | |||
+ | Donc, à ce stade, je peux dire que, pour un simple travail sur les GPIO, la carte RTK.GPIO fonctionne bien pour le développement de programmes RPi qui accèdent aux broches GPIO. Est-ce qu' | ||
+ | |||
+ | |||
+ | **I have placed the code on my github repository, as I often do. You can find it at: | ||
https:// | https:// | ||
With any luck, next month I will be looking deeply into the RPi Pico, MicroPython and possibly CircuitPython as well. My Pico should be here either on January 28th or 29th as well as the micro B USB cable that powers and communicates with the device. My plan is to talk about hookup, installing MicroPython and Circuit Python on the board, and a few test programs. | With any luck, next month I will be looking deeply into the RPi Pico, MicroPython and possibly CircuitPython as well. My Pico should be here either on January 28th or 29th as well as the micro B USB cable that powers and communicates with the device. My plan is to talk about hookup, installing MicroPython and Circuit Python on the board, and a few test programs. | ||
- | As always, until next time; stay safe, healthy, positive and creative! | + | As always, until next time; stay safe, healthy, positive and creative!** |
+ | |||
+ | J'ai mis le code sur mon dépôt github, comme je le fais souvent. Vous pouvez le trouver à l' | ||
+ | https:// | ||
+ | |||
+ | Avec un peu de chance, le mois prochain, je me pencherai sur le RPi Pico, MicroPython et peut-être aussi CircuitPython. Mon Pico devrait arriver le 28 ou le 29 janvier, ainsi que le câble USB micro B qui alimente et communique avec l' | ||
+ | |||
+ | Comme toujours, jusqu' |
issue166/python.1614441568.txt.gz · Dernière modification : 2021/02/27 16:59 de auntiee