On any typical morning in the Bigari household, you’ll hear the following phrase being yelled out:

“HAS THE DOG BEEN FED YET?”

You see, our dog Chloe had a rough life until she was rescued and eventually adopted by our family.  Now she’s the most pampered creature alive, but she had to be street smart in order to survive during those years.

Every morning, she does her “I’m hungry” dog dance, and usually one of the kids or I will feed her.  Approximately 120 seconds after she is done slurping down the last kibble, she finds a fresh mark to confuse, and begins the dance again.

I’m not sure if it’s because she forgets she’s been fed, she knows the charade works more than 0% of the time, or some canine combination of both.  But much like me, Chloe has enough mass around her belly that she doesn’t need to participate in the ancient ritual of Second Breakfast.

So how to we avoid not feeding her at all, or more likely, feeding her multiple times?  The solution I came up with brings us to the creation of this month’s project, the Dog Food Truth Detector.

What Are We Building With This Ardunio Project?

The DFTD is a device that alerts humans if the dog food bin has been opened and closed in the past 2 hours.  The time limit is customizable in code.   When the door is opened, the switch opens as well, and no current flows to the Nano.  The microprocessor detects this, turns on the LED, and begins a countdown to turn off the lamp in 2 hours.

What Problem Are We Solving?

Dogs are much more concerned with getting their next meal as soon as possible than humans are with keeping track of when they have been fed.  This leads to overfeeding on a chronic basis.  While fat dogs can be cute, it’s really not good for them so keeping the multiple feedings to a minimum is a good idea.

What Parts Do We Need to Build Our Ardunio Nano Project ?

If we take a big-picture look at what we need, we four main components.  First, we need a way of knowing when the dog food cover has been opened.  A magnetic reed switch is perfect for this scenario.  A magnet moved close to the switch causes the switch to change from “off” to “on” (or “closed” to “open”).  Second, we need a way of letting the human know if the door has been opened.  An LED or two is a great solution because it’s easy to see from a distance.  You can just glance toward the food bin and know if anyone has opened it lately or not.

breadboard of dogfood bin monitor
It won’t win any beauty awards, but it works

Third, we need something to keep track of the how long it’s been since the door was opened.  While there are many ways of doing this, I’ve went with an Ardunio-compatible microprocessor.  In this particular build, I’m using a SainSmart Nano 3.0 I have lying around, but you could use almost any Ardunio-based board.  The price of these has come down over the years, and we don’t need anything too powerful for this particular project.

Finally, we need a power solution.  The Nano can be powered by an USB cable, so that’s what I’m using for this construction.  You might find yourself in a situation where your dog food container isn’t close to an outlet.  In that case, you’ll need to use a battery source.  The easiest solution would be to use a USB Power bank you might have lying around, but you could run it with normal AA batteries as well.  Because I am assuming this will be powered by an AC Outlet, it’s not optimized to run on batteries.

For something that will permanently installed, a container to keep the components safely stored away is a good idea.  This could be as simple as a small cardboard box, or as sturdy as a plastic case.  A nice medium is often a disposable Tupperware container – I’ll use that for this project to give you an idea of to incorporate it into your own projects.

Parts List For The Dog Food Truth Detector

Here’s a list of the parts you’ll need.  While I’ve included links to get them on Amazon for your convenience, you can find these parts sometimes at better prices at places such as Adafruit or SparkFun.

  • Ardunio Nano – here’s one version of it
  • Magnetic Reed Switch
  • Male-to-Male jumper wires
  • Resistors (I’m using 2x 100 ohm because I have them at hand. Any 200-400 Ohm, 1/4w resistor will do.) – this kit contains a good assortment for future projects as well. I ordered from them on 2/21 since I’m low – I’ll update how the quality is when I receive them.
  • LED – I went with a white one I had lying around, but use whatever color you wish.  You’ll need to know the forward voltage and continuous current. Mine are 3.2V,20ma. If you don’t have any lying around this kit has gotten good reviews and you won’t run out for a long time.
  • USB 2.0 A-Male to Mini-B cable.  The distance is based on how far away your outlet is from the food container.
  • Breadboard.  Here’s a 3 pack of the type I’m using
  • A Computer with Ardunio Software loaded on it.  I used the 1.8.5 version of the IDE for Win 10.

Schematics

