print("TEMPERATURE SENSOR PROJECT")
print("date:4/12/2023")
print("By:MASBA")
print("ID: 51225223082")
print("Course:BEET (SE)")
import machine
import onewire
import ds18x20
import time
from machine import Pin, SoftI2C
import SSD1306
# Define the pins for the temperature sensor, LEDs, display, and buzzer
sensor_pin = machine.Pin(5) # Temperature sensor pin
red_led_pin = machine.Pin(2, machine.Pin.OUT)
blue_led_pin = machine.Pin(14, machine.Pin.OUT)
green_led_pin = machine.Pin(15, machine.Pin.OUT)
buzzer_pin = machine.Pin(12, machine.Pin.OUT)
#Pin declaration
i2c_oled= SoftI2C(scl=Pin(22), sda= Pin(21))
oled_width =128
oled_height =64
#Lets create OLED as an OBJECT
oled= SSD1306.SSD1306_I2C(width=oled_width, height= oled_height,i2c= i2c_oled)
#Initialize the buzzer
buzzer = machine.Pin(buzzer_pin, machine.Pin.OUT)
#the onewire object
ow = onewire.OneWire(sensor_pin)
#DS18B20 object
temp_sensor = ds18x20.DS18X20(ow)
#DS18B20 sensors
devices = temp_sensor.scan()
# Define a function to generate a sound using the buzzer
def buzz(pitch, duration):
period = 1.0 / pitch
delay = period / 2
cycles = int(duration * pitch)
for i in range(cycles):
buzzer.on()
time.sleep(delay)
buzzer.off()
time.sleep(delay)
while True:
# Get temperature readings
temp_sensor.convert_temp()
time.sleep_ms(750)
for device in devices:
temp = temp_sensor.read_temp(device)
print("Temperature:", temp, "C")
# Control LEDs based on temperature
if temp > 80:
red_led_pin.on()
blue_led_pin.off()
green_led_pin.off()
# Activate buzzer and display warning
buzz(500, 1)
oled.fill(1)
oled.text(" WARNING!", 1,10,0)
oled.text(" HIGH TEMP", 1,20,0)
oled.show()
elif temp < 20:
red_led_pin.off()
blue_led_pin.off()
green_led_pin.on()
# Activate buzzer and display low temperature
buzz(300, 0.5)
oled.fill(1)
oled.text(" ATTENTION!", 1,10,0)
oled.text(" LOW TEMP", 1,20,0)
oled.show()
elif 21 < temp < 79:
red_led_pin.off()
blue_led_pin.on()
green_led_pin.off()
# Deactivate buzzer and clear the display
buzzer.off()
oled.fill(1)
oled.text(" Normal TEMP", 1,10,0)
oled.show()
time.sleep(5)