Arduino Program-Turning on an output while an input increase

Hello,

This is my first time using an Arduino, I am using an UNO and am stuck with the programming.

What i am attempting to do is turn on an input1 with a push button, and have this input turn on an output, the outputted 5V will trigger a MOSFET which will turn on a 12 V solenoid valve, the valve will stay on as long input2 is below a certain point. The second input (input2) will be an analog input and it will be coming from an amplified pressure sensor bridged scale (the valve will be distributing water onto the scale until the certain voltage is achieved due to the weight). My code is shown below, I am having a problem with almost everything and I am not sure where to start with debugging it. The analog value will not show (I tried to make the analog value between 0-5 with line “float voltage = sensorValue * (5.0 / 1023.0);” but i am not sure if this is correct), another issue is that when the program is uploaded, my arduino automatically turns on the output for the MOSFET, even if the first input button isn’t pressed, I set the analog value to be 5V right now for shutting off the output just so that i can trigger it with a switch directly to a 5V source.

   int inpin = 2;
   int val = 0;
   
   void setup() {
   pinMode(2, INPUT);      // set the switch pin to be an input
   pinMode(3, INPUT);   // set the switch pin to be an input
   pinMode(4, INPUT);   // set the switch pin to be an input
   pinMode(6, OUTPUT);   // set the switch pin to be an output
  
 }
 
 void loop() {
   
   //Read the analog input from the scale
    int sensorValue = analogRead(A0); 
    
    // set voltage between 5 volts
    float voltage = sensorValue * (5.0 / 1023.0);
    
    //print the analog value to the analog screen
    Serial.println(voltage);
    
    //set "val" to the value on "inpin" (2)
    val = digitalRead(inpin);
    
   // read the switch input:
   if (val == HIGH) {
     
   do
     {
     // if the switch is closed
     digitalWrite(6, HIGH);    // turn on the yellow LED
     }
   
    //If the analog input from the scale is equal to or greater then 5
   while (voltage < 5.0);    
   }
   else {
     digitalWrite(6, LOW);
   }
 }

Thanks a ton!

Any comments or help with this problem is appreciated

A few things to point out:

  1. You need to initialize the serial port before you can “print” anything to the serial monitor. Make sure the port is set to the same baud rate as the monitor. Using 8N1 for the rest of the settings is common.

http://arduino.cc/en/Serial/Begin

  1. I like to set the outputs to a known safe state after they are declared.

  2. When you use a name for a pin, it’s wise to use that name, rather than the actual pin number, in the rest of the code. That way if you need to change it to a different pin, it’s done in one place.

  3. Given you don’t have strict timing requirements, don’t be afraid to place a lot of print statements in places so you can tell what paths the code is, and isn’t, taking.

  4. I don’t know how you have things wired up but my preference is to use the internal pullups on switches and have the switch, when activated, provide a ground. Unless the digital input pin is put, somehow, into a known state ASSUME it will dance between a 0 and 1. That is, use a pullup or pulldown resistor or a SPDT switch.

  5. If you’re sampling an analog voltage and using a threshold on it, think about using some hysteresis. That is if the Vin > Thresh, X happens. But Vin needs to be < (Thresh - hysteresis) before not X happens. When V is inbetween then whatever happened last continues. This keeps noise on the signal from bouncing between X and notX on every new sample.

What is supposed to happen when the switch is pressed and the voltage is < some threshold ? The FET is turned on via some output going high ? What happens if the switch is then released ? Even if the voltage stays low ?

Thank you very much for the reply!

about the comments: (I have put the project away for tonight and will add all changes I have made to the code tomorrow and see what happens but I just wanted to show what I changed and get more feedback as quick as possible)

  1. I initialized the serial port with the baud rate (I added “Serial.begin(9600);” at the beginning of the code

  2. I named all of the pins

  3. I used the name that i assigned the pin everywhere instead of the pin.

4)Do you mean print to the serial screen each time an operation happens (i.e a button is pressed?)

  1. So whenever the pin is not in use make sure that it is set as “digitalWrite(X, LOW);”? I am jsut wondering if this is implementing the pull down resistors?

  2. For this should I change the do…while statement? or should i get rid of it all in general? By the explanation it seem like an if statement would work? so in this case would i put an if statement inside an if statement, so when the main button is pressed to activate the valve (MOSFET) then inside that if statement place the if the serial value read < threshold keep the output pin for the valve on, and if the value is >threshold turn the valve pin off?

