# Micropython code for ESP32 with dual fans, LED, potentiometer, and DHT22 sensor
import machine
import time
from machine import Pin, PWM, ADC
import dht
# Define pin assignments
mosfet1_gate_pin = 22 # Connect to the gate of MOSFET controlling Fan 1
mosfet2_gate_pin = 23 # Connect to the gate of MOSFET controlling Fan 2
led_pin = 2 # Connect to the NPN transistor controlling the LED
potentiometer_pin = 34 # Connect to the potentiometer's wiper
dht22_pin = 4 # Connect to the data pin of the DHT22 sensor
# Initialize components
mosfet1_gate = Pin(mosfet1_gate_pin, Pin.OUT)
mosfet2_gate = Pin(mosfet2_gate_pin, Pin.OUT)
led_pwm = PWM(Pin(led_pin), freq=1000, duty=0)
potentiometer = ADC(Pin(potentiometer_pin))
dht22 = dht.DHT22(Pin(dht22_pin))
# Function to read potentiometer value
def read_potentiometer():
return potentiometer.read()
# Function to control fan speed based on potentiometer value
def control_fan_speed(pot_value, mosfet_gate):
duty_cycle = int(pot_value * 1023 / 4095)
mosfet_gate.duty(duty_cycle)
# Function to control LED brightness based on potentiometer value
def control_led_brightness(pot_value):
duty_cycle = int(pot_value * 1023 / 4095)
led_pwm.duty(duty_cycle)
# Main loop
while True:
try:
# Read potentiometer value
pot_value = read_potentiometer()
# Control fan speeds
control_fan_speed(pot_value, mosfet1_gate)
control_fan_speed(pot_value, mosfet2_gate)
# Control LED brightness
control_led_brightness(pot_value)
# Read temperature and humidity from DHT22
dht22.measure()
temperature = dht22.temperature()
humidity = dht22.humidity()
# Print sensor readings
print("Potentiometer Value:", pot_value)
print("Fan 1 Speed:", mosfet1_gate.duty())
print("Fan 2 Speed:", mosfet2_gate.duty())
print("LED Brightness:", led_pwm.duty())
print("Temperature:", temperature)
print("Humidity:", humidity)
# Delay before the next iteration
time.sleep(2)
except Exception as e:
print("Error:", e)
time.sleep(2)