issue112:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue112:python [2016/09/11 14:29] – andre_domenech | issue112:python [2016/09/12 14:57] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | **This month, we will be using a 16x2 LCD display with an i2c interface to do the same thing that we did last month (FCM 111) with the Dallas Semiconductor DS18B20 Temperature sensor. If you remember, we had to use many I/O pins on the RPi. This time, thanks to the i2c display, we will need only 5 pins (+5v, Gnd, Sensor Data, SDA (data) and SCL (clock)) to do the same thing. | + | **This |
- | ** | + | need only 5 pins (+5v, Gnd, Sensor Data, SDA (data) and SCL (clock)) to |
- | **Before we get started with our project, a discussion about i2c is in order. I have distilled the following discussion from a wonderful tutorial by ' | + | do the same thing. |
- | **i2c** | + | Before we get started with our project, a discussion about i2c is in |
+ | order. I have distilled the following discussion from a wonderful | ||
+ | tutorial by ' | ||
+ | https:// | ||
- | **Inter-Integrated Circuit (i2c) protocol is intended to allow multiple " | + | i2c |
- | **RS232 | + | Inter-Integrated |
+ | | ||
+ | ICs to communicate with one or more master chips. Like Serial Peripheral | ||
+ | | ||
+ | within a single device. Like Asynchronous | ||
+ | UARTs), it requires only two signal wires to exchange information. | ||
- | **No clock data is required on the lines; however, both sides must agree on the communications data rate. Requires hardware overhead | + | RS232 (Asynchronous Communications) |
- | **In addition to the 8 data bits, at least one start and one stop bit are required | + | No clock data is required on the lines; however, both sides must agree |
+ | on the communications data rate. Requires hardware overhead (UART) | ||
+ | each end. | ||
- | **Most UART devices | + | In addition to the 8 data bits, at least one start and one stop bit are |
+ | required for each data frame. While it is possible to connect multiple | ||
+ | devices | ||
+ | trying to use the two lines at the same time. | ||
+ | Most UART devices can support only certain set baud rates, the usual | ||
+ | maximum is 230400 bits per second. | ||
- | **RX < | + | RX < |
- | **TX ---------> | + | TX ---------> |
- | Ce mois-ci, nous utiliserons un écran LCD 16x2 avec une interface i2c pour faire la même chose que ce que nous avons fait le mois dernier (FCM n° 111) avec le capteur de température Dallas Semiconductor DS18B20. | + | Ce mois-ci, nous utiliserons un écran LCD 16x2 avec une interface i2c |
- | + | pour faire la même chose que ce que nous avons fait le mois dernier (FCM | |
- | Avant de commencer notre projet, une discussion à propos de i2c s' | + | n° 111) avec le capteur de température Dallas Semiconductor DS18B20. |
+ | Rappelez-vous, nous avons dû utiliser beaucoup de broches E/S sur le | ||
+ | RPi. Cette fois-ci, grâce à l' | ||
+ | besoin de 5 broches (+5v, masse, les données | ||
+ | et SCL (horloge)) pour faire la même chose. | ||
+ | Avant de commencer notre projet, une présentation de i2c s' | ||
+ | résumé la discussion qui suit d'un merveilleux tutoriel écrit par | ||
+ | SFUPTOWNMAKER sur Sparkfun.com accessible à | ||
+ | https:// | ||
i2c | i2c | ||
- | Le Protocole | + | Le |
+ | Protocole | ||
+ | destiné à permettre à plusieurs CI numériques « esclaves » de | ||
+ | communiquer avec une ou plusieurs puces maîtres. Tout comme SPi (Serial | ||
+ | Peripherical Interface - Périphérique | ||
+ | pour des communications à courte distance sur un seul appareil. Comme | ||
+ | l'Interface Série Asynchrone (RS-232 ou UART), il ne nécessite que deux | ||
+ | câbles de signaux pour échanger des informations. | ||
RS232 (Communication Asynchrone) | RS232 (Communication Asynchrone) | ||
- | Aucune donnée d' | + | Aucune donnée d' |
+ | deux côtés doivent s' | ||
+ | Il nécessite un surcoût matériel (UART) à chaque extrémité. | ||
- | Outre les données 8 bits, un bit de début et un bit d' | + | Outre les données |
+ | sont nécessaires pour chaque trame de données. Bien qu'il soit possible | ||
+ | de connecter plusieurs périphériques sur un seul port série, | ||
+ | plusieurs périphériques | ||
+ | simultanément, des problèmes s' | ||
- | La plupart des appareils | + | La plupart des dispositifs |
+ | vitesses de transmission | ||
+ | bits par seconde. | ||
- | RX ← --- TX | + | RX <--- TX |
TX ---> RX | TX ---> RX | ||
- | **SPI** | + | **SPI |
- | **The biggest drawback with SPI is the number of pins required. Connecting a single master to a single slave requires four lines, each additional slave requires one addition chip select (CS) IO pin to the master. If you want to use many sensors/ | + | The biggest drawback with SPI is the number of pins required. Connecting |
+ | a single master to a single slave requires four lines, each additional | ||
+ | slave requires one addition chip select (CS) IO pin to the master. If | ||
+ | you want to use many sensors/ | ||
+ | required number of pins can quickly overwhelm the number of available | ||
+ | inputs/ | ||
+ | |||
+ | SPI (see diagram below) is good for high data rate full-duplex | ||
+ | (simultaneous send/ | ||
+ | MHz.** | ||
+ | |||
+ | SPI | ||
+ | |||
+ | Le plus gros inconvénient de SPI est le nombre de picots nécessaires. La | ||
+ | | ||
+ | chaque esclave supplémentaire nécessite l' | ||
+ | select » (CS - sélection de circuit intégré) d' | ||
+ | maître. Si vous voulez utiliser plusieurs capteurs/ | ||
+ | au maître, le nombre nécessaire de picots peut rapidement dépasser le | ||
+ | nombre des entrées/ | ||
+ | |||
+ | SPI (voir diagramme ci-dessous) est bon pour un taux de transfert élevé | ||
+ | en full-duplex (envoi/ | ||
+ | transfert de données peut monter jusqu' | ||
- | **SPI (see diagram below) is good for high data rate full-duplex (simultaneous send/ | ||
- | **i2c** | + | **i2c |
- | **i2c requires only 2 lines like async serial, but those two lines can support up to 1008 slave devices. Unlike SPI, i2c can support a multi-master system – allowing multiple slave devices to communicate to multiple master devices. There is a limitation that the masters can not communicate with each other on the i2c bus, and they must take turns using the bus lines. i2c has a similar overhead to Async in that, for each 8 bits of data, one extra bit is required as an " | + | i2c requires only 2 lines like async serial, but those two lines can support up to 1008 slave devices. Unlike SPI, i2c can support a multi-master system – allowing multiple slave devices to communicate to multiple master devices. There is a limitation that the masters can not communicate with each other on the i2c bus, and they must take turns using the bus lines. i2c has a similar overhead to Async in that, for each 8 bits of data, one extra bit is required as an " |
- | **Data rates are somewhere between Async and SPI. Most i2c devices can communicate at between 100 KHz to 400 KHz.** | + | Data rates are somewhere between Async and SPI. Most i2c devices can communicate at between 100 KHz to 400 KHz. |
- | **In the diagram above, SDA is the data line and SCL is the clock line. | + | In the diagram above, SDA is the data line and SCL is the clock line. |
- | ** | + | |
- | **Hopefully I didn’t completely confuse you and you are ready to go ahead with our project. | + | |
- | ** | + | |
- | **A very good resource for what we are about to do is at http:// | + | |
- | **Make sure that i2c is enabled on the RPi. This is set in raspi-config.** | + | Hopefully I didn’t completely confuse you and you are ready to go ahead with our project. |
- | **Now, in a terminal, use apt-get | + | A very good resource for what we are about to do is at http://www.circuitbasics.com/ |
- | **sudo apt-get install | + | Make sure that i2c is enabled on the RPi. This is set in raspi-config. |
- | **sudo apt-get install python-smbus** | + | Now, in a terminal, use apt-get to install two support libraries. (I wasn’t able to get it to work as a single liner for some reason.): |
+ | |||
+ | sudo apt-get install i2c-tools | ||
+ | |||
+ | sudo apt-get install python-smbus** | ||
i2c | i2c | ||
- | i2c ne nécessite que deux lignes | + | Comme un port série asynchrone, |
+ | ces deux lignes peuvent supporter jusqu' | ||
+ | Contrairement à SPI, i2c peut prendre en charge un système multi-maître, | ||
+ | permettant à de multiples périphériques esclaves de communiquer avec | ||
+ | plusieurs périphériques maîtres. | ||
+ | entre eux sur le bus i2c et doivent utiliser les lignes de bus à tour de | ||
+ | rôle ; il y a donc des limitations. i2c a un « coût » semblable à celui d'Async | ||
+ | en ce sens que, pour chaque donnée de 8 bits, un bit supplémentaire est | ||
+ | nécessaire comme un bit " | ||
+ | complexes que le PSI, mais inférieure à Async. | ||
- | Les débits de données se situent entre Async et PSI. La plupart des périphériques i2c peuvent communiquer entre 100 KHz à 400 KHz. | + | Les débits de données se situent entre Async et PSI. La plupart des |
+ | périphériques i2c peuvent communiquer entre 100 KHz à 400 KHz. | ||
- | Dans le schéma ci-dessus, SDA est la ligne de données et SCL est la ligne d' | + | Dans le schéma ci-dessus, SDA est la ligne de données et SCL est la |
+ | ligne d' | ||
- | Espérons que je ne vous aie pas totalement embrouillé, et soyez prêt à continuer avec notre projet. | + | Espérons que je ne vous aie pas totalement embrouillé et que vous êtes prêt à |
+ | continuer avec notre projet. | ||
- | Une très bonne ressource pour ce que nous sommes sur le point de le faire est sur http:// | + | Une très bonne ressource pour ce que nous sommes sur le point de le |
+ | faire est sur | ||
+ | http:// | ||
- | Assurez-vous que i2c est activé sur le RPi. Ceci est situé | + | Assurez-vous que i2c est activé sur le RPi. On règle cela dans raspi-config. |
- | Maintenant, dans un terminal, utilisez apt-get pour installer deux bibliothèques de support. (Je n'étais | + | Maintenant, dans un terminal, utilisez apt-get pour installer deux |
+ | bibliothèques de support. (Je n'ai pas réussi à le lancer sur une seule | ||
+ | ligne pour une raison | ||
sudo apt-get install i2c-tools | sudo apt-get install i2c-tools | ||
Ligne 87: | Ligne 162: | ||
sudo apt-get install python-smbus | sudo apt-get install python-smbus | ||
+ | **I was able to get Fritzing to come up with the i2c backpack (shown top | ||
+ | | ||
+ | Hook up the SDA pin of the i2c backpack to PHYSICAL pin 3 on the RPi | ||
+ | (this is GPIO2) and the SCL on the backpack to PHYSICAL pin 5 (GPIO3). | ||
+ | Pick a free 5v pin on the RPi (either pin 2 or 4) and a free ground | ||
+ | (pins 6 or 9) and connect them to the backpack VCC and Gnd. Don’t forget | ||
+ | we need the temp sensor connected to GPIO4 as last month (along with | ||
+ | the resistor to +5VDC). | ||
- | **I was able to get Fritzing to come up with the i2c backpack (shown top right).** | + | Now reboot and once the RPi is up, in a terminal type: |
- | **Hook up the SDA pin of the i2c backpack to PHYSICAL pin 3 on the RPi (this is GPIO2) and the SCL on the backpack to PHYSICAL pin 5 (GPIO3). Pick a free 5v pin on the RPi (either pin 2 or 4) and a free ground (pins 6 or 9) and connect them to the backpack VCC and Gnd. Don’t forget we need the temp sensor connected to GPIO4 as last month (along with the resistor to +5VDC).** | + | i2cdetect -y 1 |
- | **Now reboot and once the RPi is up, in a terminal type:** | + | This will verify that the i2c interface |
+ | tell you what address is used by the LCD display device. Look at the | ||
+ | screen dump shown right to see what it should look like. | ||
- | **i2cdetect -y 1** | + | As you can see, my device is at 3f, but yours might be at a different |
+ | address. When you create the driver below (either typing directly from | ||
+ | the article or from the pastebin page), you will need to enter your | ||
+ | device address in line 22.** | ||
- | **This will verify that the i2c interface is working on your Pi, and also tell you what address is used by the LCD display device. Look at the screen dump shown right to see what it should look like. | + | J'ai pu obtenir le schéma Fritzing équipé avec l' |
- | ** | + | en haut à droite). |
- | **As you can see, my device is at 3f, but yours might be at a different address. When you create the driver below (either typing directly from the article or from the pastebin page), you will need to enter your device address in line 22.** | + | |
- | **The first set of code is a library that will work as a driver for the i2c LCD. This should be saved as i2c_lcd_driver.py. The code is on http:// | + | Branchez la broche SDA de l' |
+ | le RPI (qui est GPIO2) et le SCL de l' | ||
+ | 5 (GPIO3). Choisissez une broche de 5V libre sur le RPi (broche 2 ou 4) | ||
+ | et une masse libre (broche 6 ou 9) et connectez-les aux VCC (+5V) et | ||
+ | Gnd (masse) de l' | ||
+ | capteur de température à GPIO4, comme le mois dernier (avec la | ||
+ | résistance à +5VDC). | ||
- | **Now, we are going to do a short test to make sure everything works. Type in the following code and save it as i2c_test1.py, into the same folder as the driver that we just wrote...** | + | Maintenant, redémarrez et, une fois que le RPi est prêt, tapez dans le |
+ | terminal : | ||
- | **import i2c_lcd_driver | + | i2cdetect -y 1 |
- | from time import** | + | |
- | **mylcd = i2c_lcd_driver.lcd()** | + | Ceci permettra de vérifier que l' |
+ | et vous dira aussi quelle est l' | ||
+ | Regardez la capture d' | ||
+ | devrait ressembler. | ||
- | **mylcd.lcd_display_string("This is a test",1)** | + | Comme vous pouvez le voir, mon appareil est à 3f, mais le vôtre pourrait |
+ | être à une adresse différente. Lorsque vous créez le pilote ci-dessous | ||
+ | (en tapant, soit directement à partir de l' | ||
+ | pastebin), vous devrez entrer l' | ||
- | **If everything here is correct, you should see “This | + | **The first set of code is a library that will work as a driver for the |
+ | i2c LCD. This should be saved as i2c_lcd_driver.py. The code is on | ||
+ | http:// | ||
- | **That pretty much wraps up our discussion of LCDs and i2c. We will be using i2c LCDs and other i2c devices in the future, so you should keep them safe for later on. Next month, we will start working with motors, servos and stepper motors. So, run out, and get a hobby motor to be ready. In a few months, we’ll start working with the Arduino microcontroller | + | Now, we are going to do a short test to make sure everything works. Type |
+ | | ||
+ | | ||
- | **Until then have fun.** | + | import i2c_lcd_driver |
+ | from time import | ||
- | J'ai pu obtenir Fritzing livré avec le sac à dos i2c (montré en haut à droite). | + | mylcd = i2c_lcd_driver.lcd() |
- | Branchez la broche SDA du sac à dos i2c à la broche physique n°3 sur le RPI (qui est GPIO2) et le SCL sur le sac à dos de la broche PHYSIQUE n° 5 (GPIO3). Choisissez une broche de 5V libre sur le RPi (soit la broche 2 ou 4) et un espace libre (broches 6 ou 9) et connectez-les au sac à dos VCC et Gnd. N' | + | mylcd.lcd_display_string("This is a test", |
- | Maintenant, redémarrez et une fois que le RPi est prêt, tapez dans le terminal : | + | If everything here is correct, you should see “This is a test” at the |
- | + | first character position on the first line of the LCD display. If it’s | |
- | i2cdetect -y 1 | + | good, we can move forward. The following code is basically the same code |
+ | from last month modified to use the i2c display instead of the parallel | ||
+ | version. | ||
- | Ceci permettra de vérifier que l' | + | That pretty much wraps up our discussion of LCDs and i2c. We will be |
+ | using i2c LCDs and other i2c devices in the future, so you should keep | ||
+ | them safe for later on. Next month, we will start working with motors, | ||
+ | servos and stepper motors. So, run out, and get a hobby motor to be | ||
+ | ready. In a few months, we’ll start working with the Arduino | ||
+ | microcontroller and then learn to interface the RPi to control the | ||
+ | Arduino. | ||
- | Comme vous pouvez le voir, mon appareil est à 3f, mais le vôtre pourrait être à une adresse différente. Lorsque vous créez le pilote ci-dessous (soit en tapant directement à partir de l' | + | Until then have fun.** |
- | La première série de code est une bibliothèque qui fonctionnera comme un pilote pour le LCD i2c. Cela devrait être enregistré sous i2c_lcd_driver.py. Le code est disponible sur http:// | + | La première série de code est une bibliothèque qui fonctionnera comme un |
+ | pilote pour le LCD i2c. Cela devrait être enregistré sous | ||
+ | i2c_lcd_driver.py. Le code est disponible sur | ||
+ | http:// | ||
- | Maintenant, nous allons faire un petit test pour nous assurer que tout fonctionne. Tapez le code suivant et enregistrez-le sous i2c_test1.py, dans le même dossier que le pilote que nous venons d' | + | Maintenant, nous allons faire un petit test pour nous assurer que tout |
+ | fonctionne. Tapez le code suivant et enregistrez-le sous i2c_test1.py | ||
+ | dans le même dossier que le pilote que nous venons d' | ||
- | importer | + | import |
+ | from time import | ||
- | i2c_lcd_driver.lcd | + | mylcd = i2c_lcd_driver.lcd() |
- | mylcd.lcd_display_string ( "Ceci est un test", 1) | + | mylcd.lcd_display_string(" |
- | Si tout est correct, vous devrez voir « Ceci est un test » à la position du premier caractère de la première ligne de l' | + | Si tout est correct, vous devrez voir « Ceci est un test » à la position |
+ | du premier caractère de la première ligne de l' | ||
+ | nous pouvons continuer. Le code suivant est pratiquement le même que le | ||
+ | mois dernier, modifié pour utiliser l' | ||
+ | version parallèle. | ||
- | Ce joli beaucoup se termine notre discussion sur les écrans LCD et i2c. Nous utiliserons les écrans LCD i2c et d' | + | Cela termine notre présentation des écrans LCD et i2c. Nous |
+ | utiliserons les écrans LCD i2c et d' | ||
+ | vous devrez donc les garder en sécurité pour plus tard. Le mois | ||
+ | prochain, nous allons commencer à travailler avec des moteurs, | ||
+ | servos et des moteurs pas-à-pas. Alors, dépêchez-vous de vous procurer un « hobby motor » | ||
+ | (un moteur | ||
+ | commencer à travailler avec le microcontrôleur Arduino, puis apprendre à | ||
+ | | ||
- | Jusque-là, | + | Jusque-là, |
issue112/python.1473596940.txt.gz · Dernière modification : 2016/09/11 14:29 de andre_domenech