I am sorry if what I typed is confusing but I really hope that you can make sense of it, below is the updates code to what I think solved some of your pointers.

Also when the switch is pressed and the voltage is less than the threshold then the output pin should stay high for the valve to continue distributing water which will continuously increase the voltage going in on the serial port. The FET is turned on when the original push button is pressed until the threshold is met for the analog pin, if the switch is released the output to the FET should stay high until the threshold is met.

int inpin = 2;
   int val;
   int threshold;
   int output;
   void setup() {
     Serial.begin(9600);   // set baud rate for serial
   pinMode(2, INPUT);      // set the switch pin to be an input
   pinMode(3, INPUT);   // set the switch pin to be an input
   pinMode(4, INPUT);   // set the switch pin to be an input
   pinMode(6, OUTPUT);   // set the switch pin to be an output
  val = 0;              // set val to zero
  threshold = 5;        // set threshold to five
 }
 
 void loop() {
   
   //Read the analog input from the scale
    int sensorValue = analogRead(A0); 
    
    // set voltage between 5 volts
    float voltage = sensorValue * (5.0 / 1023.0);
    
    //print the analog value to the analog screen
    Serial.println(voltage);
    
    //set "val" to the value on "inpin" (2)
    val = digitalRead(inpin);
    
   // read the switch input:
 
   if (val == HIGH){
     digitalWrite(output, HIGH);
     
     if (voltage < threshold){
       digitalWrite(output, HIGH);
     }
       else if (voltage > threshold){
         digitalWrite(output, LOW);
       }
   }
      else if (val == LOW){
        digitalWrite(output, LOW);
      }
      }

Once again thank you very much for the feedback! I really appreciate it!

Brock

Brock.V:

  1. I initialized the serial port with the baud rate (I added “Serial.begin(9600);” at the beginning of the code

  2. I named all of the pins

Good

Brock.V:
3) I used the name that i assigned the pin everywhere instead of the pin.

No, you haven't. Look at this :
   pinMode(2, INPUT);      // set the switch pin to be an input
   pinMode(3, INPUT);   // set the switch pin to be an input
   pinMode(4, INPUT);   // set the switch pin to be an input
   pinMode(6, OUTPUT);   // set the switch pin to be an output

Shouldn’t that be: (so far as I can see pins 3 and 4 are not used)

   pinMode(input, INPUT);      // set the switch pin to be an input
   pinMode(output, OUTPUT);   // set the switch pin to be an output

I might have named them something different, like buttonPin and waterPin or anything that denotes their function and that the constant is a pin. BTW did you declare them as variables or constants ? And what of A0 ? Perhaps waterLevel ? You don’t have to do any of this, sometimes it helps to make the code easier to understand though.

Brock.V:
4)Do you mean print to the serial screen each time an operation happens (i.e a button is pressed?)

Maybe, maybe not. Put a "print" in where ever you think it'll help to confirm where the code is going or not. Better to put more in than less when things are not working. I've often been surprised to see that what I told the code to do is not what I really wanted it to do. Once things are working and all tested as good, you can remove the "prints" if you want to. Quite often this (removal) is needed as the time it takes to send out a bunch of ASCII characters is not insignificant. In your case timing does NOT appear to be so critical. Another trick is to make such "prints" conditional upon a constant you can set when the code is compiled and uploaded. That is, if "debug" is set to a TRUE, then the prints happen. If "debug" is set to a FALSE, then they don't. Your code is simple, short enough I wouldn't bother with this but keep it in mind.

Brock.V:
5) So whenever the pin is not in use make sure that it is set as “digitalWrite(X, LOW);”? I am jsut wondering if this is implementing the pull down resistors?

There are no pull down resistors in the Arduino, unless you added them externally. There are pullups that you can enable.

http://arduino.cc/en/Reference/Constants

http://arduino.cc/en/Tutorial/InputPullupSerial

And yes I mean an explicit write to the pin to set it HIGH or LOW, whichever is “safe”. This may be an extra step but it brings me peace of mind.

