Outils pour utilisateurs

Outils du site


issue116:python

1

Welcome back to our continuing journey to control our world using Python, the Raspberry Pi, and microcontrollers like the Arduino Uno. In this leg of our journey, we will be learning the basics of the Arduino. This requires us to have a basic understanding of the Arduino and the Arduino programming language, which, unfortunately, is NOT python, but based on the C language. The good news, however, is that it won't take you long to get the hang of it, and we won't have to get too deep into it for the things we will be doing.

Je vous re-souhaite la bienvenue dans notre voyage vers le contrôle de notre monde avec Python, le Raspberry Pi, et les microcontrôleurs comme l'Arduino Uno. Aujourd'hui, nous allons apprendre les bases de l'Arduino.

Pour ceci, on a besoin de comprendre les bases de l'Arduino et celles de son langage de programmation, qui, malheureusement, n'est PAS du Python, mais basé sur le Langage C. Cependant, les bonnes nouvelles sont que ça ne vous prendra pas trop de temps pour le comprendre, et que nous n'aurons pas à creuser trop profond pour ce que l'on veut faire.

The Arduino Microcontroller The Arduino (quoting from the Arduino.cc website) is an open-source electronics platform based on easy-to-use hardware and software. It was released as open-source hardware and software, meaning that the plans and parts list are available for you to make your own Arduino from scratch. Basically, it allows you to read inputs and control output pins on the board in a process very similar to the GPIO pins on the Raspberry Pi. You can get the genuine Arduino boards for around $35 USD, but there are good clones out there (especially the Sparkfun Red Board) for around $20 USD. There are two boards that I would suggest for our projects. The UNO and the MEGA. shown top right is a breakdown of the two. Depending on your desire to go beyond our basic projects, you might want to consider going with the Mega board. Bottom right is an image of a “standard” (albeit dusty) Arduino Uno board.

Le microcontrôleur Arduino

L'Arduino (selon le site Web arduino.cc) est une plateforme électronique Open Source basée sur un matériel et un logiciel faciles à utiliser. Il est livré comme étant un matériel et un logiciel Open Source, ce qui veut dire que les plans et les listes de pièces sont publiés pour que vous puissiez faire votre propre Arduino de zéro.

Essentiellement, cela vous permet de lire sur les broches d'entrée et de contrôler les broches de sortie sur la carte, de manière similaire aux broches standards GPIO du Raspberry Pi.

Vous pouvez vous procurer des cartes Arduino authentiques pour environ 35 $, mais il y a aussi de bonnes répliques (surtout la Sparkfun Red Board) pour environ 20 $.

Je vous suggère deux cartes pour nos projets. La UNO et la MEGA. Voir en haut à droite pour un descriptif des deux.

Si vous désirez aller plus loin que nos projets de base, vous pourrez envisager l'achat d'une carte Mega.

En bas à droite, la photo d'une carte Arduino Uno « standard » (quoique un peu sale).

2

Open the Arduino IDE and select File | Examples | 01.Basics | Blink . This will cause another window to open with the new source code. I've copied it here (next page) so we can discuss this very simple project. Of course, the first thing you will notice is that the source code is in the C programming language. Not to worry, it's pretty simple for what we will be doing. The part at the top is a block comment which starts with the “/*” and ends with the “*/”. Like Python, the C compiler ignores these block comments. The lines that start with “” are single inline comments and can be put anywhere in code. However, once the compiler sees the “”, everything else on that line is ignored. There are two functions that must be included in every project… the setup function and the loop function. You can write your own, but there must be these two. The setup function runs every time the board is powered on, or the reset button is pressed. Once the setup function finishes, the loop function is called (at least in this example), and it runs continuously doing all the steps within it. In this setup function, we only have to tell the board that pin number 13 will function as an output pin. On the Uno and Mega, there is an onboard LED that is connected to pin 13, so we will be controlling that, as well as the actual pin, and not have to worry about any external components this time.. In the Loop function, we send, via the digitalWrite function, a High signal turning the LED on, wait 1 second (using the delay function), set it back to a low signal turning off the LED, wait another second, and then start the process all over.

Ouvrez l'EDI de l'Arduino et sélectionnez File | Examples | 01.Basics | Blink (Fichiers > Exemples > 01. Bases > Clignoter). Une autre fenêtre s'ouvrira avec le nouveau code source. Je l'ai recopié ici (page suivante) pour pouvoir discuter de ce projet très simple.

Évidemment, la première chose à remarquer est que le code source est en langage C. Ne vous inquiétez pas, c'est vraiment simple pour ce que l'on va faire.

La partie du haut est un bloc de commentaires qui commence avec un « /* » et se termine avec un « */ ». Comme pour Python, le compilateur C ignore ces blocs de commentaires. Les lignes qui commencent par « » sont des lignes de commentaires simples et peuvent être mises n'importe où dans le code. Mais une fois que le compilateur voit un « », tout ce qui reste derrière sur cette ligne est ignoré.

