How to count pulses per second ?

Feb 13, 2007 103 Replies

Hi, I'm hoping someone could help me create a very basic circuit which would:



Count the number of pulses it receives per second (from a 555 timer, or a switch, for example) and if the number of pulses per second is equal to, or greater than 10 it turns on a transistor.



So basically it should check every second if a clock frequency of 10Hz is being met - if so, a transistor should turn an LED on and keep it on for as long as 10Hz a second is hitting the circuit. As soon as the clock stops, or the frequency goes below 10Hz, it should turn off the LED.



Thanks so much for your help !



MC



Microcontroller sounds like the best bet. A PIC 16F628 would be ideal for this. Much easier than messing about with lots of inflexible logic. Also a good grounding too for future projects. With a 4Mhz 628 you could realistically count upto about 100Khz with some decision making in there too. (actually runs at 1MIPS though).

A programmer (suitable for the 628) will set you back say £20 tops. One of the easier chips to program. MPLAB is also free.

Lots of examples on PICLIST (type it into Google)

Thanks for your help. I actually considered that option, but I was under the impression that a simpler circuit could do this job.

o a

e of

That can be very easily done with a single retriggerable one-shot with a 100 millisecond timeout. What\'s your application and what kind of accuracy do you need?

JF:

I dont need this to be too accurate... I'm collecting the number of times an IR beam gets broken, and if it gets broken faster than 10 times a second, I need to trigger a transistor and keep it on for as long as the beam is broken at a rate of 10Hz or above.. (this beam gets broken with human intervention, so freq could be variable, anywhere from 0Hz to 20Hz, and may not necessarily have an even duty- cycle)

Thanks so much for your help !! MC

--- Nonsense. I can do it with one chip, one resistor, one capacitor, no programmer and no learning curve.

Not only that, I can build it, test it, tweak it and post the schematic in a few hours while you'd still be struggling with source code.

-- JF

:)

I'd have to agree w/ JF. Learning curve would be big (at least for me).. the "one-chip in a few hours" would just work so much better :) JF: would you be able to shed some light on what I should do. (A schematic would be simply amazing)

MC

--- Basically, what you do is set up a retriggerable monostable multivibrator (one-shot) so that as long as the edges which trigger it keep coming in before the one-shot times out, they start a new timeout period, which should be precisely 100 milliseconds plus or minus whatever slop you can tolerate. That way, as long as the inputs occur quickly enough, the output will always be stable and the LED will never come on.

Which brings up another point:

If the signal drops below 10Hz and then goes back to >= 10 Hz, would you like the LED to stay on forever (Until it gets manually reset or something like that) or to go off automatically if/when the input signal goes back to >= 10Hz?

Be aware that if the latch is what you want it'll probably take more than one chip to get there.

-- JF

I learned to program PICs _once_, it took every bit of a few days. I don't understand why the "PIC haters" all use this as their primary argument; as if it's something you go thru for every project. Electrical theory is way harder IMO.

The OP said that you can't depend upon duty cycle or pulse regularity. Sounds like the circuit will literally have to count the actual number of pulses that occur in one second to meet his requirements. The OP hasn't stated if this kind of latency would be acceptible, but he didn't say it wasn't either.

I could have working code to do this in about 1 hour. Given that, I'm going with the micro on this one for a solution with the smallest number of parts (1 PIC, some juice and an input buffer (a resistor may be more than enough). AFAIK, a discrete solution to do the above described work would require allot more parts (a one second clock, resettable decade counter, output compare and latch).

OTOH, if a 100mS retriggerable timer will do, then you win. Or I could just change the code. ;-)

Yes but an 8 pin PIC could do this without a timing resistor and capacitor and do it in an absolute counting way that handles irregualr pulse timing like from a person pushing a switch. It would require writing some simple code and programing, of course, but would be cheaper and more stable and have a lower parts count and a smaller footprint than the one shot. The 25mA output current will drive LED's better than a one-shot. The clock is internal, no other parts required except a bypass cap and LED resistor that you have to have anyway. The one shot is OK for a do it once solution but is not the best for even moderate production of a few units. Besides, once one learns how to program and blow parts, there is no limit to what else can be done. That's a much more valuable learning curve than screwing with a one-shot.

Guys, thanks alot for your responses, all excellent & informative posts. I will most definately attempt this solution with a pic at a later time, convincing arguments in favor of the pic... and any suggestions with regards to obtaining a good and relatively inexpensive pic starter kit are definately appreciated.

In the meantime, I'd like to attempt a temporary quick-fix to my problem, as i'm working with limited components in a relatively short time-frame.

JF: I'm not sure if a monostable would fix the problem (but correct me if im wrong) Two conditions need to be met:

  1. If and only if the pulse train coming in >=10Hz (10 "up" cycles per second with no particular duty-cycle pattern) only then turn on a transistor.
  2. Otherwise, if the pulse train count goes below 10 cycles at any point, turn off the transistor at once.

So the output should be HIGH only if 10 cycles per second (or higher) are counted, otherwise output is LOW.

