Using an op amp as a voltage comparator

AKRichard:
I couldn't get it to work with the single transistor, no matter what I tried I could not get the full 12V out of the transistor with the 5v signal from the arduino for some reason (the transistors I was using are the 2n3904 and 2n3906). Granted I do not know and understand a whole lot about transistors, but if Im understanding it correctly the base of a npn transistor needs to be forward biased with respect to the emitter (which I took to mean that the base needs to be at a higher potential compared to the emitter which the arduino cant do when youre trying to run a 12v source through the transistor). I thought Id outsmart it and use a pnp transistor at that point (since it works in the opposite direction I knew I could get the base to a lower potential compared to the emitter just by sending the base to ground), however I couldn't get it to work that way either.

If you look at your relay circuit, you have the relay coil in the EMITTER circuit, you need it in the COLLECTOR circuit as NPN transistors can only LOW SIDE SWITCH.
You are trying to HIGH SIDE SWITCH so you will need a PNP and a NPN.
Like this;


Tom.... :slight_smile:

If you have an Ohm meter, you can measure the coil resistance of your relay. The static coil current will be the vehicle's nominal system voltage, about 14.5 V when running, divided by this resistance. I'd expect the coil resistance to be something like 50 to 100 Ohms, so a nominal coil current on the order of 200 mA.

One thing that follows from this is that a 2N3906 PNP transistor has specified performance to 100 mA, so you probably want to select a part with higher current capacity.

Bipolar transistors are current controlled devices. The current that can flow from collector to emitter is proportional to the base current. Transistor datasheets typically give a set of curve showing the nominal relationship of these two currents. I've attached an example (this is from a ON-Semiconductor 2N3906 datasheet). The curves show the relationship of collector-emitter voltage drop VCE versus base current IB for a handful of collector currents IC. For example a base current of 6 mA with a collector current of 100 mA (the rightmost curve), gives a VCE of about 0.3 V. For a switching application one wants a small VCE so most of the voltage is dropped across the load (your relay) and less power is dissipated in the transistor.

The design process then goes something like this:

  1. Determine the load current. In this case it is nominally the system voltage divided by the coil resistance.
  2. Select a transistor spec'ed to at least this current. It's good practice to use a device spec'ed to at least twice the nominal current.
  3. Calculate the base resistor value to get small VCE at the load current. To get the 6 mA base current for the example above, the resistor should be no greater than about 12 V/6 mA = 2000 Ohms.

TomGeorge:
You are trying to HIGH SIDE SWITCH so you will need a PNP and a NPN.
Like this;


Tom.... :slight_smile:

apologies to the OP for butting in with a side-track.

but this layout is like a Darlington pair, right ?

except with a Darlington transistor(pair), both are of the same type, PNP or NPN.

this is the first time i'm seeing a circuit with different types of transistors used together that way... "Darlington" way, if it can still be called as such.

Nothing to do with a Darlington. It's just two switches, an inverter (npn) driving a high side switch (pnp) for the relay. Nothing more, nothing less.

For a darlington, imagine two npn transistors, where the emitter of the first feeds into the base of the second, and the collectors both go to the supply.

I've measured auto relays, and they're typically 130 ohm coil resistance.

So your switch device has to handle 100mA or more.

A logic level mosfet - eg the IRL44Z - with a very low Rds on, with the source grounded and the drain to one end of the relay coil would be fine. as per reply 21

Alan

avr_fred:
Nothing to do with a Darlington. It's just two switches, an inverter (npn) driving a high side switch (pnp) for the relay. Nothing more, nothing less.

i see.

and to think i'm attempting to understand the myriad of op-amp configs, i haven't even fully grasped transistors yet !!

ChrisTenone:
For a darlington, imagine two npn transistors, where the emitter of the first feeds into the base of the second, and the collectors both go to the supply.

ahh yes - that's the difference... i was just going by the 'base' connected to the other.

Thanks ! :slight_smile:

You are trying to HIGH SIDE SWITCH so you will need a PNP and a NPN.
Like this;

TomGeorge, I put the circuit together the way you had it drawn and the new transistors work like a charm, the relays are working as they should. In fact the entire circuit appears to be working as it should. Thank you for that.

If you have an Ohm meter, you can measure the coil resistance of your relay. The static coil current will be the vehicle's nominal system voltage, about 14.5 V when running, divided by this resistance. I'd expect the coil resistance to be something like 50 to 100 Ohms, so a nominal coil current on the order of 200 mA.

One thing that follows from this is that a 2N3906 PNP transistor has specified performance to 100 mA, so you probably want to select a part with higher current capacity.

I am going to measure the resistance across the coil tonight and see if I can calculate what size of resistors I should be using. Im going to hook the breadboard up to my bench motor and try it out. If everything continues working as it should, then its on to the next step, add the circuitry to monitor the batteries so that it can figure out for itself when to start and stop the motor. I have a few ideas on how I might achieve this end, so we will see.

Unless I have difficulty calculating the resistor sizes myself, Id have to say that my problem has been solved and I learned a little bit more along the way. Id like to thank you all for taking the time to help out. It is appreciated. I have a included a schematic of the circuit as I have it now, it has a .fzz extension to it (created with the fritzing software).

LED3 and LED4 are wired incorrectly, you cannot mix your 12 volt and 5 volt supplies in that way. The LED’s need to be powered from the 12volt rail with the appropriately higher value resistor. You’ve also omitted the very necessary free wheeling diodes across the relay coils. The PNP transistors may/will fail in short order, depending upon the device type.

PS: please don’t promote bad schematic habits with Fritizing, like drawing diodes and ground symbols upside down. That software is a waste of everybody’s time and energy. Either use a real schematic editor or draw them by hand which highly preferable over fritzzy silliness.

Screenshot (6).png Click on the image for full resolution.

avr_fred:
LED3 and LED4 are wired incorrectly, you cannot mix your 12 volt and 5 volt supplies in that way. The LED’s need to be powered from the 12volt rail with the appropriately higher value resistor. You’ve also omitted the very necessary free wheeling diodes across the relay coils. The PNP transistors may/will fail in short order, depending upon the device type.

PS: please don’t promote bad schematic habits with Fritizing, like drawing diodes and ground symbols upside down. That software is a waste of everybody’s time and energy. Either use a real schematic editor or draw them by hand which highly preferable over fritzzy silliness.

I forgot to include the diodes in the schematic they are in the circuit though. About the led's though, that brings up the question of I am powering the arduino directly off of the 12v batteries through the 2.5mm jack. You mentioned I shouldnt mix the voltages like that, is that just for the positive side, the reason I ask is because everything goes to the ground rail on the breadboard (no matter if its being fed from the +5 or +12 supplies). Even though the led's work the way I have them wired, I want to follow best practices. So if I figiure 12v starting voltage down to 5v at about 20 ma Id need about a 350ohm resistor?

No problem about the schematic. Ill look around for something better.

Why not power the LEDs from the Arduino pin through a 1k resistor - you are allowed to drive both a
transistor and an LED from the same pin, so long as total current is within the limit. You only need one
transistor per pin to drive the relay.

So pin -> 220 ohm resistor to NPN base, relay + diode between collector and +12V, emitter to ground.
(This is the standard circuit to drive a relay, lots of examples out there).
Add LED + its 1k resistor from the Arduino pin to ground.

Much simpler, and saves 4 resistors and 2 transistors!

MarkT:
Why not power the LEDs from the Arduino pin through a 1k resistor - you are allowed to drive both a
transistor and an LED from the same pin, so long as total current is within the limit. You only need one
transistor per pin to drive the relay.

So pin -> 220 ohm resistor to NPN base, relay + diode between collector and +12V, emitter to ground.
(This is the standard circuit to drive a relay, lots of examples out there).
Add LED + its 1k resistor from the Arduino pin to ground.

Much simpler, and saves 4 resistors and 2 transistors!

I tried to get the one trasnsistor setup to work but never could get the full 12v out of it, but I didnt have it hooked up quite the way youre talking about, Ill give it a try tonight. Anything that saves on parts and pieces would be a savings at this point. The breadboard is already getting crowded and all I have is the circuitry in this post plus the battery monitoring circuitry that im tryin out

AKRichard:
I tried to get the one trasnsistor setup to work but never could get the full 12v out of it, but I didnt have it hooked up quite the way youre talking about, Ill give it a try tonight.

Then you probably were using the wrong circuit or not providing enough base current. Switching
uses the common-emitter configuration, and base current needs to be 5 to 10% of load (collector) current
with a BJT.

