2017年3月10日 星期五

Micropython DS1820 1wire devices

http://docs.micropython.org/en/v1.8.2/esp8266/esp8266/tutorial/onewire.html

10. Controlling 1-wire devices

The 1-wire bus is a serial bus that uses just a single wire for communication (in addition to wires for ground and power). The DS18B20 temperature sensor is a very popular 1-wire device, and here we show how to use the onewire module to read from such a device.
For the following code to work you need to have at least one DS18B20 temperature sensor with its data line connected to GPIO12. You must also power the sensors and connect a 4.7k Ohm resistor between the data pin and the power pin.
import time
import machine
import onewire

# the device is on GPIO12
dat = machine.Pin(12)

# create the onewire object
ds = onewire.DS18B20(onewire.OneWire(dat))

# scan for devices on the bus
roms = ds.scan()
print('found devices:', roms)

# loop 10 times and print all temperatures
for i in range(10):
    print('temperatures:', end=' ')
    ds.convert_temp()
    time.sleep_ms(750)
    for rom in roms:
        print(ds.read_temp(rom), end=' ')
    print()
Note that you must execute the convert_temp() function to initiate a temperature reading, then wait at least 750ms before reading the value.

import machine #pour acc?der aux GPIOs
import onewire, ds18x20 #pour lire la sonde 1-wire    
import time

# (°F - 32) x 5/9 = °C or in plain english, First subtract 32, then multiply by 5,
# (°C × 9/5) + 32 = °F or in plain English, Multiple by 9, then divide by 5, then add 32.

# the device is on GPIO12
dat = machine.Pin(12)
# create the onewire object
ds = ds18x20.DS18X20(onewire.OneWire(dat))
# scan for devices on the bus
roms = ds.scan()  
print('found devices:', roms)
# loop 10 times and print all temperatures
for i in range(10):
    print('temperatures:', end=' ')
    ds.convert_temp()
    time.sleep_ms(750)
    for rom in roms:
degreeF=ds.read_temp(rom)
degreeC=(degreeF-32)*5/9
print(degreeF,'F', degreeC ,'C',  end=' ')
    print()

# Note that you must execute the convert_temp() function to initiate a temperature reading, then wait at # least 750ms before reading the value.


沒有留言:

張貼留言

零成本學 ESP32!在 Wokwi 打造你的第一個微型氣象站 (ESP32 + DHT22 + 1602 LCD)

零成本學 ESP32!在 Wokwi 打造你的第一個微型氣象站(溫度 濕度) (ESP32 + DHT22 + 1602 LCD) 想學物聯網(IoT)與 ESP32 電子實作,卻擔心接線與電壓規範搞砸嗎?透過強大的線上模擬器 Wokwi ,我們不需要花費任何硬體費用,打開瀏覽器...