keyestudio
Share your creative ideas with us here
By ran | 16 August 2024 | 0 Comments

HOW to Detect Temperature with ESP32

Description

In this kit, there is a DS18B20 temperature sensor, which is from maxim. The MCU can communicate with the DS18B20 through 1-Wire protocol, and finally read the temperature.  In this experiment, we will use this temperature sensor to measure the temperature in the current environment. The test result is , ranging from -55 to +125. We will display the test result on shell.

Working Principle

The hardware interface of the 1-Wire bus is very simple, just connect the data pin of the DS18B20 to an IO port of the microcontroller. The timing of the 1-Wire bus is relatively complex. Many students can’t understand the timing diagram independently here. We have encapsulated the complex timing operations in the library, and you can use the library functions directly.

Schematic Diagram of DS18B20

This can save up to 12-bit temperature vale. In the register, save in code complement. As shown below;

A total of 2 bytes, LSB is the low byte, MSB is the high byte, where MSb is the high byte of the byte, LSb is the low byte of the byte. As you can see, the binary number, the meaning of the temperature represented by each bit, is expressed. Among them, S represents the sign bit, and the lower 11 bits are all powers of 2, which are used to represent the final temperature. The temperature measurement range of DS18B20 is from -55 degrees to +125 degrees, and the expression form of temperature data, S represents positive and negative temperature, and the resolution is 2﹣⒋, which is 0.0625.

Required Components

img

img

img

img

img

ESP32 Board*1

ESP32 Expansion Board*1

Keyestudio DIY 18B20 Temperature Sensor*1

3P Dupont Wire*1

Micro USB Cable*1

Required Components

Add Library

Open“Thonny”, click“This computer”→“D:”→“2. ESP32_code_MicroPython”→“lesson 37. DS18B20”.

Select“ds18x20.py”and“onewire.py”,right-click and select“Upload to”,waiting for the “ds18x20.py” and “onewire.py”to be uploaded to the ESP32.

Test Code

import machine, onewire, ds18x20, time

ds_pin = machine.Pin(15)

ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))

roms = ds_sensor.scan()

print('Found DS devices: ', roms)

while True:

  ds_sensor.convert_temp()

  time.sleep_ms(750)

  for rom in roms:

    #print(rom)

    print(ds_sensor.read_temp(rom))

  time.sleep(1)

Code Explanation

1). We set the pin to GPIO15 and obtain the temperature in the unit of ℃.

2). The Shell window displays the temperature value. Ds_sensor. Read_temp (ROM) indicates the temperature value.

Test Result

Connect the wires according to the experimental wiring diagram and power on. Click “Run current script”, the code starts executing, the shell displays the temperature of the current environment, as shown below. Press “Ctrl+C”or click“Stop/Restart backend”to exit the program.

Leave a Reply

Your email address will not be published.Required fields are marked. *
Name
E-mail
Content
Verification code