Temperature Sensor (DS18B20)

../../../../_images/ds18b20.png

Overview

Based on the DS18B20 digital temperature sensor, used for detecting environmental temperature.

Technical Specifications

Operating voltage: VCC 3.3V -5V

Module dimensions: 24*48mm

Mounting: 2 plastic building block compatible through-holes

Temperature: -55–125℃

Pin Definition

Pin

Definition

GND

Ground

VCC

Power

DI

Digital output

NC

Not connected

Example Code

Print temperature information every second

from mpython import *

from machine import Timer

import onewire

import ds18x20

_ds18x20_list = []

tim13 = Timer(13)

def timer13_tick(_):
    for p in _ds18x20_list:
        try:
            eval("ds_" + p + ".convert_temp()")
            for rom in eval("roms_" + p):
                t = eval("ds_" + p).read_temp(rom)
                eval("set_ds_value_" + p + "(" + str(t) + ")")
                break
        except: pass

tim13.init(period=1000, mode=Timer.PERIODIC, callback=timer13_tick)

import time

def set_ds_value_0(_t):
    global _ds18x20_value_0
    _ds18x20_value_0 = _t

ow_0 = onewire.OneWire(Pin(Pin.P0))
ds_0 = ds18x20.DS18X20(ow_0)
roms_0 = ds_0.scan()
_ds18x20_value_0 = 0
_ds18x20_list.append(str(0))
while True:
    print(_ds18x20_value_0)
    time.sleep(1)
../../../../_images/ds18b201.png