Connecting a 12v 8 Channel Relay to an External Power Supply and RaspberryPi

…So that you can control devices via TerrariumPI

Stuart Brown
8 min readJan 19, 2019

Apologies for the SEO-optimised H1 on this post but I had a lot of trouble trying to find out how to go about this so hopefully this post will help a few other newbies out!

In previous posts I’ve gone over getting the TerrariumPI set up on a RaspberryPi 3 and then adding a temperature and humidity sensor to the RPi. In this post I’m going to cover how to connect a device (in this case a lamp) to the RaspberryPI, and have TerrariumPI turn it on or off depending on the values from the sensor.

In order to be able to do this you will need a RPi with TerrariumPI set up (obviously!) and have a sensor connected. In addition you will need:

  1. A relay (this post is covering a 12v 8 Channel Relay)
  2. An external 12v power supply. Most of the guides I read used a 5v relay which means that you can draw power from your RPi. If you have a 5v relay there are lots of guides on YouTube / tutorial sites which show which GPIO pins you need to connect to your relay.
  3. Some jumper cables. For my board I needed female to male cables, you may need different depending on the board you have.
  4. Something to control via the relay. I chose a desk light for demo purposes.

Connecting the external power supply to the relay

I’m sure I don’t really have to say this, but PLEASE MAKE SURE YOUR POWER SUPPLY IS NOT PLUGGED IN WHILE YOU STRIP THE WIRES ETC!!

Fortunately I tend to keep a load of random plugs, cables (anyone need any SCART cables?) etc so I was able to go into the loft and find an old 12v power supply that I think used to power a Netgear router. Snip off the end jack, remove about 20mm of the isolation on the wires inside and twist the copper wire inside.

On power supply cables like this you normally see one wire with a white dashed line and one without. Typically the wire with the dashed line is the positive and the one without is the ground.

On my relay the power and ground connection were marked DC+ and DC-(unlike most of the other relays which I think were marked JD-VCC and ground) which meant that the DC+ took the positive and the DC- took the ground.

Connecting a lamp to the relay

OK, so now you have a way of getting power in to the relay so now you need to connect something to receive that power.

I chose a desk lamp and took off the outer insulation about halfway between the lamp and the plug, snipped the live wire and stripped either end (leaving the neutral untouched)

I attached one end of the live to the central COM port on the relay and the other to the NO (normally open) port on the left. Initially I had it connected to the NC (normally closed) which caused me much head-scratching later until I was corrected by one of the friendly people on the TerrariumPI Github Issue queue.

Connecting the relay to the RaspberryPi

Once again Pinout is the resource you need here. I connected the DC- (ground) on the relay module to GPIO physical pin 39 on my RPi and then connected IN1 on the relay to GPIO physical pin 37 on my RPi.

Once you are sure that you are correctly and safely wired up you can plug in the external power supply to the relay module.

Testing the RaspberryPi and Relay connection

The video below and this Python script are really useful to test that you have everything connected properly, if you do the LEDs on the board should light up in turn and you should hear a clicking sound from each relay.

Adding the relay as a switch in TerrariumPI

Phew, we got to the whole point of this post! OK, so head to your TerrariumPI at http://raspberrypi:8090 (unless you have changed the port settings for some reason) and navigate to Switches > settings and click the blue + button.

In the form I entered the below details

The address for GPIO connected devices is the physical pin number If you followed what I have done then yours will be 37 as well. If you now head back to the status submenu item you should something like the below

clicking on the on/off icon will, well, you can probably guess! If clicking to turn the switch off for some reason turns your light on you can set the hardware field in the switch settings above to GPIO Inverse and it should behave normally.

You can see mine working below

Controlling your device via a sensor

Now that we have everything connected so that we can control the light manually it’s time to turn to automating its state based on sensor readings. There are two parts to this 1) the alarm min and max values that you set on the sensor 2) the configuration that you will need to do in the environments section.

Alarm min and max settings

In Sensors>settings menu you should see something like the below.

My use case here is to turn the light on when the temperature falls below a certain temperature (the value you set in Alarm min) and turn it off when it exceeds a certain temperature (the value you set in Alarm max).

Environments configuration

Navigate to the page at System > Environment and locate the Temperature box (it was at the top of the page for me).

In the form fields:

  1. Set Mode to Sensor
  2. Click in sensors field and choose the one that you want the switch to respond to.
  3. Set your Day/night difference and Day/night source settings. These weren’t very important to me for this test so I set arbitrary values.

Now, because my use case is to turn the lights on when the temperature falls below a defined point I entered values into the Low Alarm fields. If my use case was to turn on some device when the temperature exceeded a defined point (for example turn a cooler on when the temperature exceeded 100 degrees) I would have chosen the High Alarm fields. Don’t set values in both areas, it will make the switch behave very strangely (trust me ;-) ).

Because we have chosen for our device to be controlled by sensor values rather than a timer the first four fields are not editable. You can see the values I chose below

The Power on field says it `Holds the amount of seconds that this environment part is powered on when the value is out of range.` I’m not entirely sure what this means, but I assume that it is a delay to the light going on or off when the temperature reaches its min or max value. I currently have no need for this so I set it to 0.

Update from project maintainer theyosh — re “Holds the amount of seconds that this environment part is powered on when the value is out of range: This is now only used for spraying. If this value is higher then zero, it will define the power on duration when switched on. For spraying I have a single spray time of 20 seconds. And then turn of. This way, you can trigger short power bursts. For heating and lighting it is not usable, and should enter zero to disable this behavior.”

The Settle timeout field is for `How much time must there be between two actions and after start up. Enter the amount of seconds in which this environment part can settle.` Again I’m not 100% on what this means but I think it’s to stop lights (or other devices) toggling on and off quickly as temperature rises and falls. Again I set an arbitrary value here

Update from project maintainer theyosh — re “How much time must there be between two actions and after start up. Enter the amount of seconds in which this environment part can settle.: This is used for spraying, heating and cooling. This means that there must be at least X seconds between two actions. And this is only handy for dimmers. When the temperature is to low, the environment will dimm less and therefore more power to the heating system. This will heatup the temperature. But the temp changing could take 2 minutes, and the environment will change the dimmer value every 15 seconds. So in the end, the heater will get to warm.

A second example with spraying is that after spraying the humidity will rise slowly. So I said wait 2 minutes before a second spray action. And mostly the humidity is within the valid range and the sprayer is not needed anymore.”

Once you’ve done all this head back to the Home > Dashboard page and you should see the Environments panel on the right hand side has been populated with the values you set

Now, if you put the temperature sensor somewhere where the temperature is below the Alarm min value the light should turn on. Then once the temperature gets above the Alarm max value it should turn off.

That’s it! We’ve done it! Ultimately I would like to get to a point where I can dim the power going to a device (for example my Arcadia Deep Heat) to replicate a dimming thermostat so that a device never really turns fully on or off it just dims to keep within range. This is better for the Deep Heat but I need to do some more reading to see if it is possible.

--

--