Outils pour utilisateurs

Outils du site


issue166:python

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 • 264KB of SRAM and 2MB on-board Flash memory • Drag-and-drop programming using mass storage over USB • 26 multi-function GPIO pins • 2 x I2c, 2 x SPI, 2 x UARTs and 3 x 12-bit ADC ports • 16 controllable PWM pins • 3-pin ARM Serial Wire Debug (SWD) port • Temperature sensor on board • 8 programmable I/O (PIO) state machines • Micro-USB B port for power, data, and reprogramming the Flash memory • Development for the board supports both a C/C++ SDK, and an official MicroPython port • Low power. All GPIOs use 3.3VDC (NOT 5V) • And more

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'appelle Raspberry Pi Pico. C'est leur premier produit de la classe d'un microcontrôleur, et son prix n'est que de 4 $ US (3,33 EUR). Ils ont même créé leur propre puce appelée RP2040 pour ce produit. Je ne vous donnerai pas toutes les spécifications, mais je vous en signalerai quelques-unes : - Un processeur ARM Cortex M0+ à double cœur fonctionnant jusqu'à 133 MHz - 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/sorties à usage général) multifonctionnelles - 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'état à entrées/sorties programmables (PIO) - Port Micro-USB B pour l'alimentation, les données et la reprogrammation de la mémoire Flash - 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, and then add the 3 12-bit Analogue-to-Digital converters, and you’ve got a pretty powerful little board. Add to that, it is designed to work with Micro-Python, and I think it’s a winner! The board itself is only 51x21mm (that’s just about 2×0.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.

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'interface ; ajoutez aussi les 3 convertisseurs analogiques-numériques sur 12 bits, et vous obtenez une petite carte assez puissante. Ajoutez à cela qu'elle est conçue pour fonctionner avec Micro-Python, et je pense que c'est le gros lot !

La carte elle-même ne fait que 51x21mm (c'est à peu près 2×0.8 pouces dans la tête d'une américaine) !

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'informations sur le fonctionnement de la carte. En outre, j'ai dû trouver un câble Micro-USB de type B avant de pouvoir l'utiliser. Heureusement, il me reste quelques broches d'un ancien projet, donc je devrais pouvoir démarrer dès qu'elle arrivera.

You can find more on the Raspberry Pi Pico at https://www.raspberrypi.org/documentation/pico/getting-started/. 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://www.tomshardware.com/uk/news/arduino-rp2040. I am looking forward to evaluating the new Nano RP2040 as soon as it becomes available.

Vous pouvez trouver plus d'informations sur le Raspberry Pi Pico sur https://www.raspberrypi.org/documentation/pico/getting-started/.

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'appeler Arduino Nano RP2040. On en sait très peu pour l'instant en dehors de ces détails. Vous pouvez voir l'annonce sur https://www.tomshardware.com/uk/news/arduino-rp2040. Je suis impatient d'évaluer le nouveau Nano RP2040 dès qu'il sera disponible.

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/blog/raspberry-pi-silicon-pico-now-on-sale/. Scroll down until you see the section marked Partners. You’ll see information about new products coming from Adafruit, Arduino, Pimoroni, and Sparkfun – all based on the RP2040 chip. In fact, Adafruit has their own version of MicroPython called CircuitPython, and they’ve ported it to run on the RPi Pico. There are also a number of projects that they are working on that will use the RP2040 chip. If you do an Internet search for “RP2040”, you’ll see a huge number of hits that include the partners listed above along with images, videos and other announcements.

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://www.raspberrypi.org/blog/raspberry-pi-silicon-pico-now-on-sale/. Faites défiler vers le bas jusqu'à ce que vous voyiez la section intitulée « Partners » (Partenaires). Vous y trouverez des informations sur les nouveaux produits d'Adafruit, Arduino, Pimoroni et Sparkfun, tous basés sur la puce RP2040. En fait, Adafruit possède sa propre version de MicroPython appelée CircuitPython, et l'a portée pour qu'elle fonctionne sur le RPi Pico. Ils travaillent également sur un certain nombre de projets qui utiliseront la puce RP2040. Si vous effectuez une recherche sur Internet pour « RP2040 », vous obtiendrez un grand nombre de résultats qui incluent les partenaires énumérés ci-dessus ainsi que des images, des vidéos et d'autres annonces.

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'entre vous, lecteurs, sont avec moi depuis un certain temps, et vous vous souvenez peut-être de la fin 2015 et du début 2016 où j'ai commencé à parler de la programmation du Raspberry Pi et de Python. Ces derniers temps, j'ai dû faire du développement de programmation sur ordinateur de taille réduite et un projet m'a amené à créer un prototype de programme de test sur le RPi pour une utilisation des GPIO. Je suis assez fier du fait que je possède au moins un modèle de chaque carte RPi - de la version 1 originale à la dernière version 4, et un RPi zéro. Trois cartes sont utilisées en permanence, l'une comme centre multimédia, l'autre comme serveur réseau et la dernière pour les projets « actuels » de type RPi.

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'options qui n'incluent pas le RPi. L'IOT (Internet Of Things - Objets connectés) et les capteurs nécessitent soit le RPi, soit un Arduino (ce qui est une autre histoire).

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'être » ? Nous allons le découvrir. J'en ai commandé un à peu près en même temps que le RPi PICO et il est arrivé hier soir.

