Power an actuator with reed switch

Hello everyone,
I have a question. I have a project where I want to power an actuator with 3 reed switches. I want when the arduino powers on the actuator will retract. In the loop I want the actuator to extend all the way when all three read switches are closed. I have this code I wrote with one reed switch but it seems to not retract every time I restart the arduino. Sometimes it will and sometimes it wont.
i am using a dual relay module with a 12v power supply. I have the neg of actuator to one common pin on relay and negative to the other common pin. I then have the power supply negative go to both NC and the positive go to both NO on the relay outputs. because it wasn't working I added another reed switch to try and make it retract but I would prefer it just retracts on startup.

// Define the pins for reed switch and relay module
const int reedSwitchPin = 2;
const int resetswitch = 3;
const int relayExtendPin = 8; // Control pin for extending the actuator
const int relayRetractPin = 9; // Control pin for retracting the actuator

bool actuatorExtended = false;

void setup() {
  pinMode(reedSwitchPin, INPUT_PULLUP);
   pinMode(resetswitch, INPUT_PULLUP);
  pinMode(relayExtendPin, OUTPUT);
  pinMode(relayRetractPin, OUTPUT);

  
  delay(1000);
  // Retract actuator on startup

 // digitalWrite(relayExtendPin, LOW); // Ensure extend relay is off
 // digitalWrite(relayRetractPin, HIGH); // Turn on retract relay
 
   digitalWrite(relayExtendPin, LOW); // Ensure extend relay is off
  digitalWrite(relayRetractPin, HIGH); // Turn on retract relay
 

}

void loop() {

 
 
 
  // Check if reed switch is triggered
  if (digitalRead(reedSwitchPin) == LOW) {
    // If actuator is not already extended, extend it
    if (!actuatorExtended) {
       digitalWrite(relayRetractPin, LOW); // Ensure retract relay is off
  digitalWrite(relayExtendPin, HIGH); // Turn on extend relay
  actuatorExtended = true;
     
     // extendActuator();
    }
  } else {
    // If actuator is extended and reed switch is not triggered, retract it
   // if (actuatorExtended) {
   //  retractActuator();
   // }
  }
}

//void extendActuator() { deleted to simplify
  //digitalWrite(relayRetractPin, LOW); // Ensure retract relay is off
 // digitalWrite(relayExtendPin, HIGH); // Turn on extend relay
  //actuatorExtended = true;
//}





Why did you start a topic in the Uncategorised category of the forum when its description explicitly tells you not to ?

Your topic has been moved to a relevant category. Please be careful in future when deciding where to start new topics

1 Like

Post an annotated schematic showing how you have wired it.

I am sorry. I will make sure to look at that next time.

Post a picture showing how you htave wired it.

Do you have the negative (GND) of the power supply tied into the ground of your arduino? That needs to happen.

I do not I will do that.

so is this needed because i am powering the relay with ground from arduino and 5v

sorry i am not great at the picture

So, it looks like you’re trying to implement a polarity reversing switch with two SPSTBrelays.

Ok, now, check your logic, and remember to let the motor spin down before applying reverse thrust !

..............and include a complete wiring diagram, not some chook scratch with arrows disappearing into infinity.

I suggest you to try with a code that does only that startup retract with no code in loop.
When It's working reliably go forward. This excludes possible wiring issue with relay.
Are your wires long?

No, you need to have a common ground reference for everything in your project so it works

Are the reed switches normally open (OFF) or closed (ON)? Does the actuator have builtin end limits or is that what the reeds are for?
NOTE: "Normal" means when no magnet is near.

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