114532 Views
101685 Views
86270 Views
54891 Views
51137 Views
49962 Views
Level Up your CAD Skills
Operation Pico
Raspberry Pi Home Hub
Hacky Temperature and Humidity Sensor
Robot Makers Almanac
High Five Bot
Using the Raspberry Pi Pico's Built-in Temperature Sensor
Getting Started with SQL
Introduction to the Linux Command Line on Raspberry Pi OS
How to install MicroPython
Wall Drawing Robot Tutorial
BrachioGraph Tutorial
KevsRobots Learning Platform
64% Percent Complete
By Kevin McAleer, 2 Minutes
In this lesson, we’ll further enhance the readability and presentation of temperature readings by adding labels, visual separators, and conditional formatting to highlight certain temperature ranges.
To make the output visually appealing, you can add a simple separator line:
print("-" * 30)
import machine import time # Initialize the ADC for the temperature sensor sensor_adc = machine.ADC(4) while True: # Read the raw ADC value raw_value = sensor_adc.read_u16() # Convert the ADC value to voltage voltage = raw_value * 3.3 / 65535 # Convert voltage to temperature temperature = 27 - (voltage - 0.706) / 0.001721 # Get the current time timestamp = time.localtime() # Print formatted output print("-" * 30) print(f"Time: {timestamp.tm_hour:02}:{timestamp.tm_min:02}:{timestamp.tm_sec:02}") print(f"Temperature: {temperature:.2f}°C") print("-" * 30) time.sleep(1)
You can highlight specific temperature ranges by adding conditional formatting to the output:
if temperature > 30: status = "High" elif temperature < 20: status = "Low" else: status = "Normal"
while True: # Read the raw ADC value raw_value = sensor_adc.read_u16() # Convert the ADC value to voltage voltage = raw_value * 3.3 / 65535 # Convert voltage to temperature temperature = 27 - (voltage - 0.706) / 0.001721 # Determine the status if temperature > 30: status = "High" elif temperature < 20: status = "Low" else: status = "Normal" # Get the current time timestamp = time.localtime() # Print formatted output with status print("-" * 30) print(f"Time: {timestamp.tm_hour:02}:{timestamp.tm_min:02}:{timestamp.tm_sec:02}") print(f"Temperature: {temperature:.2f}°C - Status: {status}") print("-" * 30) time.sleep(1)
enhanced_temp_readings.py
------------------------------ Time: 15:34:21 Temperature: 28.45°C - Status: Normal ------------------------------ Time: 15:34:22 Temperature: 32.10°C - Status: High ------------------------------
< Previous Next >