As for the rest of your code … it seems you want a button push to initiate the filling process. After the press the button state doesn’t matter, only the fill state/water level as determined by some analog voltage. So I might put those conditions in pseudo-code as :

  • If ((button pressed) AND (water level < threshLow) then turn on water

    If (water level > threshHigh) then turn off water


  • Of course you’ll need to sample the button and water level inputs and turn those readings into the values to be used above. Notice that I’ve incorporated some hysteresis via 2 thresholds. And that the water filling, once turned on, only gets turned off when the conditions are proper.

    Now the above doesn’t cover the case where you enable to water to come on (via the button press) but the level is above the “low” level. Presumably the water level drains towards a lower level over time. That would require somewhat different logic, where the button push is “remembered” even after it’s been released and the test is then that the button press ever happened AND the water level is too low. Can you figure out how to “remember” that the button was ever pushed ?

    Then the next question is whether, once the water level is “full” and the filling mechanism is subsequently shut off, is another button press is needed to re-start the filling process or is the original press is enough. That is, once enabled via a single button press, does the filling process work automagically, w/o user input, forever to fill and stop filling so as to control the water level ? Or is the process a one time fill-and-stop cycle per button press ?

    Thank you for another reply, you are really helping me out and i greatly appreciate the detailed comment!

    I have re-posted the code and made some adjustments based on the pointers you gave me.

    As for :

    “remembered” even after it’s been released and the test is then that the button press ever happened AND the water level is too low. Can you figure out how to “remember” that the button was ever pushed ?

    Yes, once the button is pressed, the output must stay high until the water level reaches the threshold. If the button is released the program needs to think it is still high until the threshold is met, thus distributing the requested amount of water based on the threshold.

    I added:

       if (val == LOW && voltage < threshold){               //because of pulldown, the value goes low when button is pressed
         digitalWrite(output, HIGH);
    

    this (if i did it correctly based on the references you posted) should only activate the output when both the button is pressed and the threshold is not met yet, therefore the water cannot over flow on the scale due to the threshold already being met, so when the button is pressed it won’t do anything because both need to be true. The issue with this is when it loops through again it probably won’t work because the button will be released and both will not be true causing the if statement to be false. I am not sure how to loop code within an if statement (or whether that is even possible) so that once the button is released the code inside of the if statement will stay true until the threshold is actually met.

    I would like to do something like this:

    If (val == LOW && voltage < threshold){

    digitalWrite(output, HIGH);

    (keep output high until) (voltage > threshold);

    }

    I am unsure of what to use to complete the (keep output high until) command, because as soon as the button is released the loop will stop. basically I want the button to activate an event until a certain command becomes true.

    For your last question at the end of your post, my goal was to fill the bowl until the threshold is met, empty the water, replace the bowl and once the button is pressed again allow the bowl to fill up again.

    int inpin1 = 2;
       int threshold;
       int output = 5;
      
       
       void setup() {
         Serial.begin(9600);   // set baud rate for serial
       pinMode(inpin1, INPUT_PULLUP);      // set the switch pin to be an input
       pinMode(output, OUTPUT);   // set the switch pin to be an output
      val = 0;              // set val to zero
      threshold = 5;        // set threshold to five
     }
     
     void loop() {
       
       //Read the analog input from the scale
        int sensorValue = analogRead(A0); 
        
        // set voltage between 5 volts
        float voltage = sensorValue * (5.0 / 1023.0);
        
        //print the analog value to the analog screen
        Serial.println(voltage);
        
        //set "val" to the value on "inpin" (2)
       int val = digitalRead(inpin1);
        Serial.println(val, DEC);             //print value as a decimal
    
       if (val == LOW && voltage < threshold){               //because of pulldown, the value goes low when button is pressed
         digitalWrite(output, HIGH);
       
           Serial.println(voltage);
           Serial.println(val);
         }
           else if (voltage > threshold){
             digitalWrite(output, LOW);
             Serial.println(voltage);
             Serial.println(val);
           }
       }
          else if (val == HIGH){
            digitalWrite(output, LOW);
          }
          }
    

    Brock.V:
    The issue with this is when it loops through again it probably won’t work because the button will be released and both will not be true causing the if statement to be false. I am not sure how to loop code within an if statement (or whether that is even possible) so that once the button is released the code inside of the if statement will stay true until the threshold is actually met.

    So can you say that the bowl will always be below the level when the button is pushed ? If so then the 1’st pseudo-code I posted should work fine. Walk through the code and see what happens. The code is looping, looking for a button push that has yet to happen. You push the button and the code, looping around as it does, finally detects a low on the input. The code then asks if the water is below the “low” level (via the voltage and threshold, low). If that’s true then the “tap” is turned on (the output is set to a HIGH) and water starts to flow. The code then asks, a few usec later, is the water above the filled threshold ? The answer is obviously no so the “tap” stays open and water continues to flow.

    The code loops and comes back to the button press AND water below the threshold question. Let’s say the button is released … so this test fails. But the output was set to a HIGH in a prior loop and nothing in the code has (yet) to reset it. So the “tap” stays open and the water continues to flow. That water level above the filled question is then asked again and it fails again because it takes some longer amount of time for the bowl to fill. The code continues to loop asking the same questions and getting the same answers as before. However at some point, after some large number of loops, that water level above filled test will pass and the output will be reset to a LOW and the “tap” turned off.

    At this point the water will never come back on again, even if you push the button. You can remove the bowl and replace it with an empty one. Then you need to push the button again to start the whole process over. That sounds like a good thing as you don’t want the “tap” to come on when you remove the now full bowl but before you can put a new empty bowl in it’s place. After all the code doesn’t “know” you removed the bowl, all it senses is the button and water level. The latter is surely going to read below the threshold when the full bowl is removed. So one filling cycle per button push is what you want.

    As for your code, you’ve made it more complicated than it needs to be.

    if (val == LOW && voltage < threshold){         //the value goes low when button is pressed
         digitalWrite(output, HIGH);                //turn on the water
         Serial.println(voltage);
         Serial.println(val);
       }
    if (voltage > threshold){                        //check if bowl is full
         digitalWrite(output, LOW);                  //if full, turn off water
         Serial.println(voltage);
         Serial.println(val);
       }
    

    BTW just to add to your code comments :

    http://arduino.cc/en/Tutorial/InputPullupSerial

    A pull-up resistor, and in the above tutorial they are internal to the microcontroller, is attached to the input pin and the (5V) supply voltage. Thus the resistor “pulls up” the pin to the supply voltage. The switch is connected to the pin and ground. When open the pin is still being pulled up. When the switch is closed, the pin is shorted/connected to ground and so will be at a low state. So you want a switch that is a normally open (N.O.) type and so closes when pushed.

    Okay were making progress!

    Thank you very much, and I didn’t realize the if statement would be that simple!

    It is working now for not allowing the output to turn on when the 5 volts is applied to the “voltage”, but when there is not 5V on the “voltage” the output flickers when the input1 is pressed, then shuts off completely when the input button is released. I posted the code, i added to the second if statement by making it (if (voltage >= threshold){ ) so that when the “voltage” is also equal to 5V it will also shut off the output.

    I feel that the main reason this is happening is due to the voltage on the “voltage” input when there is nothing applied. I tried to disconnect the wire completely from the “voltage” input so that there would be no voltage being applied to it but when i used a meter to measure between the input pin and ground there was roughly 2V on it? I don’t realize where this 2V is coming from or if the arduino is doing it, since there was no voltage applied to it.

    I also tried to change the threshold value to see if it would be able to read when a different value other than 5 V is the threshold (considering that i have the scale and the voltage off of the scale won’t actually be 5V for a certain weight)

    I feel as though it is a problem with the analog read, or the equation that i used to make the steps smaller, (float voltage = sensorValue * (5.0 / 1023.0):wink: I saw this on a forum and it said that the voltage would be read in steps of about 5mV and this would be multiplied by the analog voltage on “voltage”. You have already helped me tremendously and i feel bad asking for more help, but i have been trying to fix it myself but i cannot seem to fix the flickering of the output.

    I probably should have mentioned that I wasn’t going to use the actual 5V as the analog input, I was just using that as a reference to build the code at first and forgot to change it when i posted on here. My real voltage that I want the analog value to read would be 1.8V (which is why i wanted the 5mV step to be able to read a small difference), I am working on the amplifier and will hopefully get it higher, but for right now that is what it is at.

    I am sorry to bother you with another problem, and I sincerely thank you for everything you have already done to help me!

    int inpin1 = 2;
       int threshold;
       int output = 5;
       int val;
       
       void setup() {
         Serial.begin(9600);   // set baud rate for serial
       pinMode(inpin1, INPUT_PULLUP);      // set the switch pin to be an input using the "pull-up" resistor to 5V
       pinMode(output, OUTPUT);   // set the switch pin to be an output
      val = 0;              // set val to zero
      threshold = 5;        // set threshold to five
     }
     
     void loop() {
       
       //Read the analog input from the scale
        int sensorValue = analogRead(A0);
       
        // set voltage between 5 volts
        float voltage = sensorValue * (5.0 / 1023.0);
       
        //print the analog value to the analog screen
        Serial.println(voltage);
       
        //set "val" to the value on "inpin" (2)
       int val = digitalRead(inpin1);
        Serial.println(val, DEC);             //print value as a decimal
        
        if (val == LOW && voltage < threshold){         //the value goes low when button is pressed
         digitalWrite(output, HIGH);                //turn on the water
         Serial.println(voltage);
         Serial.println(val);
       }
    if (voltage >= threshold){                        //check if bowl is full
         digitalWrite(output, LOW);                  //if full, turn off water
         Serial.println(voltage);
         Serial.println(val);
       }
     }
    

    At this point it’s probably necessary to review what you have for a sensor and how it’s powered and connected to the Arduino. That you get some voltage w/o any connection is common. I just ran your code and I’m seeing 1.5 - 1.6 V indicated via the printouts. A diagram of your wiring and any info on the sensor would be useful. Also is your Arduino actually driving some device to turn on/off the water ? If so what is it and where is it getting it’s power ?

    There’s also some clean-up on the code that will make it easier to understand but I don’t think that’s your problem right now.

    Here’s cleaned up version. I did find one mistake that needed fixing. You were comparing a float type variable, the voltage, to a int type, the threshold. I’m not sure what that result would be. Would it be a good floating point comparison or would the voltage end up being chopped to a whole number and compared to the threshold. You want the former so the threshold needs to be a float type as well.

    I find it best to declare all the global constants (you didn’t have any but ideally you should, it saves RAM) and variables right at the top of the code. That way it’s easy to compare types and mismatches. Also easier to see how they are initialized at a glance.

    I tested the code below and it all tests out fine so far as I can tell. The button reads properly and when pressed turns the output pin HIGH … but only when A0 is < the threshold. When A0 becomes > threshold the output pin goes LOW and stays LOW despite repeated button presses.

    //declare the global constants to be used
    const int inpin1 = 2;          // this pin is the button input
    const int output = 5;          // this pin is the output to turn the water on and off
    const float threshold = 1.8;   // the threshold for turning the water off, volts
    
    //declare the variables used
    int val = HIGH;             // set button = to not pressed
    int sensorValue = 0;        // set the ADC reading to zero counts
    float voltage = 0;          // the converted ADC reading, volts
    
    void setup() {
      Serial.begin(9600);             // set baud rate for serial
      pinMode(inpin1, INPUT_PULLUP);  // set the switch pin to be an input using the "pull-up" resistor to 5V
      pinMode(output, OUTPUT);        // set the switch pin to be an output
      digitalWrite(output, LOW);      // make sure water is off to start
    }
    
    void loop() {
    
      //Read the analog input from the scale
      sensorValue = analogRead(A0);
    
      // convert the ADC counts into a voltage
      voltage = sensorValue * (5.0 / 1023.0);
    
      //print the analog value to the analog screen
      Serial.print("The sensor voltage is ");
      Serial.print(voltage);
      Serial.println(" volts");
    
      //set "val" to the value on "inpin" (2)
      val = digitalRead(inpin1);
      Serial.print("The button reads a ");
      Serial.println(val, DEC);             //print value as a decimal
      
      if ((val == LOW) && (voltage < threshold)){    //the value goes low when button is pressed
        digitalWrite(output, HIGH);                  //turn on the water
        Serial.println("The water is turned on");
        Serial.print("The sensor voltage is ");
        Serial.println(voltage);
        Serial.print("The button reads a ");
        Serial.println(val);
      }
      if (voltage >= threshold){                      //check if bowl is full
        digitalWrite(output, LOW);                    //if full, turn off water
        Serial.println("The water is turned off");
        Serial.print("The sensor voltage is ");
        Serial.println(voltage);
        Serial.print("The button reads a ");
        Serial.println(val);
      }
    }
    

    Thank you very much for the quick reply!

    I used the newly refined code you posted (which by the way thank you very much for taking the time to look at it in depth and get it working!) and it works! The only problem is that when my arduino has power all of the analog pins have a 1.8V output on them for some reason, so I will try and get my output from the sensors to a higher voltage so that the 1.8V won’t matter.

    I have attached a schematic of my circuit, it is a hand drawn picture because my autocad tutorial timed out and paint is very frustrating when doing things like this so i hope you can see it!

    The sensor is a FlexiForce A301 sensor :

    http://www.tekscan.com/store/flexiforce … 8pack.html

    The website doesn’t give very much info on it but it is a good visual. They are pressure sensors and change in kilo Ohms when pressure is applied, I have the op-amps as shown to increase the signal coming out from the sensors (since the resistance only changes in kilo Ohms the change in voltage wouldn’t be much) I have to play with the gains a little on the op-amps in order to get the signal high enough to override the 1.8V already on the arduino.

    As for the code you have done amazing work for someone that you don’t know and have put in a lot of time for me! I really want to thank you, and also really wish i could buy you a beer or something! Without your help I would still be trying to figure out how to get the output work!

    I will be working on the gains for the op-amps for the rest of the night and hopefully by tomorrow with your code I can have this project working :smiley: !! I actually made a mistake on the drawing, the op-amp is UA741 not U471, sorry about that, but if your curious here’s the link for the data on it:

    http://www.digikey.ca/product-detail/en … -ND/382197

    Brock.V:
    The website doesn’t give very much info on it but it is a good visual. They are pressure sensors and change in kilo Ohms when pressure is applied, I have the op-amps as shown to increase the signal coming out from the sensors (since the resistance only changes in kilo Ohms the change in voltage wouldn’t be much) I have to play with the gains a little on the op-amps in order to get the signal high enough to override the 1.8V already on the Arduino.

    OK, there are at least 2 things you should be aware of that may affect your design. First that 1.8V you see is just the result of some charge accumulating on the stray capacitance of the MUX internal to the Arduino’s 328P microcontroller (aka MCU). It’s persists because the ADC has a reasonably high input resistance and there’s no other place for the charge to go. Put a 100k resistor btw A0 (or any analog input) and ground and you’ll see it go away. You’ll have a noise floor of a few counts from the ADC. When you drive the analog input with your op-amp that charge will be insignificant and the phantom voltage you see will be gone. You can safely forget that it’s even there when the input is driven from any source with an output resistance < 10k.

    As for the op-amp gain … be aware you can effectively get “gain” from the Arduino. The ADC works by comparing the input voltage to it’s reference voltage. The counts you read is a ratio of those 2 voltages. When the voltages are equal, you get 1023 counts. If the input was 50% of the reference voltage then you’d get 50% of 1023 or about 511. The default reference voltage is the Arduino’s supply voltage, which is likely 5V for most Arduinos (some run on 3.3V). 5V / 1023 counts gets you about 4.89 mV/count. So do you need more resolution that 5 mV ? If so you should also be concerned about noise because the ADC will add a few counts of noise onto the input signal.

    Now if you want more resolution, that is a smaller mV/count, you can do that by changing the reference voltage. The Arduino gives you a few options here. You can use an external (presumably smaller) voltage or use one of the other available internal voltages.

    http://arduino.cc/en/Reference/AnalogRead (see the part about analog reference)

    http://arduino.cc/en/Reference/AnalogReference

    There’s a trade-off though. For the increased resolution, you get a decreased input range of voltages. Now if your input voltage only runs between 0 and 1.1V anyway, then setting the reference to that setting really loses nothing and increases the resolution to 1.1V/1023 = 1.08 mV/count.

    Given the end use is to compare the input to a threshold, I might question the need for such resolution. It might indicate that the method presently used to sense water level is not optimal. I will offer a GUESS that you’re weighing the bowl+water with the sensor. W/o knowing how much water (and bowl weight) and which sensor (low, med, high), I can’t do a sensitivity analysis but that doesn’t mean you shouldn’t. Also try figure out how much drift you’ll get with slightly changing supply voltages or temperature or ??? If the threshold is intended to be the same all the time, you might consider using the analog electronic to do the threshold decision. The output of your bridge amp could be fed into a comparator (LM339 perhaps). BTW a '741 op-amp is a waaaay old design. Even the LM358 that SF seems to use a lot is a better device. I suspect that given the simplicity of your circuit, the '741 will work but you’ll have to have some pots to compensate for offset voltage and bias current effects (if mV of output voltage is really a concern).

    FWIW you need a resistor in the path from the output pin to the base of your 2N2222 transistor. I’ll guess 300 or so ohms would be good.

    Sorry for the late reply!

    Everything is working and I couldn’t thank you enough!!

    If i ever have another issue with Arduino I will definately post on here and hope that you are still replying!

    Thank you :smiley: