Helping with my solenoid control project

Hi Guys,

Thank you very much for your help, now I have identified the push button, and tried to write up a code to control the solenoid valve for my project. Could you help me look at the code and tell me whether it will do what I want, or I need to modify it.

Here is what I want physically:

In my circuit, I have Arduino, a solenoid valve (12V DC), 12V power, 2 LEDs (red and green), a push button. When I push the button, the green LED will light (red LED is off) and the solenoid valve is energized (or open) for a time period (e.g. in my code is 3 s). And after 3 s duration, the valve is closed and green LED will be off while red LED will be on. Then I want the system to stop until I tell it to start again.

Questions:

I am not sure when I press or push the button once (say for the first time) and don't hold it, will the button keep sending signal to Arduino telling Arduino that the button's state is HIGH all the time after I press it once ? Or this depends on the button itself. Please see the link of the button (Tactile Switch x10 6x6mm Momentary Arduino Hobby Project PCB Push Button SPST UK) I bought on ebay:

http://www.ebay.co.uk/itm/Tactile-Switch-x10-6x6mm-Momentary-Arduino-Hobby-Project-PCB-Push-Button-SPST-UK-/151293028939?pt=UK_BOI_Electrical_Components_Supplies_ET&hash=item2339c46a4b

If yes then I think my code can work. If not then maybe I should buy another button ? Because I don't want to hold it all the time. The code is here:

// Solenoid Project 
// To initiave the program by 1 button, solenoid is energized for a certain period then off

// Assign the Pins to electrical parts
const int Button1Pin1=2;
const int RedLed1Pin1=10;
const int GreenLed2Pin2=11;
const int Solenoid1Pin1=12;

// Define the variables 
int Button1State = 0;

// Define the Pin Modes
void setup(){
  pinMode(RedLed1Pin1, OUTPUT);
  pinMode(GreenLed2Pin2, OUTPUT);
  pinMode(Solenoid1Pin1, OUTPUT);
  pinMode(Button1Pin1, INPUT);
}

// Define the Loop
void loop(){
  Button1State = digitalRead(Button1Pin1);
  if (Button1State == HIGH) {
    digitalWrite(RedLed1Pin1, LOW);
    digitalWrite(GreenLed2Pin2, HIGH);
    digitalWrite(Solenoid1Pin1, HIGH);
    delay(3000);
    digitalWrite(RedLed1Pin1, HIGH);
    digitalWrite(GreenLed2Pin2, LOW);
    digitalWrite(Solenoid1Pin1, LOW);
    delay(5000); }
  else {
    digitalWrite(RedLed1Pin1, HIGH);
    digitalWrite(GreenLed2Pin2, LOW);
    digitalWrite(Solenoid1Pin1, LOW);
  }
}

Thank you very much.
Jeff

if I could see the picture I would but I can't.
try using "Additional Options " button below to attach photo

please help how to paste a picture...clearly I failed to do so.

Here is what I did:

click image and insert the location of my picture:

[ img ] C:\Users\jeff\Desktop\Solenoid circuit [ /img ]

Apparently this did not work.....

Here is the attached picture...thank you guys.

It's a momentary push-to-make button

..... or words to that effect

EDIT.... Here's a link Mini Pushbutton Switch - COM-00097 - SparkFun Electronics

or words to that effect

Something like "SPST Momentary " ? (or are you

Jeff27:
[ img ] C:\Users\jeff\Desktop\Solenoid circuit [ /img ]

Apparently this did not work.....

No, I don't think the Internet can find your c drive :smiley:

ha ha ha , I didn't notice that until you mentioned it... XD

Tactile switch, button, push button, momentary switch, single-pole single throw (SPST) switch.

Voila!

http://components.omron.eu/Products/Switches/Tactile--Switches

Type B3F

Jeff27:
Here is the attached picture...thank you guys.

I would call that a "blurry button", and connected in a less-than optimal arrangement.

Better connected from input to ground, and using the internal pull-up function of the ATmega instead of an external resistor.

Paul__B:

I would call that a "blurry button", and connected in a less-than optimal arrangement.

Better connected from input to ground, and using the internal pull-up function of the ATmega instead of an external resistor.

Thank you for your help, I have found the item. Can you tell me more about the better way to connect it you said ? I am a noob here, not really sure how the push button is connected to Arduino..... (the picture I attached is from Youtube).

Jeff27:
Can you tell me more about the better way to connect it you said ?

Here goes:

Have a look at at the button in your pic- you'll see the left hand connection goes to 5V. The right side goes both to the data pin and to ground through the resistor. That's what known as a pull-down resistor, and when the button isn't pushed it makes sure the data pin is forced to a known state, ie ground, 0V.

When the button is pushed, left and right get connected, and so the data pin is now connected to 5V thru the switch and to 0V through the resistor. The 5V side has no resistor so it wins that battle and the pin sees 5V.

BUT if you have a look at this example you'll see the Arduino has pull-up resistors built in so if you code like in that example, you don't have to mess around with external resistors.

The logic changes though: the way you have it, the data pin is at 0V usually and 5V when pressed; with a pull-up it's at 5V usually and 0V when pressed. So your coding has to be reversed to take acount of what was on is now off, and what was off is now on.

Pull-ups are seen as being better, and this approach is called active-low, meaning voltage goes low when you activate the switch.

SEE ATTACHED OR FUTURE REFERENCE (so you don't have to type it again)

INTERNAL PULLUPS vs EXTERNAL PULLDOWNS FOR BUTTON SWITCHES.txt (1.09 KB)

raschemmel:
SEE ATTACHED OR FUTURE REFERENCE (so you don't have to type it again)

Thank you very much ! I will learn from it.

And by the way, I have written a code to control a solenoid valve for my project (version 1.0), could you kindly help me go through the code and confirm it will do what I want please ?

Here is what I want physically:

In my circuit, I have Arduino, a solenoid valve (12V DC), 12V power, 2 LEDs (red and green), a push button. When I push the button, the green LED will light (red LED is off) and the solenoid valve is energized (or open) for a time period (e.g. in my code is 3 s). And after 3 s duration, the valve is closed and green LED will be off while red LED will be on. Then I want the system to stop until I tell it to start again.

Questions:

I am not sure when I press or push the button once (say for the first time) and don't hold it, will the button keep sending signal to Arduino telling Arduino that the button's state is HIGH all the time after I press it once ? Or this depends on the button itself. Please see the link of the button (Tactile Switch x10 6x6mm Momentary Arduino Hobby Project PCB Push Button SPST UK) I bought on ebay:

http://www.ebay.co.uk/itm/Tactile-Switch-x10-6x6mm-Momentary-Arduino-Hobby-Project-PCB-Push-Button-SPST-UK-/151293028939?pt=UK_BOI_Electrical_Components_Supplies_ET&hash=item2339c46a4b

If yes then I think my code can work. If not then maybe I should buy another button ? Because I don't want to hold it all the time.

Here is my code for you to review:

// Solenoid Project 
// To initiave the program by 1 button, solenoid is energized for a certain period then off

// Assign the Pins to electrical parts
const int Button1Pin1=2;
const int RedLed1Pin1=10;
const int GreenLed2Pin2=11;
const int Solenoid1Pin1=12;

// Define the variables 
int Button1State = 0;

// Define the Pin Modes
void setup(){
  pinMode(RedLed1Pin1, OUTPUT);
  pinMode(GreenLed2Pin2, OUTPUT);
  pinMode(Solenoid1Pin1, OUTPUT);
  pinMode(Button1Pin1, INPUT);
}

// Define the Loop
void loop(){
  Button1State = digitalRead(Button1Pin1);
  if (Button1State == HIGH) {
    digitalWrite(RedLed1Pin1, LOW);
    digitalWrite(GreenLed2Pin2, HIGH);
    digitalWrite(Solenoid1Pin1, HIGH);
    delay(3000);
    digitalWrite(RedLed1Pin1, HIGH);
    digitalWrite(GreenLed2Pin2, LOW);
    digitalWrite(Solenoid1Pin1, LOW);
    delay(5000); }
  else {
    digitalWrite(RedLed1Pin1, HIGH);
    digitalWrite(GreenLed2Pin2, LOW);
    digitalWrite(Solenoid1Pin1, LOW);
  }
}

Thank you very much for your help !

JimboZA:

Jeff27:
Can you tell me more about the better way to connect it you said ?

Here goes......

Thank you for your help, I will spend time to learn it. Can you advise me on my code please, I just updated this post...