5.3 The Perch Sensor

Bird feeders come in different shapes and sizes. I opted to go with a low-tech solution for determining when a bird lands on the feeder perch. While it is certainly possible to construct an elaborate pressure switch mechanism, the time and expense required to implement it seems like a lot of work just to detect when something grips the perch. Instead, we can monitor fluctuations in electrical capacitance.

Cover the feeder perch with aluminum foil, attach a wire from the foil to a resistor connected to the digital pins on the Arduino. By measuring baseline values and fluctuations detected when a bird lands on this sensor, we can establish a threshold value to determine when a landing message should be transmitted.

Building the Sensor

Building and testing the perch sensor is the easiest part of this project. Take a piece of aluminum foil, flatten it to half the size of a gum stick wrapper, and wrap it across the bird perch. Then take a 10M ohm resistor and insert one end into the Arduino’s digital pin 7 and the other end in digital pin 10. Then connect a wire from the foil to the resistor lead that is connected to the Arduino’s digital pin 7. For the wiring diagram, refer to Figure 12, Wiring up a capacitive sensor.

/epubstore/R/M-Riley/Programming-your-home//images/capsensor.png

Figure 12. Wiring up a capacitive sensor

Programming the Sensor

Connect the Arduino to your computer and fire up the Arduino IDE to write the sensing code. Like the Water Level Notifier project, we will write code that we will use to do the following:

  1. Display the capacitive sensor’s electrical values to the Arduino IDE’s serial monitor window.

  2. Identify the baseline electrical value of the sensor.

  3. Alter the flow of current when placing a finger on the sensor.

  4. Record the new value to use for our alert trigger condition.

In order to more easily and programmatically detect the electrical changes that occur when something like a finger or a bird lands on the foil, we will call upon the help of Arduino enthusiast Paul Badger. Paul wrote an Arduino library that makes measuring changes in capacitive sensors like the foil one we constructed for this project a breeze. Called the Capacitive Sensing library,[46] the library gives Arduino programmers the ability to turn two or more Arduino pins into a capacitive sensor that can be used to sense the electrical capacitance of a body. A human body is considerably larger than a bird and will therefore create a much larger deflected value. Nevertheless, a bird also has measurable electrical capacitance, and that is the threshold value we will attempt to refine in our program.

Download this library, uncompress its contents, and copy it into your Arduino’slibraries folder. For more details, refer to Appendix 1, Installing Arduino Libraries.

Create a new Arduino project and use the # include CapSense.h;.

Due to the difference in size and surface area touching the foil, a bird will have a very different value than that of a person. If possible, measure the value difference with a bird. Fortunately my kids have pet parakeets, and these birds were all too eager to be test subjects in exchange for the seed supplied by the feeder. My test measurements concluded that the baseline value varied between 900 and 1400, and the bird’s capacitance increased that value to more than 1500. Using these values, we can use the same type of conditional code from the Water Level Notifier project to raise and reset the bird landing and departure notifications.

We will write the code that will load the CapSense library and capture and display the capacitive values to the serial monitor window.

TweetingBirdFeeder/BirdPerchTest.pde
  ​#include <CapSense.h>​
  ​​
  ​#define ON_PERCH 1500​
  ​#define CAP_SENSE 30​
  ​#define ONBOARD_LED 13​
  ​​
  ​CapSense foil_sensor = CapSense(10,7); // capacitive sensor
  // resistor bridging digital pins 10 and 7,
  // wire attached to pin 7 side of resistor
  int perch_value = 0;​
  ​byte perch_state = 0;​
  ​​
  void setup()​
  ​{​
  // for serial window debugging
  ​ Serial.begin(9600);​
  ​​
  // set pin for onboard led
  ​ pinMode(ONBOARD_LED, OUTPUT);​
  ​}​
  ​​
  void SendPerchAlert(int perch_value, int perch_state)​
  ​{​
  ​ digitalWrite(ONBOARD_LED, perch_state ? HIGH : LOW);​
  if (perch_state)​
  ​ Serial.print("Perch arrival event, perch_value=");​
  else
  ​ Serial.print("Perch departure event, perch_value=");​
  ​ Serial.println(perch_value);​
  ​}​
  ​​
  void loop() {​
  // wait a second each loop iteration
  ​ delay(1000);​
  ​​
  // poll foil perch value
  ​ perch_value = foil_sensor.capSense(CAP_SENSE);​
  ​​
  switch (perch_state)​
  ​ {​
  case 0: // no bird currently on the perch
  if (perch_value >= ON_PERCH)​
  ​ {​
  ​ perch_state = 1;​
  ​ SendPerchAlert(perch_value, perch_state);​
  ​ }​
  break;​
  ​​
  case 1: // bird currently on the perch
  if (perch_value < ON_PERCH)​
  ​ {​
  ​ perch_state = 0;​
  ​ SendPerchAlert(perch_value, perch_state);​
  ​ }​
  break;​
  ​ }​
  ​}​

Note the defined ON_PERCH value of 1500 to compare against the recorded perch_value. Due to variations in the conductivity and surface area of your foil sensor, you may need to tweak the ON_PERCH threshold value just like you did for the Water Level Notifier project so that it works best for your configuration. Also note the value of 30 assigned to the CAP_SENSE constant. This is the number of samples to poll during the capacitive measurement cycle.

Now that we have a working bird perch sensor, we need a way for the feeder to alert us when it is running low on seed. How will we do this? A photocell can help.

Programming Your Home
cover.xhtml
f_0000.html
f_0001.html
f_0002.html
f_0003.html
f_0004.html
f_0005.html
f_0006.html
f_0007.html
f_0008.html
f_0009.html
f_0010.html
f_0011.html
f_0012.html
f_0013.html
f_0014.html
f_0015.html
f_0016.html
f_0017.html
f_0018.html
f_0019.html
f_0020.html
f_0021.html
f_0022.html
f_0023.html
f_0024.html
f_0025.html
f_0026.html
f_0027.html
f_0028.html
f_0029.html
f_0030.html
f_0031.html
f_0032.html
f_0033.html
f_0034.html
f_0035.html
f_0036.html
f_0037.html
f_0038.html
f_0039.html
f_0040.html
f_0041.html
f_0042.html
f_0043.html
f_0044.html
f_0045.html
f_0046.html
f_0047.html
f_0048.html
f_0049.html
f_0050.html
f_0051.html
f_0052.html
f_0053.html
f_0054.html
f_0055.html
f_0056.html
f_0057.html
f_0058.html
f_0059.html
f_0060.html
f_0061.html
f_0062.html
f_0063.html
f_0064.html
f_0065.html
f_0066.html
f_0067.html
f_0068.html
f_0069.html
f_0070.html
f_0071.html
f_0072.html
f_0073.html
f_0074.html
f_0075.html
f_0076.html
f_0077.html
f_0078.html
f_0079.html
f_0080.html
f_0081.html
f_0082.html
f_0083.html
f_0084.html
f_0085.html
f_0086.html
f_0087.html
f_0088.html
f_0089.html
f_0090.html
f_0091.html
f_0092.html
f_0093.html
f_0094.html
f_0095.html
f_0096.html
f_0097.html
f_0098.html
f_0099.html
f_0100.html
f_0101.html
f_0102.html
f_0103.html
f_0104.html
f_0105.html
f_0106.html
f_0107.html
f_0108.html
f_0109.html
f_0110.html
f_0111.html
f_0112.html
f_0113.html
f_0114.html
f_0115.html
f_0116.html
f_0117.html
f_0118.html
f_0119.html
f_0120.html
f_0121.html
f_0122.html
f_0123.html
f_0124.html
f_0125.html