Let’s See Your AI Creations! Submit your project now Here! and get $5 off your next purchase! 🔥
[Holiday Announcement] We will be closed for the Raya holiday from March 29 to April 6. Orders placed after March 28 at 12 PM (GMT +8) will be processed starting April 7. Thank you for your understanding.
How to Create Temperature Gauge Using Pi Pico

How to Create Temperature Gauge Using Pi Pico

Join us in this tutorial as we create a temperature gauge using Raspberry Pi Pico and a servo motor! We'll cut a cardboard paper into a half circle, label it with temperature ranges, and attach a servo horn for the temperature indicator. With coding, we'll make the servo move the dial, displaying different temperatures interactively.

 

Video Tutorial

 

Components Required

Hardware Components

SG90 Micro Servo

S$2.90 S$2.90

x 1 unit(s)

SG90 Micro Servo

Not Available

x 1 unit(s)

 

Connection

Connect servo motor to port GP12.

 

Get Familiar with Thonny and CircuitPython

In this tutorial, we'll use Thonny editor & CircuitPython to build the code for this project. If you're not familiar with Thonny, check out the video below to learn how to get started with it.

 

Code

Visit the official CircuitPython website and download the latest library bundle. Make sure to select the correct bundle for your version of CircuitPython.
 

Unzip the file, and locate this library:

- adafruit_motor

Copy and paste it into "lib" folder on your CIRCUITPY drive.

import time
import board
import pwmio
from adafruit_motor import servo
import microcontroller
# Create a PWMOut object on Pin GP12
pwm = pwmio.PWMOut(board.GP12, duty_cycle=2 ** 15, frequency=50)
# Create a servo object, my_servo
my_servo = servo.Servo(pwm)
# Function to map temperature range to servo angle range
def map_temperature_to_angle(temperature, min_temp, max_temp, min_angle, max_angle):
temperature = max(min(temperature, max_temp), min_temp) # Limit temperature within the given range
temperature_range = max_temp - min_temp
angle_range = max_angle - min_angle
angle = ((temperature - min_temp) / temperature_range) * angle_range + min_angle
return int(angle)
while True:
# Read the onboard temperature in Celsius
temperature = microcontroller.cpu.temperature
# Map temperature to servo angle
angle = map_temperature_to_angle(temperature, 18, 35, 180, 0)
# Set the servo angle
my_servo.angle = angle
# Print temperature and servo angle
print("Temperature: {:.2f}°C, Servo Angle: {}".format(temperature, angle))
time.sleep(1) # Pause for 1 second
view raw code.py hosted with ❤ by GitHub

 

Project complete

Take a piece of cardboard and cut it into a shape that looks like a half circle. You can use a compass to draw the curved line. Remember to write down the numbers on the cardboard to show the temperature range you want.

Next, make a small hole in the cardboard. This hole will be used to attach the servo horn, which will act as the temperature indicator. By moving the servo, we can show different temperatures on the gauge.