2021-02-14 14:42:51 +00:00
# This example takes the temperature from the Pico's onboard temperature sensor, and displays it on Pico Display Pack, along with a little pixelly graph.
# It's based on the thermometer example in the "Getting Started with MicroPython on the Raspberry Pi Pico" book, which is a great read if you're a beginner!
2021-04-15 09:21:27 +01:00
import machine
2022-07-07 09:37:47 +01:00
import time
2022-05-12 12:45:25 +01:00
from pimoroni import RGBLED
2022-07-07 09:37:47 +01:00
from picographics import PicoGraphics , DISPLAY_PICO_DISPLAY
2021-06-17 15:33:05 +01:00
2022-07-07 09:37:47 +01:00
# set up the hardware
display = PicoGraphics ( display = DISPLAY_PICO_DISPLAY , rotate = 0 )
sensor_temp = machine . ADC ( 4 )
led = RGBLED ( 6 , 7 , 8 )
2022-05-12 12:45:25 +01:00
2022-07-07 09:37:47 +01:00
# set the display backlight to 50%
2022-05-12 12:45:25 +01:00
display . set_backlight ( 0.5 )
2022-07-07 09:37:47 +01:00
# set up constants for drawing
2022-05-28 01:19:58 +01:00
WIDTH , HEIGHT = display . get_bounds ( )
BLACK = display . create_pen ( 0 , 0 , 0 )
WHITE = display . create_pen ( 255 , 255 , 255 )
2022-07-07 09:37:47 +01:00
conversion_factor = 3.3 / ( 65535 ) # used for calculating a temperature from the raw sensor reading
2021-02-14 14:42:51 +00:00
2021-03-29 16:46:13 +01:00
temp_min = 10
temp_max = 30
bar_width = 5
2021-02-14 14:42:51 +00:00
2021-03-08 17:16:10 +00:00
temperatures = [ ]
2021-02-14 14:42:51 +00:00
2021-03-29 16:46:13 +01:00
colors = [ ( 0 , 0 , 255 ) , ( 0 , 255 , 0 ) , ( 255 , 255 , 0 ) , ( 255 , 0 , 0 ) ]
def temperature_to_color ( temp ) :
temp = min ( temp , temp_max )
temp = max ( temp , temp_min )
f_index = float ( temp - temp_min ) / float ( temp_max - temp_min )
f_index * = len ( colors ) - 1
index = int ( f_index )
if index == len ( colors ) - 1 :
return colors [ index ]
blend_b = f_index - index
blend_a = 1.0 - blend_b
a = colors [ index ]
b = colors [ index + 1 ]
return [ int ( ( a [ i ] * blend_a ) + ( b [ i ] * blend_b ) ) for i in range ( 3 ) ]
2021-02-14 14:42:51 +00:00
while True :
2021-03-08 17:16:10 +00:00
# fills the screen with black
2022-05-28 01:19:58 +01:00
display . set_pen ( BLACK )
2021-03-08 17:16:10 +00:00
display . clear ( )
2021-02-14 14:42:51 +00:00
# the following two lines do some maths to convert the number from the temp sensor into celsius
reading = sensor_temp . read_u16 ( ) * conversion_factor
2021-03-29 16:46:13 +01:00
temperature = 27 - ( reading - 0.706 ) / 0.001721
2021-04-15 09:21:27 +01:00
2021-03-29 16:46:13 +01:00
temperatures . append ( temperature )
2021-03-08 17:16:10 +00:00
# shifts the temperatures history to the left by one sample
2022-05-12 12:45:25 +01:00
if len ( temperatures ) > WIDTH / / bar_width :
2021-03-29 16:46:13 +01:00
temperatures . pop ( 0 )
2021-02-14 14:42:51 +00:00
2021-03-08 17:16:10 +00:00
i = 0
2022-07-07 09:37:47 +01:00
2021-03-08 17:16:10 +00:00
for t in temperatures :
2022-07-07 09:37:47 +01:00
# chooses a pen colour based on the temperature
TEMPERATURE_COLOUR = display . create_pen ( * temperature_to_color ( t ) )
display . set_pen ( TEMPERATURE_COLOUR )
2021-04-15 09:21:27 +01:00
2021-03-08 17:16:10 +00:00
# draws the reading as a tall, thin rectangle
2022-05-12 12:45:25 +01:00
display . rectangle ( i , HEIGHT - ( round ( t ) * 4 ) , bar_width , HEIGHT )
2021-03-08 17:16:10 +00:00
2021-03-29 16:46:13 +01:00
# the next tall thin rectangle needs to be drawn
# "bar_width" (default: 5) pixels to the right of the last one
i + = bar_width
2021-04-15 09:21:27 +01:00
2021-02-14 14:42:51 +00:00
# heck lets also set the LED to match
2022-05-12 12:45:25 +01:00
led . set_rgb ( * temperature_to_color ( temperature ) )
2021-03-08 17:16:10 +00:00
2021-02-14 14:42:51 +00:00
# draws a white background for the text
2022-05-28 01:19:58 +01:00
display . set_pen ( WHITE )
2021-03-29 16:46:13 +01:00
display . rectangle ( 1 , 1 , 100 , 25 )
2021-04-15 09:21:27 +01:00
2021-02-14 14:42:51 +00:00
# writes the reading as text in the white rectangle
2022-05-28 01:19:58 +01:00
display . set_pen ( BLACK )
2021-03-29 16:46:13 +01:00
display . text ( " {:.2f} " . format ( temperature ) + " c " , 3 , 3 , 0 , 3 )
2021-04-15 09:21:27 +01:00
2021-02-14 14:42:51 +00:00
# time to update the display
display . update ( )
2021-04-15 09:21:27 +01:00
2021-02-14 14:42:51 +00:00
# waits for 5 seconds
2022-07-07 09:37:47 +01:00
time . sleep ( 5 )