Skip to main content

You are here

Mailbox Sensor Project

29 posts / 0 new
Last post
Mailbox Sensor Project

My front door needs a new paint job (well, actually a bit more than that ;-)), but that got me wondering if I could build some kind of sensor behind the mailbox to detect when it is (slightly) opened.

The idea is to put in a custom build PCB with an ATMega, powered by a CR2032, just like I did for the moisture sensor project I did.

But to give you an idea of the situation, here's a picture (as seen from the inside) of the mailbox/hole:

As you can see, there is some space at the top, in which I could easily snug a PCB with a CR2030 to power it. AA batteries are a bit too much though, so that's no option. AAA might work as well, but I prefer it to be small.

I am thinking of using a micro switch, though I'm a bit worried that it might wear out too soon. Also, I would like to keep detecting when it's open, so I know when it closes again. Given the situation, the micro switch probably won't be "pressed" all the time when the mailbox opens, so I still have to give that some thought.

Still a long way to go, but it would be a nice addition to my home automation system. be continued...

mailbox.png657.34 KB

Magnet and reed relay? Activated only when fully closed.
Optical on some reflective tab on that plastic hinge gadget?

The bolts will be rusted through before either of those wear out ;-)


If you use a magnetic reed switch as Martyn suggests, you could could have the ultimate power save configuration. I think you could use the the reed switch to turn on the JeeNode.

You'll need one of JC's favourite modified boot/fuse configurations to get it started super quick, and a transistor across the reed switch so you can latch it on from an I/O pin and hold the power on until you have finished sending, but then you can switch the transistor off and not just go to sleep, but turn off completely. 0mA standby current :-)


Might be worth while to look at this:


@all: Thanks for the ideas! The magnet + reed relay seems like a good solution. Haven't got any experience with them, so that's an added bonus ;-). I'll have a look on ebay to see what's available...

@martynj: I just knew someone would make a comment about these nice brown bolts :-P

Edit: bought a few of 'm on Ebay. Hopefully in a week I can start doing some tests.


They're pretty good. You can get them with some impressive current carrying ability these days too. Not that that's important to you though!

With a good magnet, and no ferrous material between the magnet and reed switch, they can often work at a range of 1-2 inches (2-5cms). Glad you've got an aluminium flap.

When you get them you can have a play with location and different magnets so you get the right balance.


How is it going with the project? I'm doing something similar with a snap-action switch and would like some inspiration :-)


@tleegaard Unfortunately I haven't had much time for this project. Been a little busy with the Raspberry Pi at the moment, besides normal life stuff and work ;-).

When I get to it again, I'll update my post here, but can't tell you when ;-)


I wanted to do this too some time ago, unfortunately no time too. I would go for a simple cheap photo resistor. Combined with a simple running average algorithm it should detect sudden light changes quite easy.

Not sure how this works in the night though... (We have a entry lamp at our building which should be enough.)


Is there a reason why you opted for a photo resistor?

Seems to me that a reed switch is much more accurate (though I still haven't had the time to test it further ;-)).


Been working on the schematics, and the board layout.

I'm thinking of making a small "support" board, which has two screw holes, and an 8 pin header (sideways). I plan on screwing that in my mailbox, and attach the PC with the battery / processor / reed-switch to it. That way it can be easily removed (to replace the battery, and also to program a new sketch to it).

Got a question though: Should special considerations be made to the board layout, because of the use of magnets?

E.g.: No ATMega/RFM12B near it? How about a battery?

I'd like my layout to be as compact as possible (for stability), but if needed, I do have the space to keep the processor etc. away from the reed switch and the magnet...


@MichelV, no magnetic interactions to worry about - place the components freely.
Did you decide to switch the entire node through the reed relay? If not, remember to pass a reasonable sense current through the contacts - dry reed relays are good (usually gold-flashed contacts) but still need a minimum current flow for reliable operation (cf. wetting current).


@martynj: Thanks, you always give such insightful tips and help. I really appreciate it!

The unit will be on all the time (though most of the time in deep sleep). It will send out an "alive" message every 30 minutes, along with the current temperature (temp sensor will be activated through a transistor to save power). And, of course, a message when the lid was opened (and for how long).

Powering on the node by means of the reed mightbe added as an option on the PCB though, so that I have an alternative if the battery life is too short.

Thank you for the tip regarding the "wetting current". I didn't know that. Would it also help to switch the direction of the current? (I'm using that in my moisture sensor to help prevent oxidation on the metal rods in the ground).

I was thinking of hooking the reed switch between two digital pins on the ATMega, and adding a small resistor in between. But what would be a good value (or, how do you calculate what a good value would be) to get enough "wetting current", without using too much power?


@MichelV, the reed relay was developed for telephone exchanges and is quite successful at switching low current/voltages. The evacuated glass tube and flash coatings help a lot - nothing like as troublesome as the corrosion on moisture probes.

A possible circuit it to put the switch between an I/O pin and ground, then enable the pull-up resistor when you need to sample the switch state - that will force ~100uA through the contacts and give a logic (inverted) output directly. Would not be enough wetting current for a microswitch, but should work fine for the reed.


@martynj Thanks for your suggestions!

Instead of sampling though, I'll probably be using interrupts (though I'll have to do a little debouching of course).

I've drawn up the first version of the schematics. Still have to sleep over it, and do some tests, but it should basically be it. I've attached it for anyones viewing pleasure ;-)

Edit 22:20u: Hmm, while thinking it over, I realized that interrupts might not be the best way to go here, since when the mailbox is open, I'm losing 100uA constantly, whereas when I poll periodically I won't be losing as much because I can switch off the pullup resistor while sleeping...

mailboxsensor_schema.png47.61 KB

