SunFounder ESP32 Starter Kit
(continued from previous page)
# Measure temperature and humidity
dht11.measure()
# Get temperature and humidity values
temp = dht11.temperature()
humi = dht11.humidity()
except Exception as e:
print("Error: ", e)
# Display temperature and humidity
lcd.write(0, 0, "Temp: {}\xDFC".format(temp))
lcd.write(0, 1, "Humi: {}%".format(humi))
# If page is 0, display the soil moisture and light
else:
light = photoresistor.read()
moisture = moisture_pin.read()
# Clear the LCD display
lcd.clear()
# Display the value of soil moisture and light
lcd.write(0, 0, f"Moisture: {moisture}")
lcd.write(0, 1, f"Light: {light}")
except KeyboardInterrupt:
# Stop the motor when KeyboardInterrupt is caught
stop()
• When the code is running, the I2C LCD1602 alternately displays temperature and humidity, as well as soil
moisture and light intensity analog values, with a 2-second interval.
• Press the button to start the water pump, and press it again to stop the water pump.
Note: If the code and wiring are correct, but the LCD still fails to display any content, you can adjust the potentiometer
on the back to increase the contrast.
444 Chapter 3. For MicroPython User