ESP32 and DS18B20 sensor and Micropython example

In this example we look at a DS18b20 example in Micropython for an ESP32. Once again we use uPyCraft and again we use Wemos shields.

Lets remind ourselves about the DS18B20

The DS18B20 digital thermometer provides 9-bit to 12-bit Celsius temperature measurements and has an alarm function with nonvolatile user-programmable upper and lower trigger points. The DS18B20 communicates over a 1-Wire bus that by definition requires only one data line (and ground) for communication with a central microprocessor. In addition, the DS18B20 can derive power directly from the data line (“parasite power”), eliminating the need for an external power supply.

Each DS18B20 has a unique 64-bit serial code, which allows multiple DS18B20s to function on the same 1-Wire bus. Thus, it is simple to use one microprocessor to control many DS18B20s distributed over a large area. Applications that can benefit from this feature include HVAC environmental controls, temperature monitoring systems inside buildings, equipment, or machinery, and process monitoring and control systems

Requirements

Lets take a look a the shields and boards that are required for this example

 

Image Summary
MH-ET LIVE ESP32 MINI KIT WiFi+Bluetooth Internet of Things development board based ESP8266 Fully functional D1 MINI Upgraded
This is not an official Wemos shield – this uses a DS18B20 and is connected to D2. This does mean that you can use another I2C device
This is simply a base, you plug the Wemos Mini into one side and you can plug a shield or shields into the other side

 

Parts List

I connect the MH-ET LIVE ESP32 MINI KIT to the dual base and then put the DS18B20 shield along side this.

Name Link
MH-ET LIVE ESP32 MINI KIT MH-ET LIVE ESP32 MINI KIT WiFi+Bluetooth Internet of Things development board based ESP8266 Fully functional D1 MINI Upgraded
Wemos Base Tripler Base V1.0.0 Shield for WeMos D1 Mini
Wemos DS18B20 Temperature Sensor Shield DS18B20 Temperature Sensor Shield Wemos D1 Mini D1 Mini Pro ESP NodeMCU

Code

Create a new file called dsESP32.py and import it into uPyCraft

from machine import Pin
import onewire
import ds18x20
import time
 
ow = onewire.OneWire(Pin(21))   #Init wire
ow.scan()
ds=ds18x20.DS18X20(ow)          #create ds18x20 object
while True:
  roms=ds.scan()                #scan ds18x20
  ds.convert_temp()             #convert temperature
  for rom in roms:
    print(ds.read_temp(rom))    #display 
  time.sleep(1)

Output

You should see something like this

Ready to download this file,please wait!

download ok
exec(open(‘dsESP32.py’).read(),globals())
85.0
27.125
27.125
27.0625
27.0625
27.0625
27.0625
27.0625
27.0625
27.0625
27.0625
27.0
27.0
27.0
26.9375
26.9375
26.9375

Links