Outils pour utilisateurs

Outils du site


issue111:python

Last month, we worked with the DS18B20 Temperature Sensor. This month we will start to interface a 16×2 LCD display to show our temperatures. Don’t tear down your setup, but make sure you have enough room to mount the display on your breadboard. You’ll need about 32 pinholes for the length of the device and 16 for the pins to connect to. You will have only three pinholes left if you mount the display at the bottom of the vertical holes, so you will need to use some jumpers to connect the bottom verticals to the top verticals. Of course, the 16×2 display has 16 characters on two rows. The backlight comes in many colours. I chose a blue one. We can address each of the 32 character positions individually, or print pretty much like we do to the regular monitor. We will be making 8 connections to the RPi as well as the three that we used for the temperature sensor last month. You will need the following additional items for this month: • 10K Potentiometer • 16×2 LCD Display • Many breadboard jumpers, Male to Male and 8 Male to Female

Le mois dernier, nous avons travaillé avec le capteur de température DS18B20. Ce mois-ci, nous commencerons par interfacer un afficheur LCD 16×2 pour montrer nos températures. Ne défaites pas votre montage, mais assurez-vous que vous avez assez de place pour monter votre afficheur sur la plaque d'essai. Vous aurez besoin de 32 trous pour la longueur de la pièce et de 16 trous pour connecter les picots. Vous n'aurez que 3 trous de rab si vous montez l'afficheur en bas des trous verticaux ; aussi, vous aurez besoin de quelques cavaliers pour connecter les trous verticaux du bas à ceux du haut.

Bien sûr, l'afficheur 16×2 a deux lignes de 16 caractères. Le rétro-éclairage existe dans de nombreuses couleurs. J'en ai choisi un bleu. Nous pouvons adresser individuellement chacun des 32 caractères, ou imprimer un peu comme nous le faisons avec le moniteur normal.

Nous ferons 8 connexions au RPi, en plus des trois que nous utilisions le mois dernier pour le capteur de température. Ce mois-ci, nous aurons besoin des composants supplémentaires suivants : • un potentiomètre de 10k ; • un afficheur LCD 16×2 ; • de nombreux cavaliers pour la plaque d'essai, mâle-mâle et 8 mâle-femelle.

By the time you are done, the wiring diagram (and the resulting board) will look like a bit of a rat’s nest, but go slowly – make sure you have the wiring correct. As you can see in the graphic above, it’s pretty crazy, so I’ll lay out all the wiring for you in text. First, you will need to put a jumper between the two horizontal busses on both the top and bottom. That way, you’ll have power and ground on both busses. I chose to do it on the left side, but you can put it anywhere that is convenient for you. The next thing to do is to wire in the potentiometer. One side (it doesn’t matter which) needs to go to ground and the other side to our 5 volt supply. The center contact (the wiper) will wire to pin 3 of the LCD display. This controls the contrast, so you can control how bright the characters appear. You should already have 5 volts to the board, as well as ground, from last month. On the display, connect pin 1 to ground and pin 2 to the +5 volt buss. That makes three connections out of the twelve we need. Pin 6 of the display goes to pin 22 of the RPi. This is the Enable pin. Pin 5 on the display goes to ground, and pin 4 to pin 27 on the RPi. We are up to 6 connections so far. That makes us halfway there. Because we have to use pin 4 for our sensor, we can’t control the backlight.

Une fois terminé, le schéma de câblage (et en vrai sur la plaque) ressemblera un peu à un plat de nouilles, mais allez-y lentement, assurez-vous de faire le câblage correctement.

Comme vous pouvez le voir sur le schéma ci-dessus, c'est plutôt horrible ; aussi, je vais vous détailler tout le câblage dans un texte.

D'abord, vous devrez mettre un cavalier entre les deux bus horizontaux, en haut et en bas. Ainsi, vous aurez l'alimentation et la masse sur les deux bus. J'ai choisi de le faire sur la gauche, mais vous pouvez le mettre où vous voulez. Ensuite, câblez le potentiomètre. Un côté (n'importe lequel) doit être à la masse et l'autre à l'alimentation 5 V. Le contact du milieu (le curseur) sera câblé au picot 3 de l'afficheur LCD. Cela contrôle le contraste ; ainsi, vous pouvez régler la luminosité des caractères. Le 5 V, ainsi que la masse, devront déjà être sur la plaque (cf. le mois dernier).

