Using an op amp as a voltage comparator

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.

and here is the code I have I had to split it into multiple parts:

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

The first part of the actual code:

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

}

4294967295
add ul
4294967295UL

and the last of the code:

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.

larryd:
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.

Please! Post a schematic.

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)

AKRichard:
. . . 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.

AKRichard:
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.

AKRichard:
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".

ChrisTenone:
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.

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

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

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.

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.

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.

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... :slight_smile:

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

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

Hi, again.
These diagrams may help with interfacing.


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

Tom.. :slight_smile:

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?

AKRichard:
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.