153 A&C: PiR2Null Area Controller (153)

Introduction

The PiR2None is a Raspberry Pi with no additional hardware. But a PiR2None can read the temperature of the Pi computer as procTemp. It can control the onboard green LED as piLED. But it cannot read anything on the Raspberry Pi that can be controlled as OFF or ON by the operater. The actual PiR2 can read ambLight (the ambient Light in lux) but only if an ADC (Analog to Digital Converter) is available. If an ADC is not available, the normal PiR2 software will always read as "lit". However a minimal measure of light (digLight) can be achieved as a binary value (0 or 1) using the simple circuitry called model PiR2Null as shown below. If no such circuitry is available, then the model of PiR2 must be called PiR2None so as to avoid erroneous aberrent values. If it is attempted to read digLight without the PiR2Null circuit (shown below), it is possible that the resulting digLight values will be wrong, aberrant, noisy or variable. In a lit area, the Binary value of "digLight" is 1 meaning "Light ON". In "darkness", the resistance of the CDS is higher giving digLight a Binary value of "0".

The first version of the PiR2 that can read ambient light (in lux) is the PiR2D (Source 01).

The PiR2Null circuit

However it is possible to read digLight (the ambient Light as 0 or 1) using only a CDS (photo cell) and a 6k8 ohm resistor. The easiest way to do this is shown in the text drawing shown in Figure 1 below. This circuit works on the Raspberry Pi (or on the Pico Pi in the future). In both cases, ensure that the CDS (photo cell) is connected near the end of the Pi board, not near the middle of the Pi board. Note that the lead from the 330 ohm resistor is a female jumper that can be jumpered to any GPIO pin.
          Pi       Pi                   Pico       Pico
          GPIO     pin#  device         GPIO       pin#     signal
          ----     ----  ------         -------    ----     ------
                                  LED
          GPIO22   15 -- 330ohm---|>|--|                   the 330 ohm resistor is connected to pin 15 by a female jumper. 
                                       |
                           |-----------|  GND     
                           |              4 empty pins
                           |               (37 to 40)
                     (red) |
          3V3       1------)-------|----3V3        36
                           |       |
          SDA1      3      |       |    ADC_VREF   35
                           |      CDS
          SCL1      5      |       |    GP28(ADC2) 34
                           |       |
          GPIO04    7------|-------)----GND        33       (optional if Pi Pico not used)
                           |       |
                          -|--6K8--|-                       (Note 4)
                     (grn) |       |
          GND       9------|-------)----GP27(ADC1) 32
                                   |
          GPIO17   11--------------|----GP26(ADC0) 31       digLight (or optionally as ambLight on the Pi Pico)
                     (wht)
                                 
   Note 1 When used with the Pi Pico, GP26 can be defined as either a digital input or an analog input.
   Note 2 The "-)-" means "jump over the horizontal wire" and does not mean "connect the horizontal and vertical wires".
   Note 3 The "-|-" means "connect the horizontal and vertical wires".
   Note 4 It is recommended to replace the 6K8 resistor with a 10K0 rheostat (variable resistor) because all CDS are not identical.
   Note 5 If the nominal 3V3 voltage of the Pi is much lower than 3.7v e.g. 3.3, the 6K8 resistor should be reduced to 5k.
   Note 6 The above circuit can be plugged in "backwards" without damaging neither the Pi nor the Pico.

                     Figure 1 digLight sensor
The only components needed are the CDS photocell, a 6K8 ohm resistor and three (or a block of 6) female connectors to fit onto the Pi GPIO pins. The red wire must be connected to physical pin 1, the grn(green) wire to physical pin 9 and the wht(white) wire to physical pin 11.

Physical View of the PiR2Null circuit

In the schematic of the KY-18 photo-cell, it can be seen that the positions of the photo-cell and the resistor can be inverted. Such an inversion will have the impact of inverting the numerical value of the amount of light. In one case, more light will produce a higher value and vice-versa. The actual KY-18 device can be inverted by simply twisting the device, keeping the 3v3 pin in the middle, but interchanging the S and GND terminals. Some of the diagrams, photos and analyses pertain to one version and others to the other version.

Note that the sensor marked "S" appears on the left of the right-most photo below (labelled KY-18 Photo). The PiR2null board also has a red LED (to GND) with a 330 ohm resistor. The 330 ohm resistor is attached to a female lead that can be connected to any of the 40 GPIO pins. It is shown connected to physical pin 15. The test program causes the red LED to be lit when the photo-cell is dark. In March 2022, the pinouts of the PiR2null were altered to agree with the PiR2A. The connections to GPIO pins were made variable. This was done by soldering wires terminated with female connectors that can be connected to any GPIO pin. It was found that the KY18 light sensor produced output that was inverted compared to the push button on the PiR2A. Other than that, it serves as a PiR2null sensor (lacking the TMP102 temperature sensor)

(To enlarge .....Click it)
thumb: PiR2null.jpg
PiR2null


(To enlarge .....Click it)
thumb: pKY-18_Schematic.jpg
KY-18 Schematic


(To enlarge .....Click it)
thumb: pKY-18-photo.jpg
KY-18 Photo


KY-18-photo.jpg This simple tiny PiR2Null circuit can be used in three ways:

-alone, with neither a Pi Pico nor a WeatherHat Pro
-with a Pi Pico
-with a WeatherHat (not a WeatherHat Pro)

All 3 ways allow measurement of digLight (Binary). The second and third allow measurement of ambLight (Numerical).
                                O (soldered together)
                               /|\
                              / | \
                             /  |  |
                            /   |  o--(optional)-|
                           /    |  |             |   
                          /     |  |             |
                        CDS    6K8 |             |  (Note 4) 
                        /       |  |             |
                       /        |  |             |
                      /         |  |             |
                     /          |  |             |
   wire color: (red)|    (green)|  |(white)             |
                    |           |  |             |
                    |        |--|  |             |
                    |        |  |  |             |
   Pi physical pin  1  3  5  7  9  11            12
   Pi logic pin    3V3         GND GPIO17        GPIO18 (Note 3)
 Pico physical pin  36 35 34 33 32 31
 Pico logic    pin 3V3      GND   GP26/ADC0
Note 1 The CDS is a photo-cell and the 6K8 is a 6,800 ohm 1/4 watt resistor.
Note 2 The above circuit should work fine on a Pi Pico, if physical pin 32 is connected to physical pin 33.
Note 3 The digLight signal on GPIO17 (and optionally GPIO18) can be converted to analog (ambLight) by two actions
3a connecting a WeatherHat between the Raspberry Pi GPIO pins and the PiR2Null
. . . . The WeatherHat must be equipped with a 2x20 male & female connector
3b adding a wire from Servo-Sig (GPIO18) to A0 on the WeatherHat
Note 4 It is recommended to replace the 6K8 resistor with a 10K0 rheostat (variable resistor) because all CDS are not identical.

The digLight Python test program for the Raspberry Pi

The following Python program can be used on the Raspberry Pi to read the Binary value of digLight from GPIO17 (which is physical pin 11). Because the Raspberry Pi cannot read analog signals, it cannot read ambLight as a numerical (analog) value.
import RPi.GPIO as GPIO
import time

progName="test digLight"
print("starting ",progName)
print(" Ctrl-C will end the program")
digLight_pin=17  #GPIO pin 17 is physical pin # 11 on the Raspberry Pi
#  CDS is connected between digLight and nominal 3V3 which is often 3.75v
#  6.8k resistor is connected between digLight and GND.
# 10k   resistor is too high when the back and sides of the CDS are not kept dark.
#  The 6.8k resistor even detects between dim light and dark.
#  It is recommended to use a 10k0 rhostat (variable resistor) initially set to 6K8.
#setup the pins in the software
GPIO.setmode(GPIO.BCM)
GPIO.setup(digLight_pin,GPIO.IN)
try:
	while True:
		if GPIO.input(digLight_pin):
			print("digLight is 1: ON  i.e. lit")
		else:
			print("digLight is 0: OFF i.e. dark")
		#if end
		time.sleep(1) #sleep for 1 second
	#while end
finally:
	print("ending ",progName)
#try end
# /digLightTest.py
#program end
	
The above PiR2Null circuit and the digLightTest program were successfully tested on 2021BFeb13 by the author. This PiR2Null device can be used by the programmer to ask for a reply of "Yes" or "No" without the program being "stuck" waiting for an (absent?) operator to reply. While the program is running, to reply "Yes", cover the photocell by touching it with your finger. When covered, the program will say "dark". The same effect can be produced by turning on/off the light switch in a dark room. However, if the room is well lit by natural daylight, it is necessary to cover the photocell somehow to achieve a "dark" result.

To terminate the above program, press ctrl-C. This program was tested by D@CC on 2021BFeb09.

Reading digLight (and ambLight0) on the Pi Pico

No Python program yet exists to run the digLight Test program on the Pi Pico. But the exact pin outs selected for the Pi Pico permit the state of digLight to be read as either a binary value or as an analog value depending upon the software. If the software defines Pi Pico GPIO pin 26 as a digital input, it will be read as digLight. But if the Pi Pico software defines the Pi Pico GPIO pin 26 as an analog input (ADC0), it can be read as ambLight, which is a numerical value that can be coverted to lux.

Ambient Light Values in lux

A video (YouTube Source 01) exists about a commercial ambient light sensor for the Raspberry Pi. An ambient light meter application is also available for the iPhone. Some approximate values of surface light (in lux abbr. lx) are shown below:
	situation	       lux
        ---------	 --------------
	no light	              0
	dark		     1 -      9
	dim light	    10 -     49
	readable	    50 -     99
	home		   100 -    299
	office		   300 -    499
	shade		   500 -    799
	overcast	   800 -    999
	full daylight	 1,000 - 24,999
	direct sunlight	25,000 - 99,000
	flash               over 99,000

Pad or Litho Silkscreen Shape Meanings

More silkscreen iniformation can be found in Source 5 (Circuit Drawing Using Libre-Office). On Printed Circuit Boards (PCB), the shape of the silkscreened solder pad or marker for a pin can have various meanings (as per Source 03):
Note 1 : BC-Robotics sells some PCBs (eg U1V11F5) where a square pad means GND.

PiR2T Near-By Area Thermometer

This thermometer is designed to monitor the temperature in a refrigerator or a freezer. It is a small board that contains the following components: This is the minimum board that makes a PiR2 Area Controller usable for an extended time period. The I2C cable (even without an I2C Grove connector) permits 1 or more TMP102 thermometers to measure the temperature inside fridges or freezers. The CDS digLight sensor permits a person to boot-up the device using a very limited series of local operator questions and answers. The Yellow LED facilitates this question/answer scenario.

The boot-up sequence is further simplified if the installer has an iPhone handy to receive questions from the PiR2 via email. But reading email replies has not yet been achieved on Raspberry Pi computers. The CDS photocell permits the installer (the operator) to answer a few questions by YES/NO if the boot-up process runs into problems. The very simple boot-up is facilitated by using the Yellow LED (along with the on-board Red and Green LEDs already on the Raspberry Pi.)

If the PiR2 boot-up process becomes slightly more complicated, the installer needs to communicate with the PiR2T using RDC (Remote Desktop Connection). But to do this, the IP address of the Pi must be known. This IP address is sent in the first email sent to the installer from the PiR2T. Knowing this IP address, the installer can bring his LapTop to the final PiR2 location (e.g. beside the basement freezer). Any start-up problems can then easily be rectified by the installer using full two-way communication with the PiR2T. The RDC permits the installer to "see" the full HDMI screen of the PiR2 and make use of the keyboard and mouse of the laptop to interact with the PiR2.

The PiR2T then makes use of the wifi network to inform the owner of the house (by email) of any irregularities in the small area controlled by the PiR2T. The owner of the house can synchonize all the logs (of his various PiR2 controllers) every few days. They must be synchronized to upload the logs to the MehInCharge website that houses all of the logs. This includes all logs recorded since the owner started using his first PiR2 area controller. The owner can also use RDC to send special commands to each PiR2 via the MehInCharge synchronization process. This can be done without actually visiting the location of each PiR2 in his house. Each PiR2 can even be remotely rebooted using "Hey Google" commands, although this can only be done within the range of the building's router.

The building's full log file can be reviewed remotely, while the owner is on vacation, for example. It is only the synchronizing that needs to be done within the range of the building's router.

Talker: using a Raspberry Pi to talk to the Pico

Source 04 demonstrates how the Pi can send simple commands to the Pico using a simple data cable (USB to USB-micro) as shown in the figure below. An example of such a single command is shown below. The operator uses an interactive python3 session on the Pi to "talk" to the Pico. It is easy to set up and talks directly to the Pico using "send" and "receive" commands. The word "Type" means that the operator should type the subsequent command on the keyboard. The "on function" will turn ON the on-board LED of the Pico. The step-by-step installation and more detailed information (with some other commands) are available at Source 04. Some minor transcription errors appear in Source 04 (e.g. the final ")" is missing and has been added to the command in the example below).
Type t.send('on()')
Talker Example (immediately above)

(To enlarge .....Click it)
thumb: pi-n-pico.jpg
pi-n-pico for Talker


The materials needed are:

Other Thoughts

Source 06 by D@CC describes a mainMenu program for Servers like the Pico, RP2040 and the WioTerminal.
     It runs a program by invoking an improvised function that performs the program's task.
    

Source 07 by D@CC describes an RPC (Remote Procedure Call) program for Servers like the Pico, RP2040 and the WioTerminal.
     An RPC tells a remote server to perform a function; ie send an A/C signal to a specific pin on a local or remote
     Server. The ixmRPC mechanism has not yet been written but is a concept that is gaining popularity.
     Similar to Talker, an RPC mechanism permits a Client core to request an action on a Server core.

Sources

Youtube Sources

Youtube Source 01: BH1745NUC Ambient Light and Color Sensor for the Pi (8:00 min) Dcube Tech Ventures 2016 Jun 29

Web Sources

Web Source S153:01: Pi: PiR2D Area Controller Article 146 by David KC Cole on 2021B Feb 03
Web Source S153:02: Pi: PiR2A Area Controller Prototype Article 139 by David KC Cole on 2021B Feb 03
Web Source S153:03: Soldering Hints by SolderGear (viewed on 2021B Feb 12)
Web Source S153:04: Talker (Via this, the Pi will talk to the Pico) by romilly on github (viewed on 2021B Feb 13)
Web Source S153:05: 160 IT: Circuit Drawing Using Libre-Office by David Cole in 2022.
Web Source S153:06: www Wio: wioMainMenu (190.html) by D@CC on 2023DApr29
Web Source S153:07: www A&C: ixmRPC (eRPC lite) Remote Procedure Calls (193.html) by D@CC on 2023DApr29


WebMaster: Ye Old King Cole

Click here to return to ePC Articles

Created: 2021 B Feb 08
Updated: 2024 F Jun 30

/153.html