# https://wokwi.com/projects/474155768301636609
from machine import Pin, PWM, ADC
import dht
import time
import math
# -----------------------------
# Pins
# -----------------------------
DHT_PIN = 40
SERVO_PIN = 45
THERMISTOR_PIN = 4
BUTTON_PIN = 5
# -----------------------------
# Humidity range
# -----------------------------
MIN_HUMIDITY = 0
MAX_HUMIDITY = 100
# -----------------------------
# Thermistor beta coefficient
# -----------------------------
BETA = 3950
# -----------------------------
# Program running flag
# -----------------------------
running = True
# -----------------------------
# Component setup
# -----------------------------
# DHT22 sensor
sensor = dht.DHT22(
Pin(DHT_PIN)
)
# Servo
servo = PWM(
Pin(SERVO_PIN),
freq=50
)
# Thermistor analog input
thermistor = ADC(
Pin(THERMISTOR_PIN)
)
thermistor.atten(ADC.ATTN_11DB)
# Stop button
# GPIO5 is normally HIGH because of the internal pull-up.
# Pressing the button connects GPIO5 to GND.
button = Pin(
BUTTON_PIN,
Pin.IN,
Pin.PULL_UP
)
# -----------------------------
# Interrupt function
# -----------------------------
def stop_program(pin):
global running
# Interrupt changes the running flag
running = False
# Trigger interrupt when button is pressed
button.irq(
trigger=Pin.IRQ_FALLING,
handler=stop_program
)
# -----------------------------
# Servo function
# -----------------------------
def servo_angle(angle):
# Servo pulse range:
# 500–2500 microseconds
min_us = 500
max_us = 2500
pulse_us = (
min_us
+ (angle / 180)
* (max_us - min_us)
)
# Convert pulse width into
# a 16-bit PWM duty value
duty = int(
(pulse_us / 20000)
* 65535
)
servo.duty_u16(duty)
# -----------------------------
# Humidity to servo angle
# -----------------------------
def humidity_to_angle(humidity):
# Keep humidity between
# 0% and 100%
if humidity < MIN_HUMIDITY:
humidity = MIN_HUMIDITY
if humidity > MAX_HUMIDITY:
humidity = MAX_HUMIDITY
# Convert 0–100% humidity
# into 0–180 degrees
angle = (
(humidity - MIN_HUMIDITY)
* 180
/ (MAX_HUMIDITY - MIN_HUMIDITY)
)
return angle
# -----------------------------
# Thermistor function
# -----------------------------
def read_thermistor():
# Read analog thermistor value
analog_value = thermistor.read_u16()
# Prevent division by zero
if (
analog_value <= 0
or analog_value >= 65535
):
return None
# Convert analog reading
# into degrees Celsius
temperature = (
1
/ (
math.log(
1 / (
65535 / analog_value - 1
)
) / BETA
+ 1 / 298.15
)
- 273.15
)
return temperature
# -----------------------------
# Main program
# -----------------------------
while running:
# Read the DHT22
sensor.measure()
dht_temperature = sensor.temperature()
humidity = sensor.humidity()
# Read the thermistor
thermistor_temperature = read_thermistor()
# Calculate servo position
# from humidity
angle = humidity_to_angle(humidity)
# Move the servo
servo_angle(angle)
# Display DHT readings
print(
"DHT Temperature:",
dht_temperature,
"C"
)
print(
"Humidity:",
humidity,
"%"
)
# Display thermistor reading
if thermistor_temperature is not None:
print(
"Thermistor Temperature:",
round(
thermistor_temperature,
1
),
"C"
)
else:
print(
"Thermistor reading error"
)
# Display servo position
print(
"Servo Angle:",
round(angle),
"degrees"
)
print()
time.sleep(2)
# -----------------------------
# Program stopped
# -----------------------------
servo.deinit()
print(
"Program stopped by button interrupt."
)Loading
esp32-s3-devkitc-1
esp32-s3-devkitc-1