161 A&C: PiR2Pico "Too Hot" Alarm (161.html)

Keywords

A&C PiR2Pico Alarm fridge freezer room "air conditioner" fireplace stove heater furnace ICH180RR "Raspberry Pi Zero W" thermometer CDS "photo cell" "green LED" "white LED" artificial heat "Pi 4 Model B" Chromebook RP2040 GP22 GP28 ADC3 ambLight whtLED UF2 "WiFi Router" config.txt pir2/files "Temperature Alarm" "Open Door Alarm" "3.14 minutes" 3:14:02 "Unique gmail address" "self test" self-test "covered with a finger" finger "Temperature Value Display" pir2Time "2 degree increase" SOS morse code "too HOT" "too COLD" "Owner Test" "Door OPEN Error" "button pressed" "PiR2 restarted at" log MehIncharge ambTemp.py PiR2_Main "Pico Tips and Tricks" MicroPython Pir2Z Pir2Z_F Pir2Z_H

Introduction

The PiR2Pico is a temperature alarm that signals when a fridge or freezer (or even a room, air conditioner, fireplace, stove, heater or furnace) is too hot. The computer at the heart of the PiR2Pico is a Raspberry Pi Zero W (costing about US$15.00). It is a tiny inexpensive model of the Raspberry Pi Computer. The model W has WiFi functionality. (A normal Raspberry Pi, with WiFi, can be used instead of the less expensive Raspberry Pi Zero W.) Temperature is measured using a US$4.00 Raspberry Pico which contains a thermometer. The ambient Light is optionally measured by a CDS photo cell. The owner of the fridge or freezer is notified of a Temperature Alarm via an email message, which is not repeated more often than every 4 hours. An optional "Open Door" Alarm will be repeated more often because it should be corrected quickly. The PiR2Pico has a built-in green LED and an optional white LED and a single optional CDS photo-cell. The green LED sends signals to the person installing, monitoring or observing the PiR2Pico. The optional CDS photo cell measures ambient light as an analog value. The 360 ohm resistor (for the optional White LED) provides a source of "artificial" heat to slightly "warm up" the thermometer in the Pi Pico. The owner or installer can also use the optional CDS as an input device by touching it (covering it up with a finger), as if it were a non-mechanical pushbutton.

The Raspberry Pi Zero W uses the PiR2Pico to repeatedly read the temperature in the fridge or freezer. To do this, the Pi Pico is taped to the inside wall of the fridge (or freezer) at the end of a thin cable. This cable usually enters the appliance near the hinges of the door. In the image below, the Raspberry Pi 4 Model B (not a Raspberry Pi Zero W) is near the top and the Pi Pico is in the bottom center. In the photo, they communicate using a black USB cable. Of the many wires in the photo, only the wire to the Power Supply (in the lower left) and one USB cable (lower right), to the Pico, are needed. This simplest PiR2Pico does not require any additional electronic components. The Pico is so new that not many photos are available of the Pi with the Pico. It is possible for the Pico to be connected to many other computers (such as a Windows, Apple or Linux laptop), but this article is limited to a Pico being controlled by a Raspberry Pi. It is not yet known whether a Pico can be controlled by a Chromebook, but I believe it can.

The actual processor chip (part number RP2040) on the Pi Pico can easily be seen in the photo below. The RP2040 is the biggest black square chip that is located just below the "(c) 2020" letters at the top center of the Pi Pico. This is the IC chip that does all of the processing in the Raspberry Pi Pico. Of course, the small Pi Pico printed circuit board has much more latent unused capability. This project makes use of only a tiny fraction of the capabilities of the Pico board.

(To enlarge .....Click it)
thumb: PiWithPico.jpg
A Pico (bottom center) with a Raspberry Pi Model 4B above it


The more technical readers might be interested to know that:

Only four General Purpose Input Output (GPIO) pins on the Pi Pico are needed for its optional electronic components. The Pi Zero W communicates with the Raspberry Pico via a wire (or cable) plugged into a USB connector on the Raspberry Pi. One GPIO pin on the Pico is needed to read the CDS photo cell; an analog input. A second GPIO digital pin (on the pico) is used to drive a White LED. The green LED already exists on the Pico. The exact GPIO pins used on the Pi Pico are listed below:

