Go Down

Topic: Using an op amp as a voltage comparator (Read 2564 times) previous topic - next topic

TomGeorge

#15
Mar 30, 2018, 09:13 am Last Edit: Mar 30, 2018, 09:18 am by TomGeorge
Hi,
Can I suggest you STOP.

List ALL your inputs and what voltages they produce.
List ALL your outputs and list what voltage and current they need.

Forget about getting you overall program running,

Design and get your interfacing circuitry working.

Just write individual  codes to check each input and output control.

When you have the responses  you require, then think about code for your process.

I know it sounds slow and laborious, but it means you will be combining known operating hardware.
This will minimize bugs and you will learn about interfacing and structure for your code.

Can you please tell us your electronics, programming, Arduino, hardware experience?

Thanks..  Tom...   :)
Everything runs on smoke, let the smoke out, it stops running....

TomGeorge

Hi, 
This at the start of the code has me wanting to dump it and write my own.



Code: [Select]
//This program was written by Richard Foreman  richforeman6846@gmail.com the key debounce routine I used was something I found at the following 
//website:https://www.arduino.cc/en/Tutorial/Debounce#toc5 and the web site gave the following for authorship:
//  created 21 Nov 2006
//  by David A. Mellis
//  modified 30 Aug 2011
//  by Limor Fried
//  modified 28 Dec 2012
//  by Mike Walters
//  modified 30 Aug 2016
//  by Arturo Guadalupi

//  The debounce routine has only minor alterations from what I found,  I just made it capable of keeping track of two keys at the same time.  The rest of the code is mine
//I make no waranties as to the suitability or even the ability for this program to handle anything at all.  you accept all risks and liabilities if you decide to use it.
//just remember, if it werent for people like us the firemen wouldnt have much of a job. so live a little, do something above your skill level, and hopefully if it smokes
//it wasnt expensive lol.

//  This is the basic vehicle program,  it operates thus.  The black button is for starting and stopping the vehicle, it can be run either by pressing the black key
//momentarily then releasing it in which case it will automatically start or stop the vehicle (depending on if it is already running or not) or, you can hold
//down the black key and press the green key, this will crank the motor for as long as your holding the green key, when you release the green key the starter should
//stop cranking the motor but the motor should stay running.  At this point if you have the timer enabled it will run until the run period which you set has expired.
//  The green button is to toggle the timer on and off and to set the runtime.  To set the runtime hold down the green key and each time you press the black key it
//will add ten minutes to the runtime.  When finished release the green key and the program will blink the timer led once for each ten minute increment you set.
//You can only set the timer when the timer is being toggled on so if it is already on youll have to toggle it off, wait a few seconds for the wait timer, then toggle it
//back on, when you press it to toggle on hold the green key down while you set the timer.

//  The code is fully commented, but a few words, most of the variables you mnay wish to change will be found in the global area.  
//The CRANKTIME is the amount of time in seconds that you want to crank the motor (I plan on adding the hardware and the code for a more robust way to determine when
//the cranking should end but for now this works rather well on my vehicle.
//The DEADTIME is the time between turning on the power to the vehicle and when you start cranking over the motor, some vehicles need more time then others to get ready.
//the WAIT is the amount of time the program waits between key presses before registering another.  This could probably be done away with its just here because I had
//a problem with my debounce routine that I believe is fixed now.
//The SECS and MINS were because I thought the millis() counted ticks (the way they do in computers) so that was there to make the time calcs easier.
//timeron is a flag for the timer, carrunning is the flag for if the vehicle is running or not, and waiting is the delay I put there for between accepting key presses.
//button states and debounce times are for the debounce routine the only one you may wish to change would be the debounceDelay.
//The runtime is the variable that holds whatever you set it to by the green key (the default is 30 minutes).
//The startime keeps track of when the vehicle was started the last time, the stoptime keeps track os when the vehicle should be killed,and the waittime keeps track
//of when the waiting period is over.

