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
------------------------------
You can use the arrows β β on your keyboard to navigate between lessons.
Comments