---
# System prepended metadata

title: Raspberry Pi Pico W - Temperature monitoring device Mårten Ström Bäverlind ms228cq

---


# Overview
This project covers how to create a basic temperature monitoring device using a Raspberry Pi Pico WH. The device will be able to retrieve the temperature with the use of a temperature sensor and the user will be able to monitor this value online. With the use of Wi-Fi the device will be able to pass information to an online cloud storage (Adafruit) for online monitoring. The device will also include offline  monitoring through communication by LED lights. The time required to complete the project is anywhere between two hours to a few days depending on the user's knowledge of the subject. Though the project should take no more than a few hours following this tutorial.

# Objective
The reason for this project is to let the user create an IoT device that can have some practical use for the user. The device will be able to monitor the temperature of the surrounding air and send warnings to the user if the temperature gets too cold or hot. This can be useful for many things e.g: monitoring the temperature condition of plants which thrive in specific temperatures. Following the project should give the user some further insight into IoT and serve as a basic device which the user can improve with more functionality by using sensors such as a humidity sensor or a soil humidity sensor. 


# Material
The following list of material was used for this project and was a part of a starter kit bought from ElectroKit for 399kr: https://www.electrokit.com/produkt/start-kit-applied-iot-at-linnaeus-university-2023/:
* 1x Raspberry Pi Pico WH - Microcontroller 
* 1x Breadboard 840 connections - Allows for connecting sensors to the Pico easily 
* 1x USB cable-A - For powering the Pico 
* 1x LED 5mm red diffuse 1500mcd  - Used to display a red light 
* 1x LED 5mm green diffuse 80mcd  - Used to display a green light 
* 1x MCP9700 TO-92 temperature sensor - Used to receive the temperature around the device
* 2x Resistor carbon film 0.25W 330ohm (330R) - Needed for the LED lights 
* 7x Jumper wires male-male 30cm - Used to connect everything on the breadboard


# Computer setup
This project was programmed using the IDE Thonny, however you can also choose Visual Code. 

Steps to using Thonny:
1.  Download Node.js
2. Download Thonny IDE installer
3. Run the installer and then open Thonny.exe 
4. Whilst holding down the BOOTSEL button on your PICO, connect it to your computer with the USB cable-A
5. In Thonny click Run at the top left corner and select the MicroPython(Raspberry Pi Pico) interpreter. Before you close this window click on intsall firmware in the bottom right corner.
6. Finally, in Thonny click on View and then select Files to make it easier to see the project structure

Now you are all set to start programming in micropython using the Thonny IDE. Here is an easy step by step guide how to start writing code and uploading it to your Pico:
1. Inside Thonny click on File and select a directory where you want your project to be stored or create a new one 
2. On the left side of the IDE right click underneath the project folder and select "new file"
3. In the new file write Print("Hello World") and save the file to your computer (CTRL + S)
4. To upload the code to your Pico make sure that it is connected to the computer and then right click on your file and select upload/, click OK and the file has now been uploaded to your Pico


# Putting everything together
The Pico is inserted in the middle of the breadboard and has one jumpwire from Pin 38(GND) to negative, one jumpwire from Pin 37(3V3_OUT) to positive. The temperature sensor is connected with its left pin(flat side towards me) connected to positive, its middle pin to Pin 32(GP27) on the Pico and its right pin to negative through three jumpwires. The LEDs are connected with its short pin to negative and long stick to a 330 ohm resistor which in turn is connected to the raspberry Pico using any of the available GPIs. In my case the resistor connected to the green LED connects to GPI15 and the red to GPI13. 