Physical   Pico          Sensor/Control
Pin #   GPIO Name  Use         Name     Component(s)
***     *********  *******     ***      ****************************
29      GP22       dig. Out    whtLED	(from pin 29) through a 360 ohm resistor then
                                          through a white LED's short lead to Ground (pin 33).
33      GND  AGND  Ground      Ground	from Ground through a 6K8 (6800 ohm) 1/4 watt 
                                          resistor to ambLight (pin 34).
34      GP28 ADC2  Analog In   ambLight	from ambLight (pin 34) through the CDS photo cell 
                                          to Vcc (pin 36).
36      3V3  (OUT) 3.3v source Vcc	

Note: Route the 360 ohm resistor over (near) the square black processor chip below the 
        letters "(c)2020" printed on the Pi Pico board.



The physical positions of these 4 components should be approximately as shown below:

                         physical pico           PiR2pico    Note
                              pin name              name  /  use
                               v
          *******-----------O 36  3V3
          *     *
          * CDS *           O 35  ADC_REF
          *     *
          *     *-o---------O 34  A2                "ambLight"
          *******  \-6K8-\
          --LED-----------o-O 33  GND               The white LED is close to the CDS.
          |
          |                 O 32  A1  or 1SCL       future use
          |
        **|********         O 31  A0  or 1SDA       future use
        * |       *
        * |       *         O 30  RUN               The 360 ohm resistor is
        *  \-360- *                                   above the RP2040 IC.
        *        \----------O 29  GP22              "whtLED"
        * RP2040  *
        ***********         O 28  GND
 
                            O 27  GP21              future use

       NOTE: A temporary connection of RUN to GND will reset the RP2040.

The initial software for the Pi Pico can be downloaded from the Raspberry Foundation at Source 04. It is downloaded as a .UF2 file (UF2 is defined in Source 05). See Source 02 for very easy-to-follow detailed information about how to download the UF2 file and how to start using your Pi Pico.

Requirements

-The Raspberry Pi Zero W needs a 5V DC power supply that must be plugged into a 110v outlet to have enough power to operate.
-The Raspberry Pi Zero W also needs access to a WiFi router in the building.
-The PiR2Pico needs to be connected to the Pi Zero W via a micro-USB cable.
-The config.txt file (described below) must be created by the owner and placed in folder "pir2/files/" on the Raspberry Pi Zero W.

Temperature Alarm Trigger

The "Temperature Alarm" is triggered when the temperature (read by the thermometer) deviates from the "alarmTemp" by more than 3 degrees Centigrade. This wide range is to eliminate ALL false temperature alarms. Most modern fridges and freezers can maintain the temperature within a range of plus or minus 3 degrees Centigrade. A malfunctioning PiR2Pico can be "tested" by manually removing it from the fridge or freezer until its temperature rises more than 3 degrees. The ambient temperature will be displayed by blinking the green LED as described below. Furthermore, a properly functioning PiR2Pico accidentally left outside a fridge or freezer will eventually send an error message by email.

Optional Open Door Alarm Trigger

The "Open Door Alarm" ONLY occurs when the PiR2Pico is equipped with the optional CDS photo cell. When the PiR2Pico "sees" that the door has been open for a little over 3 minutes, an alarm email is sent. This setting is very sensitive (happening after only 3.14 minutes). It is very sensitive in hopes that the person (who has left the door open) is the person who will receive the email. Hopefully that person is still in the building and can easily return to the fridge or freezer to close the door. This "Open Door" alarm is sent every 3.14 minutes. (After all . . . . this alarm is named after the mathematical number Pi which is approximately 3.14159.) If this "3 minute" alarm is a nuisance, the Raspberry Pi can simply be unplugged to cease its operation. The "Open Door Alarm" can be easily "turned off" by covering the PiR2Pico with a piece of cardboard, held in place by some masking tape. The PiR2Pico also signals an "Open Door Alarm" situation by causing the green LED to signal SOS as described below. A future possible enhancement to the PiR2Pico will be the addition of an audible alarm (a "beeper") connected to the Raspberry Pi.

Control and Communication