Go on Ebay and buy a couple of ZVN4206A relay drivers. They work like an npn transistor (2N2222) but don't need any additional components. No resistors, no diodes. All that stuff is built in.

Then go back to Ebay and buy a couple 12v auto relays with sockets. They are real easy to hook up.

As the previous post suggests, I don't understand why you think you need a comparator.

Just hook up your relays to the Arduino using the ZVN4206A relay drivers. If you can't figure that out, I would be happy to make you a drawing.

I think the best free software to draw schematics is ExpressPCB. You can download both PCB layout software and Schematic software for free. All you need is the schematic software (but you will get both)

Google ExpressPCB.

The schematic software will allow you to export the drawing as a BMP file. Bob's your Uncle..

Greg

Just hook up your relays to the Arduino using the ZVN4206A relay drivers. If you can't figure that out, I would be happy to make you a drawing.

I just looked up the spec sheet for the ZVN4206A and have just a few questions. One the pins are labeled dgs is that for drain, ground, and source. hook up would be source to +12v, drain to relay coil, and ground to one of the arduino pins? assuming that is correct I would shut it down by switching the pin its on to high? and I can turn it completely on/off with the 5v from the arduino?

If thats the case, Ill by them right now (pack of 50 for 20 bucks heck ya).

Then go back to Ebay and buy a couple 12v auto relays with sockets. They are real easy to hook up.

I already have the relays Im going to use wired in under the dash, Ive been using them for a few years now, but I have the run relay wired to a toggle switch, and the start relay is controlled by a momentary contact button that is supplied with power through the aforementioned toggle switch.

I think the best free software to draw schematics is ExpressPCB. You can download both PCB layout software and Schematic software for free. All you need is the schematic software (but you will get both)

Ill check them out, thanks for the info.

Then you probably were using the wrong circuit or not providing enough base current. Switching
uses the common-emitter configuration, and base current needs to be 5 to 10% of load (collector) current
with a BJT.

I didnt have a chance yet to try it out the way you are talking yet, I will as soon as I get a chance life is just getting in the way at the moment and demanding most of my time.

Here is the Application Note for the ZVN4206

http://www.gregssandbox.com/arduino/ZVN4206AV%20relay%20mosfet%20Application.pdf

Yes you hook it up as an open collector device. D=Collector G=Base S=emitter

+12 to the relay coil
D to the relay coil
G to the Arduino Pin (High will turn it on)
S to ground

Hold the device with the flat side towards you and the legs down.

S G D

Hello again all,

Thanks for all the help yall have givin so far. The project is up and running correctly. I have the basic board built and running on the test bench and fixing to install it in the vehicle. I have it running correctly using the transistors to run the relays. The software side of it has been completely refactored and I wrote a little program in visual studio to monitor everything since all it has are the leds for visual indicators (and it will change the various settings while it is in operation without recompiling and uploading to the arduino). I will upload a schematic, the sketch, and the monitoring program a little later. However, Id like to change it.

The project as it sits now, how 4 leds to show the various states it is in, and two buttons to effect changes, but using the two buttons to make the changes makes it difficult to make those changes (especially with just the leds for indicators). Id like to have multiple buttons but dont want to take up a bunch of the arduino pins, so I was thinking what if I took a series of buttons, hook the first straight to the +5v supply, and run the power in series from there with resistors between each so that when it was pushed each button would show a different voltage at the pin. I could theoretically have 1000+ buttons on a single pin then. However, I ran a quick test on the concept and am running into the problem that the same button doesnt allways show the sasme reading (in fact sometimes I has a difference of 20+). Im thinking that is probably because it depends on how many of the pins are allready outputting part of the current and not leaving me with the full at the supply pin, or am I missing something? as it stands now I could still use it but Id have to leave a pretty wide set of readings for each pin (you know check for say between 651 and 680 for one button and between 681 and 710 for the next), or run a dedicated power supply just for the buttons that way it shouldnt fluctuate.

So basically Im just asking for yalls thoughts, multiple buttons would make it easier to change the settings, and the way I have the program working at the moment it requires both buttons to be pushed at times, is their a way to achieve that with the way Im talking about having all the buttoins go to the same pin?

Here is the current schematic of the circuits I have built and operational so far, as well as both the code for the sketch and the visual studio project for monitoring the sketch and changing various elements in the sketch.

Verhicle Code.zip (84.7 KB)