Light automation project – Part 1

So today I’ll talk about the final parts of my light automation project. This includes a combination of the light sensor, presence detection using Bluetooth and PIR sensor. I also use the schematics designed for switching 24V using a microcontroller.

Always protect yourself (ATmega328) against relays

I normally planned to finish this project, but some problems arrived. I also used a relay to switch some 240V lights. I never really thought about relays so I just connected them and placed a diode to protect my ATmega328. This failed totally, my ATmega328 is ruined (cannot write to it, random ports light up).

So I decided to buy a new one (not expensive) and create a new circuit that will protect my ATmega328. I used the same idea from my 24V transistor switch but used a BC547B transistor and calculated the new resistor value:

R_b = \frac{V_b * h_{FE(min)}}{5 * I_{c(max)}}

R_b = \frac{5V * 200}{5*0.03A} = 6666.667\Omega \approx 6k8\Omega

The result is that I now have a save way to switch a relay without blowing up my micro controller. (Finding a 5V relay is quite hard in Multisim, apparently for such an easy component I need to pay extra…)

The fixed circuit for switching a relay

Building a wooden enclosure

I decided to use a breadboard instead of a PCB. The main advantage is that I can change my breadboard over time. I also don’t need to wait on my PCB or pay a lot of money for it. I used wood to build an enclosure for the breadboard and added DC jack connectors so I can plug-in my lights and sensors.

I think it turned out quite well. I only had a normal saw and some glue (and a drill of course). I also rounded the corners (I hope apple wont sue me now), to give it a smooth edge. It’s 21cmx10cm for people who are wondering.

The state machine that does all the work

For software I decided to use a state machine. They are quite common and very easy to implement (switch statement anyone?). There are libraries out there and very advance stuff but I kept it basic.

There are 4 different states, one special and 3 normal states:

  1. Idle state: We start in this state, the Arduino will check the light sensor every 5 minutes, if the value of the sensor drops below a threshold we advanced to the next state: the dark state
  2. Dark state: We always check the PIR sensor in the dark state, it’s now dark enough to switch the light on. We also check for Bluetooth devices every 5 minutes. If we find a device or the PIR sensor gets triggered we advance to the light state.
  3. Light state: We switch the light on and keep checking if it’s dark enough and if a Bluetooth device is active.
  4. Remote state: In this state we do nothing, only the commands send from a web interface are executed.
The Bluetooth search for devices doesn’t always turn out well. Sometimes no device can be found when clearly my cell phone is next to the dongle. To solve this we assume that we need 4 concurrent errors (or no Bluetooth devices found) before we switch the light off. If a device can be found then we reset the error counter.
This also means that, if your Bluetooth search has an interval of 5 minutes, it will take 20 minutes (4 concurrent errors) before the lights will turn off after a Bluetooth device is turned off. I’ll release the source code next time, there are still some little errors and problems in the code.

The web interface

I also added a web interface and added new functions in the ArduinoPi 1.5 library. First I’ve added 3 functions to the ArduinoPi:

  1. toggleRemoteLight: This function will enable the remote controls (sending the ATmega328 in the remote state).
  2. requestParLight: The function will request all the parameters that can be edited and display them in the web interface.
  3. configParLight: Send the new parameters back to the ATmega328

On the ATmega328 side I also added the new command’s. The result is the following web interface.

The web interface made in bootstrap

Next time I’ll post my code and talk about implementing a relay so it can switch 230V safely. Hopefully I wont blow up another ATmega328

Power dissipation from 24V to 5V switching regulator

Let’s go back to electronics. I’m planning to power my complete setup using a 3A 24V power supply. This means I have to lower the voltage from 24V to 5V for the AVR chip using a switching regulator.

I started with buying a Atmega328 AVR kit (it includes a 16MHz crystal, capacitors and a linear voltage regulator) and just followed the Standalone bread board tutorial on Arduino. It’s a great tutorial but one point of advice, instead of taking pictures of everything why not use schematics? If somebody created a simple schematic I figured it out in 5 minutes without reading all the steps.

Power dissipation problems with linear voltage regulators

We use a 7805 linear voltage regulator, reading from the specifications it’s possible to connect a 24V supply to it and the voltage regulator will output 5V. One question where did the 19V go to? Let’s figure that out with a little calculation, the Atmega328 has a load of 150mA (assuming we connected some sensors).

We can calculate the power the 7805 needs to dissipate:

P = U*I = (24V-5V) * 0.15 =2.85W

