Pi Pico Wireless Time and Temperature Indicator
by TomGoff in Circuits > Raspberry Pi
83 Views, 0 Favorites, 0 Comments
Pi Pico Wireless Time and Temperature Indicator

This is a relatively simple and fun project using the Raspberry Pi Pico W to tell the time and give me an indication of the outside temperature.
I have recently started to learn how to use the Raspberry Pi Pico W and wanted a project to explore its many excellent features.
I commute to work everyday on a bike and one of the first things I do in the morning is look at my thermometer to see what the outside temperature is so I can make an informed decision on what to wear. So I thought I could make use of the Raspberry Pi Pico to help me solve this problem.
Rather than just use the Raspberry Pi Pico W as a simple thermometer I decided to make use of its internet accessing capability and get it to read an online weather forecast at my final destination. Because I don't have much experience using APIs (Web Application Interface) and JSON (JavaScript Object Notation) data, this project gave me the perfect excuse to integrate these two esoteric ideas into my project.
So my final project will be a device that accesses the internet through my home router, gets some weather data off a website and then uses this website to light up a some LEDs depending on the outside temperature. I will also add a clock to the project as a fun extra feature. The clock will use the Pico's internal real-time-clock to read the time, the clock will be set and calibrated using NTP (Network Time Protocol). The time will be displayed on a 4-digit TM1637 7-segment display.
The project took me about a day to build and should be suitable for someone with beginner / intermediate level electronics / maker skills. I used the laser cutter at Norwich Hackspace - home but you could make the enclosure with simple woodworking tools.
Supplies

I wanted to make use of parts and equipment that I had left over from other projects, so this informed may of my choices for the components used.
Here's a list of the materials I used:
- Raspberry Pi Pico W x 1
- Copper Dot Prototyping Board x 2
- TP4056 Charge Controller x 1
- 18650 Lithium Ion Battery x 1
- 18650 battery holder x 1
- Male / Male Header Pins Strip x 3
- Female / Male Header Pins Strip x 2
- 220 ohms Resistors x 4
- 3mm Blue LEDs x 3
- 3mm Green LEDs x 3
- 3mm Yellow LEDs x 3
- 3mm Red LEDs x 3
- TM1637 7-Segment Display x 1
- 1x6 5mm Pitch Screw Terminals x 1
- Hook-up Wire Set x 1
- 3mm Clear Acrylic Sheet x 1
- 3mm Plywood x 1
- Solder x 1
Here's a list of the tools I used:
- Soldering Iron
- Wire Cutters
- Wire Strippers
- Fume Extractor
- Safety Glasses
- Laptop with Thonny Installed (for programming the Pi Pico)
- Laser cutter / engraver
Solder the Circuit Board





I've attached a simple wiring diagram showing how all the components are connected, please take a look.
The first thing I did was place all the components of the copper dot prototyping board to check the layout.
Next I soldered the male / male header pins to the Raspberry Pi Pico W. an easy way to do this is to push the header pins into a solderless bread board first to hold them into position. You can Then solder the pins to the Pico with the pins secured in place.
I then soldered all the components to the breadboard. It's good to use a fume extractor when soldering to prevent you inhaling any nasty fumes. Also safety glasses are a good idea, it's easy to forget you are holding a hot soldering iron and poke yourself in the eye when you scratch your nose!
A handy tip is to hold the components in place with some blue tack or tape when you solder them, this means you can turn the circuit board upside-down and the components will stay in place.
With the components all soldered in place I then finally soldered all the hook-up wires in place to connect the components.
Build Enclosure





The design of the enclosure fulfilled two purposes. Firstly to safely enclose the circuit board, secondly to integrated a laser engraved acrylic sections that are illuminated the correct colour depending on the outside temperature. The laser engraving meant that I could integrate some text that illuminates with the corresponding LEDs, for example "COLD" for the blue LEDs when the temperature is cold and "HOT" for the red LEDs when the temperature is hot.
I made a 4-level laser engraved acrylic sign with each section separated with a gap to ensure they illuminated independently. The acrylic section were held in place by a frame made from plywood. I held the acrylic sections in place by making the plywood frame in the layers the middle layer had rebates cut out to support the acrylic sections in place. The acrylic frame also housed the LED modules. The illuminated frame is held in place by four slots in the main enclosure that it drops in to.
Write Some Software

