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
}
}
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’.
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.
You said MOSFETs but that drawing shows incorrectly connected bipolar (NPN) transistors. Do you have kickback diodes connected across each solenoid coil?
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?
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