//A few notes on the circuitry.  I do not have any training in electronics, the little bit I know (and I dont claim to know anything at all with any confidence)  I learned
//by tinkering around with crap (and smoking a few circuits along the way).  This current incarnation is meant to be rather simple, all its meant to do is start and stop the 
//vehicle.  I spend a lot of time in my van in the summer time, I have the back set up with a bed and a heater (and a camping stove, sleeping bag, fishing poles, guns)
//and I love my electronics so I have 2 car batteries in the back of the van (hooked in parallel) that are in addition to the one in the motor compartment.  I have a relay
//up front that engages when the motor is running so that the alternator will charge the batteries when its running but my electronics will not drain the motor battery when its not.
//This is meant to be the starting point for a more intricate system.  You see when the back batteries drain to the point that it wont keep my electronics going everything just dies
//and I hate that, so in the future I plan on adding the ability to monitor the batteries and when they drain to a certain point it will start the van automatically and run until they
//are charged.  I also plan on adding the hardware to monitor the vehicle for overheating, low oil pressure, low fuel and have it handle all that automatically.  I am also
//considering having it control the heater but we will see, one step at a time.

//lets start with how I currently have it wired.  I have an lm358 dual op amp set up as a comparator.  Pin 8 goes to +12 volts, pin 4 to ground, pins 2 and 6 (the inverting pins)
//are hooked into a voltage divider with 3 10 ohm + 2 4.7 ohm resisitors in series coming off the 12 volts and a 10 ohm rersistor going to ground (the numbers came out to 39 ohm
//from the 12 volt side and a 10 ohm to ground to give me 2.5 volts 250 ma at the inverting side of the comparators but those ressistors were the closest I could get)
//pin 3 gets hooked up to the RTRAN pin on the arduino, pin 5 goes to the STRAN pin on the arduino, pin 1 goes to the coil on a 12 volt relay to power up the vehicle, and pin 7 goes to 
//coil of a second 12 volt relay to engage the starter (of course the relays the other side of the coils go to ground, ,the common pins go to whatever the relay is driving, and the
//normally open pins are hooked up to +12 volts).  There is also a wire coming off of pins 3 & 5 which go to LEDs for a visual cue of when the power and starter are engaged.  Then there 
//is a lead going from the TLED pin on the arduino to an led for a visual indication if the timer is active or not, and a lead going from the +5 on the arduino to a fourth led
//just to show it is powered up.  I have a lead going from +5 on the arduino to both the buttons, a 1k resistor going from the other side of each button to ground, and a lead
//going from the lead with the resistor on the green button to the GBUT pin on the arduino and a lead going from the lead with the resisitor on the black button going to
//the BBUT pin on the arduino.  Of course I have a resistor on the cathode side of each led going to ground.  I have the arduino powered from the batteries in the back through
//the jack on the arduino, I am pulling the +12 volts from the vin pin on the arduino to feed the dual op amp.  The grounds are being fed through the arduino, and the +5 volts
//are coming from the +5 volt pin on the arduino.
//I will update this as I figure it out and when I start adding capabilities to it, Ill hand all that out also.


It has been modified and modified and there is not circuit, just a "description".
It was written for a particular application.
Even Lady Ada has had a go.  (Limor Fried)
"I do not have any training in electronics" is a bit worrying in the "circuit" description.
Tom... :o :o :o
Everything runs on smoke, let the smoke out, it stops running....

TomGeorge

#17
Mar 30, 2018, 09:40 am Last Edit: Mar 30, 2018, 09:43 am by TomGeorge
Hi, again.
These diagrams may help with interfacing.



https://learn.sparkfun.com/tutorials/voltage-dividers

Tom.. :)

Everything runs on smoke, let the smoke out, it stops running....

AKRichard