The thermal conductivity from the die to the case (called Rjc) is 5 C/W and the thermal conductivity from the case to air (called Rja) is 65 C/W. We now can calculate the temperature difference between the die and the tab:

T_{die} = P * Rjc = 2.85W * 5C/W=14.25C

Without a heatsink we can calculate how much warmer the package temperature will be compared with the air surrounding it:

T_{air} = P * Rja = 2.85W * 65C/W =182.255C

That means the package temperature will be 182.3 °C warmer then the air surrounding it. The device will thermally shutdown at 125 °C. It’s possible to add a heat sink to dissipate the 2.85W so the device wont thermally shut down.

Switching regulator for power dissipation problems

The 150mA is an overestimate but displays the problem. One solution is to cascade voltage regulators. This means adding a 12V voltage regulator before the 5V, but the same problem appears. The 2.85W will now be dissipated over an 7805 (1.05W) and a 7812 (1.8W), so they both need a heat sink.

Another possibility is using a switching regulator. Unlike the linear regulator the switching regulator has a high-efficiency. Depending on the circuit, manufacturer or random internet guy, the efficiency lies between 80%-98%. A linear voltage regulator lowers the voltage by dissipating heat (think variable resistor) and has an efficiency of a merely 50%.

A switching regulator has a switch that switches (clever I know). The switch goes on and off at a fixed rate usually between 50kHz-100kHz. It also needs an extra circuit with a coil, capacitor and diode. More information about the operation.

So now I’m waiting on my parts, in the mean time I started merging the code from my little research and steps to form a light automation code that will switch my light depending on external variables. More on that next time!

Motion detection using Motion

Last time I talked about using Bluetooth presence detection and using a PIR sensor. Today I’ll talk about the last way to detect somebody in a room, now using a webcam and Motion. The Arduino can’t handle a webcam so I attached my webcam to the Raspberry Pi.

Before using Motion

Before using motion, make sure that your USB webcam is recognized by the Raspberry Pi. Simply run the following command.

And as you can see my Microsoft LifeCam VX-1000 gets picked up quit easily (didn’t had to do anything). Then its time to install motion. Simple run apt-get install motion and everything is installed.

One note, I ran apt-update & apt-upgrade and I updated to the latest firmware, great but my internet connection was gone. Luckily the developers decided to include the most common wireless drivers so everything worked out.

Configuring Motion

Configuring Motion is more like playing with the settings a bit, just stick with the lowest resolution (320×240) and a frame rate of 2 frames/sec. A higher frame rate may be possible, your Raspberry Pi will likely crash or mess up the frames so you’ll notice your frame rate was to high or you changed something you shouldn’t.

I started by creating 2 folders in my home folder, one named motion for the resulting video/pictures and webcam that holds my specific webcam configurations. In the webcam folder I created a file cam0.conf with the following:

Then here is my motion configuration file, I’ve changed a bit and disabled a lot of options. I only requested pictures when there was motion detection (meaning more than 1500 pixels changed) and didn’t need any video, database integration or remote control/streaming. This is a bare bone configuration.

Now the important part is the on_event_start option. You can select a program/script that needs to be executed when motion is detected. For example I created a simple PHP script that will save the date and time in a log file. This is my motion.php (you can of course change the path to something else).

Results of using Motion

I spent a day changing the commands, trying out stuff and looking that the results. I never did get the color working and everything is in black and gray with sometimes a flare of color (really strange).

The resulting log file from my PHP script shows that it logged 3 times.

Conclusion of presence detection

I’ve tested three ways to detect if somebody is in a room or not: Bluetooth, PIR sensor and Webcam (using Motion). A conclusion:

  • Bluetooth: Easy detection that only works if somebody has their Bluetooth on of course. It’s quite reliable and you don’t need to pair the device in order for it to work. A disadvantage is that scanning is slow and takes some time. Also if I’m sitting in the next room it also detects my “presence” because I’m still in range.
  • PIR Sensor: Fairly easy and accurate. It’s small, works without a delay and easy to program. A big disadvantage is that if a person is present in the room but not moving (like when you watch a movie) then there is no detection. A PIR sensor is great for triggering the light when you enter a room.
  • Webcam: Provides the user with a lot of options for detection motion. Provides images/movies of the detected motion. A disadvantage is that it’s a lot of overhead for detecting if somebody is in the room. In a dark room the user of IR LEDs might come in handy.

For my light Automation project I decided to use the Bluetooth detection to see if somebody is still in a room after a while (and check this every 5 minutes) and the PIR sensor for detecting when somebody enters a room, so they don’t have to wait till the Bluetooth detection kicks in.