@MichelV - can't follow your circuit for the reed activating the main 3.3v supply. I'd use a high-side MOSFET there, or just debounce the reed with a fat cap downstream and bridge directly from battery to 3.3v rail.


@martynj: well, you're probably right... ;-)

The idea was that the reed switch would turn the transistor, thereby powering the ATMega. Which in turn would set the digital out to high, in order to keep receiving power when the reed switch is turned off again...

I didn't take into account that the reed switch would only be on momentarily, which of course would totally not be long enough for the atmega to boot and turn his output pin high... :-/

And there probably are a few more things wrong with this circuit ;-).

I think I'll just strip it off completely and stick with the "always on" solution. Even with the suggestions you gave, this is way over my head ;-)


Updated the schematic. I removed the option of turning on the power via a reed switch, and added a status LED, and an extra power connector (in case I want to feed it off a battery pack instead of the CR2032).

Edit: small error in the design, R2 should be connected to +3.3v (instead of GND).

mailboxsensor_schema2.png32.33 KB

Just to let you know that this project is far from dead: the PCB's were ordered, and are currently being manufactured.

Hopefully in a few weeks I get them back, so I can give it a try...


Another small update. The PCB's are in, but unfortunately my soldering iron broke down on me. Luckily it decided to break down on me AFTER I soldered the MailBoxSensor PCB ;-)

The MailBoxSensor itself is the large PCB (front and back displayed). The other three are support PCB's.

From left to right:

  • FTDI interface board (basically only lowers the voltage from the USB-BUB from the standard 5v to 3.3v)
  • ISP board for initial programming
  • the support board which will be mounted in the mailbox.

There was a minor glitch on the PCB, in that the connector for the temperature sensor was placed the wrong way. Not really a problem, but I can't fix it right now (which is a bit annoying ;-)).

Programming the board works fine via the ISP interface. The FTDI interface however is giving me headaches... For some obscure reason, it doesn't work... It powers the board, but it refuses to accept new sketches.

I double checked the PCB for both design errors, and bad contacts, but all seems fine... And I can't imagine that the fact that it's running on 8Mhz could be the culprit...

Oh well, guess I'll have to program the board via the ISP interface then. I can live with it ;-)

I'm not done with the software part of thing yet though, and I haven't rigged it up to my mailbox yet. Probably need a stronger magnet for that as well... So, there's still more to come (though not today) ;-)

MailBoxSensor.jpg121.44 KB

Since the bootcode is simulating an asynch connection by bit banging, don't you need to recompile it with 8Mhz clock rate defined? Or does Optiboot do autobaud rate detection?


Ah, silly me... I found it... Forgot to upload a bootloader ;-)

I had been trying quite a few things before I got it working via the ISP port, and ultimately forgot to write a bootloader the last time...

Now it works via the FTDI port as well!


I need some new magnets unfortunately, to be able to mount it correctly. So I guess it's on hold until I get some.


The mailboxsensor is installed and working now!

I ended up using super glue instead of screws, as it was impossible to get these in with the tools I have available. I used superglue for the magnet as well.

I had a small error in the PCB, which caused the temperature sensor to not work correctly. But nothing a few wires couldn't fix ;-).

Today, I wrote the software for it. Both for the sensor, as well as the server side of things (as well as the web interface that runs against the central server database).

It alerts my central server whenever the mailbox opens or closes (and also sends a message once every hour, to let me know it's still up). It's the servers job to detect and handle situations where messages might have been lost, as well as taking and storing a screenshot from the webcam whenever the mailbox opens.

So far, it seems to work great, though I'll have to give it a few weeks to be sure.

Not sure about the battery life though, might be a bit short I'm afraid (I'm guessing about a month, but it's mostly guessing at this point).

Nevertheless, I'm pleased it got this far today. It's been laying on my workbench way too long ;-).

mailboxsensor_installed.jpg59.79 KB

@Michel, success! But those bolts don't look any better ;-)


@martynj Hehe, that's true... I just hope that on the long run the super glue will hold. Removing the sensor to replace the battery does put some strain on it.

But then again, they didn't call it SUPER glue for nothing, now did they? ;-)


MichelV, you should definitely add your project to the Cafe/Projects!


@zooer: Well, certainly not at this stage, as there is yet a lot of stuff I still need to test/improve.

I'm really worried about the battery life, for example. If it only lasts a month (and it may as well be shorter than that!), than that's not good enough in my eyes.

Besides, it's all SMD stuff, so probably not really suitable for a Cafe project.

Ultimately, it's only a "JeeNode with a Reed switch" (credits for the reed switch idea go to martynj btw!), only much smaller. You could probably do the same with a SMD Kit and a reed switch, if you'd bypass the regulator, and somehow connect a CR2032 battery to it ;-).

But thanks for the complement.

As for the battery side of things: The main loop is currently checking the state of the reed switch (momentarely putting power on it), and then putting the ATMega to sleep for 500ms. Once every hour (or if the state of the reed switched changed) I grab a temperature measurement, and send out a message.

I should probably hook it up to my scope, using the "10 Ohm" technique JCW uses, to get a better understanding of it's energy usage (and where it's all going). Unfortunately I haven't got a much time as I would have wanted right now, so I just plugged in a "somewhat" fresh battery (I really need new ones :-)), and see if it survives for more than a few days...

When there is more news, I'll post an update here though.


Thanks MichelV for the insight.

Still want to argue a little, it certainly is not just a simple JN with a switch at least to me and most likely many other JeeLabbers. It involves other aspects that you might not think are such a big deal. Heck, I'd reeally like to hear more about "server side of things (as well as the web interface that runs against the central server database)". Maybe at some point??

Premium Drupal Themes by Adaptivethemes