I'm afraid I still dont have a working solution (PIC programming aside :)

Guys, thanks for all your help again.

If available, there is an 8 pin chip called the 567 that is a tone decoder. It doesn't count pulses it compares frequencies. Might be of use. bg

Please bottom post. That\'s a horse of a different color, I think. Duty cycle usually refers to the percentage of time a pulse is active compared to the sum of the active and inactive time of the pulse, but it seems like what you\'re asking for is to detect the occurrence of 10 or more pulses in one second regardless of the pulse width and separation between pulses, no? That\'s a little trickier, but still easily doable in hardware. Also, I asked earlier whether you wanted the LED to go off permanently if the pulse train dropped below 10pps or to come back on automatically if it subsequently rose to >= 10pps, but I haven\'t seen your reply. Which would you prefer?

JF: Correct, i need to "detect the occurrence of 10 or more pulses in one second regardless of the pulse width and separation between pulses", and the LED to "come back on automatically if it subsequently rose to >= 10pps" .... so the only time the LED would be off if pps

--- Try this (View in Courier)

Vcc--------------------+--------+------+ | | | +---+ | | | | | | [1M]---|CPO | +----|RC R B|--+ 13|___ ___|12 | 1|_ |13 +-O|CP1 5-9|----|---O|A Q|--+ |15| | | +---+---+ | +--|MR | [4.7µF]HC123|14 [1K2] | +--------+ | | |A | HC4017 | | [HLMP4700] | | | | GND>--+----------------+--------+------+

An HLMP4700 is a 2 mA high-efficiency red LED which you can drive directly from the HC123's output.

If you cant get one, or any other low-current LED, or you'd rather use a 20mA LED, do this:

Vcc--------------------+--------+------+------------+ | | | | +---+ | | | | | | | [120R] [1M]---|CPO | +----|RC R B|--+ | 13|___ ___|12 | 1|_ |13 C +-O|CP1 5-9|----|---O|A Q|---[1000R]---B 2N3904 |15| | | +---+---+ E +--|MR | [4.7µF]HC123|14 | | +--------+ | | | | HC4017 | | | | | | | GND>--+----------------+--------+-------------------+

Also, you won't be using the other one-shot, so connect the unused inputs (pins 9,10,and 11) to ground.

I haven't shown the power pins either, and +5V goes to pin 16 and ground goes to pin 8 on both packages.

-- JF

I just noticed on sed that you\'ve multiposted this thread instead of crossposting. It would be better if you crossposted, since that way everybody gets to follow the thread and post to it without having to jump back and forth from newsgroup to newsgroup. I\'m going to crosspost this one over to sed even though it\'ll repeat here. (seb)

JH: Excellent suggestion. I truly appreciate all your help, especially the schematic. I will be ordering the HC4017 to do the trick.. hopefully it'll be here in a few days... in the meantime, i'm trying to simulate in orcad pspice and cant find the an HC4017 equivalent in the library, what did you use to simulate (if anything) ?

many thanks again.

MC

Does someone sell a pic-for-beginners kit?

I'd like to get into it but I really don't want to take the time to build my own cable and put together my own programmer....

(It's not that I am lazy; I literally don't have the time. I am an embedded programmer by profession and do this all the time at work - so I don't want to spend my hobby time building cables and debugging the development environment; I want to be able to program the thing....)

I am really looking for a linux-based solution as well.

I am willing to buy a homegrown kit - if an individual has one to sell or if someone is building these semi-commercially.

Thanks,

--Yan

I didn\'t, but I think your library has a 4017 in it, no?

If I can throw in my two cents, I'd recommend checking out the AVR series of microcontrollers by Atmel. They are in a similar price range, are much faster, have a greater feature set and are much easier to program. Atmel's own high quality development environment AVR Studio is free, along with a complete suite of open source stuff including the GCC C / C++ compiler.

The best part is that AVRs can be programmed in circuit with a parallel port cable and a pair of wire cutters. If you don't want to mess around with that you can get some cheap programmers at

formatting link
but I prefer the slightly more expensive AVRISP Mk II from Atmel, which is about $30 from Digikey. It's USB and is supported by AVR Studio and all the open source programmers.

I like the ATMega8 as a good starting point and prototyping microcontroller. It's got 8K of program Flash, 1K of SRAM and 512 bytes of EEPROM along with a ton of peripheral features. A simple circuit to get one running requires only power, ground and a 10k pullup on reset. The ATMega8 has a built in RC oscillator up to 8MHz (8 MIPS!) and you can get em for a few bucks on Digikey.

I hate to sound like an advertisement but I used to work with PICs and then I found AVRs. When I was working with PICs (about 4 years ago) they were a big pain in the butt. Programmers were complicated, there was no good, free C compiler and Internet resources were limited. Maybe the PIC scene has gotten better but AVRs just keep getting nicer and I see no reason to go back to using PICs.

Here's an instructable on getting started:

formatting link
Don't know if it's any good or not but it looks like it covers the basics.

Jason von Nieda

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required