Il y a deux fonctions qui doivent être incluses dans chaque projet : la fonction de démarrage et la fonction de boucle. Vous pouvez écrire les vôtres, mais il doit y avoir les deux. La fonction de démarrage est lancée à chaque fois que la carte est allumée ou que l'on appuie sur le bouton reset. Une fois qu'elle est terminée, la fonction de boucle est appelée (au moins dans cet exemple) et elle tourne continuellement en faisant toutes les étapes qu'elle contient.

Dans cette fonction de démarrage, nous devons seulement dire à la carte que la broche n° 13 fonctionnera comme une sortie. Sur la Uno et la Mega, il y a une LED qui est connectée à la broche 13, nous pourrons ainsi la contrôler, au même titre que la broche elle-même, et n'aurons pas à nous préoccuper de composants externes pour l'instant. Dans la fonction de boucle, nous allons envoyer, par la fonction digitalWrite, un signal Haut pour allumer la LED, attendre 1 seconde (avec la fonction delay), renvoyer un signal Bas pour éteindre la LED, attendre une autre seconde, puis recommencer le processus.

Plug your Arduino board into the USB port of your computer and click on the round check-mark button to compile and verify your code. Once it reports that the compile has completed successfully, click on the button next to it (the one with the arrow pointing to the right) to upload your code to the Arduino board. In a few moments, if everything worked well, you should see the LED on the Arduino start to flash on and off in one-second cycles. If you got any error messages on the upload, check the settings under the Tools menu item. The board needs to be set to the type of board you are using, Port should be the one connected to the Arduino (which is USUALLY sensed for you). Now let's start to have a little fun modifying this code. The delay function takes one parameter, which is the number of milliseconds to delay. In this case we are waiting 1000 milliseconds, which is one second. Let's change both of the delay calls to 250 milliseconds and see what happens.

Branchez votre carte Arduino sur le port USB de votre ordinateur et cliquez sur le bouton à cocher rond pour compiler et vérifier votre code. Une fois qu'elle indique avoir terminé la compilation avec succès, cliquez sur le bouton d'à côté (celui avec la flèche vers la droite) pour charger votre code sur la carte Arduino. Dans quelques instants, si tout va bien, vous devriez voir la LED de l'Arduino commencer à clignoter chaque seconde de façon cyclique.

Si vous avez des messages d'erreur au moment du chargement, vérifiez le paramétrage grâce au menu Tools. La carte doit être paramétrée avec le type de carte que vous utilisez, le Port doit être celui connecté à l'Arduino (ce qui est NORMALEMENT votre cas).

Maintenant amusons-nous un peu à modifier ce code. La fonction delay prend un argument, le nombre de millisecondes à attendre. Dans notre cas nous attendons 1000 millisecondes, c'est-à-dire une seconde. Changeons les deux délais à 250 millisecondes et voyons ce qui se passe.

delay(250); Don't forget to end the statement with a semicolon. Click on the check button (re-compile) and then upload to the board. Hopefully it is now blinking twice a second. Now let's get a bit more creative. Let's make the board blink in Morse Code the SOS signal. That is three short blinks, a bit of delay, three long blinks, a bit of delay and three more short blinks. I won't try to get fancy at this point, but will simply copy and paste more digitalWrite and delay calls with the appropriate delays. Here is the modified loop function. Compile and upload the code. Next time, we will start working with some of the components we used in the earlier Raspberry Pi projects, so grab some LEDs and resistors from your component archives to be ready. Until then, play with some of the sample projects that you find in the Arduino IDE and have fun.

delay(250);

N'oubliez pas de terminer la ligne par un point virgule. Cliquez sur le bouton à cocher (re-compilation) et chargez le code sur la carte.

Normalement, elle clignote deux fois par seconde.

Soyons un peu plus créatifs. Faisons en sorte que la carte envoie le code morse S.O.S. C'est à dire trois clignotements courts, un petit delai, trois clignotements longs, un petit délai, et trois courts de plus. Je ne vais pas essayer de faire le malin pour cette étape, mais simplement copier/coller d'autres appels digitalWrite et delay avec les durées correspondantes. Voici la fonction boucle modifiée.

Compilez et chargez le code.

La prochaine fois, nous commencerons à travailler avec des composants que nous avons utilisés précédemment dans nos projets Raspberry Pi ; soyez prêts en ramenant des LED et des résistances de vos placards.

En attendant, jouez avec quelques-uns des exemples de projets que vous trouverez dans l'EDI d'Arduino et amusez-vous bien.

issue116/python.txt · Dernière modification : 2017/01/11 17:28 de andre_domenech