# Import modules
from machine import I2C, Pin, ADC
from time import sleep
import utime
from pico_i2c_lcd import I2cLcd
from dht import DHT22
# Initialize I2C for LCD
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
# Set I2C address
I2C_ADDR = i2c.scan()[0]
# Create objects for DHT22, LCD, and ADC for LDR
dht = DHT22(Pin(5))
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
ldr = ADC(Pin(28))
# Stepper Motor and LED Pins
DIR_PIN = Pin(2, Pin.OUT)
STEP_PIN = Pin(3, Pin.OUT)
LED_PIN = Pin(27, Pin.OUT)
# Threshold values for temperature, humidity, and light level
TEMP_THRESHOLD = 40 # Temperature threshold (40°C)
HUM_THRESHOLD = 50 # Humidity threshold (50%)
LDR_THRESHOLD = 5000 # LDR threshold (50,000)
# Function to run the stepper motor and turn off the LED
def run_stepper_motor():
DIR_PIN.value(1) # Set direction
LED_PIN.value(0) # Turn off LED when motor is running
STEP_PIN.value(1)
sleep(0.005)
STEP_PIN.value(0)
sleep(0.005)
# Function to stop the stepper motor and turn on the LED
def stop_stepper_motor():
STEP_PIN.value(0) # Ensure motor is off
LED_PIN.value(1) # Turn on LED when motor is stopped
# Main loop
while True:
# Read temperature and humidity from DHT22
dht.measure()
temp = dht.temperature()
hum = dht.humidity()
# Read the LDR value
ldr_value = ldr.read_u16() # Reads a value between 0 and 65535
# Display on LCD
lcd.clear()
lcd.putstr("The remote contol car is started")
lcd.move_to(0, 1)
# Check conditions for motor activation
if temp > TEMP_THRESHOLD or ldr_value < LDR_THRESHOLD:
print("Starting the remote control car")
run_stepper_motor()
else:
print("Stopping the remote control car")
stop_stepper_motor()