Low-power sketch for reed switch pulse counting on EmonTH

Hi,

I wrote a new low-power sketch for EmonTH V1.4 that counts pulses from a reed switch. This is adapted from code written by Robert Wall here.

I like it because it allows me to debounce the circuit, send the pulse count periodically, and extend battery life. After a month of operation on my gas meter, no pulse lost and the battery voltage didn't change at all.

It requires that your meter is equipped with a reed switch that does not switch too fast. How does it work? The EmonTH sleeps all the time and wakes up every 0.5s in order to poll the state of the input. When catching the sequence 00001111, it detects a rising edge. The total number of pulses is sent every minute.

It means that either the 'on' time or the 'off' time of the pulse exceeds 4*0.5=2s. This is the case with my gas counter. You can easily change the polling period (500ms) to manage different pulse rates for other water meters, gas meters, etc.

This sketch may be easily modified to work on EmonTH V1.5 and also send the temperature/humidity every minute.

Eric

 

Patrice Diliakou's picture

Re: Low-power sketch for reed switch pulse counting on EmonTH

Thank you !

I'll test it soon ;)

Patrice Diliakou's picture

Re: Low-power sketch for reed switch pulse counting on EmonTH

Hello,

No result.

It transmit at start the battery power and nothing after. The total blackout...

I puted a pulldown between 3 and 4, tried the other wire to 2, 5 or 6 : no response.

I am beginning to think that my emonth is broken...

Robert Wall's picture

Re: Low-power sketch for reed switch pulse counting on EmonTH

What is your emonTH, V1.4 or V1.5? 

The pulse input is D2 on the V1.4, D3 on the V1.5 - so you might need to change "const int PULSEPIN=2;"

[ERIC: You need to add that in a comment.]

Patrice Diliakou's picture

Re: Low-power sketch for reed switch pulse counting on EmonTH

Hello,

Sorry, i precised it on another thread. It is V1.4.

Connecting my reed is not clear. Where connect it ?

I use a pull-down resistor between pin 4 and ground to fix 0v when no working on reed.

I tried to connect other wire to 3v to send 3v to pin 4 when sollicited.(pin 1).

Robert Wall's picture

Re: Low-power sketch for reed switch pulse counting on EmonTH

"Sorry, i precised it on another thread. It is V1.4."

That is what happens when you cross-post. Please do not do that, it wastes everyone's time. You cannot expect anyone to remember a post from 2+ weeks ago on another thread.

"Connecting my reed is not clear. Where connect it ?"

Look at the circuit diagram - you will find it in the Wiki. You will see Pin 4, the pulse input, has provision for a pull-down resistor to GND. Therefore you connect your reed switch between Pin 4 and 3.3 V, which is Pin 2. If no resistor (R2) is fitted on yours (it is underneath the battery holder), use an external 10 kΩ resistor between Pin 4 and Pin 3.

Patrice Diliakou's picture

Re: Low-power sketch for reed switch pulse counting on EmonTH

It wasn't a cross-post but an other sketch from Eric :) So i missed to precise it on this one.

Otherwise, thank you for your answer. It confirm my situation : My Emonth doesn't work. For the pull down i didn't put it behind battery pack yet because i wanted to test it before.

You confirm my 3.3V  use for it. So, Dig2 is protected and can accept 3v load. I wasn't sure and i supposed that i fused it.

I will make other tests, before to decide to trash it... and buy an other one. I think this one is broken. No transmission after first start and first battery value sent.

 

Thank you.

Changed JeeLib libraries because i had old one (2014) No way.

Tried to resold weldings : no way. (electronic lab fully equiped)

Robert Wall's picture

Re: Low-power sketch for reed switch pulse counting on EmonTH

If you have a programmer, you can try a simple sketch:


#include <JeeLib.h>

void setup()
{
  pinMode(9, OUTPUT); digitalWrite(9,HIGH);   
  delay(10000);
  digitalWrite(9,LOW); 
}

void loop() 
{
  digitalWrite(9, digitalRead(2));
}

With your reed switch and pull-down resistor connected, the LED should come ON for 10 s, after that it should come on when the magnet is near the reed switch, and it should go off when the magnet is away from the reed switch.

Patrice Diliakou's picture

Re: Low-power sketch for reed switch pulse counting on EmonTH

Thank you, i test it now.

 

EDIT :

Working well !

So my connection is good.

Will try to understand sketch programmation... :)

EDIT2 :

Well...

Nothing. No reaction. No back to console too.

Robert Wall's picture

Re: Low-power sketch for reed switch pulse counting on EmonTH

So, the problem is either the sketch is not working as it should, or your reed switch is "bouncing" so much that the sketch cannot recognise the transition from 0 to 1.

The main loop runs and then sleeps for 500 ms. Every time, that is every 500 ms, it reads the switch. The last 8 switch states are recorded in the bits in a byte, and when you have 00001111 (0x0F), you have detected the switch closing.  If you have a really bad switch, it might be bouncing for a long time. But it should not bounce for more than 500 ms, and the switch must remain in each state long enough to give that bit pattern. i.e. it must be in each state for at least 2 s.

 

Patrice Diliakou's picture

Re: Low-power sketch for reed switch pulse counting on EmonTH

Thank you for your precisions.

Are my fingers and a peace of wire can generate boucing phenomen ?

Ill will try to modify some parameters to taste more things?

Robert Wall's picture

Re: Low-power sketch for reed switch pulse counting on EmonTH

Yes, a piece of wire or a very cheap switch are both very good ways to get contact bounce. Using the 'interrupt-driven' pulse sketch, I could see anything up to about 40 pulses with pressing the button once only.

Patrice Diliakou's picture

Re: Low-power sketch for reed switch pulse counting on EmonTH

Thank you for the precisions.

Eric_AMANN's picture

Re: Low-power sketch for reed switch pulse counting on EmonTH

Hi,

Attached is a new version of the sketch described above but for EmonTH V1.5.

It also sends the temperature/humidity every minute. It's derived from the emonTH_DHT22_DS18B20_RFM69CW sketch (V1.6.1).

NB : When reading temperature and humidity with the DHT22, one pulse may be ignored because this sensor is very slow (2s).The DS18B20 sensor which is faster (375ms) is less problematic

Eric

 

glyn.hudson's picture

Re: Low-power sketch for reed switch pulse counting on EmonTH

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.