Control a servo motor with a 2 connection toggle switch (on/off switch)

hello!
I am trying to control a servo motor with a toggle switch for a school project so that when the circuit is closed, the servo goes to 40 degrees and, when it's opened, it goes back to 0 degrees. Could someone help me figure out the code for this? Mine doesn't quite seem to work. Also, for the electrical circuit, does the one on the attached website work?
toggle switch with servo, electrical circuit

Always show us a good schematic of your circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘reply menu’ to attach the copied sketch.

I suggest you first break the problem down into simpler parts:

You'll need to figure out how to wire up the switch and to read its position (on/off) in a sketch. Start with that to make sure you understand.

Then you may want to create a new sketch to figure out how to control servo motors. There are lots of diagrams online, so this shouldn't be very difficult. Make the sketch move the servo back and forth between the two positions (you can use a delay between them to make it tic back and forth).

THEN... it should be fairly easy to figure out how to move portions of the servo control code into the first sketch.

We're always here and willing to help! If you have some code that you're wrestling with, post it (using the </> icon).

We NEVER like doing someone's homework, but we ALWAYS like to teach others if they're willing to learn. :slight_smile:

Pat.

1 Like

Hi! Thanks for your answer!
this is the code I have so far;

#include <Servo.h>
int pinSwitch = 1;
int pinServo = 8;
Servo myServo;

void setup() {
Serial.begin(9600);
myServo.attach(pinServo);
  pinMode(pinSwitch, INPUT);
}

void loop() {
  if(digitalRead(pinSwitch) == HIGH){
monServo.write (40);
  }
  else{
monServo.write(0);
  }
}

How is your switch wired ?

S1
S2
S3

Using pin 1 is not recommended as it is used for communications int pinSwitch = 1;

You're close. How did you wire up the switch and the servo? Can you post a picture or describe the wiring connections?

  1. You only need the Serial.begin() line if you're writing messages to the output console. You can delete that.

  2. I recommend that you never use pins 0 or 1 in your sketches since they are tied to the serial input and output lines and odd things may happen.

  3. If you didn't use a pull-up resistor, then you'll need to change the INPUT to INPUT_PULLUP when you set the pinMode of the pinSwitch. Google "pullup resistor."

  4. You call the servo myServo at the top and monServo at the bottom. This won't compile. You probably copied code from somewhere else and pasted it in there.

1 Like

Two important points…

  1. You’re using pins 0 and 1
    On the UNO etc, these pins are reserved for serial rx/tx with the standard core.

  2. Remember you can’t power the servo from the arduino, it draws too much current… it needs to be powered separately - with a common 0V rail.

@patduino beat me to the serial comments!

i just changed some stuff and it worked!! thanks so much to everyone who helped!

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