Ceci est une ancienne révision du document !
This month, we will be using my current favorite temperature sensor; the Dallas Semiconductor DS18B20 One Wire sensor. It looks like a ‘normal’ transistor, but is a very accurate sensor, much more so than the DHT11 that we used last month. It doesn’t do humidity, but for temperature readings, it’s a very good and inexpensive device. All data requests and output are sent on a single pin. It has an operating range from -55°C to 125°C ( -67°F to 257°F) and should be able to run about 3 metres (9.8 feet). It also has a parasitic mode that allows power to be derived from the data line. The data sheet can be found at https://datasheets.maximintegrated.com/en/ds/DS18B20.pdf. Here is what one sort of looks like… Wiring a single sensor is very easy. Shown right is the diagram.
Ce mois-ci, nous utiliserons mon capteur de température favori actuellement : le capteur Dallas Semiconductor DS18B20 One Wire. Il ressemble à un transistor « ordinaire », mais c'est un capteur très précis, beaucoup plus que le DHT11 utilisé le mois dernier. Il ne fait pas l'humidité, mais, pour les lectures de température, c'est un composant très bon et peu cher. Toutes les demandes et sorties de données se font sur un seul picot. Il a une plage de fonctionnement de - 55°C à 125°C (-67°F to 257°F) et devrait être capable de fonctionner avec 3 mètres de câble. Il a aussi un mode parasite qui permet de dériver l'alimentation de la ligne de données.
La feuille de spécifications peut être trouvée sur : https://datasheets.maximintegrated.com/en/ds/DS18B20.pdf. Voici à quoi il ressemble…
Le câblage d'un seul capteur est facile. Voir à droite sur le dessin.
There are only three connections to the RPi. Ground (sensor pin 1) to RPi pin 6, 3.3v (sensor pin 3) to RPi pin 1, and data (sensor pin 2) to RPI pin 7 (GPIO 4). You need to put a 4.7k resistor between sensor pins 2 and 3 (data and +Voltage). That’s it. If you wish to add more sensors to the project, simply connect them ground to ground, +voltage to +voltage and pin 2 to pin 2 of the “main” sensor. No additional resistors should be needed for a reasonable line length. Next page, right-hand side, is an example of a three sensor project. The Code One thing you have to do is tell the operating system you want to use kernel support for one-wire devices. If you are using Raspbian Jessie, this is done in raspi-config. If you are using another OS, then you must add the following line to the /boot/config.txt file.
Il n'y a que trois connexions au RPi. La masse (picot 1 du capteur) vers le picot 6 du RPi, 3,3 V (picot 3 du capteur) vers le picot 1 du RPi, et les données (picot 2 du capteur) vers le picot 7 du RPi (GPIO 4). Vous avez besoin d'un résistance de 4,7k entre les picots 2 et 3 du capteur (données et alim +). Elle y est. Si vous souhaitez ajouter d'autres capteurs au projet, connectez-les simplement de masse à masse, d'alim+ à alim+ et picot 2 à picot 2 vers le capteur « principal ». Pas besoin de résistances additionnelles pour une longueur de ligne raisonnable. Page suivante, à droite, vous trouvez un exemple d'un projet avec trois capteurs.
Le code
Vous devez dire au système d'exploitation quel noyau support vous souhaitez utiliser pour les capteurs à un fil. Si vous utilisez Raspbian Jessie, c'est déjà disponible dans raspi-config. Si vous utilisez un autre OS, vous devez alors ajouter la ligne suivante au fichier /boot/config.txt :
dtoverlay=w1-gpio The following two commands load the 1-Wire and thermometer drivers on GPIO 4. sudo modprobe w1-gpio sudo modprobe w1-therm We then need to change directory cd to our 1-Wire device folder and list ls the devices in order to ensure that our thermometer has loaded correctly. cd /sys/bus/w1/devices/ ls In the device drivers, your sensor should be listed as a series of numbers and letters. In this case, the device is registered as 28-000005e2fdc3. You then need to access the sensor with the cd command, replacing our serial number with your own.
cd 28-000005e2fdc3 The sensor periodically writes to the w1_slave file, so we simply use the cat command to read it. cat w1_slave This yields the following two lines of text, with the output t= showing the temperature in degrees Celsius. A decimal point should be placed before the ending three digits, e.g. the temperature reading we’ve received is 23.125 degrees Celsius. 72 01 4b 46 7f ff 0e 10 57 : crc=57 YES 72 01 4b 46 7f ff 0e 10 57 t=23125 Next page, top right, is how we had to do it in the “old days”, the library that we will actually use.
Timo Furrer has provided a wonderful library for us to use on the RPi written in pure Python. You can get it at https://github.com/timofurrer/w1thermsensor. The current version is 0.3.1 and is also available through pypi. The beauty of this library is that it takes almost all the work out of dealing with the sensors and allows you to just concentrate on your code. Next page, bottom right, is the “current day” code using Timo’s library… There are actually only 7 lines of code needed here. Those lines that are commented out are so you can see the other ways to get and print the data in various temperature units (celsius and kelvin). As I mentioned above, you can have more than one sensor on the same data line. So here is the code to make a single call to get the temp readings from all sensors in the system…
from w1thermsensor import W1ThermSensor for sensor in W1ThermSensor.get_available_sensors(): print(“Sensor %s has temperature %.2f” % (sensor.id, sensor.get_temperature())) Of course, you’ll want to do more than one data pull, so use the code above to modify it the way you want. If you would like to make a call to a particular sensor, you can use this code as a baseline. from w1thermsensor import W1ThermSensor sensor = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20, “28-000007444532”) temperature_in_celsius = sensor.get_temperature()
So, you can see, by using Timo‘s library, we can basically go from 27 lines of code to 3 (for a single call). That’s wonderful. I wanted to show you how to use a 16×2 LCD display with this, but I think I’ll leave room for the other authors and we’ll push that part out to next month. Don’t lose your project hardware, we’ll use it next month. Until then, enjoy checking out the temperatures around your office/abode.