'''
Multithreading simple example in Raspberry Pi Pico
Adrianos Botis
'''
# Import Packages
import time, _thread, machine, utime
from machine import Pin, PWM
# Define Built in Temperature read sensor
sensor_temp = machine.ADC(4)
conversion_factor = 3.3 / (65535)
# Define the parallel task
flag = False
def task(n,delay):
global flag
# Construct PWM object, with LED on Pin(25).
pwm = PWM(Pin(25))
# Set the PWM frequency.
pwm.freq(1000)
# Fade the LED in and out a few times.
duty = 0
direction = 1
for _ in range(n):
duty += direction
if duty > 255:
duty = 255
direction = -1
elif duty < 0:
duty = 0
direction = 1
pwm.duty_u16(duty * duty)
time.sleep(delay)
flag = True
# Start the LED dimmer thread
_thread.start_new_thread(task,(4096,0.005))
# Main Loop to Read Temperature
while not flag:
reading = sensor_temp.read_u16() * conversion_factor
# The temperature sensor measures the Vbe voltage of a biased bipolar diode, connected to the fifth ADC channel
# Typically, Vbe = 0.706V at 27 degrees C, with a slope of -1.721mV (0.001721) per degree.
temperature = 27 - (reading - 0.706)/0.001721
print("Temperature: " + str(temperature) + " C")
utime.sleep(0.05)