Controlling solenoid relay using arduino

Hello, can someone check my code and let me know if it's right? I am making a spot welder using a car battery for my project and using arduino to control the solenoid. Basically, the solenoid acts as a switch when pushbutton is pressed and lets the current pass for 30 milliseconds. I got the debounce code from some user from this forum couple of months ago and found the code for solenoid on a different site. For convenience I have attached my schematics, too.

int solenoid_pin = 5;
const byte ButtonPin = 2;
const unsigned long DebounceTime = 30;

void setup()
{
  Serial.begin(115200);

  pinMode(solenoid_pin, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode (ButtonPin, INPUT_PULLUP);  // Button between Pin and Ground
}

void loop()
{
  unsigned long currentTime = millis();
  boolean buttonPressed = digitalRead(ButtonPin) == LOW;  // Active LOW
  static boolean buttonAlreadyPressed = false;
  static unsigned long buttonStateChangeTime = 0;

  // Check for button change and do debounce
  if (buttonPressed != buttonAlreadyPressed &&
      currentTime -  buttonStateChangeTime > DebounceTime)
  {
    // Button state has changed
    buttonStateChangeTime = currentTime;
    buttonAlreadyPressed = buttonPressed;

    if (buttonPressed)
    {
      // Button was just pressed
      digitalWrite(LED_BUILTIN, HIGH);
      digitalWrite(solenoid_pin, HIGH);
      delay(20);
      digitalWrite(LED_BUILTIN, LOW);
      digitalWrite(solenoid_pin,LOW);
    }
  }
}

and lets the current pass for 30 milliseconds.

No, that is NOT what the code does.

Any idea where am I supposed to make the change?

990614:
Any idea where am I supposed to make the change?

Where you think you are making the current flow for 30 milliseconds, but are, in fact, only letting it flow for 20.

Not seeing why you need debouncing on the switch. Just start the current flow when you see a high-to-low transition on the switch input. Your 20 (should be 30 as PaulS hinted) ms of delay letting the current flow will more than cover any switch bounce time.

Once the 30 ms cycle is done, don't start another until you see the next high-to-low transitions.

Sorry for the late reply and thanks both of you.

I need some guidance regarding my code. I used oscilloscope to observe how my solenoid would behave and I found that it has 17ms closing delay and 12 ms opening delay. I haven't carried out my experiment yet because I don't want to overheat (possibly expulsion due to high current for extended period of time) my metal specimens. Do I need to change my codes or does this code work just fine?

int solenoid_pin = 5;
const byte ButtonPin = 2;
const unsigned long DebounceTime = 30;

void setup()
{
  Serial.begin(115200);

  pinMode(solenoid_pin, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode (ButtonPin, INPUT_PULLUP);  // Button between Pin and Ground
}

void loop()
{
  unsigned long currentTime = millis();
  boolean buttonPressed = digitalRead(ButtonPin) == LOW;  // Active LOW
  static boolean buttonAlreadyPressed = false;
  static unsigned long buttonStateChangeTime = 0;

  // Check for button change and do debounce
  if (buttonPressed != buttonAlreadyPressed &&
      currentTime -  buttonStateChangeTime > DebounceTime)
  {
    // Button state has changed
    buttonStateChangeTime = currentTime;
    buttonAlreadyPressed = buttonPressed;

    if (buttonPressed)
    {
      // Button was just pressed
      digitalWrite(LED_BUILTIN, HIGH);
      digitalWrite(solenoid_pin, HIGH);
      delay(30);
      digitalWrite(LED_BUILTIN, LOW);
      digitalWrite(solenoid_pin,LOW);
    }
  }
}

Any help is appreciated.

1 Like

What do you expect the code to do? Have you tried it to see if it does what is expected?