Controlling a Solenoid to push-pull once when input is high or low

Hi all,

I'm hoping I've got this right and it seems to work on TinkerCad but just want someone to confirm as I'm new to Arduino and Solenoids.

Here's the setup. Apologies TinkerCad doesn't have a solenoid so I used a DC Motor instead. The idea is I have a third-party controller that acts like a relay so I want to feed an input in/out and then when the inputPin reads either High or Low it triggers the solenoid for a period of time only once. On the board, I added an LED to do the same.

The mini breadboard illustrates the third-party controller and in this picture the input would be high. Will probably play around and create a relay/switch on the mini breadboard to make it more accurate.

Here's the code which works.

// Constants:
const int ledPin = 13;  	// the pin that the LED is attached to
const int relayPin = 2; 	// the pin that the third-party controller is attached to
  

// Variables:
int relayState = 0;        // current state of the relay
int lastrelayState = 0;		// last state of the relay

void setup()
{
  // initialize the relay pin as a input:
  pinMode(relayPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()
{
  // read the relay input pin:
  relayState = digitalRead(relayPin);
  
  // compare the relayState to its previous state
  if (relayState != lastrelayState) {
    if (relayState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      digitalWrite(ledPin, HIGH);
      delay(1500);                       
      digitalWrite(ledPin, LOW);
      Serial.println("Relay State is ON");
      lastrelayState = relayState;
    } else if (relayState == LOW) {
	  // if the current state is LOW then the button went from on to off:
      digitalWrite(ledPin, HIGH);
      delay(1500);                       
      digitalWrite(ledPin, LOW);      
      Serial.println("Relay State is OFF");
      lastrelayState = relayState;
    }   
    delay(50);
  }
  
   if (relayState == 0) {
     Serial.println("Relay State is OFF");
   }
    delay(50);

}

Any suggestions or comments on what's right and wrong with this?

Thanks in advance for helping.

  • Sorry but your diagram is completely wrong for this project.

  • Do you have a relay ? MOSFET ? BJT ?

  • What current/voltage does the solenoid require ?

  • What controller are you settling on ?

FYI

Relay example:

@LarryD thanks for the reply.

So the third party controller: "Takes EITHER the positive OR negative for each appliance to be switched. Our control board works as an inline circuit toggle." In this case the applicance is the Ardunio board to control the solenoid.

Its a 5v solendoid so assume dont need another power supply.

Hope that makes sense.

  • You will need an external power supply for your solenoid.
  • You will need to control the solenoid with a driver of some kind, relay/MOSFET/BJT.
  • The solenoid power supply needs to be able to supply the current required.
1 Like

Does that mean that you have to apply a positive and a negative pulse to extract/retract the solenoid? Then you need an H-bridge driver.

@DrDiettrich - the solenoid has a spring return so you only have to apply a positive for a period of time and then it returns back afterwards.

@LarryD - thanks I followed https://core-electronics.com.au/guides/solenoid-control-with-arduino/ and assumed it was right but now reading the comments people do talk about adding an external power supply. I have a 5v relay (srd-05vdc-sl-c) - I assume I could add this into the circuit to give the solenoid the extra power it would require? Any help is appreciated. Thanks Kev

Documentation for solenoids usually specify the energized time vs. the off time. Solenoids get hot. Be sure your ratio of energized time vs. off time falls in the manufacturers limits.

@Paul_KD7HB - thanks the solenoid only operators for a short energized time and then has plenty of time off.

Update circuit diagram which now includes a seperate power feed for the solenoid and a relay based on @LarryD feedback.

Hopefully this looks better. Any more feedback or am I getting close for a newbie.

Thanks

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.