Category: "Nano"

In this part we're going to expand the node to have some additional functionality.The node is going to get treated to a couple of motion sensors, a relay output and a switch.

I've added two three way screw down connectors, one for each motion sensor, also added a three way connector for the relay and a two pin connector for the switch.The switch could be a manual push button or a door contact etc.

First, because I've got space on the board I'm going to add a strip of 4 ground connections. I don't need them at this point but they are useful to have when testing

Next I'm going to add the three way connector for the relay. I'm going to drive a 5V relay as pictured below

The underside of the connector

And the topside of the relay connector

Ok, it's confession time. I was originally going to use a 12V relay, but couldn't get it to work as it would have required a driver transistor to reach its turn on voltage so some of the later pictures here may show the power of the relay connected to 12V, not the 5V as shown in the image below.

Next I'm going to add two three way screw down connectors, one for each of the motion sensors. I need to slightly enlarge the holes on the perf board to accomodate the pin diameter of the connectors. I have used two different colours for clarity

The movement sensors I'm using are inexpensive passive IR sensors, I run them at 5V and when tripped they give a 3.3V output. To ensure there are no spurious signals I want to fit a pull-down resistor to each of the movement sensor data pins. 10K will suffice. I prefer to have physical pull-up and pull-down resistors, but am aware that the Arduino can do this internally ( at least for the pull-ups ), so the choice is up to you.

Now I'm going to add a two way connector for a switch. This will use a pull-up resistor to keep the input high when the switch is not pressed. It is a momentary switch (doesn't stay latched ) so when the button is pushed the switch pin ( D2 ) will go low and when it is released D2 will go high again.

I've also added another connector for the PWM outputs, so the board now has male and female connectors for the PWM, just for convenience.

Now we're good to go with the hardware after double checking for short circuits. I use a current limiting bench power supply to run the node before connecting it to the computer, this allows me to limit the current to none damaging levels and is the result of not finding some shorts.

Let's move over to PiDome and set up the new hardware.

/* PiDome_Man Cave 1 Tutorial Code

Spiralux 2015

man cave is going to have 4 x pwm - code done 04 Dec 2015 will also have control over a 4 way power strip has a DHT11 temp/humidity Sensor and LDR The relay for the power strip has been added and the code done 11th Dec 2015 Added connections for a switch on digital pin 2 and connections for two IR movement sensors connected to A0 and A1. A0 and A1 have pull down 10K resistors. D2 has a 10K pull up resistor The analogue pins (A0 and A1 ) will be used as digital input pins. The switch input will go low on switch press Code for switch and both motion sensors has been added, with debouncing on the switch*/#define SN "Man Cave Tutorial Code"#define SV "1.4"

/* motion sensor states */bool sensor1CurrentState = 0; /* set the current state of the sensor to a known value */bool sensor1PreviousState = 0; /* and the previous state to the same as the current state */bool sensor2CurrentState = 0; bool sensor2PreviousState = 0;

In this part of the tutorial we're going to use the existing PWM devices (well three of them anyway) from the previous tutorial. We'll use pins 3,6 & 9 to drive an LED RGB strip.The strip has been cut to about 1 metre and is mounted inside a 16mm x 16mm aluminium channel. The opening on the channel is 13mm wide and I got some opal white acrylic sheet cut fit, locally I can get two strips (2440mm long x 13mm wide) for about AU$5.

We're going to connect the 12V supply to the +12V of the RGB strip. On my board the orange screw down tag strip nearest the radio provides easy access to the 12V. The Red connection from the RGB strip goes to FET1 drain which is driven by pin 3, the Green to FET2 (pin 6 ) and the Blue to FET3 ( pin 9).

We don't need to add any extra hardware to this board for this tutorial and I'm just going to clone the device skeleton we used earlier.

Here's the code. The RGB code is borrowed from redviper2100 and modified to store the last value locally as well as requesting the status of the panel from the gateway on boot.

/* PiDome_Man Cave 1 Tutorial Code

Spiralux 2015

man cave is going to have 4 x pwm will also have control over a 4 way power strip has a DHT11 temp/humidity Sensor*/#define SN "Man Cave Tutorial Code"#define SV "1.0"