![](https://hackmd.io/_uploads/Bks6EmD_2.png)



# Platform
The platform used for this project is the cloud service Adafruit. It is the only platform I tried so I cannot make any comparisons with other platforms. Howerver the UI of Adafruit is very friendly and easy to learn for beginners. Adafruit allows the user to easily store,visualise,send and retrieve data through feeds/dashboards and actions. The project uses the free version which is enough for this small sized project, however this would not be enough if the project were to be scaled up since sending more data and using more storage would require a paid subscription of Adafruit. 

# The code
To implement the core funcionality of the code such as wifi connection and MQTT protocol I have used these tutorials:
https://hackmd.io/@lnu-iot/r1yEtcs55
https://hackmd.io/@lnu-iot/rJVQizwUh
Below are some important parts of the code used in this project

# Libraries

```
import time                   # Allows use of time.sleep() for delays
from mqtt import MQTTClient   # For use of MQTT protocol to talk to Adafruit IO
import ubinascii              # Conversions between binary data and various encodings
import machine                # Interfaces with hardware components
import micropython            # Needed to run any MicroPython code
import random                 # Random number generator
from machine import Pin       # Define pin
import utime
TEMPERATURE_INTERVAL = 15000    # milliseconds
last_temperature_sent_ticks = 0  # milliseconds
led_green = machine.Pin(15,machine.Pin.OUT)
led_red = machine.Pin(13,machine.Pin.OUT)
```



# Adafruit IO (AIO) configuration
```
AIO_SERVER = "io.adafruit.com"
AIO_PORT = 1883
AIO_USER = "martenbaverlind"
AIO_KEY = "aio_Cesf08G8HlpV4P86VgMDSeqxb4bz"
AIO_CLIENT_ID = ubinascii.hexlify(machine.unique_id())  # Can be anything
AIO_LIGHTS_FEED = "martenbaverlind/feeds/lights"
AIO_TEMPERATURES_FEED = "martenbaverlind/feeds/temperature"
```

# Temperature sensor code
```
def getTemperature(): # https://hackmd.io/@lnu-iot/r1hUdtzI3
    apin = machine.ADC(27)
    millivolts = apin.read_u16()
    adc = machine.ADC(27)
    sf = 4095/65535 # Scale factor
    volt_per_adc = (3.3 / 4095)
    millivolts = adc.read_u16()
    adc_12b = millivolts * sf
    volt = adc_12b * volt_per_adc
    dx = abs(50 - 0)
    dy = abs(0 - 0.5)
    shift = volt - 0.5
    temp = shift / (dy / dx)
    print(temp)
    return temp
```
# Function for reading received msg and switching the green and red LED lights 
```
def sub_cb(topic, msg):    # sub_cb means "callback subroutine"
    print((topic, msg))          
    if msg == b"green":    # turn on the green light and turn off the red light         
        led_green.on()                 
        led_red.off()
    elif msg == b"red":    # turn on the red light and turn off the green light       
        led_red.on()
        led_green.off()
    else:                        # If any other message is received ...
        print("Unknown message") # ... do nothing but output that it happened.
```

# Transmitting the data / connectivity

Data is sent from the device to Adafruit every fifteen seconds containing information of the current temperature. This is done using the wireless protocol Wi-Fi and transport protocols MQTT and webhook. Communication between the device and Adafruit is accomplished with MQTT, however warning messages sent from Adafruit to Discord to notify the user of dangerous temperatures are sent through webhook. 

Here is an image of Adafruit using webhook to send a warning to me through discord containig a link to the dashboard so I can easily monitor the temperature: 
![](https://hackmd.io/_uploads/BJgBxxCP2.png)



# Presenting the data
The dashboard contains a graph showing the temperature in the Y-axis and date in the X-axis which depends on the feed that is receiving temperature information from the device every fifteen seconds. Also there is an indicator above the graph that is either red or green. If the temperature is too hot or cold then the indicator is colored red otherwise it is colored green.
Since I am using a free version the data is stored in the database for 30 days and all new data received is instantly saved.

![](https://hackmd.io/_uploads/B1n61lRPh.png)




# Finalizing the design

The complete project is a device that is able to communicate with Adafruit through Wi-Fi about temperature and light either its red or green LED depending on the temperature. Green means that the temperature is currently too hot, red means too cold and no light means moderate temperature. I think that the project went well and was a fun first step into working with IoT devices. Here is a picture of what the finished hardware looked like:


![](https://hackmd.io/_uploads/HJLtwe0P2.png)





