Devices & Components
Raspberry Pi Pico
Software & Tools
Thonny IDE
Project description
Code
Multithreading for LED controll and Temperature Reading
python
--
1''' 2Multithreading simple example in Raspberry Pi Pico 3Adrianos Botis 4''' 5# Import Packages 6import time, _thread, machine, utime 7from machine import Pin, PWM 8 9# Define Built in Temperature read sensor 10sensor_temp = machine.ADC(4) 11conversion_factor = 3.3 / (65535) 12 13 14# Define the parallel task 15flag = False 16def task(n,delay): 17 global flag 18 # Construct PWM object, with LED on Pin(25). 19 pwm = PWM(Pin(25)) 20 21 # Set the PWM frequency. 22 pwm.freq(1000) 23 24 # Fade the LED in and out a few times. 25 duty = 0 26 direction = 1 27 for _ in range(n): 28 duty += direction 29 if duty > 255: 30 duty = 255 31 direction = -1 32 elif duty < 0: 33 duty = 0 34 direction = 1 35 pwm.duty_u16(duty * duty) 36 time.sleep(delay) 37 flag = True 38 39# Start the LED dimmer thread 40_thread.start_new_thread(task,(4096,0.005)) 41 42 43# Main Loop to Read Temperature 44while not flag: 45 reading = sensor_temp.read_u16() * conversion_factor 46 47 # The temperature sensor measures the Vbe voltage of a biased bipolar diode, connected to the fifth ADC channel 48 # Typically, Vbe = 0.706V at 27 degrees C, with a slope of -1.721mV (0.001721) per degree. 49 temperature = 27 - (reading - 0.706)/0.001721 50 print("Temperature: " + str(temperature) + " C") 51 utime.sleep(0.05)
Comments
Only logged in users can leave comments