Need help with simple counter circuit design

I am a newbie interested in building a circuit that can count the number of times a voltage level exceeds 2 volts.  Long story but I have an EMG device that will output 2 volts once a threshold is exceeded.  There is no way to automatically capture the number of times this happens.  Can anyone point me to a project kit that I could purchase or to a circuit with a component list that I could build?  I am fairly new so hopefully this circuit would not be too complex.  While not critical if it is also possible to capture the average duration the level stayed above 2 volts that would be great.  Understanding that this may complicate things beyond my ability the simple counter without duration would suffice.  Thanks.?

sort by: active | newest | oldest
The way to do this then is to use an Arduino chip, but probably not a standard board.

You need as little power consumption as possible, so the way to do this is to arrange the processor so that it "sleeps" most of the time, and wakes up briefly, does something, then goes back to sleep. If its awake 1% of the time, the processor consumes 99% less electricity.

When the chip wakes up, it checks the analogue inputs, if its above 2V, it increments a counter, then it goes to sleep again.

Steve
fsudal (author)  steveastrouk5 years ago
Thanks for all of your suggestions. Seems like they all point to the Arduino board. I read up a little on it last night and will pick one up at Radio Shack along with a guide today. Adafruit is selling a data logger with an example of how to capture how often a refridgerator door is opened and what happened to the temperature. I wonder if I can modify their data logger to accomodate my project (how many times input went to 2+ volts and how long it stayed above 2 volts each time it happened. Any suggestions while I am learning all of this on how to go about it would be further appreciated as I don't know C code. I'll post back on how it turns out but it may be a while as I am starting at square one. Thank you for pointing me in the right direction.
I didn't know you could pick them up at Radio Shack - they must have raised their game recently. Last time I was in the USA the Shack had hardly anything.

Come back when you need some more guidance !

Steve
fsudal (author)  steveastrouk5 years ago
I stopped by my local RadioShack today but they did not have any kits just the board so I went to MicroCenter instead. They had the Sparkfun Inventor's Kit with the Arduino UNO board and a guidebook for beginners like me. I was able to download the software on my computer and get a few basic circuits put together and programmed. As a newbie it is all a bit overwhelming.

I hate to ask this but could you or someone help me get a simple counter circuit up and running? Then I can at least get some data logging in and as time goes buy and I get more comfortable I can do more advanced things. Basically I would just like help getting started with being able to increase an led readout by 1 every time an input goes high (2.5 volts). The kit did not come with an led readout so I would also need to know what to buy in addition to any code that would be needed to program the UNO. Sorry for being so needy. Just want to get up and running while I take the time to learn and get on my own feet over time. Any help would be greatly appreciated. Thanking you in advance for your help.
Lets just use your PC to log events for now. Add a display later.

Steve
fsudal (author)  steveastrouk5 years ago
Thanks Steve. Good suggestion. Learning about the serial print function now. Any thoughts on the best place to hook up the positive and negative leads on the Arduino UNO board that will either have 2.5V or 0V on it? Do you know of any code that I could cut and paste to get started with?
Why ? Just use the standard 5V referenced A/D. It has an output of 0...1023 - if you measure the voltage and see its >2 *1023/5, or 512, its more than 2V.

Steve
fsudal (author)  steveastrouk5 years ago
Hi Steve,

I got my Arduino up and running.  Can you or anyone take a look at this code and let me know where I went wrong? I want to start out by counting how many times an alarm was triggered by detecting 2 volts on pin 2. When I upload the below code to my UNO it counts up with no voltage on pin 2. Then when I apply 2 volts it stops counting. If I ground pin 2 it stops counting.  How can I get it to count up only when 2 volts is detected on pin 2?
****************************************************************************
//Count how many times alarm was triggered when 2 volts is detected
//on pin 2
//When alarm is triggered 2 volts will be sent to pin 2. When alarm is off
//no voltage will be sent to pin 2

int alarmVoltage = 2;
int val;
int alarmState;
int alarmTriggered = 0;

void setup()  {
  pinMode(alarmVoltage, INPUT); 

  Serial.begin(9600); 
  alarmState = digitalRead(alarmVoltage); 
}

void loop() {
  val = digitalRead(alarmVoltage);
  if (val != alarmState)  {
    if (val == HIGH)  {
      alarmTriggered++;

      Serial.print("Alarm Triggered ");
      Serial.print(alarmTriggered);
      Serial.println(" times");
    }
  }
  alarmState = val;
}
Ah, you haven't defined your input as ANALOGin !

Here's a basic example for you.

http://arduino.cc/en/Tutorial/AnalogInput

You can modify it to use sensorvalue like this:

if (sensorvalue>= 512) {
alarmtriggered++
Serial.print("Alarm Triggered ");
Serial.print(alarmTriggered);
Serial.println(" times");
}

//Add a delay of a few milliseconds

delay(400);

That can be prettier, but it will work.

steve
fsudal (author)  steveastrouk5 years ago
Thanks Steve! I just discovered that the EMG unit doesn't just put out 0 or 2 volts like I expected. It actually puts out voltage that varies between 0 to 2 volts based on the strength of the signal. This means that I can plot the events not only by the number but I will also be able to monitor the varying intensity. Based on this new information I followed your suggestion and looked over your example in addition to the one you pointed to and came up with this so far;

int sensorPin = A0;
int sensorValue = 0;

void setup() {
Serial.begin(9600);
}

void loop() {
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
delay(250);
}

Is there a way that I can convert the value to represent the actual voltage values for the serial printer? This way I see 2.5 volts instead of 512?

Thanks again for all of your help.

p.s. I will be incorporating your alarm code later once I figure out how to connect a buzzer that will sound once the alarm has been triggered.
Sure. Input "volts" = ADC number *5/1024
fsudal (author)  steveastrouk5 years ago
Nice! Thanks again for your help.
Note that 5/1024 is a CONSTANT. Don't evaluate it every time you need a voltage - save it AS a constant - 0.005, near enough.
You are going to have a lot of data after a night's sleep, if you aren't careful, and you'll need to add some more memory to the arduino.

Steve
fsudal (author)  steveastrouk5 years ago
Thanks for the heads up Steve. I just received an SD data logger from adafruit today. Spent the evening putting it together and testing out the card and clock. Hopefully this will solve the memory issue.
Now get even more cunning and time stamp the readings, so you can perhaps plot when you exceed your threshhold.
framistan5 years ago
I would use a simpler solution rather than a micro-controller. Just use a single IC called a "comparator." The LM339 quad comparator would do this nicely. This device has 4 comparators, but you only need to wire up to one of them. Each comparator has 2 inputs. One input is set to whatever voltage constantly you want to use as a reference voltage. The other input can go up and down from the changing source. If the input exceeds the other input then the output GROUNDS to negative. (it does not put-out a voltage at the output, it connects to ground (negative). Now all you need is a counter. Ebay sells electronic counters that you only need to connect to 5 volts. Just connect the comparator's output to the counter's UP-count wires.
fsudal (author)  framistan5 years ago
Sweet! I like this idea as it is simple. I found the LM339 at RadioShack but could not locate a display counter on ebay. A lot of pedometers come up when I search for "electronic counter". I tried a few other variations but didn't see anything obvious. Is there a favorite counter that you would use in this case that you could point me to? Thanks again for your help and suggestions.
Easiest way, is to use a processor to measure the voltage directly and make a counter in software.

Steve
fsudal (author)  steveastrouk5 years ago
Not sure how to do this. Counter also needs to be battery powered due to the sensitivity of the EMG device. Let me provide more context. I grind my teeth at night and have begun to use an EMG device (Myotrac 4000) that detects the small electrical signals from my jaw muscle. When I grind the threshold that I set on the device is exceeded and an audible alarm sounds. Since I am sleeping the sound disturbs me just enough to stop grinding but not enough to wake me up. The problem is that I have no way of knowing how I am progressing since there is no way to capture the data (number of grinds per night). Fortunately the device has an output that will produce 2.65 volts when the threshold is exceeded (otherwise it is 0 volts). I wanted to take this on/off signal and somehow input it into some kind of counter circuit that would capture the number of times the threshold is exceeded. This way when I wake up I can see the number of times the alarm went off. Now I can begin to see what may cause me to grind more (caffine consumption, stressfull day) or less in an effort to "teach" myself not to grind anymore. Maybe too much info for you but at least you have a better idea of what I am trying to accomplish. Also, I know enough to be dangerous and can solder but I would consider myself at the novice level. So any help would need extra clarification. Thanks again for your interest.
frollard fsudal5 years ago
Steve has the correct response, EASIEST way is to build a circuit with a microcontroller that listens for that voltage. Steve will think I'm beating a dead horse by suggesting an arduino, but really it's one of the easiest to learn as uC projects go. In software you can log internally, to an sd card, or even spit it out a serial port where you log the results in a file then graph them.
You'd have to really work hard to use a stock arduino, because you need to put the processor to sleep often.
http://hackaday.com/2009/08/13/sleepy-arduino-saves-batteries/
There's a really good project where its described in a sketch called "nightingale"
For this it's appropriate :) Overkill if you were manufacturing them, but for ease of getting a working prototype an arduino or other prebuilt platform's fine :D

fsudal, out of interest, what's the application? I read the Wiki article on Electromyography but was just interested to know what you're up to :D

James
fsudal (author)  Jayefuu5 years ago
I grind my teeth at night and have begun to use an EMG device (Myotrac 4000) that detects the small electrical signals from my jaw muscle. When I grind the threshold that I set on the device is exceeded and an audible alarm sounds. Since I am sleeping the sound disturbs me just enough to stop grinding but not enough to wake me up. The problem is that I have no way of knowing how I am progressing since there is no way to capture the data (number of grinds per night). Fortunately the device has an output that will produce 2.65 volts when the threshold is exceeded (otherwise it is 0 volts). I wanted to take this on/off signal and somehow input it into some kind of counter circuit that would capture the number of times the threshold is exceeded. This way when I wake up I can see the number of times the alarm went off. Now I can begin to see what may cause me to grind more (caffine consumption, stressfull day) or less in an effort to "teach" myself not to grind anymore.
fsudal (author)  frollard5 years ago
Thanks frollard. Would you be able to point me to anything that would provide step by step instructions on how to do this with something that is battery powered (cannot use ac due to the sensitivity of the emg device)? Meanwhile I will google "circuit that listens for voltage" and "arduino".
frollard fsudal5 years ago
www.arduino.cc

there are example programs to do all of the functions mentioned above. It's a good idea to start small and add features. Every pin and function is described on that page with examples for each.
How fast are these events happening ? Typically how long do they list ? How accurately do you want to measure the timing ?
fsudal (author)  steveastrouk5 years ago
Hi steveastrouk,

Thanks for your quick reply. The events may happen every few seconds or not for a couple of hours. My guess would be that there would not be more than 999 events in any given monitoring period (typically 8 hours). The events may last from a couple of seconds to under an hour each. I would like the counter to be fairly accurate but if it's off by a couple no worries.

I wonder if timing the entire time the signal was above 2 volts vs. trying to create an average would make things easier? Of course just the counter would suffice. I greatly appreciate your interest and look forward to your response. Please note that any circuit would need to be battery powered. The EMG device is very sensitive to electrical signals and will not work on an anything plugged into an outlet.