Created
September 12, 2021 23:01
-
-
Save splch/ad9e36f23919d678f809da73ab20092f to your computer and use it in GitHub Desktop.
Blink the Raspberry Pi Pico faster with heat.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def temperature_blink(temp_sensor, fahrenheit=True): | |
# get 16 bit unsigned integer | |
reading = temp_sensor.read_u16() | |
# conversion factor from 3.3V battery | |
to_volts = 3.3 / 65535 | |
# scale temperature | |
ADC_voltage = reading * to_volts | |
# approximate temperature (C) | |
temperature = 27 - (ADC_voltage - 0.706) / 0.001721 | |
# optional temperature conversion (F) | |
if fahrenheit: | |
temperature = temperature * 9 / 5 + 32 | |
# print('{} V to {} {}'.format(ADC_voltage, temperature, 'F' if fahrenheit else 'C')) | |
return temperature, ADC_voltage | |
def scale(voltage, v_min=0.693953, v_max=0.711163, w_min=0.011111, w_max=0.5): | |
numerator = (w_max - w_min) * (voltage - v_min) | |
denominator = v_max - v_min | |
# linearly scale voltage to time | |
return numerator / denominator + w_min | |
def main(): | |
# import pico functions | |
from machine import ADC, Pin | |
# import sleep function | |
from utime import sleep | |
# set led pin | |
led = Pin(25, Pin.OUT) | |
# temperature sensor GPIO pin | |
temp_sensor = ADC(4) | |
# temperature scale | |
fahrenheit=True | |
while True: | |
temperature, voltage = temperature_blink(temp_sensor, fahrenheit) | |
# determine how long to sleep from voltage | |
time = scale(voltage) | |
print('{} {}\t{} V\t{} s'.format(temperature, 'F' if fahrenheit else 'C', voltage, time)) | |
sleep(time) | |
# toggle light | |
led.toggle() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment