14 Jan 2023

Coordinated Holiday Lights - box 1

The first box that I put together features a RaspberryPi 3 and accesses the network via ethernet-over-power (i.e. powerline ethernet). The RaspberryPi 3 runs on the Broadcom BCM2837; a 64-bit, quad-core, ARM Cortex-A53 SoC. My intention is to use this device to run the lights on the house and deck.

Parts:

One of the nice things about the RasberryPi is that, like so many such similar boards, it can be powered via its header pins. Therefore a nice, compact switching power supply to convert between mains voltage and the 5V3A can be plugged directly into either of the 5V pins of the RPi's 40-pin header. I chose the Mean Well RS-15-5 switching power supply. Simply connect the 3 mains wires on one side, and take 5V and ground off from the remaining pins; plug these 2 into the RPi's header and you've got power. One significant little detail that I did stumble over with these power supplies is the "adjustment" screw. When I first plugged the power supply and the RPi together, the RPi couldn't boot. Hooking up a console cable I could see some of the messages coming through okay at the start, but then there would be a whole bunch of garbled characters on the console. Garbled and legible text would come and go as the device booted. It was quite odd. It was as if the baud rate was fluctuating as the board ran. The leds on the board would come on, blink a little bit, then obviously start over again. It was clear the board was in a reboot cycle. I was about to try a different device (fortunately I had bought a couple) when I noticed the "adjustment" screw. I never did end up measuring anything, but I noticed the screw was turned all the way to the left (counter-clockwise). On a hunch I turned the screw all the way to the right (clockwise) then suddenly everything worked just perfectly.

The standard RPi header has quite a few GPIOs, and any one of which could be used for controlling the relay. I chose (completely arbitrarily) to use GPIO21, pin 40. As part of prototyping, you can test out controlling pin 40 with the gpioset program which is part of the libgpiod-tools Yocto package. The gpioinfo program can be used to dump out the Linux kernel's understanding of which GPIOs go where. I'm running this command on a Linux kernel 5.15.56:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
root@raspberrypi3-64:~# gpioinfo 
gpiochip0 - 54 lines:
        line   0:     "ID_SDA"       unused   input  active-high 
        line   1:     "ID_SCL"       unused   input  active-high 
        line   2:       "SDA1"       unused   input  active-high 
        line   3:       "SCL1"       unused   input  active-high 
        line   4:  "GPIO_GCLK"       unused   input  active-high 
        line   5:      "GPIO5"       unused   input  active-high 
        line   6:      "GPIO6"       unused   input  active-high 
        line   7:  "SPI_CE1_N"       unused   input  active-high 
        line   8:  "SPI_CE0_N"       unused   input  active-high 
        line   9:   "SPI_MISO"       unused   input  active-high 
        line  10:   "SPI_MOSI"       unused   input  active-high 
        line  11:   "SPI_SCLK"       unused   input  active-high 
        line  12:     "GPIO12"       unused   input  active-high 
        line  13:     "GPIO13"       unused   input  active-high 
        line  14:       "TXD1"       unused   input  active-high 
        line  15:       "RXD1"       unused   input  active-high 
        line  16:     "GPIO16"       unused   input  active-high 
        line  17:     "GPIO17"       unused   input  active-high 
        line  18:     "GPIO18"       unused   input  active-high 
        line  19:     "GPIO19"       unused   input  active-high 
        line  20:     "GPIO20"       unused   input  active-high 
        line  21:     "GPIO21"       unused   input  active-high
        line  22:     "GPIO22"       unused   input  active-high 
        line  23:     "GPIO23"       unused   input  active-high 
        line  24:     "GPIO24"       unused   input  active-high 
        line  25:     "GPIO25"       unused   input  active-high 
        line  26:     "GPIO26"       unused   input  active-high 
        line  27:     "GPIO27"       unused   input  active-high 
        line  28: "HDMI_HPD_N"       unused   input  active-high 
        line  29: "STATUS_LED_G" "led0" output active-high [used]
        line  30:       "CTS0"       unused   input  active-high 
        line  31:       "RTS0"       unused   input  active-high 
        line  32:       "TXD0"       unused   input  active-high 
        line  33:       "RXD0"       unused   input  active-high 
        line  34:    "SD1_CLK"       unused   input  active-high 
        line  35:    "SD1_CMD"       unused   input  active-high 
        line  36:  "SD1_DATA0"       unused   input  active-high 
        line  37:  "SD1_DATA1"       unused   input  active-high 
        line  38:  "SD1_DATA2"       unused   input  active-high 
        line  39:  "SD1_DATA3"       unused   input  active-high 
        line  40:   "PWM0_OUT"       unused   input  active-high 
        line  41:   "PWM1_OUT"       unused   input  active-high 
        line  42:    "ETH_CLK"       unused   input  active-high 
        line  43:   "WIFI_CLK"       unused   input  active-high 
        line  44:       "SDA0"       unused   input  active-high 
        line  45:       "SCL0"       unused   input  active-high 
        line  46:   "SMPS_SCL"       unused   input  active-high 
        line  47:   "SMPS_SDA"       unused  output  active-high 
        line  48:   "SD_CLK_R"       unused   input  active-high 
        line  49:   "SD_CMD_R"       unused   input  active-high 
        line  50: "SD_DATA0_R"       unused   input  active-high 
        line  51: "SD_DATA1_R"       unused   input  active-high 
        line  52: "SD_DATA2_R"       unused   input  active-high 
        line  53: "SD_DATA3_R"       unused   input  active-high 
gpiochip1 - 8 lines:
        line   0:      "BT_ON"       unused  output  active-high 
        line   1:      "WL_ON"       unused  output  active-high 
        line   2:  "PWR_LED_R"       "led1"  output   active-low [used]
        line   3:    "LAN_RUN"       unused  output  active-high 
        line   4:         "NC"       unused   input  active-high 
        line   5:  "CAM_GPIO0" "cam1_regulator" output active-high [used]
        line   6:  "CAM_GPIO1"       unused  output  active-high 
        line   7:         "NC"       unused   input  active-high

As you can see, some wonderful kernel developer has done a great job associating the various pins with their common names. One doesn't have to consult any datasheets or schematics to see that if I want to control GPIO21, all that is required is to fiddle with pin 21 of gpiochip0:

1
root@raspberrypi3-64:~# gpioset 0 21=1

If everything is wired up correctly, then your relay should now be active. The gpioset utility also understands chip names as well, so instead of saying "gpioset 0 21=1" you could also write "gpioset gpiochip0 21=1". To turn off the relay, run the same command setting "21=0".

When working with relays, make sure to keep the mains and the electronics sides of the board separate. On the electronics side hook up the other 5V pin from the RPi header (i.e. the 5V pin not being used to supply power to the RPi from the switching power supply) to the relay's "Vin" pin, ground from the RPi to the relay's "GND" pin, and hook up the RPi's GPIO 21 (pin 40) to the relay's "Sig" pin. On the mains side make sure you come directly from mains power into the relay's "Common" connector. Relays have 2 modes: if you want your microcontroller to turn something on when it activates the relay, then connect the load to the "NO" (normally open) side. If you want your load to turn off when the microcontroller activates the relay, then connect your load to the "NC" (normally closed) side. In my case I want the lights to come on when the microcontroller activates the relay, therefore I connect my load to the NO side.


HUGE NOTE: By convention: in the world of home electrical wiring in North America the black wire is the hot wire, the white is neutral, and the green is ground. However in the world of electronics the black wire is ground and the red wire is VIN (12V, 5V, 3V3, 1V8 etc). When looking at the following diagrams and pictures do not confuse the black, hot, home-wiring, AC wires with the black, ground, electronics, DC ground wires -- they are separate!




The conceptual drawing of the complete box in Fritzing looks as follows:




The completed box looks like (oops! looks like I forgot to remove the serial console cable before taking this pic!):


Before installation:

After installation:


No comments:

Post a Comment