from machine import Pin, I2C, ADC, PWM
import ssd1306
import time
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
NTC_pin = Pin(32)
NTC = ADC(NTC_pin)
relay = Pin(25, Pin.OUT)
motor_pwm = PWM(relay)
motor_pwm.freq(1000)
pot = ADC(Pin(33))
button_marche = Pin(34, Pin.IN)
button_stope = Pin(35, Pin.IN)
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
motor_running = False
def temperature(ntc_value):
return 80 - ((ntc_value * 1) / 65535) * 104
while True:
reading_marche = button_marche.value()
reading_stope = button_stope.value()
ntc_value = NTC.read_u16()
temperature_en_C = temperature(ntc_value)
pot_value = pot.read_u16()
motor_speed = int((pot_value / 65535) * 1023)
motor_speed_percent = int((pot_value / 65535) * 100)
if reading_marche == 1:
motor_running = True
if reading_stope == 1:
motor_running = False
if temperature_en_C >= 60:
motor_running = False
motor_pwm.duty(0)
oled.fill(0)
oled.text('Temp: {:.2f} C'.format(temperature_en_C), 0, 0)
oled.text('Moteur: {}'.format('Marche' if motor_running else 'Arrete'), 0, 10)
oled.text('Vitesse: {}%'.format(motor_speed_percent), 0, 20)
if motor_running and temperature_en_C < 60:
motor_pwm.duty(motor_speed)
else:
motor_pwm.duty(0)
oled.show()
time.sleep(1) # Afficher les données toutes les secondes