Voltage comparator?

abenn

Senior Member
I have a situation where I want to detect when two independent (though with a common ground) LEDs are lit and, if so, switch on another array of LEDs. I know I can do it with an 08M2, but is there a simple electronic device that will switch, like a simple transistor does, only on receipt of two valid separate inputs rather than just one?
 

cravenhaven

Senior Member
Three attempts at an answer:p.
A couple of diodes wired as an AND gate might do it.
Depends on how the controlling LEDS are switched, are they switched on the high side or the low side?.
 

abenn

Senior Member
Thank you for the replies.

In most cases the LEDs are switched, via transistors, on their high side. But there are two pairs that are switched on their low side.

The logic I require is AND, i.e. both LEDs on results in a whole bunch of other LEDs switching on. I anticipate the total load will be less than 100mA.

I hadn't heard of the two-diode solution, but stan74's link makes it look simple. I still don't understand how the AND configuration works -- it seems to be two diodes each blocking their inputs, so how does it manage to switch the output? And presumably the circuits need to have a common ground with the LEDs that are to trigger them?
 

rossko57

Senior Member
I still don't understand how the AND configuration works -- it seems to be two diodes each blocking their inputs, so how does it manage to switch the output?
By default, the AND gate's output is pulled high, by the resistor connected to +ve. If either of the diode inputs is pulled low, the tiny current through the resistor is diverted through the diode and the AND gate's output is pulled low. Overall effect, both inputs must be high (or disconnected) for the output to be high. Logical AND.

Yes you'd need a common ground - you could have reasons to avoid that (mains powered kit, ground loop through several different boxes, etc.) in which case you might want to look at opto isolators connected across the target LEDs.
 

abenn

Senior Member
Thanks rossko. But isn't the input effectively disconnected if the LED supplying the input is off? So both input LEDs in the same state (on or off) would trigger the output? I only want to trigger it if both of the inputs are high.

If I'm wrong in my understanding, looking at the circuits in stan74's link, it looks to me like I need something like the first circuit in the bottom row (two diodes and a transistor) to avoid the circuit drawing too much current from the source LEDs, and to handle enough current for the other LEDs which it's switching. Would that be right?

Is this basically a 2-diode AND gate in a single package?

The two LEDs which will supply the inputs already use a common ground, and are controlled by separate 08M2 circuits which share a common mains-sourced 5v DC power supply. My proposed logic circuit and extra LEDs will also share the same power supply.
 

sghioto

Senior Member
Is this basically a 2-diode AND gate in a single package
Technically it's a NAND gate meaning the output is LOW when both inputs are HIGH. Should work, connect the inputs to the transistors controlling the LEDs on the high side with a say 100K resistor to ground.

Steve G
 

abenn

Senior Member
Thanks sghioto. I'm still less than an amateur at understanding spec. sheets; what I'd like to know is how many mA that device (or the 2-diode solution, or similar) will consume from the input voltage source. In see Iik is -50 to +20mA, or should I be looking at Iin (10uA) for that information?
 

abenn

Senior Member
Thanks stan. In fact I may be able to use a multi-channel one, provided it doesn't "suck" too much current from the LEDs it's sensing (please excuse my technical terminology :) ).

The reason is, on the control panel where I want to monitor them, the LEDs are supplied with 5v 10mA regulated current from remote sources. If I have to go back and tap into the +5v before the regulator, it means more long wires from the remote sources, which I'd like to avoid.
 

sghioto

Senior Member
what I'd like to know is how many mA that device (or the 2-diode solution, or similar) will consume from the input voltage source
Around 100ua total using 100K pull down resistors with a 5 volt supply at 25 degrees Celsius. Most of the current drain is from the pull down resistors. The standby current draw on the NC7SZ00 is 20ua in worst case conditions according to the specs.

Steve G
 

AllyCat

Senior Member
Hi,

In situations like this, there are two factors which need to be cosnsidered: are the control signals "Active High or "Active Low" and do the switches give "On/Off" or "High/Low" (Logic level) signals. An "Active High" (Positive Logic) AND gate is equivalent to an "Active Low" (Negative Logic) OR gate, and vice versa.

The outputs from a PICaxe will give true logic signals (i.e. approximately 0 and 5 volts) but a single transistor or relay contact will normally only "pull up" or "pull down" (depending whether it is connected to the supply rail or to earth). Some loads such as a filiament lamp or a "brushed" motor will pull the drive to the opposite logic level, but a LED is a diode which may have a significant forward voltage drop (several volts, above a typical "0" logic level, for blue and wite LEDs).

"Active Low" drive to higher powered devices (such as relays and LEDs connected directly to a positive supply rail) is often used (and may be preferable) but you have said that your LEDs are connected to ground. So they are driven "Active High", but by a transistor, which (only) pulls High. Therefore, the LED may be left to "float" when it is off, but adding a pull-down resistor for each "logic" input is probably all that's needed.

