ds18x20 on ESP32

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

ds18x20 on ESP32

Post by devnull » Mon May 07, 2018 11:12 am

Having used these devices for many years on on microchip devices and seeing that there are ready-built libraries for onewire and ds18x20 I thought that reading one would be a breeze.

I am using normal power (not sacrificial) and using 3V3 and have tried GPIO.0 and GPIO.4, the ESP32 appears to be getting a recognisable return but is unable to identify the 18S20.

Before I start hooking up my scope and analyse the signals, has anyone else RECENTLY succeeded in reading the 18S20 on the ESP32 device using the built in libraries ??

Code: Select all

import time
import machine
import onewire, ds18x20

dat = machine.Pin(4)

# 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:
        print(ds.read_temp(rom), end=' ')
    print()

Code: Select all

>>> from test import ds18x20
found devices: []
temperatures: 
temperatures: 
temperatures: 
temperatures: 
temperatures: 
temperatures: 
temperatures: 
temperatures: 
temperatures: 
temperatures: 

User avatar
rdagger
Posts: 143
Joined: Tue Feb 28, 2017 6:16 pm
Contact:

Re: ds18x20 on ESP32

Post by rdagger » Thu May 10, 2018 3:47 pm

I've been using a DS18b20 with the LoBo build and it works great.

Code: Select all

from machine import Onewire
ow = Onewire(23)
ds = Onewire.ds18x20(ow, 0)
temp = ds.convert_read()  # Poll temperature sensor
print(temp)
ow.deinit()
Make sure you use a 10K pull up resistor between 3.3V and the data line.

ajocius
Posts: 83
Joined: Mon Feb 19, 2018 6:31 am

Re: ds18x20 on ESP32

Post by ajocius » Wed May 30, 2018 12:23 pm

I use following code to read ds18x20 sensor data with original build:

Code: Select all

from time import sleep
from machine import Pin, ADC
import machine
import micropython
import onewire, ds18x20
import network, utime

t1 = machine.Pin(17)   # DS18B20 temp sensor device is on GPIO17
ds = ds18x20.DS18X20(onewire.OneWire(t1)) # create the onewire object

# Reading Temperature sensor data
    elif msg == b"temp":
        roms = ds.scan()
        print('found devices:', roms)
        addr = roms.pop()
        ds.convert_temp()
        utime.sleep_ms(750)
        temp = ds.read_temp(addr)
        print(temp)
        utime.sleep_ms(3000)
        if isinstance(temp, float) :  # Confirm sensor results are numeric
            msg = (b'{0:3.1f}'.format(temp))
            client.publish(TOPIC2, msg)  # Publish sensor data to MQTT topic
            print("Published", msg)
        else:
            print("Invalid sensor readings.")

Post Reply