Tuesday, July 16, 2013

Deer Defense System: PIR motion sensor

Today I will provide information necessary to build a Deer Defense System (DDS). The specific plans I will be using were leaked from an undisclosed source at the DoD and are highly classified. Continue reading at your own risk.

This summer has been a unique adventure at the family garden. First, for whatever reason our squash have grown huge this year. Second (and more relevant), the deer have been particularly aggressive and determined.

For those of you that may not know, in certain areas of the Southern United States white-tailed deer (Bambi) are quite prolific. What may be less known is that these deer love to eat bean plants and will tolerate eating squash, tomatoes, cucumbers, and anything else you don't particularly want eaten. I can vouch for this from experience. Feast your eyes on the destruction.

Well we explored several more traditional methods of repelling deer. We tried all sorts of smelly concoctions. Some worked until it rained. Planting garlic did not help. Marigolds and mothballs did not help. The .30-06 did not work (We didn't try. Deer are out of season. Babies). They sell motion activated sprinklers that would probably work, but they are $50 a piece. No dice.

Having too much free time, I set about making a motion activated deer scaring device (these plans weren't really stolen from the DoD). Here is an overview.

At the heart of the matter is a Pyroelectric InfraRed  sensor. Mine was purchased from Ebay for $1.98. It does work, but be warned, when I asked for the datasheet they said it was a "trade secret." Right. Several other vendors carry them. For details on how to use a PIR sensor see THIS link. Basically, a pin goes high when it senses motion.


 My sensor had several adjustments on it. I found the picture on the left that tells what everything does. By wiring up an LED as discussed in the link above, I then found the settings that worked best for me.

After getting my motion sensor working I had to decide what brains to put in it. I considered another ATtiny45 , but decided to try something simpler first. I just hooked a transistor to the output of the sensor and used it to turn on a bright LED and sound a buzzer. 


Now the biggest concern I have here is the power consumption. I am powering the entire thing on 3 AA batteries. I read online that most of these PIR sensors use about 1.2 mA while on idle. I need a way to shut it down during the day (deer eat gardens after dark). Basically I want a nightlight circuit. 

I had a photo-resistor, but unfortunately the only transistors I had were NPN. This led me to use THESE plans to turn on the power to the PIR sensor only when it is dark outside. When it is light, it supposedly uses in the realm of .1 mA. With this addition, the DDS should last a month or so depending on how often the alarm goes off. Below is a rough diagram of what I built. The circuitry on the left turns on the power at night. The stuff on the right scares the deer.
The switch is the PIR sensor
I soldered everything together and hot glued it into a nice watertight container. Now it is ready to go. I don't know if it will actually scare off the dear or not. I suspect it will not do too much. Regardless, it was a fun project. I hope you enjoyed it!

-Matthew 




Saturday, July 13, 2013

Charlieplexing on an ATtiny85: 3 pins, 6 LEDs

While exploring the world of ATtinys and Arduinos I came to a problem. I needed to control 4 LEDs with only 3 pins. Well this is a problem. I scurried around trying to conceive a clever way to borrow a pin from another application to no avail.

Having previously encountered multiplexing, I turned to that. There I found my solution. Charlieplexing. According to Wikipedia, Charlieplexing uses the tri-state logic capabilities of microcontrollers to control gobs of LEDs with a few pins. The details of Charlieplexing are a bit confusing. I don't feel fully qualified to explain them, so here are the important things to know.

1) An Arduino pin can be in 3 modes: OUTPUT: HIGH, OUTPUT: LOW, or INPUT. When a pin is set as an input, it is put in a high-impedance state. Outputs are in a low-impedance state. If you have no idea what I'm talking about, see THIS link.

2) LEDs (Light Emitting Diodes) only work in one direction. If you did not know this you may have at some
point wondered why your LED was not working. Don't worry. Ignorance isn't a crime...that I know of.

3) Your Arduino (even your ATtiny) can turn LEDs on and off many more times a second than your eye can see. Think how a video shot at 30 frames per second (NTSC TV) looks. That's 30 Hz. A "normal" Arduino runs at 16 MHz (16000000 Hz). Now granted you can't turn an LED on and off that fast, but you get my point. I will be running the ATtiny at 8MHz. This is the fastest you can run it without external hardware.

All this combined means that I can control 6 LEDs with only 3 pins. Great! Next I searched for a library to take care of all the heavy lifting. Introducing the Charlieplex Library. This library takes care of all the tedious stuff to let us worry about what we want the LEDs to do at the Macro level.

I'm not going to go into the details of how to use the library because the Arduino playground page does a pretty good job. However, for those that are interested HERE is my code. It is a basic modification of the SimpleCharlie example to control 6 LEDs.

To the right is a schematic for how to wire up the LEDs. There are also resources available on Google to help you do that. HERE is one resource on how to draw your own schematic. Video would be rather anticlimactic, and I have already disassembled it. The schematic will have to do.


Hope this works for you. 
-Matthew

Thursday, June 13, 2013

Serial Communication on a ATtiny85 with the SoftwareSerial Library.

"Serial is giving me errors on my ATtiny! What can I do?" We have a solution.

Serial communication is not difficult on an ATtiny thanks to the SoftwareSerial Library. While the ATtiny85 does not have the hardware of a "real" Arduino, it can still function in similar fashion. If you're just getting started with using an ATtiny, here are some resources you might need.

  • Information on programming them from High-Low Tech. You can also look back at my previous posts.
  • My nifty programming adapter. A few minutes of soldering made my life much easier.
  • My USBtinyISP is discussed in THIS post. It also makes life easier.
  • Previous posts on Running Servos and the NewPing Library on an ATtiny.
  • THIS previous post mentioning my USB to UART converter cable
Now we are ready to go. First things first, I am using Arduino 1.0.4. The SoftwareSerial Library is included as a default library, so there is no reason to get a 3rd party library.


Step One: Wire it up. With my USB to UART cable it is as follows.
  • Black: Ground
  • Green: Tx (using Tx as pin 4)
  • White: Rx (using Rx as pin 3)
  • Red: 5v (this is optional if you have an external power supply)
Connect an LED with appropriate resistor to pin 1.

Step Two: Ensure that your ATtiny is burned to run at 8MHz. Now load THIS sketch onto the ATtiny. Note that parseInt() works with the SoftwareSerial Library.

Step Three: Open the Serial Monitor and set it to the correct Baud rate. To reset the ATtiny, bridge the reset pin to ground momentarily. When you see the connected message, enter an integer and count the flashes.

That's all there is to it. I you don't have a USB UART cable, this is easily adaptable to communication with another Arduino with a USB port. See THIS post for code. If you want to see a practical application of serial communication on an ATtiny85, check out my serial sonar controller HERE.

Hope this works for you all. As usual, if there are any questions, just let me know.
-Matthew