The Pi Pico (in the PiR2Pico) is controlled by a Raspberry Pi computer. This control is accomplished via the micro-USB cable between the Pico and the Pi. A person can manually control the Pico from a keyboard on the Pi. But the Pi can also control the Pico automatically without a person at the keyboard of the Pi. In fact, this is the normal way the Pico is controlled. In order to send the email messages to the owner, the Pi must use its WiFi capability to "talk to" the Router. The measurments and the electronics are controlled by some software running in the Pico. But other software must be running concurrently in the Pi. The communications along the serial USB cable are described in detail in Source 06. Because all of the PiR2Pico software will soon be available, it is not necessary to do any programming. The owner only needs to create the "config.txt" file that is described below. But it is also necessary for the owner to create a unique Gmail address for the Raspberry Pi to use when generating the email alarm messages. An email message cannot be sent unless the "creator" of each message has an email address. If you have difficulty setting up a gmail address, get help from someone who is more knowledgeable about email.

Software

The software to run the Pi Zero W is the standard PiR2 software. The PiR2 software has been updated to
-read the config.txt file.
-read the temperature of the Raspberry Pi Pico
-optionally accept user input using the CDS photo cell as a "pushbutton" when it is covered up with a finger
-optionally read the amount of ambient light inside the fridge or freezer

This PiR2 software automatically starts when the Raspberry Pi Zero W is powered on. If "unattended", the software will fully start up, will continually monitor the temperature of the fridge or freezer and will send an email when the temperature is "Too HOT". It blinks the grnLED approximately every 10 seconds to let the owner know that the PiR2Pico is operational and to signal the temperature in Centigrade (See "Temperature Value Display" below). A daily email message is sent at pir2Time (which is suggested to be 03:14:02 am). This daily email is to signal to the owner that the PiR2Pico is operational. Each time the PiR2Pico is started up after losing power; a similar email is sent to indicate that the PiR2Pico is once again operational.

config.txt

The PiR2Pico requires one configuration file so that it knows how to operate. A typical "config.txt" file is shown below:
# Configuration File for PiR2Pico
alarmTemp      = "4"           # in degrees Centigrade
alarmDoor      = "3.14"        # in minutes (or "0" to disable it)
alarmIf	       = ">"	       # ">" means "temperature is too High"
#                              # "<" means "temperature is too Low"
applianceName  = "GE_Fridge"
piArea         = "01_Kitchen"  # floor # and room name
piBuilding     = "621_Ottawa"
routerName     = "Cole180"
routerPassword = "boy99999"
emailPi        = "pir2Email_for_621_Ottawa_at_gmail.com"
emailOwner     = "ownerName_for_621_Ottawa_at_gmail.com"
installDate    = "2021CMar28"
pir2Time       = "03:14:02"    # daily after midnight
selfTest       = "2"           # 2 normal self test
#                              # 0 never self test
#                              # 1 ignores the "heat" test
#                              # set it to 1 if the optional 360 ohm resistor is absent 
logStatus      = "0"           # 0 means log is disabled
deviceName     = "PiR2Pico"    # name of software is PiR2_Main
#/config.txt in folder pir2/files/

Self Test

The PiR2Pico does a self-test once a day at pir2Time. The self-test is as follows:

- the whtLED is turned on which causes current to pass through the 360 ohm resistor.
- the resistor heats up slightly.
- the warm resistor slightly warms up the Pico processor (that is located very near the resistor).
- the Pico software watches and waits for a 2 degree increase.
- if not (within 3 minutes); an error message is sent.
- if so; then the thermometer in the Pico is functioning.
- the whtLED will provide enough light to be seen by the CDS photo-cell when the door is closed.
- if the photo-cell "sees" the increase in light, then it and the LED are functioning.
- if not. . . an error message is sent.
- if everything is functioning correctly, the daily "everything is operational" message is sent.

Temperature Value Display

