Measuring time between highs of an irregular signal

Discussion in 'The Projects Forum' started by mortenfyhn, Nov 26, 2012.

  1. mortenfyhn

    Thread Starter New Member

    Nov 26, 2012
    7
    0
    I'm attempting to measure the time between blinks of a flashing LED. (Very brief flashes, but usually several seconds between.) I intend to make the detector part of the circuit like this:

    [​IMG]

    The other part will then recieve a brief 'high' signal every time the LED flashes. Do you have any ideas as to how I can measure the elapsed time between each pulse? The essential bit is to always store the time between the two previous pulses, and use that to calculate a value to be displayed on a 7-segment display. It would be neat to have it store every interval over the last hour or day, or even week or month. Then again, if at all possible, I would like to make a circuit that does the calculations not with a programmed microcontroller but rather 'hard coded' into the circuit. The calculation is simply to divide 3600 by the measured interval in seconds.

    I'm just getting into this stuff, so be gentle.
     
    Last edited: Nov 26, 2012
  2. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    Frankly, using a microcontroller you will get the functional circuit much faster and cheaper than if you do it the hard-wired way. With all that functionality you will need around 20 ICs I guess, and you won´t be able to remember more than a few last counts. Also think about the fact that if you prototype it on a pcb you will need a new one for each revision, and a breadboard will be very messy and unreliable. Wirewrapping might be a little less dreadfull, but still it will be huge amount of wires.

    If you simulate the circuit beforehand, you may be able to get it working on the first go, but simulating it will again take a lot of time to do properly.
     
  3. Dodgydave

    Distinguished Member

    Jun 22, 2012
    4,979
    744
    A scaler timer or use a digital stopwatch, ideally a pic can be used for this project
     
  4. mortenfyhn

    Thread Starter New Member

    Nov 26, 2012
    7
    0
    Got it, I think I'll discard the hardwiring plan right away.
    Do you have some clues to get me started with designing the reciever circuit? (The two parts will be separated by some five metres of cable.) I guess the first obstacle will be to record interval lengths and store them.
     
  5. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    My first thought is to get a better simulator. I recommend using micro-cap from spectrum soft, it has a pretty good range of part models. It is not as visual as the one you´re using, but it more accurate and realistic.

    Almost all microcontrollers have an analog comparator included, so the detector circuit could be as simple as the LDR-375k divider connected to one pin, and a potentiometer to set the threshold to the other pin.
     
  6. mortenfyhn

    Thread Starter New Member

    Nov 26, 2012
    7
    0
    I know I should get a proper simulator.

    I've wired up a LDR-375k divider now, soldered to a female 3.5mm stereo jack. Testing with a 6 meter cable (comparable to the actual length I will use), there doesn't seem to be any noise troubles. But I'm worried the LDR might be too slow. With the LED flashing every 1.5 seconds-ish, I get measurements jumping between roughly 1.5 and 3.5 V. This should be fine, but at some point it will be too sluggish to work properly. Maybe I ought to use a photodiode...

    Anyway, I've been reading up on microcontrollers. Can I simply timestamp every occurence of the signal voltage going from below to above a threshold? And could I store the timestamps in a non-volatile memory? (I understand some microcontrollers have flash memory.)
     
  7. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    Almost all microcontrollers have flash memory, but that is used for the program itself. For storing timestapms you would need some external flash memory like an sd card or a memory chip.
     
  8. mortenfyhn

    Thread Starter New Member

    Nov 26, 2012
    7
    0
    I understand. Do you know if interfacing with such memory would be a complicated matter? I consider simplifying the project to just using the most recently measured interval at any given time.

    I guess I haven't stated the actual intention of the circuit, but the point is detecting the LED in my fusebox, which flashes once for every watt-hour consumed, and calculating and displaying the current power consumption based on how rapidly it flashes.
     
  9. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    Interfacing external memory is pretty easy.
     
  10. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    A "timestamp" sounds very complicated, and thus expensive in terms of parts needed. If all you require is to know "how much power am I using right now" then all you need do is determine how long it has been since the last pair of flashes.

    Most any PIC should be capable of doing this.

    As far as detecting the pulses, consider using your sensor to turn on another LED: sure, in terms of the system this is pretty useless, you've only moved the LED closer, but if the two LEDs flash together then you *know* you are properly detecting the flashes even without a computer to make the measurements.

    Finally, a 7 segment display would be my last choice for this. It is a lot of wires, a lot of power, and all you get are numbers. For about the same number of I/O lines (and probably less) you can get a very nice alphanumeric LCD character display that runs on very little current. I've used a 20 character by 4 line display I get off EBay in several projects now, they cost me $10.49 (free shipping) each.
     
  11. mortenfyhn

    Thread Starter New Member

    Nov 26, 2012
    7
    0
    That's pretty much true, although I'm not sure if I understand you correctly when you say time since the last pair of flashes. What I will need to measure is the time between the two most recent flashes.

    The idea of a second LED sounds very clever. I would need some way of checking that it works properly, after all. Do you think I should I wire the LED directly to the signal voltage (in an analog fashion), or wire it 'after' the microcontroller?

    I suppose I could use an LCD instead. The arguments for a 7 segment are lower cost and better visibility (I assume). But hell, I can order a 16 by 2 LCD or something. I don't think I'll need a larger one.

    Is there something that makes a PIC especially well suited? I don't know how it differs from, say, an AVR.
     
  12. KrisBlueNZ

    Member

    Oct 17, 2012
    111
    14
    I would make a small module to go next to the LED, containing an LDR or a photodiode or phototransistor, and a buffer. Something like a CD40106 would be fine. Use decoupling capacitors. This module would connect to your main board with a three-wire cable - power, ground, and signal. The length of this cable would not affect the signal quality much, since the buffer would drive the signal line strongly. You could monitor the signal with an LED at the main board end.

    The choice between LDR and photodiode/phototransistor depends on the duration of the blink on the LED, since LDRs respond relatively slowly. Presumably this duration is fixed; only the gap between blinks is variable. If you can get a good clean signal out of an LDR in response to the blink, then you can use an LDR. Otherwise you'll need a photodiode or phototransistor.

    PIC and AVR are somewhat similar in terms of features and built-in peripherals. The PIC's architecture is a lot cruder in my opinion, but both can be programmed in C and you can get "BASIC stamp" modules for the PIC which may be the easiest way to get your application up and running. The BASIC implementation will have support for the PIC's peripherals, which (if you choose a suitable device) will include a free-running timer with input capture, which will timestamp transitions on the signal. You can calculate intervals, convert them to kW figures, and store them in an array, output them via SPI or a parallel interface to a display, and so on. Google PIC BASIC stamp.

    Edit: I've never used BASIC stamps so I'm not sure how well they support the PIC peripherals. I'm just assuming that they would, since they would be pretty dumb if they didn't.
     
    Last edited: Dec 6, 2012
Loading...