
In this tutorial we will see how to read the temperature measured by the DTH11 sensor and display it on an SSD1306 display.
Connecting wires
To perform the assembly
For DTH11 sensor, we connect:
the DATA pin to pin D23 of the ESP32 board
the VCC pin to the 3.3V pin of the ESP32 board
the GND pin to the GND pin of the ESP32 board
For SSD1306 display, we connect:
the SDA pin to the D21 pin of the ESP32 board
the SCL pin to pin D22 of the ESP32 board
the VCC pin to the 3.3V pin of the ESP32 board
the GND pin to the GND pin of the ESP32 board
Here is the program which reads the temperature measured by the DTH11 sensor and displays it on SSD1306 display.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
from machine import Pin from machine import Pin,I2C import ssd1306 import dht import time i2c = I2C(scl=Pin(22), sda=Pin(21)) #Init i2c oled=ssd1306.SSD1306_I2C(128,64,i2c,0x3c) p23=Pin(23, Pin.IN) d=dht.DHT11(p23) while True: d.measure() #Measurement of temperature t=d.temperature() #read the temperature print('Temperature=', t, 'C') time.sleep(1) #wait 1s oled.fill(0) oled.text("Temperature",10,10) oled.text(str(t),80,20) #display the temperature on the display oled.show() |
Note: you must import the following libraries: ssd1306