*Edited 20-12-2013 to update Arduino code and de
I finally managed to get on with my next Arduino project, and as its Christmas, why not do something with Christmas lights, an arduino, some PIR sensors and relay board. My idea was to use some little xmas trees I bought a few years earlier and do something a little smarter with them. Out of the box they are a chain of 7 1-foot high trees with various lightbulbs on them all powered from an AC power supply (low voltage). Nothing amazing, they didn't even flash originally, so the plan was to do this:
* Disconnect each tree, so that they could be controlled independantly
* Attach a PIR (Passive Infra-Red [motion sensor]) to each tree
* When the tree senses movement, light it up
* Run in a sequence to flash all the trees
So, first job, was to cut the tree cables down so that I had a single common wire running to each, I could then wire a cable to each one to run as the 'trigger' cable. The small trees look like this:
So I cut each of the cables and remade them with new cable runs, tested them and I could light up each tree individually.
Next job was to start with the PIR sensors. These are ones I picked up from eBay and were only around 1-2 uk pounds each, so pretty cheap really.
As you can see there are 3 pins for connection, +ve (5 volts), ground and trigger. When powered and operating they will send high the trigger to +ve to indicate the unit has sensed motion. The sensors also had two pot's on them to control the TX time and the distance control. I didn't get a lot of difference playing with either to be honest but that didn't matter, the defaults seemed to do the trick. I powered them up as a test and found that movement in front of them triggered the output for around a second then when motion stopped it removed the trigger. All looked good, but I had to waterproof them. First attempt and the cheap solution was to wrap them in freezer bags, not sure what this did to the PIR sensitivity I covered on and re-tested, no real difference to the triggering so this was ideal. I wrapped each one in the bags and tied them shut with freezer ties and wound the bag around so they were pretty snug in there.
There you can see the bagged up PIR, and also it nestling inside the tree, I opted to put them low down and try to bury them inside the tree a little, this hid them as much as I could from view and let me put the other tree branches up over them so the general view wasn't the rather unsightly bag and PIR sensor itself.
Next up, to control them I needed a relay board. This is because I was going to be controlling AC and an unknown current (Yes, I could have tested, but to be honest I knew it would be within the ratings of the relays, but way too high for an Arduino output, and I wanted isolation). So again I turned to eBay and found relay boards that you could get for around 5 ukp, and these were optically isolated too, so even better keeping the Arduino isolated and safe so it wasn't going to stress the outputs. Below is an image of the relay board as I was starting to wire it up.
The pins along the left of the board were labelled as ground, 1-8 and +ve. The relays are triggered by 0/LOW from the Arduino, initially I wired the relays up to be powered by the Arduino +5v rails and used the digital outputs as a test. This all appeared to work correctly, setting the digital pins to OUTPUT and when driven LOW the relay clicked and engaged. The relays are single-pole changeover so the outputs can be triggered on either throw of the relay. I wanted to use mine when the relay was engaged/energised the output to the tree would be on. The only thing that started to appear was as I did some test Arduino sketches that sometimes everything would hang and just stop dead, the arduino wouldn't continue its loop and the relays would be left on whatever sequence they were in. After some playing around I made an assumption. The 5v draw from the relays was proving too much, or too electrically noisy to be driven from the Arduino itself, so I separated it. I got another 5v supply, connected its ground to the GROUND on the relay board, and the +5v to the +ve on the relay board. I had to also tie the +ve to the Arduino 5v pin to keep it common but that worked great, the Arduino now only gave signals to the relay board and the other 5v supply powered the relay board itself.
Next was to start wiring the Arduino Uno up. I have the digital pins 0-13 available, so since I need 7 outputs I chose pins 6-12. Setting them in the code to OUTPUT pins and setting them to HIGH initally (Remember the relay board uses LOW to trigger!)
As you can see, I used a small breadboard to use as breakouts for a few of the connections, this was just to make the links to the tree lights themselves and common 5v rails easier to wire up.
For input I used the 5 analogue input ports but used them in a digital way, as all I was detecting was +ve or not that was simple. Referring to them in the code as pins 14-18 as they were being used as digital input pins.
Also you'll probably notice a tri-colour LED on the breadboard, another initial idea (since I already had 5 of them) was to put these on top of each tree and let it cycle through colours, in the end I gave up on that idea as I needed more output pins, and more importantly a lot more wires and the wire I was using already looked a little untidy/unsightly, so I gave up on that idea.
The code for the Arduino too quite a few attempts to get it right, mainly because I needed a few different sequences and logic to make it work right. The completed code is shown below, but it's not very neat, any modifications to it would be welcome! But the general program works as expected, so it will sit in a sequence flashing each tree lights in a sequence until one of the PIRs sense movement. When they do, they will turn off all trees except the ones sensing movement, so you can 'walk' up past the trees and they will light sequencially, you can also jump to particular trees and they will light in any order too!
The biggest bug/drawback that I've found is the PIR sensing component. since the PIRs aren't very precise sometimes they detect movement when none happens (I'm guessing this is because they're outside and picking up all sorts of stray IR activity or heat signatures), but also their field of vision is difficult to control, so knowing where they will detect movement is a bit of trial and error. But they generally work!
Merry Christmas everyone!
Arduino code below:
(Moved code to pastebin) : pastebin.com/FJ2qP74m
Problems: 20-12-2013
I hit a few problems after setting it all up and letting it run a while. The biggest problem I'm having is the Arduino locking up and just stopping where it is in the code. The result are the lights getting stuck at whatever point they were at. I've tried to work around this in various ways. Firstly I thought it was the PSU I was using for the Arduino as it was a USB phone charger and I was feeding the Arduino over the USB port. I've swapped this to use a 9v PSU into the barrel jack so its got a 'proper' power supply and this made no immediate difference. I suspect this is due to EMI (Electro-magnetic-interference) from the relays and switching the low-voltage AC for the lights, so trying to re-route cables and move the relays away from the Arduino has helped but not enough. Finally I've put in a watchdog timer/code into my software so that at least if the Arduino locks up, it'll reboot itself and reset the sequence. I've not managed to figure this out much further other than it being an utter annoyance as its when I want to show the system off to somebody! I still suspect its due to EMI and need to try and protect things a little better, perhaps I've not isolated enough of the relay board from the Arduino output pins, but open to suggestions please folks!
No comments:
Post a Comment
Note: only a member of this blog may post a comment.