The software for this project was written in MicroPython. This project is only the second time I have used MicroPython, so it was a really good way to get some experience. I am used to working with microcontrollers like PICs, Arduino's and ESP32's so it felt quite different working with the Pi Pico. Unlike microcontrollers programmed in C / C++, you don't compile the code and upload it to the micro controller. You simply save the python programs directly onto the Pi Pico. There's loads of introductions on working with the Pi Pico so I'm not going to go into the basics of how to download MicroPyton etc. One of the best places to look is on Pico-series Microcontrollers - Raspberry Pi Documentation.
I used the Thonny IDE to write the software as I think it's great to use and there's a load of support to go with it, but I'm sure there are plenty of other IDE's to use.
I have attached a copy of the main code (main.py) and the TM1637 library (tm1637.py) for you to use if you wish, however you will need to change the location on the API request to the location of your choice.
So, here's a run through of how the code works. The aim of the code is to display the current time on a 4-digit TM1637 7-segment display and to show outdoor temperature using LEDs, which update automatically from the internet.
It connects to Wi-Fi, synchronises its clock using NTP (Network Time Protocol), and retrieves the weather from the Open-Meteo API every 15 minutes.
I've broken the code down into 8 sections to help explain it in detail.
Section 1- Importing Libraries
The libraries are imported and used for the following:
network: Controls the Wi-Fi connection.
urequests: Allows the Pico to make web requests (HTTP GET).
time: Provides time and sleep functions.
ntptime: Synchronises the clock with internet time servers.
machine.Pin: Controls hardware pins for LEDs and the display.
tm1637: A library for controlling the 4-digit 7-segment display.
Section 2- SSID and Password
As you will be connecting your Pi Pico to the Internet to get the time and weather data it needs your routers SSID and Password to log in. You will need to use your own SSID and Password for your own router. These are stored as variables to be used in a function later on. These are in plain text in your program so are not really that secure so be careful not to let anyone else you do not trust access your Pi Pico once it's programmed.
Section 3 - Setting Up The Hardware
I have four LEDs and these are connected to GPIO pins on the Pi Pico. The program need to identify that these pins are outputs and are stored as variables. To be more specific they are a pin object stored in a variable.
A TM1637 display is connected with its clock (CLK) and data (DIO) pins, we are creating an object called tm, which represents our 4-digit 7-segment TM1637 display. The TM1637() part is a constructor which comes from the TM1637 library imported earlier at the start of the program. Inside the parentheses, we pass two arguments (clk and dio) that tell the library which pins the display is connected to.
Section 4 - Connecting to the Wi-Fi
The Pico is set to station mode (STA_IF) so it can join an existing Wi-Fi network. It uses the SSID and Password credentials we saved as functions earlier so that it can log in. The program waits in a loop until the connection is successful, then prints the assigned IP address.
Section 5 - Synchronising Time (NTP)
We are using NTP which ensures the clock is accurate without manually setting it. The returned time is in UTC, so we add a time offset (e.g., 3600 seconds = 1 hour) to match the local time zone. I'm in the UK in British Summer Timer so I need to set the clock forward 1-hour. You will need to adjust the time for your time zone.
Section 6 - Fetching the Weather Data
This is the section of code I was really interested in because I get to use an API (and start to feel like a proper coder...). This is a function called fetch_weather() that sends a GET request to Open-Meteo’s API using your latitude and longitude. You will need to put in your own longitude and latitude for your location to get your local weather, for example if you live in San Francisco you need to replace latitude=52.63 and longitude=1.30 with latitude=37.77 longitude=-122.41942. It extracts the current temperature (in °C) from the json file and returns it to be used later.
Section 7 - Controlling the Temperature LEDs
This is another function, This function is to control the state of the LEDs. When called it first turns all the LEDs off, this puts them in a known state and prevents you turning on two colours at the same time.
Next there's a series of if / elif / else statements that determine the state of the LEDs depending on the temperature returned from the fetch_weather() function, that is sent to the update_leds(temp) function as temp. If the temperature is below 5°C the blue LEDs light up and "COLD" acrylic segment is illuminated. If the temperature is above or equal to 5°C and below or equal to 16°C the green LEDs light up and "CHILLY" acrylic segment is illuminated. If the temperature is above or equal to 16°C and below or equal to 25°C the yellow LEDs light up and "WARM" acrylic segment is illuminated. If the temperature is above 25°C the else statement is true and the the red LEDs light up.
Section 8 - Setting up for the Main Loop
These three lines of code are important as they set up some variables for the main loop and set the initial conditions.
last_weather = 0 creates a variable called last_weather and sets it to 0 initially. It is used to store the timestamp of the last time the weather was updated. By setting it to 0 at the start, the program ensures that on the very first run, the difference between the current time and last_weather is large (because current time is always much greater than 0), forcing an immediate weather update.
temperature = fetch_weather() calls the function fetch_weather(), which sends a request to the Open-Meteo API. It receives the weather data response in JSON format. It extracts the current temperature and the resulting temperature (in °C) is stored in the variable temperature.
update_leds(temperature) calls the function update_leds() and passes the temperature value obtained above. The function then proceeds as detailed earlier.
Section 8 - The Main Program Loop
The main program loop is the equivalent of "void loop ()" if you are used to working with Arduino, it runs continuously (everything else previously is only done once or is just for setting up variables and libraries etc.) This main loop basically does four things:
- Gets the current time from the Pico’s internal clock (adjusted for local time).
- Updates the weather every 900 seconds (15 minutes).
- Displays the current hour and minute on the TM1637.
- Waits for 1 second before repeating.
I'm new to working with the Raspberry Pi Pico so I hope I have explained the code correctly. The files are attached for you to use and modify.
Assemble and Test




With all the hardware manufactured and the software written it was time to test it out.
First I inserted a battery and powered it up. After a minute or so it had connected to the internet and the time was displayed and the corresponding LEDs illuminated.
To check all the LED's worked correctly I made some adjustments to the update_leds(temp) function in the code. By adjusting the values I could get the different LEDs to illuminate.
If you need to change your Wi-Fi Router or relocate this project you will need to update the code to reflect the new SSID and Password. The next step to improve this project would be for it to be more secure and it to be able connect to a phone or tablet so that the SSID and Password could be changed with an app rather than having to upload new MicroPython Code.