The PiR2Pico displays the temperature of the fridge or freezer by blinking the green LED. For example, if the temperature is 3 degrees Centigrade, the PiR2Pico blinks the green LED three times, then stays off for 5 seconds. It will do this repeatedly. If the temperature is -4 degreees Centigrade, the PiR2Pico turns the green LED on for 2 seconds (indicating the minus value), then it blinks 4 times, then stays off for 5 seconds. It will do this repeatedly. If the temperature is 0 degrees Centigrade, the green LED stays on for 2 seconds (signifying a minus sign) then off for 5 seconds (as if the temperature were "minus 0 degrees Centigrade".

When a "Too HOT" alarm (or a "Too COLD" alarm) is triggered, an email message is sent. In addition to the email, the alarm is also signalled on the grnLED. It signals the alarm on the LED by sending "SOS" in Morse code each time the temperature alarm is read and output via the grnLED. The SOS signal is 3 short blinks followed by 3 long blinks followed by 3 short blinks again. This is the international code for "distress". Superfluous email messages are not sent, the email Temperature Alarm message is not sent more often than once every 4 hours. However, the SOS alarm signal is signalled in the grnLED each time an alarm is noticed after each time the temperature is read (not just once every 4 hours).

You might ask why a "Too COLD" alarm was considered. When the author goes on vacation in the wintertime, a furnace problem could cause his/her house to get so cold that the water pipes might freeze. The "Too COLD" alarm would warn him/her soon enough so that preventive action could be taken.

Installation

Installation of the PiR2Pico is very simple. The Pi Pico (containing the thermometer) at the end of the thin cable must be placced in the fridge or freezer and taped to an inside wall of the appliance. A new Gmail address must be created for the PiR2Pico to use. (This is perhaps the most difficult part of the installation.) Then the config.txt file must be set up on the Raspberry Pi. This is usually done by the installer at his place of work before bringing the Pi Zero W and the PiR2Pico on site. Then the power supply of the Raspberry Pi must be plugged in to a 110v outlet. The cable to the Pico (containing the thermometer) must also be attached to the Raspberry Pi Zero W. Installation instructions accompany the PiR2Pico. Once installed, the PiR2Pico will initialize itself, self-test and send you an email. If you don't receive an email within 5 minutes of installation, verify that you followed the set-up instructions correctly.

The CDS photo cell and the white LED are optional. If they are not available, the plain Raspberry Pi Pico ("as is" with no additional components) can be used. During installation or start-up, if the resistor for the white LED doesn't "warm up" the Pico processor, these two optional components are presumed to be missing.

Owner Test

If the door of the fridge or freezer is opened, the optional CDS on the PiR2Pico "notices" that the light comes on. If it stays on too long, a "Door OPEN" error notification is sent by email. If someone touches the CDS for less than 5 seconds, the PiR2Pico interprets this as a "button pressed". This initiates the "self-test" routine described above. The owner should receive an email message from the Raspberry Pi Zero W in response to his/her having "pressed" the CDS "button" (by covering it with a finger). Another way to initiate the "self test" is to restart the Raspberry Pi Zero W by powering it off/on. This is simply done by unplugging it from the 110v outlet and reconnecting it.

Start-Up Notification

After each start-up of the PiR2Pico, for example after a loss of power to the building, the self-test program runs with the additional message "PiR2Pico restarted at ....".

Log

If the logStatus variable in the config.txt file = "1" then a log of the PiR2Pico activities is kept. The most recent log is regularly sent to the owner (accompanying the "Self-Test" email message). The owner can optionally send each log to the www.MehInCharge.com website. Use of this website is beyond the scope of this article.

The "ambTemp.py" Program

The MicroPython program (shown below) displays the temperature every 2 seconds on the Desktop of the Raspberry Pi connnected to the Pico. It only demonstrates that the PiR2Pico can operate. Alone, it will NOT trigger the Alarms mentioned in this article.
import machine
import utime

sensor_temp = machine.ADC(4)
conversion_factor = 3.3 / (65535)

time0=utime.time()
print("time degC")
while True:
	reading = sensor_temp.read_u16() * conversion_factor
	temperature = 27 - (reading - 0.706)/0.001721
	rndTemp=str(round(temperature,2))
	secCnt=utime.time()-time0
	print(secCnt,rndTemp)
	utime.sleep(2)
#while end
# by:     D@CC
# date:   2021DApr06
# status: tested OK on Pi Pico
# source: https://how2electronics.com/read-temperature-sensor-value-from-raspberry-pi-pico/
# folder: C:/Users/David/Documents/2021/PiR2_Pico/
#/ambTemp.py end

Output of the above program is shown below.  The "89937" is the number of elapsed seconds.
The 16.75 is the temperature recently read (in Centigrade) in our basement.

time degC
. . . . . 
89937 16.75
89939 16.28
Some of the above Python code was found in Source 09.

Value of the Pico's Internal Pull-Up / Pull-Down resistors?

Replies to the question at Source 08 show the value (on the Raspberry Pi) to be 50K ohms. Other answers elsewhere indicate that the value of these internal resistors on the Pico is the same.

Project Status

As of 2021DApr08, I have recently received a few Pi Pico boards. I was able to connect a Pico board to a Raspberry Pi Model 4B. I successfully turned on/off the green LED on the pico. Using the MicroPython program above, I successfully read the temperature of the Pico board using MicroPython in Thonny. But it is much more complicated to use the pico as a thermometer that sends temperature readings to the Raspberry Pi in an autonomous mode. I'll keep you posted on my progress. Source 07 might facilitate Pico Programming, but it costs $30.00 to join.

Circuit Python Library for Pico

See Source 11 for a library of many Python functions for the Pico on-board device.

Related Thoughts

Source 01 is a schematic that was originally designed for the PiR2Pico "Too Hot" Alarm. It may be of interest, although the diagrams in this article (161) (and especially Source 14 Pir2Z_H) are more recent. Source 03 is an extensive list (as of 2023DApr07) of all the Python functions that have been created by the webmaster over the years. It is referenced by Article 190.

Raspberry Pi Pico Tips and Tricks

Source 13 (by Malcolm Maclean) is the full on-line text of a RPi Pico book. It explains how to use a Pico to drive a stepper motor, a normal motor etc. It also shows how to hook up an SD card to a Pico. It also describes using the Pico W (for wireless connectivity). For every circuit, the MicroPython code (eg import machine) is provided. This book does NOT use CircuitPython. It also mentions Prometheus and Grafana. The "board" class of functions drives the Green LED using code compatible with both the original Pico and with the Pico W (see "Universal LED Blink"). It also shows how to send email from a Pico. It shows how to hook up a RTC to a Pico. And much more. . . . Wow. . . What a book!

Perhaps this book will be usable with many of the devices in the Wio Terminal. It uses Phonny as its recommended version of Python.

Sources

Web Sources

Web Source S161:01: PiR2Z Schematic Vsn F (deprecated by Source 14)
Web Source S161:02: Getting Started with Raspberry Pi Pico by Ravi Teja 2021B Feb 17
Web Source S161:03: IX functions Python List by D@CC on 2023 D Apr 07
Web Source S161:04: Empty UF2 by Raspberry Pi Foundation on 2023 G Jul 24
Web Source S161:05: .UF2 File Extension by Microsoft on 2020LDec29
Web Source S161:06: How to Setup Raspberry Pi Pico Serial Programming? USB Serial output by Ravi Teja 2021C Mar 22
Web Source S161:07: Drag-n-Drop Pico Programming by Ashley Whittaker on 2021C Mar 30
Web Source S161:08: What is the value of a Pull-Up or Pull-Down resistor on the Pi? (or the Pico?) by wwg 2013Jan21
Web Source S161:09: read-temperature-sensor program by admin on October 21, 2022
Web Source S161:10: Getting started with Raspberry Pi Pico by Raspberry Pi Foundation
Web Source S161:11: CircuitPython Libraries on any Computer with Raspberry Pi Pico by Carter Nelson on 2023GJul05
Web Source S161:12: CircuitPython Libraries on any Computer with Raspberry Pi Pico: GPIO (& ADC,PWM,I2C,SPI,NeoPixel) (40 p) by Carter Nelson on 2023GJul05
Web Source S161:13: Raspberry Pi Pico Tips and Tricks (>100 p) by Malcolm Maclean before 2023GJul24
Web Source S161:14: PiR2Z Schematic Vsn H (deprecates G & Source 01 PiR2Z_F.jpg) by D@CC on 2024ISep16

WebMaster: Ye Old King Cole

Click here to return to ePC Articles by Old King Cole

Date Written: 2021 C Mar 20
Last Updated: 2024 H Aug 28

Font: Courier New 10
/161.html