1 WIRE
Christian Dupaty
BTS Systèmes Numériques
Lycée Fourcade - Gardanne
Académie d’Aix-Marseille
RASPBERRY PI et PYTHON : 1 WIRE
1) TP : 1 WIRE :DS18B20
1 WIRE est une liaison série asynchrone half-duplex avec possibilité de transfert de l’alimentation
sur la ligne de donnée développée par Dallas-Semiconductors. Cette liaison série est donc très
économique en terme de connexion.
Le bus 1 WIRE : http://www.maximintegrated.com/products/1-wire/flash/overview/index.cfm
La ligne DATA du capteur est connectée sur GPIO4 avec pull-up VDD 3.3v (4.7KΩ)
voir : http://www.maximintegrated.com/datasheet/index.mvp/id/2812
Module w1
https://www.kernel.org/doc/Documentation/w1/w1.generic
Le maître (Raspberry Pi) recherche périodiquement de nouveaux esclaves.
Lorsqu’un esclave est trouvé, w1 cherche son driver, si aucun driver n’est trouvé un driver par défaut est
chargé.
La famille des drivers pour capteurs de température est w1_therm pour les mémoires : w1_smem.
BTS systèmes numériques http://genelaix.free.fr 2/4
RASPBERRY PI et PYTHON : 1 WIRE
Test en mode ‘normal’, attention le capteur DS18B20-PAR ne fonctionne qu’en mode ‘parasites’, il faut
utiliser ici un capteur DS18B20 :
sudo modprobe w1-gpio
sudo modprobe w1-therm
Pour charger ces modules automatiquement à chaque démarrage :
Ajouter au fichier /etc/modules les lignes
w1-gpio
w1-therm
cd /sys/bus/w1/devices
ls
cd 28-xxxxxxxxxx
cat w1_slave
Cette dernière commande affiche la dernière lecture des registres (SCRATCHPAD) du DS18B20 (format little
endian, poids faibles en premiers)
01B8 (pf puis PF): temperature sur 12bits , resolution 0,0625°C : 01B8=440 : 440x0.0625=27,5 °C
4B46 (PF puis pf): temperature d’alarme
7F : mesure sur 12bits (Ob01111111)
FF0810 : réservés par MAXIM-IC
8A : CRC
# DEMO LECTURE DS18B20 MAXIM-IC
#!/usr/bin/python
import os # commandes linux (modprob)
import glob # recherche de chemins (path)
import time # pour la boucle principale
os.system('modprobe w1-gpio') # active le protocole 1WIRE
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/' # recherche le numero du capteur
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
def read_temp_raw(): # lecture
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp(): # extraction de la temperature
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
BTS systèmes numériques http://genelaix.free.fr 3/4
RASPBERRY PI et PYTHON : 1 WIRE
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
return temp_c
while True:
print('Temperature sur DS18B20 : %f degC' %read_temp())
time.sleep(1)
© WIKIPEDIA
BTS systèmes numériques http://genelaix.free.fr 4/4