Raspberry Pi Pico weather display (MicroPython)
Your first MicroPython project: temperature and humidity on an OLED.
Shop by category
Shop all componentsThe Pi reads a PIR sensor with gpiozero and sends an email with Python's smtplib when it sees movement. Most email providers need an app password rather than your normal password, which you create in the account's security settings and store in a file outside the script. A five minute cooldown prevents a flood of messages. Never share the password or publish the script with it inside.
Tick anything you already have and the total updates.
microSD card for the operating system
Follow the colours: red is power, dark grey is ground, blue is a signal.
Official Pi 4 power supply.
pir_mail.pyimport smtplib, ssl, time, os
from email.message import EmailMessage
from gpiozero import MotionSensor
# ~/.mailcfg holds three lines: sender address, app password, recipient address
with open(os.path.expanduser("~/.mailcfg")) as f:
SENDER, APP_PASSWORD, RECIPIENT = [l.strip() for l in f.readlines()[:3]]
pir = MotionSensor(17)
COOLDOWN = 300
last = 0
def send():
msg = EmailMessage()
msg["Subject"] = "Motion detected"
msg["From"] = SENDER
msg["To"] = RECIPIENT
msg.set_content("The PIR saw movement at " + time.strftime("%H:%M:%S on %d %b %Y"))
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=ssl.create_default_context()) as s:
s.login(SENDER, APP_PASSWORD)
s.send_message(msg)
while True:
pir.wait_for_motion()
if time.time() - last > COOLDOWN:
last = time.time()
try:
send()
except Exception as e:
print("Mail failed:", e)
pir.wait_for_no_motion()
Use an app password, not your normal password, and check two-step verification is on.
Check the spam folder and the recipient address.
Keep the PIR away from heaters, windows and fans.
Better than in the script. Keep it out of any shared folder and give it chmod 600.
Yes. Change the SMTP server and port to the provider's.
Yes, with a camera module and the Pi camera timelapse project's approach.
6 parts: Raspberry Pi 4 Model B 2GB RAM Single Board Computer, Official Raspberry Pi 4 USB-C Power Supply – 15.3W, Official Raspberry Pi 32GB A2 Class Micro SD Card, HC-SR501 PIR Motion Detector Sensor Module, Solderless Breadboard and Dupont Jumper Wires (40 pcs). 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 1.5 hours. The build on this page has 6 steps.
No. This page has the complete, tested code (pir_mail.py).
About ₹8,526 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Reading a PIR with gpiozero; Sending email with smtplib over SSL; Keeping secrets out of your script.
Ideas that reuse this build: Pi zero motion logger. 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.