issue112:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue112:python [2016/08/29 14:03] – créée auntiee | 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 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. | ||
- | 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 ' | + | 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:// | ||
- | i2c | + | i2c |
- | Inter-Integrated Circuit (i2c) protocol is intended to allow multiple " | + | Inter-Integrated |
+ | Circuit (i2c) protocol is intended to allow multiple " | ||
+ | ICs to communicate with one or more master chips. Like Serial Peripheral | ||
+ | Interface (SPi), it is intended for only short distance communications | ||
+ | within a single device. Like Asynchronous Serial Interfaces (RS-232 or | ||
+ | UARTs), it requires only two signal wires to exchange information. | ||
RS232 (Asynchronous Communications) | RS232 (Asynchronous Communications) | ||
- | No clock data is required on the lines; however, both sides must agree on the communications data rate. Requires hardware overhead (UART) at each end. | + | No clock data is required on the lines; however, both sides must agree |
+ | on the communications data rate. Requires hardware overhead (UART) at | ||
+ | each end. | ||
- | 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 on a single serial port, there are issues with multiple devices trying to use the two lines at the same time. | + | 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 | |
- | Most UART devices can support only certain set baud rates, the usual maximum is 230400 bits per second. | + | devices on a single serial port, there are issues with 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. | ||
+ | Rappelez-vous, | ||
+ | RPi. Cette fois-ci, grâce à l' | ||
+ | besoin de 5 broches (+5v, masse, les données du capteur, SDA (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 | ||
+ | |||
+ | Le | ||
+ | | ||
+ | | ||
+ | communiquer avec une ou plusieurs puces maîtres. Tout comme SPi (Serial | ||
+ | Peripherical Interface - Périphérique à Interface Série), il est prévu | ||
+ | pour des communications à courte distance sur un seul appareil. Comme | ||
+ | l' | ||
+ | câbles de signaux pour échanger des informations. | ||
+ | |||
+ | RS232 (Communication Asynchrone) | ||
+ | |||
+ | 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 sur 8 bits, au moins un bit de début et un bit d' | ||
+ | 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, si | ||
+ | plusieurs périphériques tentent d' | ||
+ | simultanément, | ||
+ | |||
+ | La plupart des dispositifs UART ne peuvent supporter que certaines | ||
+ | vitesses de transmission pré-réglées, | ||
+ | bits par seconde. | ||
+ | |||
+ | RX <--- TX | ||
+ | TX ---> RX | ||
+ | |||
+ | |||
+ | **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/ | ||
+ | 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 | 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/devices connected to the master, the required number of pins can quickly overwhelm the number of available inputs/outputs. | + | Le plus gros inconvénient de SPI est le nombre de picots nécessaires. La |
+ | | ||
+ | chaque esclave supplémentaire nécessite l'addition | ||
+ | select | ||
+ | maître. Si vous voulez utiliser plusieurs capteurs/dispositifs connectés | ||
+ | au maître, le nombre nécessaire de picots peut rapidement dépasser le | ||
+ | nombre des entrées/sorties disponibles. | ||
+ | |||
+ | 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 " | ||
Ligne 40: | Ligne 121: | ||
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.): | 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 | ||
+ | |||
+ | Comme un port série asynchrone, i2c ne nécessite que deux lignes, mais | ||
+ | ces deux lignes peuvent supporter jusqu' | ||
+ | Contrairement à SPI, i2c peut prendre en charge un système multi-maître, | ||
+ | | ||
+ | plusieurs périphériques maîtres. Les maîtres ne peuvent pas communiquer | ||
+ | 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' | ||
+ | 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. | ||
+ | |||
+ | 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 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:// | ||
+ | |||
+ | 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'ai pas réussi à le lancer sur une seule | ||
+ | ligne pour une raison inconnue.) : | ||
sudo apt-get install i2c-tools | sudo apt-get install i2c-tools | ||
Ligne 45: | 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 right). | + | **I was able to get Fritzing to come up with the i2c backpack (shown top |
+ | right). | ||
- | 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). | + | 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). | ||
Now reboot and once the RPi is up, in a terminal type: | Now reboot and once the RPi is up, in a terminal type: | ||
Ligne 53: | Ligne 176: | ||
i2cdetect -y 1 | i2cdetect -y 1 | ||
- | 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. | + | 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. | ||
- | 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. | + | 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:// | + | J'ai pu obtenir le schéma Fritzing équipé avec l' |
+ | en haut à droite). | ||
- | 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, | + | 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). | ||
+ | |||
+ | Maintenant, redémarrez et, une fois que le RPi est prêt, tapez dans le | ||
+ | terminal : | ||
+ | |||
+ | i2cdetect -y 1 | ||
+ | |||
+ | Ceci permettra de vérifier que l' | ||
+ | et vous dira aussi quelle est l' | ||
+ | Regardez la capture d' | ||
+ | devrait ressembler. | ||
+ | |||
+ | 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' | ||
+ | |||
+ | **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:// | ||
+ | |||
+ | 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, | ||
+ | as the driver that we just wrote.. | ||
import i2c_lcd_driver | import i2c_lcd_driver | ||
- | from time import | + | from time import |
mylcd = i2c_lcd_driver.lcd() | mylcd = i2c_lcd_driver.lcd() | ||
Ligne 68: | Ligne 226: | ||
mylcd.lcd_display_string(" | mylcd.lcd_display_string(" | ||
- | 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 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. | + | 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 | ||
+ | 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. | ||
+ | |||
+ | 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. | ||
+ | |||
+ | Until then have fun.** | ||
+ | |||
+ | La première série de code est une bibliothèque qui fonctionnera comme un | ||
+ | | ||
+ | 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' | ||
+ | |||
+ | import i2c_lcd_driver | ||
+ | from time import | ||
+ | |||
+ | mylcd = i2c_lcd_driver.lcd() | ||
+ | |||
+ | 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' | ||
+ | nous pouvons continuer. Le code suivant est pratiquement le même que le | ||
+ | mois dernier, modifié pour utiliser l' | ||
+ | version parallèle. | ||
- | 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 | + | 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, des | ||
+ | servos | ||
+ | (un moteur miniature de loisirs) pour être prêt. Dans quelques mois, nous allons | ||
+ | commencer à travailler avec le microcontrôleur | ||
+ | | ||
- | Until then have fun. | + | Jusque-là, amusez vous bien. |
issue112/python.1472472202.txt.gz · Dernière modification : 2016/08/29 14:03 de auntiee