0% found this document useful (0 votes)
4 views3 pages

She Matic

Uploaded by

tdat1406
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

She Matic

Uploaded by

tdat1406
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Bài 1

import machine
from machine import Pin, SoftI2C, Timer
from lcd_api import LcdApi
from esp32_i2c_lcd import I2cLcd
from time import sleep
import dht

# I2C and LCD setup


I2C_ADDR = 0x27
totalRows = 2
totalColumns = 16
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=100000)
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)

# DHT22 sensor setup


sensor = dht.DHT22(Pin(15))

# LED setup
red = Pin(2, machine.Pin.OUT)
green = Pin(4, machine.Pin.OUT)
blue = Pin(5, machine.Pin.OUT)

# Hàm đọc dữ liệu từ cảm biế n và kiểm tra nhiệt độ


def read_sensor(timer):
try:
blue.on() # Bật LED xanh khi đọc dữ liệu
sensor.measure() # Đo nhiệt độ và độ ẩ m
temp = sensor.temperature()
hum = sensor.humidity()
blue.off() # Tắ t LED xanh sau khi đọc xong

print(f'Temperature: {temp:.1f} C')


print(f'Humidity: {hum:.1f} %')

# Điề u khiển LED đỏ dựa trên ngưỡng nhiệt độ


if temp > 30:
red.on()
print('Cảnh báo: Nhiệt độ quá cao! LED đỏ bật.')
else:
red.off()

# Hiển thị nhiệt độ và độ ẩm lên màn hình LCD


lcd.clear()
lcd.putstr(f'Temp: {temp:.1f} C\nHumidity: {hum:.1f} %')

except OSError as e:
print('Failed to read sensor.')

# Sửdụng Timer đểđọc cảm biế n định kỳ mỗi 5 giây


sensor_timer = Timer(0)
sensor_timer.init(period=5000, mode=Timer.PERIODIC,
callback=read_sensor)

# Chương trình sẽ tiếp tục chạy mà không cần vòng lặp chính do Timer
đang chạy
while True:
sleep(1) # Giữ chương trình chạy đểTimer tiế p tục hoạt động

Bài 2

from machine import Pin


import time

# Define the LED pins and the button


led_pins = [
Pin(19, Pin.OUT), # LED 1 - GPIO 19
Pin(18, Pin.OUT), # LED 2 - GPIO 18
Pin(5, Pin.OUT), # LED 3 - GPIO 5
Pin(17, Pin.OUT), # LED 4 - GPIO 17
Pin(16, Pin.OUT), # LED 5 - GPIO 16
Pin(4, Pin.OUT), # LED 6 - GPIO 4
Pin(2, Pin.OUT), # LED 7 - GPIO 0
Pin(15, Pin.OUT) # LED 8 - GPIO 2
]

button = Pin(14, Pin.IN, Pin.PULL_DOWN) # Using GPIO 12 for the button


with pull-down

# Control variables for mode and LED state


mode = 0 # Default mode when starting is mode 0 (simultaneous
blinking)
led_state = 0

# Function to change mode when the button is pressed


def change_mode(pin):
global mode
# Debouncing
time.sleep(0.1) # Wait 100 ms to ignore bounce
if button.value() == 1: # Check if the button is still pressed
mode = (mode + 1) % 3 # There are 3 modes: 0, 1, 2
print("Current mode:", mode)

# Assign the change_mode function to the interrupt when the button is


pressed
button.irq(trigger=Pin.IRQ_RISING, handler=change_mode) # Detect when
the button is pressed

# Main loop
while True:
if mode == 0: # Simultaneous blinking
led_state = not led_state
for led in led_pins:
led.value(led_state)
time.sleep(0.5) # Change the blinking time of the LED here

elif mode == 1: # Sequential blinking


for i in range(len(led_pins)):
led_pins[i].value(1)
time.sleep(0.1) # Time for the LED to be on
led_pins[i].value(0)
time.sleep(0.5) # Time between each blink

elif mode == 2: # Alternating blinking


# Blink even LEDs
for i in range(len(led_pins)):
if i % 2 == 0:
led_pins[i].value(1)
else:
led_pins[i].value(0)
time.sleep(0.5) # Time to keep even LEDs on

# Blink odd LEDs


for i in range(len(led_pins)):
if i % 2 == 1:
led_pins[i].value(1)
else:
led_pins[i].value(0)
time.sleep(0.5) # Time to keep odd LEDs on

You might also like