I'm going to start with the last reply and work backwards.

  I am designing a circuit that for now will just turn my vehicle on and off.  The Arduino needs to drive 2  12 v relays. ( To power up the vehicle and 1 to engage the starter).  This is the base, there is going to be a lot more to it but I am taking it one step at a time (it will monitor the batteries and when they discharge to a certain point it will start the vehicle until they are charged again, and I plan on adding the circuitry to monitor the state of the vehicle as in temp, fuel, oil pressure and such). 

  As far as experience goes, I've been programming sinc I was 14 (I am 50 now). I am confident in basic, c++, and I still do a lot of assembly language programming (mainly math stuff slot of encryption and physics stuff). I am a carpenter by trade but I probably spend 20 to 40 hours a week at programming anyways it helps me to relax.  Electronics, I've messed around a little bit in it but not much.  I would confidently say I know enough in electronics to get into trouble but not enough to know how to avoid those troubles. But I learn quick.

  One of your replies mentioned current limiting resistors to the transistors.  I do not have them anywhere around the transistors.  The relays are only pulling like 250 ms or so as they are just running the coils, do I need resistors?  And do IIjust need them at the base (that's only taking like 5 volts). Or do I need them at the emitter of the second transistor that is receiving the 12 volts?
Why do poptarts have artificial fruit flavoring if they have real fruit filling?

Southpark

I would confidently say I know enough in electronics to get into trouble but not enough to know how to avoid those troubles.
If it's for a public road vehicle.... then consider safety (your own, and other people) very carefully. But if it's not for public road, then ..... still consider safety carefully.

MrMark

#20
Mar 30, 2018, 10:18 pm Last Edit: Mar 30, 2018, 10:18 pm by MrMark
. . .

One of your replies mentioned current limiting resistors to the transistors.  I do not have them anywhere around the transistors.  The relays are only pulling like 250 ms or so as they are just running the coils, do I need resistors?  And do IIjust need them at the base (that's only taking like 5 volts). Or do I need them at the emitter of the second transistor that is receiving the 12 volts?
Yes, you need current limiting resistors on both of the transistor bases.  You probably also need new transistors if you ran the circuit you've drawn without them.

Without knowing the characteristics of the relay, it's nominal current in particular, I've drawn a circuit with resistors below.  This was done in the online circuit simulator at http://www.falstad.com/circuit/circuitjs.html which you might want to take a look at.

In this drawing the 40 Hz square wave represents the Arduino pin.  The 270 Ohm resistor limits the NPN transistor base current and the Arduino pin current to about 16 mA1 to stay within the Arduino 20 mA spec.  The 150 Ohm emitter limits the PNP base current to about 75 mA2 to prevent either of the transistors going up in smoke.  The 50 Ohm resistor represents a relay with 250 mA current draw.  The diode is flyback protection for the PNP transistor switching an inductive load.


1 - (5V - 0.7V base-emitter drop) / 270 Ohms = 16 ma
2 - (12V - 0.7V emitter-base - 0.2V collector-emitter saturation) / 150 Ohm = 75 mA, this may not be optimum for your particular relay.


TomGeorge

#21
Mar 31, 2018, 01:31 am Last Edit: Mar 31, 2018, 01:32 am by TomGeorge
One of your replies mentioned current limiting resistors to the transistors.  I do not have them anywhere around the transistors.  The relays are only pulling like 250 ms or so as they are just running the coils, do I need resistors?  And do IIjust need them at the base (that's only taking like 5 volts). Or do I need them at the emitter of the second transistor that is receiving the 12 volts?
Hi,
This is how you should be controlling your relay with the minimum of current and components and an NPN transistor.

You need to look up a BJT tutorial to explain how a transistor works.
Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

AKRichard

Quote
It has been modified and modified and there is not circuit, just a "description".
It was written for a particular application.
Even Lady Ada has had a go.  (Limor Fried)
"I do not have any training in electronics" is a bit worrying in the "circuit" description.
Tom... :o :o :o
I had to give credit for the part that I used that I didn't write, yes it looks to have been modified several times (including by me) however it worked. :)




Quote
If it's for a public road vehicle.... then consider safety (your own, and other people) very carefully. But if it's not for public road, then ..... still consider safety carefully.

yes it is a public road vehicle (my personal van) that has been modified by me in several ways.  I don't do any mods until I am confident that they are safe and personally tested by me.  That includes the circuit I am working on now.  Once I decide on a final design I will solder all the parts to a board, but it will not be installed until I have had it working on a bench working with starting an actual motor that I have mounted to the bench (my testing motor for various mods Ive done), for a few weeks and make sure that I don't get any nasty surprises inside of my van.  I live in Alaska, my ol lady and I go to some remote places such that if something did happen to the van, we are on our own as cell service is fine in the city, but unless youre on one of the three main highways in the entire state, youre not getting service.  Ive been stranded where it took me three days walking to get to help! so safety is always paramount.



Quote
Yes, you need current limiting resistors on both of the transistor bases.  You probably also need new transistors if you ran the circuit you've drawn without them.

Without knowing the characteristics of the relay, it's nominal current in particular, I've drawn a circuit with resistors below.  This was done in the online circuit simulator at http://www.falstad.com/circuit/circuitjs.html which you might want to take a look at.


Funny you should mention that.  As a matter of fact I did fry two sets,  but it was because I had hooked the diode up in the wrong direction on both relays. fried them almost instantly.  Other than that, they  have worked so far, having said that, I am here asking questions because of what I do not know to look out for, and it seems to be a consensus around here that the circuit should have resistors in it so I will be throwing them in there.  I cannot find any specs on the relays other then what is written on them (even the company website does not have any data sheets I can find).  The relays are standard 5 terminal automotive relays rated at 12vdc 30A/40A, but I cant find anything on nominal current for the coil in the relay.  I do know that once a battery has been drained to around 9 or 10 volts that quite a few of these relays will not operate, so how should I go about figuring out what size of resistors to use?




Code: [Select]


Hi,
This is how you should be controlling your relay with the minimum of current and components and an NPN transistor.


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.






  Anyways, I have run into a new snag.  As I mentioned above, I fried a set of my transistors, and have only one working set left of the originals that I had (2n3904 and 3906).  Since Radio Shack shut there doors in Alaska, we have only one electronics supplier here now, and they don't stock those transistors.  They do however stock nte transistors, and after looking them up online the two transistors mentioned above cross referenced into nte 159 and nte 123ap.  The datasheets on all of them are very similar and I found multiple websites stating these are the correct nte versions of those transistors.  However they are not working as the other ones do.  The pnp transistor is always fully on even when the signal to the npn transistor is off.  Ive went over the wiring and it is correct, and I verified that there is no signal coming into the npn but the pnp is still fully on, not only that, but  when the arduino does signal the npn to turn on, it appears to do so for a moment, but then the pnp starts sending a voltage through the base to the collector of the npn (which goes to ground through the emitter).  The working set does not operate this way; on the working set when the npn is activated it connects the base of the pnp (connected to the collector of the npn) to ground through the emitter of the npn turning it fully on and allowing the full 12v through the emitter to the collector of the pnp and to the relay.  Anyways, Im not quite sure what to look at next to figure it out.  Any suggestions?
Why do poptarts have artificial fruit flavoring if they have real fruit filling?

TomGeorge

#23
Apr 01, 2018, 05:19 am Last Edit: Apr 01, 2018, 05:20 am by TomGeorge
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.... :)
Everything runs on smoke, let the smoke out, it stops running....

MrMark

#24
Apr 01, 2018, 06:17 am Last Edit: Apr 01, 2018, 06:27 am by MrMark
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.  

BabyGeezer

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

Tom.... :)
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.
Attach your images to be viewed directly - THIS WAY;
https://forum.arduino.cc/index.php?topic=519037.0

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.
 

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.
What, I need to say something else too?

allanhurst

#28
Apr 01, 2018, 10:46 pm Last Edit: Apr 01, 2018, 10:52 pm by allanhurst
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

BabyGeezer

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 !!


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 ! :)
Attach your images to be viewed directly - THIS WAY;
https://forum.arduino.cc/index.php?topic=519037.0

Go Up