Using a toggle switch to send a one time pulse when moved

All, I am trying to set up a solenoid so that it moves when a toggle switch is moved from one position to another, but the output from the Arduino needs to be a pulse about 20ms long so that the solenoid doesn't remain powered. I am using the Arduino to drive a couple of MOSFETs that supplies the power to the solenoid poles

The sketch below seems to keep repeating the pulse, whereas I want just one pulse, then only when the toggle is moved to the other position should it then send another pulse to the other pole of the solenoid.

I looked at the 'change of state' example in Arduino IDE but that seems to more tailored to counting button presses

How can I stop the Arduino sending repeated pulses?

#define control1 2   // pin that controls the MOSFET
#define control2 3   // pin that controls the MOSFET
#define pulsePin1 2  // pin that controls the MOSFET
#define pulsePin2 3  // pin that controls the MOSFET

bool buttonState1, buttonState2;
const int BUTTON_PIN1 = 4;  // the number of the pushbutton pin
const int BUTTON_PIN2 = 5;  // the number of the pushbutton pin

void setup() {
  Serial.begin(9600);  // Start the Serial monitor with speed of 9600 Bauds
  pinMode(BUTTON_PIN1, INPUT_PULLUP);
  pinMode(BUTTON_PIN2, INPUT_PULLUP);
  pinMode(control1, OUTPUT);  // define control pin as output
  pinMode(control2, OUTPUT);  // define control pin as output
}

void loop() {
  buttonState1 = digitalRead(BUTTON_PIN1);
  buttonState2 = digitalRead(BUTTON_PIN2);

  if (buttonState1 == LOW) {
    digitalWrite(pulsePin1, 1);  // Set output to 1
    delayMicroseconds(20);       
    digitalWrite(pulsePin1, 0);  // Set output to 0
  }

  if (buttonState2 == LOW) {
    digitalWrite(pulsePin2, 1);  // Set output to 1
    delayMicroseconds(20);       
    digitalWrite(pulsePin2, 0);  // Set output to 0
  }
}

Cheers

Les

20 microseconds (uS), too short…. the solenoid won’t move.
More like 50-100ms will do the trick, but won’t be much use to anybody.
Maybe try 100-200ms or greater.

Next challenge, look for the switch ‘changing state’ from LOW to HIGH and vice-versa.
Search for Arduino state change and make you event happen on either ‘edge’.

Hello Lesthegringo

What is the task of the sketch in real life?

Post a photo of that solenoid or a link to it's datasheet.
How many "poles" does it have?

It's one of those two pole little model rail solenoids, made by peco, like this, which uses about 18v (which is why I am using the MOSFET)

I just tried the example in IDE for 'state change', with just a press button and an LED so that I can try and understand, and while it loads fine, it does nothing, other than when you press the button the LED lights, and on the serial monitor it counts the button presses up to 4 and then just stops doing anything. The LED never goes off, so it seems like it's not doing what I expected.

  • Time to show everyone your schematic.

I would think a 20 to 50 millisecond pulse would be needed.
Maybe this will help:


https://www.modeltrainforum.com/threads/powering-peco-pl10-motors.206237/

Make sure your solenoids are not AC, if they are and you are putting DC on them, they will quickly overheat and burn out.

Which is exactly why I want the switch to cause a pulse and not a steady signal

Here's a rough schematic of what I am trying to do, I know there should be some resistors but for speed I just did a simplified diagram

Hopefully that helps

You said MOSFETs but that drawing shows incorrectly connected bipolar (NPN) transistors. Do you have kickback diodes connected across each solenoid coil?

Apologies, it's a TIP120 Transistor - I'm not that into electronics so sort of assume they are all MOSFETs

EDIT

I was using schematics off the web so if I have wired incorrectly it's because I wasn't savvy enough to know better!

As for the diodes, I was unaware, can you explain?

By the way, I have some of these - maybe a better solution?

Well, first thing we need to know is whether your solenoids are AC or DC, if AC you really need SSRs (solid state relays) to control them. Can you post the EXACT part number?

Can't find a part number but they are definitely DC

Do you have a meter to test the resistance of a coil?

If I am reading my meter rightly, about 2 ohms

If you put 18 volts across 2 Ohms, the current would be 9 Amps, I think you have AC solenoids.

So a search on the net says it can be used with both. I was sure it was DC only

However whichever way, it needs to be pulsed, which is actually the intent of the original question. I think that the schematic and electronics need to be done at a later time, but what I want is that when the toggle switch changes position, it sends out a pulse on one pin, then nothing more until the switch moves back to the original position, then you get a pulse on pin 2.

Having a pulse means that the solenoid is only energised by what ever electrical system I end up with for just long enough for it to be moved then depowers

What I want is an output from the arduino that is a trigger signal, that I can vary in length of milliseconds

Give me a few minutes to cook up a pulser.

Thanks for the help!