Outils pour utilisateurs

Outils du site


issue116:python

Ceci est une ancienne révision du document !


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 microcontrolleurs comme l'Arduino Uno. Aujourd'hui, nous allons 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 fait à partir de Langage C. Cependant, les bonnes nouvelles sont que ça ne vous prendra pas trop de temps à 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 microcontrolleur Arduino

L'Arduino (selon le site web arduino.cc) est une plateforme électronique open-source basée sur un matériel et un logiciel facile à utiliser. Il est livré comme étant open-source matériel et logiciel, 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.

En fait, 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-ci contre.

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

En bas à droite, la photo d'une carte Arduino Uno «standard» (quoiqu'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 parcourrez le nmenu File | Examples | 01.Basics | Blink . Une autre fenêtre va s'ouvrir avec le nouveau code source. Je l'ai recopié ici (page suivante) pour pouvoir discuter de ce projet très simple.

Evidemment, 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 dasn 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 le bouton reset est pressé. 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 avons seulement à dire à la carte que la broche n°13 fonctionnera comme une sortie. Sur la Uno et la Mega, il y a une DEL 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 envoyons, par la fonction digitalWrite, un signal Haut pour allumer la DEL, attendre 1 seconde (avec la fonction delay), renvoyer un signal Bas pour éteindre la DEL, attendre un 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. 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.

issue116/python.1484067325.txt.gz · Dernière modification : 2017/01/10 17:55 de frangi