Check out https://uk.pi-supply.com/products/ryanteck-rtk-gpio-pc-gpio-interface, and you’ll see the product. It’s a tiny little board (5.5cm by 5 cm) that is less than $17 USD (shipping extra). We’ll go back to FCM 104 and recreate a very simple project for our first test. But there are some things that have to be done (not including purchasing and receiving the board) before we can get started. Following the instructions at https://learn.pi-supply.com/make/getting-started-with-the-rtk-gpio-board/#getting-started-with-the-rtk-gpio-board, we need to make sure that our user account on our Linux box is part of the dialout group. So in a terminal, we need to use the following command to do this: sudo usermod -a -G dialout $USER

Consultez le site https://uk.pi-supply.com/products/ryanteck-rtk-gpio-pc-gpio-interface, et vous verrez le produit. Il s'agit d'un tout petit circuit (5,5 cm sur 5 cm) qui coûte moins de 17 dollars US (frais de port en sus). Nous allons revenir au FCM 104 et recréer un projet très simple pour notre premier test. Mais il y a certaines choses à faire (sans compter l'achat et la réception de la carte) avant de pouvoir commencer.

En suivant les instructions sur https://learn.pi-supply.com/make/getting-started-with-the-rtk-gpio-board/#getting-started-with-the-rtk-gpio-board, nous devons nous assurer que notre compte d'utilisateur sur notre ordinateur Linux fait partie du groupe « dialout ». Dans un terminal, nous devons donc utiliser la commande suivante pour ce faire :

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. 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: sudo apt-get install python3-pip python3-setuptools python3-wheel 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, que la connexion à la carte RTK.GPIO non seulement alimente la carte, mais communique via l'un des ports série du boîtier Linux.

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

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. 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

Maintenant que tout cela est fait, nous pouvons commencer à ajouter les composants à notre carte d'essai. Bien sûr, vous aurez besoin d'une carte d'essai, de quelques cavaliers, d'une LED, d'une résistance (220 ohm) et d'un poussoir.

N'oubliez pas que les LED ont une branche positive et une branche négative. L'anode est la branche la plus longue et doit être connectée à la tension positive, la cathode est la branche négative (plus courte).

Dans notre circuit (page suivante, en haut à gauche), nous allons mettre la broche GPIO à un niveau bas afin d'allumer la LED ; aussi, nous allons connecter la résistance de 220 ohm directement entre la broche de 3,3 volts et l'anode de la LED, puis connecter la cathode à notre broche GPIO pour compléter le circuit. Pour l'interrupteur, nous connecterons un côté de l'interrupteur directement à la terre et l'autre directement à une autre broche GPIO.

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 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.

Passons maintenant au code. J'utilise le même code que nous avons utilisé dans l'article original du FCM n° 104, avec quelques modifications là où c'est nécessaire.

# importer RTk.GPIO en tant que GPIO

import RTk.GPIO as GPIO

Tout d'abord, bien sûr, nous devons importer la bibliothèque GPIO. Sur le RPi, nous avons utilisé la bibliothèque RPi.GPIO, mais pour notre carte RTK, nous devons utiliser la bibliothèque RTk.

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'origine, nous avions la possibilité d'utiliser soit les numéros de pin du BCM GPIO, soit les numéros physiques de la carte. À ce stade, il semble que la carte RTK ne supporte que le schéma de numérotation des broches BCM. Je n'ai pas encore vérifié cela, mais pour gagner du temps, nous allons nous en tenir au schéma de numérotation BCM. Heureusement, la carte RTK a les numéros de pins sérigraphiés directement sur la carte, juste à côté des pins GPIO. Dans le code original, j'ai choisi d'utiliser les numéros de la carte, donc j'ai tout simplement commenté ces commandes et décommenté le code qui traite de la numérotation BCM (en haut à droite).

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. def destroy(): GPIO.output(LedPin, GPIO.HIGH) GPIO.cleanup() Finally, we’ll create the entry point code to get things started (next page, top right).

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.

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 GPIO.

def destroy() :

  GPIO.output(LedPin, GPIO.HIGH)
  GPIO.cleanup()

Enfin, nous allons créer le code du point d'entrée pour démarrer (page suivante, en haut à droite).

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'allumer tant que vous appuyez sur le bouton.

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'elle fonctionnera avec d'autres capteurs et modules conçus pour le RPi ? Eh bien, cela reste à voir, puisque ma carte RTK.GPIO est arrivée hier soir. Je vais passer une partie du temps de la quarantaine que je me suis imposé à travailler avec cette nouvelle carte. Je sais, grâce au site Web de la carte, qu'il y a des questions sur le travail avec QUELQUES appareils i2c, et je n'ai rien entendu sur le support des appareils SPI. Mes tests porteront donc sur ces deux dispositifs ainsi que sur d'autres modules simples comme les relais et l'essai de certains autres composants « normaux ». Je vous tiendrai informés.

I have placed the code on my github repository, as I often do. You can find it at: https://github.com/gregwa1953/FCM166 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!

J'ai mis le code sur mon dépôt github, comme je le fais souvent. Vous pouvez le trouver à l'adresse : https://github.com/gregwa1953/FCM166

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'appareil. Mon plan est de parler du branchement, de l'installation de MicroPython et de CircuitPython sur la carte, et de quelques programmes de test.

Comme toujours, jusqu'à la prochaine fois ; restez en sécurité, en bonne santé, positif et créatif !

issue166/python.txt · Dernière modification : 2021/03/04 14:28 de andre_domenech