soleniod issues

This is my first real project so please bear with me. I am working on a circuit that will open a 12V solenoid for a certain amount of time depending on what setting a rotary switch is on and when a momentary switch is pressed. I built a breadboard Arduino following the instructions Arduino put out in Arduino - Setting up an Arduino on a breadboard. So far everything is working except the first setting of the rotary switch. The first setting is supposed to keep the solenoid open for as long as the rotary switch is on setting one and the momentary switch is being pressed. The circuit works, but the digital pin is only sending out .24V to the TIP120 transistor. Not enough to open the solenoid. Setting two and three both work and are pushing out 2.27V to the transistor and is letting the solenoid open. I’ve tried moving digital pins and deleting setting one and two from the code but I still cant get setting one to push enough voltage to work. I’m not sure if this is a coding issue or wiring. Any help would be greatly appreciated. Thank you

fuelPump2.ino (1.29 KB)

pinMode(buttonPin,INPUT_PULLUP);

if (digitalRead(buttonPin)==HIGH)//if the button is pressed

Usually when the switch input has the internal pullup enabled, that means that the switch is wired from the input to ground and the switch is LOW when pressed. How is the switch wired?

Please read the "How to post" guidelines found on top of every main page.

OP's code:

const int buttonPin = 13;
const int solenoidPin = 11;
const int knobSetting1 = 3;
const int knobSetting2 = 5;
const int knobSetting3 = 6;


void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(solenoidPin, OUTPUT);
  pinMode(knobSetting1, INPUT_PULLUP);
  pinMode(knobSetting2, INPUT_PULLUP);
  pinMode(knobSetting3, INPUT_PULLUP);
}

void loop() {

  //setting one. The setting I am having trouble with
  if (digitalRead(buttonPin) == HIGH) //if the button is pressed
    if (digitalRead(knobSetting1) == LOW) //if the rotary switch is on setting one
    {
      digitalWrite(solenoidPin, HIGH); //solenoid open
    }

    else if (digitalRead(buttonPin == LOW)) //if the button is released
      delay(10);
  { digitalWrite(solenoidPin, LOW); //solenoid closed

  }

  //setting two
  if (digitalRead(buttonPin) == HIGH) //if the button is pressed
    if (digitalRead(knobSetting2) == LOW) //if the rotary switch is on position two

    {
      digitalWrite(solenoidPin, HIGH); //solenoid open
      delay(3000);//solenoid open for 3sec
      digitalWrite(solenoidPin, LOW); //solenoid closed
    }

  if (digitalRead(buttonPin) == HIGH) //if the button is pushed
    if (digitalRead(knobSetting3) == LOW) //if the rotary switch is in position three

    {
      digitalWrite(solenoidPin, HIGH); //open solenoid
      delay(6000);//keep solenoid open for 6sec
      digitalWrite(solenoidPin, LOW); //close solenoid


    }
}

pinMode(buttonPin, INPUT_PULLUP); // assumes you are going to connect the button between pin and ground, without using any external resistors

if (digitalRead(buttonPin) == HIGH) // if the button is pressed // this is wrong. A button with pull up is normally HIGH, and becomes LOW when pressed.

Check your curly braces. Leo..

Thank you for the advice. When i would put the pin as LOW the solenoid would stay on the whole time regardless of what position the button was in. I changed the button to a regular breadboard momentary button and changed the code to buttonPin LOW, but the same thing is happening. Setting two and three work while setting one barely has enough voltage to light an LED.

How is the switch wired?

Wawa suggested that you:

Check your curly braces.

This part here for instance (auto formatted for clarity).

  else if (digitalRead(buttonPin == LOW)) //if the button is released
         delay(10);
   {
      digitalWrite(solenoidPin, LOW); //solenoid closed

   }

You see the else if, when true, only calls the delay function. The digitalWrite(solenoidPin, LOW) is executed unconditionally. If you put every { and every } on their own line and use the auto format tool (CTRL-T or Tools, Auto Format) the arrangement of code blocks is more apparent.

Modified

else if (digitalRead(buttonPin == LOW)) //if the button is released
             {
                delay(10);  
                digitalWrite(solenoidPin, LOW); //solenoid closed
              }

Now the delay and the digitalWrite are executed conditionally.

This tutorial on curly brackets might help.

How are you powering the solenoids - hopefully not from the Arduino …

Should ofcourse be:

else if (digitalRead(buttonPin == HIGH)) //if the button is released

As explained, a pin with pull up is normally HIGH, and becomes low when the button is pressed. Assuming you DID connect the button between pin and ground, no resistors. Leo..