-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpe_temp.py
59 lines (45 loc) · 1.93 KB
/
pe_temp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# This example takes the temperature from the Pico's onboard temperature sensor, and displays it on Pico Explorer, 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!
import machine
import utime
# Pico Explorer boilerplate
import picoexplorer as display
width = display.get_width()
height = display.get_height()
display_buffer = bytearray(width * height * 2)
display.init(display_buffer)
# reads from Pico's temp sensor and converts it into a more manageable number
sensor_temp = machine.ADC(4)
conversion_factor = 3.3 / (65535)
i = 0
while True:
# the following two lines do some maths to convert the number from the temp sensor into fahrenheit
reading = sensor_temp.read_u16() * conversion_factor
temperature = round(((27 - (reading - 0.706) / 0.001721) * 9/5) + 32)
# test temp for display tuning
# temperature = 100
# this if statement clears the display once the graph reaches the right hand side of the display
if i >= (width + 1):
i = 0
display.set_pen(0, 0, 0)
display.clear()
# chooses a pen colour based on the temperature
display.set_pen(0, 255, 0)
if temperature > 68:
display.set_pen(255, 0, 0)
if temperature < 55:
display.set_pen(0, 0, 255)
# draws the reading as a tall, thin rectangle
display.rectangle(i, height - (temperature * 2), 6, height)
# draws a white background for the text
display.set_pen(255, 255, 255)
display.rectangle(1, 1, 65, 33)
# writes the reading as text in the white rectangle
display.set_pen(0, 0, 0)
display.text("{:.0f}".format(temperature) + "f", 3, 3, 0, 4)
# time to update the display
display.update()
# waits for 5 seconds
utime.sleep(5)
# the next tall thin rectangle needs to be drawn 6 pixels to the right of the last one
i += 6