Raspberry Pi Pico weather display (MicroPython)
Your first MicroPython project: temperature and humidity on an OLED.
Shop by category
Shop all componentsThe servo turns the HC-SR04 in 5 degree steps, the Pico measures the distance at each angle and prints it as a bar of # signs. It shows how robot vacuums and rovers look around, and you can walk in front of it to see the bars shorten.
Tick anything you already have and the total updates.
Sends a 40 kHz ultrasonic ping and times the echo to measure distance from 2 cm to about 4 m.
Small hobby servo that turns to any angle from 0 to 180° on command.
Follow the colours: red is power, dark grey is ground, blue is a signal.
main.py# Pico servo sonar: a servo sweeps an ultrasonic sensor and prints a radar line (MicroPython)
from machine import Pin, PWM, time_pulse_us
import time
servo = PWM(Pin(15))
servo.freq(50)
trig = Pin(14, Pin.OUT)
echo = Pin(13, Pin.IN)
def angle(deg):
servo.duty_u16(int((0.5 + deg / 180 * 2.0) / 20 * 65535)) # 0.5 ms to 2.5 ms pulse
def distance_cm():
trig.value(0); time.sleep_us(2)
trig.value(1); time.sleep_us(10)
trig.value(0)
t = time_pulse_us(echo, 1, 30000)
return t * 0.0343 / 2 if t > 0 else -1
while True:
for deg in list(range(15, 166, 5)) + list(range(165, 14, -5)):
angle(deg)
time.sleep_ms(120)
d = distance_cm()
bar = "#" * int(min(d, 100) / 4) if d > 0 else "-"
print("%3d deg %5.0f cm %s" % (deg, d, bar))
TRIG and ECHO are swapped, or the sensor has no 5 V. Swap the two signal wires and check VCC.
Average several readings and keep wires short. Two HC-SR04s pinging at once will confuse each other.
Power the servo from a separate 5 V supply and join the grounds; add a 470 µF capacitor across the servo supply.
5 parts: HC-SR04 Ultrasonic Range Finder Sensor Module, TowerPro SG90 9g Micro Servo (180°), Raspberry Pi Pico, Solderless Breadboard (830 points) and Dupont Jumper Wires (40 pcs). Tools: Digital Multimeter and Wire Stripper and Cutter. On this page you can tick the parts you already have and add only the rest to your cart.
It is a intermediate project and takes about 45 minutes. The build on this page has 4 steps.
No. This page has the complete, tested code (main.py).
About ₹640 for the parts at the prices on this page, plus tools if you do not have them. Quantity price breaks lower the cost when you build several.
PWM pulses and servo control; Time-of-flight measurement with sound.
Ideas that reuse this build: Pico ultrasonic distance OLED and Obstacle-avoiding robot. They are listed under Take it further on this page.
Your first MicroPython project: temperature and humidity on an OLED.
A web page with ON and OFF buttons that switches a relay over Wi-Fi.
A warm ring light that fades up at dusk and down at dawn.
Turn a Pi and a touchscreen into a home command centre with a clock and to-do list.
Learning project. Check every circuit and the datasheet of each part before you power it up.