from machine import Pin, PWM
import time
# Pin tanımları
dout = Pin(14, Pin.IN) # HX711 DT
sck = Pin(15, Pin.OUT) # HX711 SCK
dht_pin = Pin(27, Pin.IN) # DHT22 DATA pin
servo = PWM(Pin(26)) # Servo motor
servo.freq(50) # 50Hz PWM frekansı
# DHT22 manuel okuma fonksiyonu
def read_dht22():
try:
# DHT22 başlatma sinyali
dht_pin.init(Pin.OUT)
dht_pin.value(0)
time.sleep_ms(20)
dht_pin.value(1)
time.sleep_us(30)
dht_pin.init(Pin.IN)
# Başlangıç sinyalini bekle
timeout = 0
while dht_pin.value() == 0:
timeout += 1
if timeout > 1000:
return None
while dht_pin.value() == 1:
timeout += 1
if timeout > 2000:
return None
# 40 bit veri oku
data = []
for i in range(40):
while dht_pin.value() == 0:
pass
start_time = time.ticks_us()
while dht_pin.value() == 1:
pass
duration = time.ticks_diff(time.ticks_us(), start_time)
data.append(1 if duration > 40 else 0)
# Veriyi byte'lara çevir
humidity = 0
temperature = 0
for i in range(16):
humidity = (humidity << 1) + data[i]
temperature = (temperature << 1) + data[i + 16]
return temperature / 10.0
except:
return None
def read_weight():
sck.value(0)
while dout.value() == 1:
pass
count = 0
for i in range(24):
sck.value(1)
count = count << 1
sck.value(0)
if dout.value():
count += 1
sck.value(1)
sck.value(0)
if count & 0x800000:
count = count - 0x1000000
return count / 420
def move_servo(angle):
# Açıyı PWM duty cycle'a çevir
duty = int(1000 + (angle / 180) * 8000)
servo.duty_u16(duty)
# Ana döngü
while True:
weight = read_weight()
temp = read_dht22()
print(f"Ağırlık: {weight:.2f} kg")
if temp:
print(f"Sıcaklık: {temp:.1f}°C")
else:
print("Sıcaklık: Hata")
# Servo kontrolü
if weight > 5.0:
move_servo(90)
print("Servo: 90° (Ağır)")
elif temp and temp > 30:
move_servo(45)
print("Servo: 45° (Sıcak)")
else:
move_servo(0)
print("Servo: 0° (Normal)")
print("-" * 20)
time.sleep(3)