Sur l'afficheur, connectez le picot 1 à la masse et le picot 2 au bus +5 V. Déjà 3 connexions faites sur les douze. Le picot 6 de l'afficheur est relié au picot 22 du RPi. C'est le picot « Enable » (Activer). Le picot 5 de l'afficheur va à la masse, et le picot 4 au picot 27 du RPi. Nous en sommes à 6 connexions. Nous voici à mi-chemin. Puisque nous devons utiliser le picot 4 pour notre capteur, nous ne pouvons pas régler le rétro-éclairage.

Now we will work backwards from pin 16. Pin 16 goes to ground, and pin 15 to +5v. Pin 15 is actually the backlight voltage on mine. If you find the display too bright, you could put the wiper of another potentiometer connected between +5v and ground and control the display backlight. Now for the data lines. There are actually 8 data lines, but thankfully, we will be using only 4. Pins 11 to 14 are D4, D5, D6 and D7 (counting from 0). Here is the connection list. Now everything is hooked up, so we will continue with some sample code to test the display. But we need to get the Adafruit python library for LCDs. In a terminal window, type the following… git clone https://github.com/adafruit/Adafruit_Python_CharLCD cd Adafruit_Python_CharLCD sudo python setup.py install cd examples

Maintenant, nous allons travailler en descendant depuis le picot 16. Le picot 16 va à la masse, et le picot 15 au +5 V. Sur le mien, le picot 15 est en fait le voltage du rétro-éclairage. Si vous trouvez l'affichage trop brillant vous pouvez mettre le curseur d'un autre potentiomètre, connecté entre l'alimentation et la masse, pour régler l'intensité de l'affichage.

Maintenant, les lignes de données. Il y a réellement 8 lignes de données, mais, heureusement, nous n'en utiliserons que 4. Les picots 11 à 14 sont D4, D5, D6 et D7. (on compte à partir de 0). Voici le tableau de connexion :

Maintenant tout est connecté et nous continuerons avec un échantillon de code pour tester l'afficheur. Mais nous devons obtenir la bibliothèque python Adafruit pour LCD. Dans un terminal, tapez ce qui suit :

git clone https://github.com/adafruit/Adafruit_Python_CharLCD

cd Adafruit_Python_CharLCD

sudo python setup.py install

cd examples

Now load char_lcd.py into your favorite editor. Or, you could type it in from the previous page. Ignore the backlight messages, but you should see… Hello World! Show Cursor_ Blink Cursor_ Scroll (right and left) Flash backlight in 5 seconds… Goodbye! If everything worked, we are ready to proceed. If not, go back and check your wiring. Here is the modified program from last month that includes snippets from this example (top right) from Adafruit. (New code is in bold.) That’s about it for this month. Next month we will look at changing out our regular 16×2 display for a 16×2 I2C display (which uses only 2 lines for data and all control, and 2 lines for power.) We will also discuss the different ways of using serial communication for interfacing displays and other devices. Until then, have fun!

Maintenant, chargez char_lcd.py dans votre éditeur favori. Ou vous pouvez le copier à la main de la page précédente.

Ignorez les messages de rétro-éclairage ; mais vous devrez voir :

Hello World! (Bonjour le monde) Show Cursor_ (curseur visible) Blink Cursor_ (curseur clignotant) Scroll (right and left) (déplacement, à droite et à gauche) Flash backlight in 5 seconds… (rétro-éclairage flashant dans 5 secondes) Goodbye! (Au revoir !)

Si tout a bien fonctionné, vous êtes prêt pour un essai. Sinon, revenez en arrière pour contrôler votre câblage.

Voici le programme du mois dernier, modifié, qui inclut des bribes de code venant de cet exemple (en haut à droite) d'Adafruit. (Nouveau code en gras.)

C'est tout pour cette fois. Le mois prochain, nous examinerons le remplacement de notre afficheur 16×2 classique par un afficheur IC2 16×2 (qui n'utilise que deux lignes pour les données et tout contrôle et deux lignes pour l'alimentation). Nous présenterons aussi les différentes façons d'utiliser une communication série pour interfacer des afficheurs et autres dispositifs. Jusque-là, amusez-vous bien !

issue111/python.txt · Dernière modification : 2016/08/24 11:18 de andre_domenech