As mentioned above, the "two diodes" are probably your best solution, but it may be necessary to consider the factors above. The two diodes actually form an "Active Low" OR gate: i.e. if either input 1 OR input 2 is pulled low then the output must be low. Thus it can be considered as an "Active High" AND gate. But the diodes can only pull Low, so you may need a pull-up resistor to guarantee the "High" output voltage level.

For most Logic gates a weak pullup will be sufficient (>> 10 kohms), but the base drive to a bipolar npn transistor may need a lower resistance. The pullup current (or resistance) then determines how much pull-down needs to be provided through the diodes. Typically the pull-down resistor(s) should be several times smaller than the pull-up resistor, but normally still could be a mA or less.

Cheers, Alan.
 

hippy

Technical Support
Staff member
Not knowing what the polarity of the inputs are, not necessarily sure what the logic function or output polarity should be, are some of the reasons small micros like the 08M2 may be chosen over simpler and cheaper logic circuits.

Connect it up, run the code and get it to report what the inputs are for the conditions one wants to set the output for. Then write some code for that. Ensure it has correct output polarity and job done.
 

abenn

Senior Member
You're right hippy. My initial instinct was to use an 08M2 (or maybe a larger one, to cater for more "channels"), but I then thought that maybe there's a simpler, cheaper solution that needs no programming, and hence this thread. Turns out there is, but it's becoming evident to me that I might be better off going the PICAXE route because I don't have the electronic expertise to know what kind of input I've got anyway, and then whether or not I need pull-up and pull-down resistors, and what value they should be.

sghioto and AllyCat, thank you for your comments. I think I've got a few diodes in my box of bits, so I'll have a quick dabble to see if I can make a circuit that'll do what I want. But if it takes me too long I'm going to switch over to the PICAXE solution :)
 

abenn

Senior Member
This afternoon I've experimented with a basic 2-diode AND gate, and can't make head nor tail of it, so I'm going to go for an 08M2 solution.

Following is diagram of my proposed interface. Note that the LED is driven by an 08M2 with it's own 5v power supply, via a transistor switching its +ve side. The 08M2 that will be used to detect if the LED (and one other one from yet another independent circuit) is on or off will have its own 5v power supply. Is this the correct way to do it? And can I tweak the two resistors' values so that the detecting 08M2 will siphon off as little as possible of the power from the lit LED? My Plan B is to tap into the LED's supply before the 10mA driver, in which case current draw would not be an issue, but that involves extra wiring from the remote circuit that's driving the LED, or moving the 10mA driver from the remote circuit board to the LED's lead on the panel in which it resides.

ScanImage002.jpg
 

BeanieBots

Moderator
All other issues aside which others have commented on, sticking just with the diagram you have posted:-
You do not need the 10k resistor unless PICAXE #2 is ever to be disconnected which would leave the input floating.
I would change the 1k resistor to a higher value such as 22k just in case PICAXE #1 was ever powered when PICAXE #2 was not powered to avoid high current going into PICAXE #2 input.
The voltage across the LED will be quite low compared to 5v. About 1.2v if the LED is a red one. This will not be enough to give a reliable digital input so use ReadADC to detect if the LED is on or off rather than testing for input high/low.

If you have several such inputs and put a high-ish value resistor (say 22k) on the PICAXE input side, you could use ReadADC to determine if one or more LEDs are on.
 

AllyCat

Senior Member
Hi,

"Pin 0" is the serial (programming) output on an 08M2, so it's not the most appropriate pin to use (but it's not impossible, even to read an ADC value).

If the "5v" is a switch (or a pull-up transistor) then IMHO the 10k resistor is necessary. But as BB says, a Red LED may limit the voltage to below the digital threshold; other colours, particularly blue and white should be sufficient. Or a READADC value of less than 10 should indicate that any LED is off.

Cheers, Alan.
 

abenn

Senior Member
Thanks guys.

The LED is a green one drawing 10mA (limited by the LED driver), but it's driven from the 08M2 by a transistor because there's two solid-state relays in parallel with it, drawing more current.

I'll experiment with higher resistor values to see what will give me a detectable voltage change between the LED being on and off, but knowing that the circuit is viable is a good step forward for me. The transistor driving the LED is switching the LED's positive 5v supply on and off, with the LED's -ve leg permanently connected to ground -- does that make it "pull up"? I included the 10k resistor because the configuration seemed to me to be similar to one using a switch as the input in the PICAXE manual.

By the way, I won't be using Pin 0 -- that was just what's on the cut-and-paste I did from the manual.
 

BeanieBots

