Arduino Forum

Using Arduino => General Electronics => Topic started by: AKRichard on Mar 25, 2018, 11:48 pm

Title: Using an op amp as a voltage comparator
Post by: AKRichard on Mar 25, 2018, 11:48 pm
I am attempting to setup the arduino uno (will be replaced with with the arduino pro mini when the bugs are worked out) anyways, I am trying to just get it to start and stop the engine on my vehicle at the moment.  I have two relays wired into the dash (both are 12 volt automotive relays) one turns on the power to everything in the vehicle the other engages the starter, this set up works fine with a switch and a momentary contact button).  Anyways I am having a hard time getting the arduino to drive the relays.

  I started out trying to use a npn transistor with the base coming from one of the arduino pins the collector hooked to +12 volts and the emitter going to one side of the relay coil,  I then tried routing the 12 volts through a 5 volt relay I had laying around however it couldn't handle it either (completely vaporized one of the pins). 

  So not I am trying to use an lm358 dual op amp as a comparator.  From what Ive read when used without the feedback resistor, it should swing between full on and full off.  So to start off with here is the wiring the arduino pins mentioned are what I named them and if you need to know exactly where they are hooked up that info can be found in the program which I will include in a moment:

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.


Anyhow the current configuration that I am having a hard time making work correctly was to use a dual op amp (lm358) with the inverting side hooked up to a voltage divider in such a way that the inverting side recieves about 2.5 volts (this will vary a little bit as I have it hooked into the back batteries which are draining as I use the electronics but I figure since Im only letting it drop about a volt and  a half before charging it should be negligible.  the vin pin is hooked to tp the vin pin on the arduino, ground to ground.  From what ive read with no feedback resistor this should give me full on when I do a digitalWrite(RTRAN, HIGH) (as in the output should be the full 12 volts when the non inverting side
is recieving +5 volts or close to it) and should give me close to ground when low.  Unfortunately this is not the way it is working.  when I put it in operation Im only getting around 3 or 4 volts going to the relays.  I have good 12 volts where there is supposed to be 12 volts, I have almost 5 volts coming from the arduino to the op amps when they are set to HIGH, and I am reading like 2.2 volts at the inverting pins through the voltage divider.  I originally had two npn transistors with the RTRAN and STRAN going
to the bases, the collectors hooked to the +12 and the emitters going to the relay coils but couldnt get the full 12 volts through it, thats why I settled on the dual op amps. I tried the experiments out in the digital circuits book I have and it worked (though in the book it calls for a 272 and I have the lm358 however it worked as expected).  I tried routing the 12 volts through a smaller relay that operated at 5 volts, however they couldnt handle the current required and fried both of them.  I figure the run
relay is probably pulling 10 to 15 amps, the starter relay should only be pulling a few amps as it is going straight to the starter relay in the engine compartment. To be honest I didnt expect those little relays to be able to handle it but theyve been sitting in my parts tray for quite a while now and at least I know.


the code I have will be in the next post as it doesn't like this one being over 9000 characters.
Title: Re: Using an op amp as a voltage comparator
Post by: AKRichard on Mar 26, 2018, 12:00 am
and here is the code I have  I had to split it into multiple parts:

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.
Title: Re: Using an op amp as a voltage comparator
Post by: AKRichard on Mar 26, 2018, 12:11 am
The first part of the actual code:


Code: [Select]




const uint8_t GBUT=A0;                  //Green button
const uint8_t BBUT=A4;                  //BlACK button
const uint8_t TLED=A1;                  //Timer led
const uint8_t RTRAN=9;                  //Run transistor
const uint8_t STRAN=A5;                 //start transistor
const int CRANKTIME=3;                  //Time in seconds to crank
const int DEADTIME=5;                   //Time between powering on the run transistor and powering on the crank
const unsigned long SECS=1000;          //time to multiply secs by
const unsigned long MINS=60000;         // time to multiply minutes by
const unsigned long WAIT=5000;          //time to wait between button pushes before registering another button
const unsigned long debounceDelay = 50; //key debounce variable

bool timeron=true;                      //flag indicating whether the timer is on or not
bool carrunning=false;                  //flag to indicate whether the car is running or not
bool waiting=false;                     //flag to indicate whether another button can be registered or not

int bbuttonState;                       //key debounce variable
int blastButtonState = LOW;             //key debounce variable

int gbuttonState;                       //hey debounce variable
int glastButtonState = LOW;             //key debounce variable

unsigned long blastDebounceTime = 0;    //key debounce variable
unsigned long glastDebounceTime = 0;    //key debounce variable
unsigned long runtime=30;               //variable to keep track of how long to run the vehicle
unsigned long starttime=0;              //variable to hold the start time
unsigned long stoptime=0;               //variable to hold when the car should be shut down
unsigned long waittime=0;               //how long to wqait beforew regiastering another button


void setup() {

  Serial.begin(57600);

  pinMode(GBUT, INPUT);
  pinMode(BBUT, INPUT);
  pinMode(RTRAN, OUTPUT);
  pinMode(STRAN, OUTPUT);
  pinMode(TLED, OUTPUT);
 
  digitalWrite(RTRAN, LOW);
  digitalWrite(TLED, HIGH);
  digitalWrite(STRAN, LOW);

  carrunning=false;
  timeron=true;
 
}
void StartEngine(){

  if(!carrunning){                          //is car already running?
   
    unsigned long currenttime=millis();     //get the current time
   
    digitalWrite(RTRAN, HIGH);              //power up the run transistor

    delay(DEADTIME*1000);                   //give the vehicle a little time to power up

    digitalWrite(STRAN, HIGH);              //power up the start transistor
 
    delay(CRANKTIME*1000);                  //give it the predetermined amount of time to crank over

    digitalWrite(STRAN, LOW);               //turn off the starter relay
   
    carrunning=true;                        //set the car running flag

  }

  waiting=true;                             //set the waiting flag so it doesnt read errant signals as another key press
  waittime=millis();                        //calculate when the flag shpiuld be cleared
  waittime+=WAIT;                           //finishing the calculation
       
  if(waittime>=4294967295){                 //if we happen to be right where the rollover point is
 
    waittime=waittime-4294967296;           //adjust the calculation to reflect it
   
  }

  starttime=millis();                       //save the start time
  stoptime=runtime*MINS;                    //calculate when the engine should be shut down
  stoptime+=starttime;                      //finish the calc

  if(stoptime>=4294967295){                 //if we are at the rollover point

    stoptime=stoptime-4294967296;           //adjust the stop time to reflect it
   
  }

}

void KillEngine(){
 
  digitalWrite(RTRAN, LOW);                 //power down the run transistor
  digitalWrite(STRAN, LOW);                 //the start transistor should already be dead, but it doesnt hurt to make sure
  carrunning=false;                         //clear the car running flag

}

void CheckCar(){
 
  unsigned long currenttime=millis();       //get the current time
     
  if(currenttime>=stoptime){                //compare it to the stop time
   
    KillEngine();                           //if that time has reached then kill the engine
   
  }

}

void CheckGreen(){

  if(!waiting){                             //if we are not in the waiting period
 
    timeron=!timeron;                       //toggle the timer state
 
    runtime=10;                             //reset the run time to ten minutes
 
    while(timeron && digitalRead(GBUT)==HIGH){  //if the timer is on and the green button is still pressed
 
      int reading = digitalRead(BBUT);      //start the black key debounce routine

     if (reading != blastButtonState) {     //if the current state does not equal the last state read

       blastDebounceTime = millis();        //then the key was toggled and reset the last debounce time
       
     }
 
     if ((millis() - blastDebounceTime) > debounceDelay) {    //if the debounce delay has beem reached

       if (reading != bbuttonState) {       //if the button state does not equal the current state take it as the key has toggled
       
         bbuttonState = reading;            //set the state to the current state
 
         if (bbuttonState == HIGH) {        //if the current state is the key pressed
         
            runtime+=10;                    //then add another ten minutes to the runtime
         
         }
         
       }
       
     }
 
     blastButtonState = reading;            //set the last button state to the current state
     
    }

    if(timeron){                            //if the timer is on
     
      digitalWrite(TLED, LOW);              //turn off the timer led
   
      delay(1000);                          //give it a second delay
   
      int endt=runtime/10;                  //calculate how many ten minute iuntervals where just set
   
      for(int x=0;x<endt;x++){              //we are going to blinb the timer led how many ten minute intervals we counted
   
        digitalWrite(TLED, HIGH);           //so turn on the timer led
        delay(1000);                        //for one second
        digitalWrite(TLED, LOW);            //turn off the timer led
        delay(1000);                        //for one second and repeat
       
      }
 
      digitalWrite(TLED, HIGH);             //turn on the timer led to indicate the timer is active

      stoptime=runtime*MINS;                //calculate the new stoptime
      stoptime+=starttime;                  //finish the calc
       
      if(stoptime>=4294967295){             //if we are at the rollover point
 
        stoptime=stoptime-4294967296;       //then adjust as necessary
   
      }

    }
 
    else{
     
      digitalWrite(TLED, LOW);              //else turn the timer led off
     
    }

    waiting=true;                           //set the waiting flag
    waittime=millis();                      //calculate when the wait period is over
    waittime+=WAIT;                         //finish the calculation
   
    if(waittime>=4294967295){               //if we are at the rollover point
 
      waittime=waittime-4294967296;         //then adjust as necessary
   
    }
   
  }

  else{                                     //else we are in the waiting period still

      if(millis()>=waiting){                //see if the end of the waiting period has been reached

        waiting=false;                      //clear the waiting flag if it has
       
      }
     
  }

}



Title: Re: Using an op amp as a voltage comparator
Post by: larryd on Mar 26, 2018, 12:14 am
4294967295
add ul
4294967295UL


Title: Re: Using an op amp as a voltage comparator
Post by: AKRichard on Mar 26, 2018, 12:16 am
and the last of the code:

Code: [Select]


void CheckBlack(){

  if(!waiting){                             //are we in the waiting period?
    
    bool check=false;                       //just a temp flag so we can check what to do

    while(digitalRead(BBUT)==HIGH && !waiting){     //while the black button is pressed and the waiting flag is not set (it can be set from within this routine if we start the vehicle by the buttons

      while(digitalRead(GBUT)==HIGH){       //if the green button is pressed

        check=true;                         //set our temp flag
        
        if(!carrunning){                    //if the car running flag is not set then start the vehicle
                      
          digitalWrite(RTRAN, HIGH);        //power on the run transistor
          digitalWrite(STRAN, HIGH);        //power on the start transistor
          
        }

        else{                               //else the vehicle is already running so lets kill it
          
          digitalWrite(RTRAN, LOW);         //power down the run transistor
          digitalWrite(STRAN, LOW);         //the start transisitor should already be powered down but it doesnt hurt

        }

      }
              
        digitalWrite(STRAN, LOW);           //in case we started the vehicle this stops the engine from cranking anymore

        if(check){                          //if our temp flag is set then we either started or killed the vehicle so we dont want anything else to be done
        
          carrunning=!carrunning;           //toggle the car runnning flag

          starttime=millis();               //set the start time
          stoptime=runtime*MINS;            //calculate when the vehicle should be killed
          stoptime+=starttime;              //finish the calc
      
          if(stoptime>=4294967295){         //if we happen to be at the rollover point

            stoptime=stoptime-4294967296;   //adjust the calc to reflect it
  
          }

      }
          
    }

    if(!check){                           //if our temp flag was not set then we didnt do anything except press the black button so use the start/stop routine
      
      if(carrunning){                     //if the car runnning flag is set
 
        KillEngine();                     //car is running so kill the engine
        
      }
  
      else{                             //else the car is not running
    
        StartEngine();                  //so start the vehicle
      
      }

    }

  }

  else{                               //the waiting flag was set

    if(millis()>=waittime){           //so check to see if the period has expired

      waiting=false;                  //if it has then clear the wait flag
      
    }
    
  }

  waiting=true;                       //set the waiting flag this will cause the routine to exit even if the black button is still pressed
  waittime=millis();                  //calculate whebn the waiting period is over
  waittime+=WAIT;                     //finish the calcuklation
    
  if(waittime>=4294967295){           //if we are at the rollover point
  
    waittime=waittime-4294967296;     //then adjust as necessary
    
  }
  

}
void loop() {
      
    if(carrunning && timeron){        //if the car running flag and the timer flag are both set
  
      CheckCar();                     //then run the check car routine
  
    }
      
    if(!waiting){                     //if the waiting flag is not set

     int reading = digitalRead(BBUT); //get the current status of the black button

     if (reading != blastButtonState) {   //if it doesnt equal the last state then it must have toggled

       blastDebounceTime = millis();      //so reset the debounce time to the current time to restart the delay timer
      
     }
  
     if ((millis() - blastDebounceTime) > debounceDelay) {    //if the delay time has been reached

       if (reading != bbuttonState) {     //compare it to the last state that reached the debounce timne
        
         bbuttonState = reading;          //if its not equal the set the button state to the current state we will take it as acceptable toggle
  
         if (bbuttonState == HIGH) {      //if the key is pressed
            
            CheckBlack();                 //then run the check black key routine
        
         }
        
       }
      
     }
  
     blastButtonState = reading;        //set the last button state to the current state
    
     reading = digitalRead(GBUT);       //read the green key
  
     if (reading != glastButtonState) { //if it doesnt equal the last state registered

       glastDebounceTime = millis();    //then reset the debounce timer because the key has toggled
      
     }
  
     if ((millis() - glastDebounceTime) > debounceDelay) {  //if the debounce timer has exceeded the debounce delay then accept it as a valid toggle

       if (reading != gbuttonState) {   //if the current state does not equal the last valid state
        
         gbuttonState = reading;        //then set the valid state to the current state
  
         if (gbuttonState == HIGH) {    //if the key is pressed
            
            CheckGreen();               //then run the check green key routine
        
         }
        
       }
      
     }
  
     glastButtonState = reading;        //set the last button state to the current state

    }

    else{                               //the waiting flag was set
      
      if(millis()>=waittime){           //check to see if the waiting period has expired

        waiting=false;                  //if it has then clear the wait flag

      }
      
    }
      
}




 Sorry if this seems rather long winded, I am just trying to provide all the information I can as I am stuck and not quite sure which direction to jump to next.  I am a firm believer in the simpler the better, having said that, this is just a starting point I plan on expanding on this, but those changes should not affect the starting circuitry which is all this part of it is.

 Thank you in advance for your time.
Title: Re: Using an op amp as a voltage comparator
Post by: AKRichard on Mar 26, 2018, 12:25 am
4294967295
add ul
4294967295UL



I am sorry, Im not sure what youre trying to tell me.  this may be something very basic and if it is I apologize.
Title: Re: Using an op amp as a voltage comparator
Post by: avr_fred on Mar 26, 2018, 12:32 am
Please! Post a schematic.
Title: Re: Using an op amp as a voltage comparator
Post by: AKRichard on Mar 26, 2018, 12:36 am
I am working on downloading some software to do just that, and I will post it as soon as I get it downloaded (and figure out how to use it)
Title: Re: Using an op amp as a voltage comparator
Post by: MrMark on Mar 26, 2018, 12:44 am
. . . I started out trying to use a npn transistor with the base coming from one of the arduino pins the collector hooked to +12 volts and the emitter going to one side of the relay coil,  .  . .
This isn't right.  Typically the relay is between the +12 V and the transistor collector.  See here (https://www.electronics-tutorials.ws/blog/relay-switch-circuit.html).
Title: Re: Using an op amp as a voltage comparator
Post by: ChrisTenone on Mar 26, 2018, 08:48 am
I am sorry, Im not sure what youre trying to tell me.  this may be something very basic and if it is I apologize.
You specified a number, namely 4294967295. That quantity can only be held in an unsigned long data type. So you need to explicitly use notation that makes it an unsigned long, which is to append the letters "UL" to the number.

I am working on downloading some software to do just that, and I will post it as soon as I get it downloaded (and figure out how to use it)

The easiest way to post a schematic is to scrawl one out using pencil and paper. Actually the neater you can make it, the better. That way, there is no need to learn complicated software. When you are done, take a picture of it with your phone, and post it!

Speaking of complex software ... can you summarize your project in two or three paragraphs? I (and many others) have a short attention span. In other words "TLDR".
Title: Re: Using an op amp as a voltage comparator
Post by: AKRichard on Mar 29, 2018, 03:06 am
You specified a number, namely 4294967295. That quantity can only be held in an unsigned long data type. So you need to explicitly use notation that makes it an unsigned long, which is to append the letters "UL" to the number.

The easiest way to post a schematic is to scrawl one out using pencil and paper. Actually the neater you can make it, the better. That way, there is no need to learn complicated software. When you are done, take a picture of it with your phone, and post it!

Speaking of complex software ... can you summarize your project in two or three paragraphs? I (and many others) have a short attention span. In other words "TLDR".
I'll draw one out tonight, I'm not very good at drawing buy I should be able to do something passable.

  And yes unsigned long are used because I use the millis() to keep track of time (it doesn't have to be exact) and what you were seeing was the code to correct it just in case it was coming up on the time for it to rollover.

In a couple of paragraphs? Sure.  All this is meant to do is start my van when I push the button for  whatever amount of time I set it to run and then shut it down when that time has passed.  The code just looks a little complicated because I'm doing everything with just two buttons.  The program part of it I'm not too worried about.  I am a dumb ass carpenter that likes to do math and have been programming since I was a kid.  I'm.pretty comfortable in basic, c++ , and assembly language.  Anyways the program is working as it should it's just the hardware I really need help with.

  And I think I've got it figured out anyways.  Thanks to the link Mrmark showed me.  I think I've got it working using a non transistor driving a PNP transistor. The article he pointed me to reminded me of one of the circuits in the digital electronics kit from radio shack.  I'd still like to know why o couldnt get the op amps to work the way I was trying to.

Anyways I'll post schematics tonight, and thank y'all for the re replies.
Title: Re: Using an op amp as a voltage comparator
Post by: Smajdalf on Mar 29, 2018, 10:48 am
Hint: try to make and present minimal (non)working example. Remove everything not related to your problem but post an actual sketch which does not work as you wish. Say what you want it to do and what it does.
If I understand it right you are trying to turn on a relay but it does not work right. For example you may start with simple sketch
Code: [Select]

void setup() {
pinMode(9, OUTPUT);
delay(1000);
digitalWrite(9,HIGH);
}
void loop(){}

This code should turn on (or off?) the relay connected to pin 9 (via your circuitry, not directly!) for 1sec each time you press the reset button. If it does not the hardware is wrong...
Title: Re: Using an op amp as a voltage comparator
Post by: AKRichard on Mar 29, 2018, 11:30 pm
ok so the first attachment is a hand drawn schematic of just the lm 358 circuit (the voltage divider is not shown), the second attachment is the same circuit done with transistors that is working, the third attachment is the full schematic with the lm358 and showing the voltage divider.

  The skecth itself is/was working great, its just the hardware part of it I couldnt figure out.  Even though I have a working circuit Id still like to know why I couldnt make it work with the dual op amp.

  In the circuit with the lm358 the theory was that Id send approx 2.5 v to the inverting pins (2 and 6) on the op amps so that the arduino could fully turn it on with its +5v (at pins 3 and 5) and fully turn it off, the relays (pins 1 and 7) were supposed to get the full 12v going to the supply voltage (pin 8).

  but I am only getting like 3 or 4 v out to the relays.
Title: Re: Using an op amp as a voltage comparator
Post by: avr_fred on Mar 30, 2018, 01:05 am
You don't have a voltage divider as shown on pins 2/6 and you sure as heck wouldn't want 60 ohms in that circircuit as a voltage divider. 2k at least or higher would be fine, there is no current required. You'll also not get 12 volts out with 12 volts supply on '358 as it is not a "rail to rail" opamp, it's a 30 plus year old design. I suspect you've exceeded the current limit abilities of the opamp which is why the voltage is low. Honestly, there's not enough detail provided to know for sure but I doubt it matters at this point.

You don't need or want the opamp at all for digital voltage level translation, you can do it with transistors as you've found but your schematic lacks base resistors for current limiting, hopefully you have them otherwise you're going to kill your Arduino.
Title: Re: Using an op amp as a voltage comparator
Post by: Southpark on Mar 30, 2018, 01:31 am
You're probably better off describing what you want to do in general to start with. Eg....  you want to start and stop an engine with relays. So you should mention what your plan is.... like use an arduino to make a relay turn on or off. So describe what sort of device the arduino is going to control.....such as ...is the arduino going to drive a logic-level mosfet, which in turn is able to put enough current through a relay (to make the relay work). Avoid (if possible) making readers read a sizeable essay - especially if they need to read it 20 times over and over to try understand the situation.

Basic block diagram followed by some circuit diagram will help a lot.

Also, consider safety aspects as well. Is this for a general public road car? If so..... make sure it's all road legal and safe.
Title: Re: Using an op amp as a voltage comparator
Post by: TomGeorge on Mar 30, 2018, 09:13 am
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...   :)
Title: Re: Using an op amp as a voltage comparator
Post by: TomGeorge on Mar 30, 2018, 09:34 am
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
Title: Re: Using an op amp as a voltage comparator
Post by: TomGeorge on Mar 30, 2018, 09:40 am
Hi, again.
These diagrams may help with interfacing.
(http://forum.arduino.cc/index.php?action=dlattach;topic=537493.0;attach=251485)
(http://forum.arduino.cc/index.php?action=dlattach;topic=537493.0;attach=251487)

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

Tom.. :)

Title: Re: Using an op amp as a voltage comparator
Post by: AKRichard on Mar 30, 2018, 07:19 pm
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?
Title: Re: Using an op amp as a voltage comparator
Post by: Southpark on Mar 30, 2018, 07:28 pm
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.
Title: Re: Using an op amp as a voltage comparator
Post by: MrMark on Mar 30, 2018, 10:18 pm
. . .

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 (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.

(http://forum.arduino.cc/index.php?action=dlattach;topic=537493.0;attach=251575)
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.

Title: Re: Using an op amp as a voltage comparator
Post by: TomGeorge on Mar 31, 2018, 01:31 am
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.
(http://forum.arduino.cc/index.php?action=dlattach;topic=537493.0;attach=251597)
You need to look up a BJT tutorial to explain how a transistor works.
Tom... :)
Title: Re: Using an op amp as a voltage comparator
Post by: AKRichard on Apr 01, 2018, 03:41 am
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?
Title: Re: Using an op amp as a voltage comparator
Post by: TomGeorge on Apr 01, 2018, 05:19 am
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;
(https://forum.arduino.cc/index.php?action=dlattach;topic=537493.0;attach=251718)
Tom.... :)
Title: Re: Using an op amp as a voltage comparator
Post by: MrMark on Apr 01, 2018, 06:17 am
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.
(http://forum.arduino.cc/index.php?action=dlattach;topic=537493.0;attach=251724)

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.  
Title: Re: Using an op amp as a voltage comparator
Post by: BabyGeezer on Apr 01, 2018, 06:02 pm
You are trying to HIGH SIDE SWITCH so you will need a PNP and a NPN.
Like this;
(https://forum.arduino.cc/index.php?action=dlattach;topic=537493.0;attach=251718)
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.
Title: Re: Using an op amp as a voltage comparator
Post by: avr_fred on Apr 01, 2018, 10:14 pm
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.
 
Title: Re: Using an op amp as a voltage comparator
Post by: ChrisTenone on Apr 01, 2018, 10:29 pm
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.
Title: Re: Using an op amp as a voltage comparator
Post by: allanhurst on Apr 01, 2018, 10:46 pm
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
Title: Re: Using an op amp as a voltage comparator
Post by: BabyGeezer on Apr 02, 2018, 04:56 am
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 ! :)
Title: Re: Using an op amp as a voltage comparator
Post by: AKRichard on Apr 03, 2018, 07:52 am
Quote
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.

Quote
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).
Title: Re: Using an op amp as a voltage comparator
Post by: avr_fred on Apr 04, 2018, 03:41 am
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.
Title: Re: Using an op amp as a voltage comparator
Post by: larryd on Apr 04, 2018, 04:55 am
(https://forum.arduino.cc/index.php?action=dlattach;topic=537493.0;attach=252077)  (https://forum.arduino.cc/index.php?action=dlattach;topic=537493.0;attach=252077)
Click on the image for full resolution.
Title: Re: Using an op amp as a voltage comparator
Post by: AKRichard on Apr 04, 2018, 07:53 am
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.
Title: Re: Using an op amp as a voltage comparator
Post by: MarkT on Apr 04, 2018, 12:41 pm
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!
Title: Re: Using an op amp as a voltage comparator
Post by: AKRichard on Apr 05, 2018, 05:36 am
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
Title: Re: Using an op amp as a voltage comparator
Post by: MarkT on Apr 05, 2018, 01:51 pm
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.
Title: Re: Using an op amp as a voltage comparator
Post by: gmcmurry on Apr 06, 2018, 08:40 am
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




Title: Re: Using an op amp as a voltage comparator
Post by: AKRichard on Apr 06, 2018, 01:58 pm
Quote
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).


Quote
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.


Quote
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.

Quote
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.


Title: Re: Using an op amp as a voltage comparator
Post by: gmcmurry on Apr 06, 2018, 04:18 pm
Here is the Application Note for the ZVN4206

http://www.gregssandbox.com/arduino/ZVN4206AV%20relay%20mosfet%20Application.pdf (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



Title: Re: Using an op amp as a voltage comparator
Post by: AKRichard on Apr 25, 2018, 11:23 am
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?
Title: Re: Using an op amp as a voltage comparator
Post by: AKRichard on Apr 25, 2018, 06:01 pm
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.