Counting pulses

Hi. I'm trying to look for a method to count pulses with my arduino. My electric scheme is this one:

I know that the pulse transmitter commands pulses between 17 and 28V so i tried to make a voltage divider to convert the signal and get a voltage between 2,5 and 5 V so the arduino will be able to read it. However, i'm not reading anything in my arduino.

What am i making wrong?

Thank you for the help!

A voltage divider divides to ground, I don't see ground mentioned in that diagram...

Assuming that one of the pins on the sensor that you have the wires on is ground, and the other is the signal, you need to connect Arduino ground to sensor ground. "voltage" is not absolute, but relative - a single point can never have a voltage, there is only the difference in voltage between two points. We define voltages relative to ground

I don't think those divider values are right - 5k and 8k will not turn 17-28v into sub-5v territory. Try like 5k and 22k (with the 5k side closer to ground) - this doesn't quite get you there, but it's close enough that the chip won't mind (see the specs in datasheet)

First of all i want to thank you for the help.

I was thinking that the diagram was equivalent to this one:


https://drive.google.com/file/d/0B57tAoUCVHR2U0J3dTg3X2RBOVE/view?usp=sharing

But maybe i'm wrong. I am suposing one of the wires is ground because i though it had to be, but...

The sensor is actually a energy counter (datasheet in spanish). It has an output that transfers pulses. The idea is to count this pulses in order to know the energy consumed (1kwh = 2000 pulses).

I thought about connecting the arduino ground and the other wire, but i am not sure how to do it. It came to my head two options, but it is clear i don't have much idea of electricity (i appreciate any explanation about the theme):

Op1:


https://drive.google.com/file/d/0B57tAoUCVHR2eW5naEVsakVWOUE/view?usp=sharing

Op2:


https://drive.google.com/file/d/0B57tAoUCVHR2OHByRFRSNG42U2c/view?usp=sharing

Thank you all so much for the help!!!!!!

Use Op2 as Op1 will not work due to the lack of a ground.

However, i'm not reading anything in my arduino.
What am i making wrong?

You need to post your code. Read this first:-
How to use this forum

Ok. I'm trying one code very simple just for see if i get any kind of digital read in my arduino:

void setup() {
  Serial.begin (9600);
  pinMode (32, INPUT);
}

void loop() {

if (digitalRead (32)==LOW){Serial.println("LOW");}
    else {Serial.println("HIGH");}

}

The instalation i'm making is the next one:



I'm following your instructions: the voltage divider is formed by two resistances of 5k and 22k (5k next to the ground) and the one of 5k next to the ground.

Still don't get anything in the arduino when i turn on the heater.

Thank you very much for the help.

I have jus tried another code using the interruptions and now i get some pulses, but not the total that the counter is tramsitting:

int pulGlobal = 0;
int cal = 0;

void setup() {
  Serial.begin (9600);
  
  attachInterrupt (2, detectarPulsos, RISING);
}

void loop() {
  
  if (pulGlobal == 3200){
    
    cal++;
    
    pulGlobal = 0;
    
    }
    
    Serial.print ("Calorias totales: ");
    Serial.print (cal);
    Serial.println (" kwh");
    Serial.print ("Pulsos: ");
    Serial.print (pulGlobal);
    Serial.println ("");
    
    delay (30000);

}

void detectarPulsos (){
  
  pulGlobal++;

}

What do i count much less pulses?

Why the previous code doesn’t detect never any HIGH level in the pin?

I've been all day trying things but i haven't maid any progress. Truth is i'm a bit tired and desperate. I can't detect anything with my arduino except when i turn on and turn off the heater (even when i don't have the red and blue wires from the transmitter connected to the board). In that case, the variable counts lots of pulses (i supose that's for some kind of induction¿?)... I leave some images of the installation and the code... Thank you all for the help. I don't know what else to try...

volatile int co = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode (18, INPUT);
  attachInterrupt (5, coE, RISING);
  Serial.begin (9600);

}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.print ("Pulses counted: ");
  Serial.println (co);

  delay(3000);
}

void coE (){

  co = co + 1;
  
}

And the results

Pulses counted: 0
Pulses counted: 25
Pulses counted: 25
Pulses counted: 25
Pulses counted: 32
Pulses counted: 32

It growed up to 25 when i turned on the heater and then to 32 when i turned off. Between those actions, the arduino doesn't detect anything besides the LED blinks, so the energy counter should be sending impulses trough the wire... Thank you all.

If you need help you have to post a proper schematic and links to the equipment you are using. What you have given so far does not allow us to see what you have done.

Ok. I'm gonna try.