Moderator
A green LED will give more volts than red but it will still be a bit low for tripping a digital input.
Some inputs are TTL, some are CMOS and some can even be switched to be either but let's not get too complicated here.
I would suggest going for something that you know will work. Namely, the use of ReadADC. Test using sertxd to get an idea of ADC values and choose a suitable value for detection.
 

abenn

Senior Member
Sorted!

I've made up a prototype board with a very basic circuit and an 08M2, omitting the 10k resistor to ground, and using 22k between input and PICAXE pin c.1. The ADC value with a lit green LED is somewhere around 33, so by testing if ReadADC is greater than 20 I think I've got a reliable trigger.

Thank you for all the suggestions.
 

AllyCat

Senior Member
Hi,

..omitting the 10k resistor to ground, and using 22k between input and PICAXE pin c.1.
Hmm, I don't quite know what you mean by that.

But, the voltage across a Green LED applied to a PICaxe READADC input via 22k (which is a little higher than Microchip recommend for an ADC measurement) should read at least 50, normally nearer 100. Also, with no pull-down resistor (10k) there is a risk that the voltage may "float" at an ADC input level of more than 20, with the LED off. :(

Cheers, Alan.
 

abenn

Senior Member
Thanks AllyCat. I was referring to my diagram in post #16 on the previous page. There was a suggestion in post #18 that I don't need the 10k pulldown resistor so long as my circuit will never be disconnected from the LED its monitoring, which is the case. It was also suggested that I could use a 22k resistor, and it seems to be working okay.

The LED I'm monitoring has its current limited by a 10mA "LED driver" chip -- would that be why my ADC values are less than you expect? I've run my test program with an ADC trigger value as low as 5, and it still reliably (so far!) switches on and off in sympathy with the monitored LED. Also, disconnecting it from the monitored LED altogether is the same as when connected with the monitored LED off.
 

AllyCat

Senior Member
Hi,

For a green LED to emit light, it must have around 1.5 volts across it, maybe more and certainly not less than 1 volt. The PICaxe ADC gives a value of 255 if the input is as high as the reference voltage, which cannot be much moire than 5 volts (the supply rail). So it should indicate about 50 for each input volt, so typically 75 for the LED.

Not doing so suggests that less than 1.5 volts is reaching the PICaxe pin. I can think of (only) two reasons: either there is a resistive load (perhaps around 10k) directly on the ADC pin to ground, or the "LED driver" is using PWM so that the PICaxe sees a lower average voltage:

Normally the PWM would only switch the current on and off and the average voltage wouldn't change much (because a LED normally has a very high impedance at lower voltages). But perhaps the driver actually contains some type of pull-down circuit; that would also explain reading a very low voltage when the LED is "off". If it is using PWM, then that would be a very good reason for NOT using a digital input to detect if the LED is alight!

However, when you unplug the PICaxe connection, then the pin would seem to be left "floating". There are numerous threads on the forum where "unexplained" erratic behaviour is eventually traced to a floating input, so I still believe that including a pull-down resistor is a "worthwhile" addition (even if not actually essential).

Cheers, Alan.
 

abenn

Senior Member
Thanks for that explanation AllyCat. The LED driver I'm using is this one. I'm not knowledgeable enough to understand how it works, but your explanation about PWM sounds reasonable, since there's no other connection to the ADC pin.

Just one last clarification, please: When you say NOT to use a digital input to detect if the LED is on, I see that READADC reads the analogue value, so what would be an example of a digital input to read voltage?
 

sghioto

Senior Member
That LED driver is not a PWM device but a linear constant current regulator or basically a variable resistor. You can also connect it on the low side between the cathode of the LED and 0 volts. May have enough voltage across the driver to detect as a digital input. A digital input means there needs to be enough voltage to be detected as a "high".
 

AllyCat

Senior Member
Hi,

Yes that's a simple (non-PWM) "constant current" generator, so I (now) have no explanation why the ADC is reporting a much lower value than expected. The majority of Green LEDs are specified as 2.5 - 3.0 volts (at a somewhat higher 20 mA current) which would suggest an ever higher ADC voltage should be detected. I presume you don't have a multimeter to check the voltage?

The digital threshold voltage is typically 1.5 volts above ground (except IIRC C.2 is a ST input, which may be rather higher, in an 08M2) and most commands will read the digital value, for example: IF pinc.1 = 1 THEN : SERTXD("c.1 is high") : ELSE : SERTXD("c.1 is low") : ENDIF .

Cheers, Alan.
 

abenn

Senior Member
Just in case anyone's interested, I've now constructed my prototype using SMD devices, and have tested it using two green LEDs which are fed from separate 5v sources (with common ground) via 330R resistors to keep their current to 10mA -- instead of the 10mA LED drivers I used before. No other changes from my previously-described circuit.

This time the AVC value for a lit LED is closer to what has been suggested it should be, and I've been able to set my trigger at 90 for consistently reliable detection.
 
Top