We'll need to add the RGB strip to the device skeleton. I've cloned the skeleton from one of the previous tutorials, if the PWM's are already in the skeleton they will need to be deleted.

After the device skeleton has been cloned we'll need to edit it and add the RGB Child ID. I'm going to use a high number out of the range of the node ID's I'm currently using.

I've used a Child id of 101, this needs to be the same as the 'strip' in the Arduino code.

Add the colour picker

Save the cloned device

Cloned device visible in list

Add the colour picker to the dashboard

Now we should be cooking with gas. In the version of PiDome that I'm using there is an error generated everytime the colour is changed through the dashboard, I guess it will be fixed in the future. It doesn't seem to affect macros that change the colour, but it is visible in the video.

Below are some static images of some of the colours the strip produces

Ok, we'll get the node to do something useful in this part. The Nano I'm using has 6 PWM pins which can be used to drive such things as LED lights and fans. These pins are 3,5,6,9,10 & 11 but we can't use 11 as it is used by the radio immutably. The default MySensors setup also uses pin 6 for the radio but in this setup it is overridden to use pin 7 instead.

PWM works by varying the 'duty cycle' of the digital output and is an power efficient dimming DC lights such GU5.3 LED downlights. If you only want to drive a small number of LEDs then you can drive them directly from the PWM pins, but if you want to drive a more useful DC ( Direct Current ) load, such as the LED downlights, then you'll need something that can handle the power.

I'm using a FET to handle the load. The advantages and disadvantages of various types and applications for FET's are discussed at length elsewhere on the Internet. The intended load for the PWM's in this setup is less than 1 Amp at 12V which is well within the load capability without the need for heatsinking. Please check that you're operating your FETs within their specifications.

The 220Ω resistor between the gate and the PWM pin is to limit the current drawn by the FET. There is also another resistor between the gate and ground to ensure the FET is switched off when not being driven by the PWM pin. I've used 82KΩ as I couldn't find a higher value in my box of resistors.

As I'm using 4 PWM pins I need 4 sets of the above components.

WARNING: I am not responsible for you hurting yourself or others with your implementation of these tutorials. Check your wiring at least twice. If possible use a current limiting ( Bench ) power supply so your newly soldered creation doesn't emit the magic blue smoke that renders electronics useless.

I'm also going to add a small power supply ( LM2596S )to the board to supply 5V to the Arduino. The PSU will be run from 12V so we can tap the input to drive the LED. It should be noted that the PWM's in this circuit switch the ground side of the circuit and as such even when all the PWM's are switched to off there will still be 12V available for the LEDs so don't short it out when you are soldering.

OK, first step with my existing board. I've mounted all four PWMs and they'll share a common source rail. Apologies for the poor focus, but it's too late to redo now :). I've left space between the FETs to mount the resistors.

The next image shows the 220Ω resistors that are in series with the Gate and the PWM pins.

The next images show the bleed resistors ( between gate and source/ground 82KΩ ) and the wiring to the PWM pins ( 3,6,9 & 10)

Ok, let's have a look at the underside of the board. FET's 4,3 & 2 are fully visible, but FET 1 is being a little camera shy. Note the common ground rail running down the right hand side of the FETs and the breaks in the tracks.

The next image shows the 4 pin header added. One for each FET drain, the left hand one is connected to FET 1 and the right hand side one is connected to FET 4.

Let's have a look at the underside.

Now on to the power supply. I've adjusted the output to 5V using the little gold screw on the turnpot. I'm going to feed it with 12V which also be supplied to the PWM devices ( remember PWM is switching the ground side of the circuit not the positve side). This also take the 5V conversion away from the Arduino so it runs cooler. The power supply can be seen below.

Below you can see the mounting holes ( and track cuts around them ) for the power supply.

In the next shot you can see the power supply mounted with M3 bolts. I couldn't get the nut on the lower left hand one so I secured it with a blob of solder. The output is connected to the 5V pin and the nearest ground pin on the Arduino

Now I'll mount a three way screw down terminal block for the 12V output to the PWM devices. The underside can be seen along with the stripped wire that will be soldered along them to raise the current carrying capacity above that of the copper track. The other end of this is connected to the input of the power supply.

Once the 12V connection is soldered on all that remains is to hook up the ground to the FETs as can be seen below with the black cable running from the Arduino ground to the FETs common ground rail. Observe the build up of solder on the track to ensure greater current handling capacity.

Ok, now the hard work is done. Next I'll upload a simple sketch that will drive the PWM outputs up and down so we can test the outputs before proceeding to the PiDome integration.

/* for driving pwm on pins 3,6,9 & 10 so pwm outputs can be tested It drives PWM1 and PWM3 to the same values And drives PWM2 and PWM4 to the inverse of PWM1/3 eg. if PWM1 is 255 ( full ) PWM2 will be 0 ( off )*/

If you've got a current limiting power supply now would be the time to break it out, set its output voltage to 12V and limit the current to a low level. If you haven't got a current limiting power supply you could run the whole node of a 9V ( PP3 ) battery. A cheap zinc-carbon one will limit the current to about 400mA which may help reduce any damage caused by wiring faults.

Once we've done that we can hook up some loads and test the outputs. I'll hook up an Osram Superstar MR16 7.8W Warm White bulb as detailed Osram LED light. Note I am not endorsing the supplier, just linking to their description.

Each LED hooked up should vary in brightness.

Here's a video showing them in action. The middle of the three GU16 has a colour temperature of 6500K ( daylight ), the ones on either side are warm white ( 3000K ). The led strip is blue. You can see the current being drawn from the PSU.

Now we know the PWM outputs are all working we can upload the sketch to drive them from PiDome.

/* PiDome_Man Cave 1 Tutorial Code

Spiralux 2015

man cave is going to have 4 x pwm - code done 04 Dec 2015 will also have control over a 4 way power strip has a DHT11 temp/humidity Sensor and LDR*/#define SN "Man Cave Tutorial Code"#define SV "1.2"

Now let's move over to the PiDome control panel and modify the device skeleton we created earlier. We'll add the four PWM devices to the skeleton and then they'll be available to the dashboard after a reboot of the sensor node. You'll need to select Management | Devices | Custom devices and then click edit on the device skeleton we want to modify.

Drag the 'Add group' control below the last entry ( the light level entry ) and fill in the details as below. The 'Group id' is the same as the PWM Child ID (and PWM pin number ) set in the Arduino code.

Next we want to drag the 'Add slider' control onto the group we've just added and configure it as below. Note we're using the V_PERCENTAGE control id as the V_DIMMER has been deprecated as of MySensors V1.5. If you're using V1.4 you'll need to use V_DIMMER here and in the Arduino message processing code.

Save the changes and then we'll repeat the process for PWM2, PWM3 and PWM4

Now you should see the four PWM controls in the device skeleton.

Remember to click the 'Update device' or else you'll lose the settings.

Now, after you've updated the device we can set about adding the PWMs to the dashboard

Click Management | Dashboards and edit the dash you want the controls to appear on. Then click the 'Add item' button and configure the four PWM devices as shown below.

Now click the 'Save dashboard' button and go view the fruits of your labours.

The video below shows a demonstration of the 4 PWM controls working. The power supply shows the approximate current that is being drawn by the node and it peaks at just under 2.5A. Each of the GU16 lights draws about 0.7A at full brightness, so the LED strip is drawing around 0.4A at full brightness.

In the next part we'll modify the code to use the same hardware to drive an RGB LED strip light from PiDome.

Now I have a working device let's walk through the PiDome setup for the sensors. The sensors currently on the node are

Humidity Sensor (Child ID 0)

Temperature Sensor (Child ID 1)

LDR (Child ID 2)

The child ID's will be used in PiDome along with the MySensors type for the device ( V_HUM, V_TEMP and V_LIGHT_LEVEL )

First I need to set up a skeleton for the device by selecting Custom devices.

On the Custom devices page there is an option to 'Create a new device'. Clicking on that gives a window that allows configuration of our new device, such as which driver it's using, what sensors it has etc. The first part of the configuration is below

I've selected the MySensors driver in the first selection box, you can choose your own device name and device description. The 'Generated id' is automatic.

