# How can I count analogue pulses over 10secs.

Discussion in 'General Electronics Discussion' started by JPU, Sep 22, 2012.

1. ### JPU

281
1
May 19, 2012
Hi All

I have been working on a heart pulse counter. I am now happy(ish) with the analogue side and my scope shows quite a nice signal.

Here is the video:

I now wish to use the readadc command on a PICAXE chip to count the beats per minute. I have given this some thought and its giving me a little headache as I'm struggling to work out how can I count the pulses received above 2V and then calculate the Beats per minute. I realise I have to count the beats over, lets say a 10 sec period and multiply by 6 to get the BPM but how do I count over a 10 second period. I think I am a little lost as how I can guarantee the time to be exactly 10secs.

Any help or pointers would be appreciated.

Thanks

JPU

2. ### (*steve*)¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥdModerator

25,501
2,841
Jan 21, 2010
Time the interval between pulses and convert that to pulses per minute.

To reduce noise, apply an averaging algorithm.

3. ### Harald KappModeratorModerator

11,996
2,810
Nov 17, 2011
You don't need the ADC. It is possible to use it but it would make the program unnecessarily complicated. If you want to do it, you will have to read the ADC continuously and detect the peaks in software by comparing the ADC values to a pre-set value. Whenever the ADC reading crosses that value (from lower to higher) count one pulse.

May I suggest another method? Within the analog part of your circuit first use a low-pass filter to smooth your signal without loosing the peaks. After the filter, use a Schmitt-Trigger comparator circuit to generate a clean digital signal from the filtered analog signal. Use that digital signal as input to a timer/counter of the PICAXE. Reset the timer/counter and wait for a specified time interval (e.g. 1 minute). After that interval read the timer/counter and you have directly the number of beats per minute. If you set the wait-interval to another time (e.g. 1/2 minute to shorten the delay), multiply the timer/counter value accordingly (by 2 in this example).

4. ### JPU

281
1
May 19, 2012
Thanks for your replies.

My original thought was similar to your suggestion Harold. However it was suggested to me that the pulse was to small for a schmitt-trigger to work reliably?

What are your views on this?

JPU

5. ### gorgon

603
24
Jun 6, 2011
Make a Schmitt trigger based on a normal comparator, and you can set the trigger levels as you wish.

TOK

6. ### BobK

7,682
1,688
Jan 5, 2010
Personally, I would stick with the ADC since you already have the microcontroller, why add extra hardware? What you have to do is wait for the signal to go above a certain level, count a pulse, then wait until it goes below another lower level before looking for the next pulse. This emulates a schmidt trigger, and you can fine tune it with software. And I would go with what Steve said about counting the time between pulses instead of pulses in a 10 second interval. Use a running average of maybe 4 or 5 pulses.

Bob

7. ### gorgon

603
24
Jun 6, 2011
To get a correct count, you need to get the time from the start-to-start, or end-to-end of the pulses. That's the period to convert to pulses/time unit.

TOK

8. ### JPU

281
1
May 19, 2012
Hi Guys

Thanks for your ideas, this is slowly starting to make a little sense to me.

I can see how counting the space between pulses is the way to go. So can you guys help me along this path..

Idea---->

Use a picaxe using the readadc command to sense when the peak occurs and ends.
Start a timer which counts until the next readadc value goes high (100+) again. (stop timer)
divide 60 by the count, the result is the BPM

This sounds good but problem I see is If I program the pick to readadc until the input goes 100+. I will then wait until the peak ends, ie<100 before I start the timer. How do I then instruct the counter to count in a recognised or time interval equivalent until the next peak.

I have come up with this simple idea but the problem is how do I work out what the time equivalent of the count variable is in my program (is there a way?), How long does it take for the subroutine jumpout to loop. If I know this I will know the value of count as a time interval. Should I introduce a pause???

(please forgive me syntax, I fiddled with BASIC 25years ago. I know that I will have to revise the instructions for picaxe but you guys will know what I mean)

start:
if readvalue=1 then goto jumpout
goto start

jumpout:
let count=count+1
goto jumpout

clockstopped:
let bpm=60/count
print bpm

end:

Thanks for taking the time to help.

JPU

Edit: I just saw Gorgons post so I will incorporate his advice in the program..It is not shown in the above program.

9. ### CDRIVEHauling 10' pipe on a Trek Shift3

4,960
652
May 8, 2012
Yes Justin. Do as Harold says. The ADC isn't the best choice here.

FYI to members joining this thread. This topic is a spin off of another topic by JPU. It would be nice if Steve would splice the part of that thread in to this one where it veered off "Sweat Monitor" to counting heart beat. Also, JPU is using OpAmps and I was trying to explain using them as differential amps to utilize common mode rejection and eliminate some airborne mains noise that the body picks up like an antenna. We only progressed a tad before this thread was started.

Chris

10. ### JPU

281
1
May 19, 2012
HI Chris

So, I should use the Schmitt trigger and the count command?

Justin

11. ### CDRIVEHauling 10' pipe on a Trek Shift3

4,960
652
May 8, 2012
Let's put it this way. There's a number of opinions here and each have merit. What the ADC option has going for it is you already have it and, as stated, keeps component count down. If doesn't work well you have nothing to loose but a little time. The Axe code can always be re-written.

Chris

12. ### KrisBlueNZSadly passed away in 2015

8,393
1,271
Nov 28, 2011
I agree with using the ADC - I think you'll need to - and with timing the intervals between beats and using a running average for filtering.

I think you should look at the waveshapes of some typical signals that your ADC will see, including some abnormal ones if your device needs to be able to handle those, and make sure your algorithm to process the ADC data will make the right decision in all cases on what constitutes a "pulse".

Using a simple threshold won't work if there are two pulses of similar heights, and could also get confused if there are two with different heights. I think some types of digital signal processing (DSP) algorithms might be helpful; I have no experience with DSP but others on this forum probably do. At least there's no great speed requirement! Have you chosen an ADC sampling rate? I would suggest something in the region of 100 samples per second.

13. ### BobK

7,682
1,688
Jan 5, 2010
It comes down to more hardware or more programming. The choice is yours. I am probably biased because my current job is working on a program with >1M lines of code, so software complexity might not be as intimidating to me as it would be to you!

Bob

14. ### CDRIVEHauling 10' pipe on a Trek Shift3

4,960
652
May 8, 2012
Justin, I've been working with you for quite some time now. I think the programming end of this will be less intimidating. Besides, I'd like to see how an ADC solution works out.

Chris

15. ### JPU

281
1
May 19, 2012

Chris,

I intend to start with the programming this week, so I will let you know how its going. I know I will need help at some point,,,soon!...

Thanks

Justin