I have a common energy counter (kwh meter) that emits an impulse each time a quantity of 0.0003125 kWh passes throw it. The type of this signal is SO (optocoupler), each pulse lasts more than 30 ms and has a maximun tension of 27 v and a minimum of 18v. The schematic can see in this link (but it is in spanish, i couldn't find it in english).

My goal is to count this impulses with my arduino so i could know how much power my appliances are consuming.

To achieve this, i took a common heater and made the following instalation:

So the heater is connected to the mains through the kwh meter, and the energy counter emits impulses through the terminals S0+ and S0-. I reduce the voltage of this signal with a tension divider (so it is between 2,5 and 5V) and connect it to the arduino. Then, a put a wire to have the same tension reference in the arduino and in the kwh meter (i'm not sure if it is clear).

The code i'm using is the next one:

int co = 0;

void setup() {

  attachInterrupt (5, coE, RISING);
  Serial.begin (9600);

}

void loop() {
  
  Serial.print ("Pulsos contados: ");
  Serial.println (co);

  delay(3000);
}

void coE (){

  co = co + 1;
  
}

When i turn on the heater, i see the kwh meter emiting pulses, but i don't read any with my arduino...

I hope my problem is clearer now. Thank you for the help.

OK, all variables used in an interrupt service routine and also outside must be declared as volatile so you need that first line to be:-

volatile int co = 0;

Yeah. I did that but still have the problem. Thank you for the response.

Checho360:
I know that the pulse transmitter commands pulses between 17 and 28V ...

Now, about these "pulse transmitter commands pulses":
is 17V the low and 28V the high
or
are they of some broad range (from Gnd) where the high may be 17 to 28 volts, but the low is Gnd?

The following is an interrupt counter using a Uno (INT0 / D2).
I used FALLING because it's easier to implement (fewer components).
[Modify to meet your requirements.]
I used "co" and "newco" so that it won't Serial.print a stream. It just prints when there is new information.

//  counting Interrupts
//
//
int co = 0;
int volatile newco = 0;
void setup ()
{
  pinMode(2, INPUT_PULLUP);
  attachInterrupt(0,pop,FALLING);
  Serial.begin(9600);
  Serial.println(co,DEC);
}
void loop ()
{
  if (co != newco)
  {
    Serial.println(co,DEC);
    co = newco;
  }
}
void pop ()
{
  newco++;
}

Checho360:
I know that the pulse transmitter commands pulses between 17 and 28V so i tried to make a voltage divider to convert the signal and get a voltage between 2,5 and 5 V so the Arduino will be able to read it.

I see no such indication in the datasheet you cite.

It suggests the output is an opto-isolator specified for 27v and 27 mA maximum. That being the case, you would connect it between an Arduino input and ground, and provide a 1k pull-up resistor to +5V. An opto-isolator does not provide voltage, it merely switches across a voltage you provide.

And at 2000 pulses per kWh with a maximum current of 32A at 220V, it will take at least 8.5 minutes to accrue that many pulses, maximum rate less than four pulses per second. Certainly no reason to use interrupts to count this!

Paul__B:
I see no such indication in the datasheet you cite.

It suggests the output is an opto-isolator specified for 27v and 27 mA maximum. That being the case, you would connect it between an Arduino input and ground, and provide a 1k pull-up resistor to +5V. An opto-isolator does not provide voltage, it merely switches across a voltage you provide.

And at 2000 pulses per kWh with a maximum current of 32A at 220V, it will take at least 8.5 minutes to accrue that many pulses, maximum rate less than four pulses per second. Certainly no reason to use interrupts to count this!

That makes much sense to me.

In the installation guide, has written:

Passive pulse contact:

-Corresponding to "SO conditions of DIN 43864:

18~27V: max 27 mA: max length of lead: 20m
impulse length>30ms, connection to terminal SO+ & SO-
-Limits of values: max 60VDC: max 30mA
diode against wrong connection is integrated (paralell)

I supose that supports your theory.

Either way, i tried this two configurations:

And didn't recibe anything with the arduino (i also tried to change my code from RISING to FALLING to try)

Thank you very much for the help, really!

I guess I'd get on it with a voltmeter.

Try this as code:-

void setup() {
  Serial.begin (9600);
  pinMode (32, INPUT);
}

void loop() {

if (digitalRead (32)==HIGH){
    Serial.println("HIGH");}

}

The code as you had it originally spent too long printing out "LOW" and is likely to have missed a pulse while it was busy writing LOW all the time. This way you only do the printing when you see that short pulse.
Wire it up as you did in the right hand circuit of the last diagram you posted.

Hi. I finally was able to count the impulses!! I just wired it as shown in the next picture and now it works! (more or less):

The code i'm using:

volatile int co = 0;

void setup() {

  attachInterrupt (5, coE, FALLING);
  Serial.begin (9600);

}

void loop() {
  
  Serial.print ("Pulsos contados: ");
  Serial.println (co);

  delay(3000);
}

void coE (){
  
  co = co + 1;
  
}

I just have one problem right now. It works properly when the heater is turned on, but in the moment i turn it on or when i turn it off, the arduino counts lots of impulses.

One thing i know, is that the impulse length must be greater than 30ms. Is there any way to program that in the interruptions?

Some of you told me not to use the interruptions. But i don't really know another way to catch all the impulses. I know i could use something like this:

int co = 0;
void setup() {
  Serial.begin (9600);
  pinMode (32, INPUT);
}

void loop() {

if (digitalRead (32)==HIGH){
    co++;}

}

But i am using the arduino with other purposes so i will have more code. If i use something like that, i supose i could loose some impulse, couldn't i?

Thank you very much for the help.

One thing i know, is that the impulse length must be greater than 30ms.

How do you know this?

Is there any way to program that in the interruptions?

Yes basically it is called debouncing.

You make a note of the time that the interrupt occurred by putting the time given by millis() into a long variable. Then when the interrupt happens again you check that the time given by millis() minus the time in the variable is greater than 30. If it is you increment your counter and then set that variable again. If it is not then just return from the interrupt without incrementing anything.