The 'Group id' needs to be the same as the CHILD_ID_HUM in the Arduino code, which in my case is 0. Choose a group name that is appropriate. I'm not 100% sure of the significance of the Group name at this point.

Next we'll add a data field by dragging the 'Add data field' selector onto the group that's just been created. It'll create a blank data field and we'll need to click on the spanner to edit it.

Once the spanner is clicked there is a long list of options. At this point we're not interested in most of them.

The 'Control id' is V_HUM, the same as MySensors refers to it, the data type produced by the Arduino code is a float so select that here. The type of graph is your own choice, I haven't really explored the differences yet, neither have I hidden a device. 'Remember last value' will keep showing the last received value for the humidity sensor even if the sensor is removed from the network. The only thing I have changed in the features area is the 'Representation' to Humidity.

Now we'll do the same thing with the temperature using a 'Group id' of 1 and the V_TEMP variable, so drag the 'Add Group' below the humidity group and name it as suits.

Notice the representation of the Temperature. Adjust to your requirements.

Now we can set up the LDR using the 'Group id' of 2 and V_LIGHT_LEVEL, so again drag the 'Add group' over below the temperature group.

Now we have set up the skeleton for the sensor in its current configuration. We can come back later and add more sensors to the node which will be available to all the devices using this skeleton. First though the skeleton needs to be saved.

Now we can see the new skeleton added under the custom devices

Now let's add an actual device based on this device skeleton.

Click the 'Add new device' button.

Then fill in the appropriate info. Before you can proceed past this point you'll have to have defined the floors and rooms in your layout, at least the floor and room in which this sensor is going to reside. This is where we'll use the node id set in the Arduino code ( 30 ), this goes in the 'Address box'. Select the MySensors driver in the drop down at the top of the form and the node location as required.

After clicking the 'Add device' button you should see your new device in the list like below

We're ready now to add the output of the temperature, humidty and light level on a dashboard. First we need to pick the dashboard to edit. I have setup a user just for this dash.

Here we have the test dashboard with a couple of sensors already added.

Click the 'Add item' button and select as below, choosing the device we have just added and humidity as the control to match against.

Click the 'Add item' button and then fill in the next page as below

I've chosen 'Gauge' as the display type, graph is another option you may prefer. I've set the upper limit of the humidity gauge to 100. Click the 'Add' button and we'll proceed to adding the temperature sensor.

Click the 'Add' button and fill in the next page

I've used 60 as the maximum temperature for the gauge, choose as suits your needs. Click the 'Add' button when done, then click the 'Add item' button again and we'll add the light sensor.

Almost done, click the 'Add item' button and fill in the details as below.

Right, now we're done we can save the dashboard and then see the results of the hard work.

Just before we get to the display it's worth looking at the messages being received by the gateway. In particular we are looking for messages from node 30. Give the node a quick reset by pushing the button on the Arduino so it can present the sensors to the gateway and then click on Drivers and select the MySensors gateway and you should see some messages from your node. Blowing on the temperature sensor will cause the output to change, but as the screen does not auto update you'll have to click on the MySensors driver again to see the new messages.

Once your happy your sensor is communicating then log on as the user assigned to the dashboard and you should be presented with something like below.

Looking good? Feeling pleased? You should be! Go grab a coffee and in the next part we'll add four PWM (Pulse Width Modulation) drivers suitable for driving low voltage LED strips or fans etc. We'll be using some FETs (Field Effect Transistors) to handle the load. This setup will be capable of driving four single colour LED light strips or 1 RGB LED and 1 single colour LED strips.

It's time now to have a look at the code for the sensor. Just to recap, I have built the node with a DHT11 temperature/humidity sensor and a simple LDR for measuring light level. I'm manually assigning the node ID, partly because I'm a control freak and partly because I've been using these nodes for a couple of years now and auto assignment of addresses was not something that worked reliably with different controllers. The code I'm running on the node is below. In the next part we'll walk through adding the node to PiDome and having it display the temperature, humidity and light level on the dashboard.

/* PiDome_Man Cave 1 Tutorial Code

Spiralux 2015

man cave is going to have 4 x pwm will also have control over a 4 way power strip has a DHT11 temp/humidity Sensor*/#define SN "Man Cave Tutorial Code"#define SV "1.0"