Here’s the visual schematics of how the circuit should be put together.

schematics for the dog food truth detector

 

A few notes on why I made some of the decisions I did in this circuit:

  • A resistor is needed when using a LED rated for a lower voltage than the power supplied.  In this case, we are using the 5V supply of the Nano, and the LED is rated for a max of 3.2V.  Therefore we need to use a current limiting resistor.  I decided on 200 Ohms, 1/4w just because I had 2 100 Ohm resistors lying around.  300 Ohms is probably better from a margin of safety perspective. Here’s a nice chart on how to read resistor values.
    How did I know the right resistor value?  If you want to drop a current with a resistor, you can use an online calculator to spit out the size of resistor you need.   You can also learn a bit more about Ohm’s Law if you want to dig in further. The box below shows the calculation I did for this case.
    What Resistor Do I Need For This Project
    Resistors are used to limit the current through the LED. Without it, the LED will “eat” current until it melts. In our case, we are using the internal 5V supply of the Nano. We want to drop the voltage to 3.1V. While our LED’s are rated for 20ma, 5ma will be plenty bright. Plugging it into the equation, we get (5-3.1V)/.005A = 380W. You are safe using any ohm rating above that level – your light just won’t be as bright. What if you wanted to run the LED’s at the stated maximums of 20ma? The result of the equation now becomes 1.9/.02A = 95 ohms. This means you are safe using a resistor above this level if you are in a pinch – 300 and above is perfectly safe.
  • The higher-ohm resistor connected to D4 is called a pull-up resistor.    It’s not technically needed because the Nano has a pull-up resistor built in that we call in code, but I add it out of habit.  Sue me.  Pull-up resistors are there to help steer the pin to a known state if there’s no input. In English, the microprocessor needs to have a default value to be able to compare if a switch is pressed or not.  The pull-up resistor helps with that.

Ardunio Sketch

Here’s the code I used in this project.  Feel free to copy and paste it into the Ardunio IDE.

//Global Variables
const byte REED_SWITCH=4; // pin on the Nano the magnetic reed switch is connected to
const byte LED = 8; //pin on the Nano the LED is attached to

unsigned long switchActivated; //store here when did dog the bin open
unsigned long turnOffDelay = 7200000; // 2hrs.=7.2m 3hrs = 10.8million milliseconds

void setup() {
pinMode(REED_SWITCH,INPUT_PULLUP); //Let the Nano know REED_SWITCH is an input pin, and activate the internal resistor
pinMode(LED,OUTPUT); //Let the Nano know we want to use the LED pin for output
digitalWrite(LED,LOW); //Start with light off

}

void loop() {
// get the time at begining of loop
unsigned long currentMillis = millis();

//is the switch away from the magnet? If not, skip next part
//our reed switch is closed when near the magnet.  This translates
//to a HIGH state (there is 5V running through the pin)
if (digitalRead(REED_SWITCH) == LOW){
//record time when reed moved away from the magnet
   switchActivated=currentMillis;
   digitalWrite(LED,HIGH);
}
//has enough time gone by?
if ((unsigned long)(currentMillis - switchActivated) >=turnOffDelay){
digitalWrite(LED,LOW);

}
}

Here’s the project in action, in a very temporary location.   I modified the code so the light would only stay on for a few seconds when opened – I figured no one was interested in starting at the wall for two hours of video to wait for a light to turn off!

Next Steps For Improvement:

At this point, you have a fully-working detector.  Congratulations!  You probably will want to modify a few features to make it fit your individual needs.

For example, the wire length of the reed switch is pretty short.  I happen to have open wire shelves right next to where I store our dog food so this isn’t a problem for me.  Most people will likely need to splice some wire onto the switch leads so they can comfortably reach the food tub from a more remote location.

I just used tape to secure the magnet and switch to the food container.  Frankly, I’m okay with that but glue or small screws will likely provide a more robust solution.

Finally, there are Nano’s that come without the pins already soldered in.  For a project like this that is a more permanant solution to a problem, we’d likely want to solder the wires to the Ardunio board itself.  Breadboards are great to create with, but they don’t always hold wires the greatest over the long term.  If you decide to use this solution in your house for the long-term, you’ll want to make a circuit that is more physically robust.

I hope you had fun building this project.  If you have any questions or improvements, please leave them in the comments below.

 

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.