Servo initiated and stopped with switch state

I'm using a servo to operate an indicator for a pressure gauge (simulated for fun, not exact). The operation I wish to achieve is as follows:

  1. When toggle switch is flipped on, gauge (servo) rotates to 90 degrees
  2. When toggle switch is flipped off, gauge rotates back to original 0 position.
    (Much like an oil pressure gauge in your car)

I can get the first part thanks to interwebs, but it's the second half I'm not sure of. I'm fairly certain I need to configure a pin to the toggle switch instead of only using it as a power switch as shown in the image below.

Here's the sketch

#include <Servo.h> 

Servo myservo;

void setup() 
{ 
  myservo.attach(9);
  myservo.write(90);  // set servo to mid-point
} 

void loop() {}

Why do you have a ground and a V+ wire to the switch, that could short out your power supply?
Run a wire from the right end of the switch to ground, another wire from the center pin of the switch to pin 2.

#include <Servo.h>
Servo myservo;

void setup()
{
  pinMode(2,INPUT_PULLUP);
  myservo.write(0);  // set servo to 0
  myservo.attach(9);
}

void loop()
  {
    if(digitalRead(2))
      myservo.write(0);
    else
      myservo.write(90);
  }

Which simulating software you have used to design this image? It's so elegant and it's not of Fritzing. Can you please let me know the software. Thanks in advance !!!

outsider:
Why do you have a ground and a V+ wire to the switch, that could short out your power supply?
Run a wire from the right end of the switch to ground, another wire from the center pin of the switch to pin 2.

#include <Servo.h>

Servo myservo;

void setup()
{
  pinMode(2,INPUT_PULLUP);
  myservo.write(0);  // set servo to 0
  myservo.attach(9);
}

void loop()
  {
    if(digitalRead(2))
      myservo.write(0);
    else
      myservo.write(90);
  }

Thanks for the reply, I was in a hurry when i made the diagram. I'll try this out and get back to you.

jackthom41:
Which simulating software you have used to design this image? It's so elegant and it's not of Fritzing. Can you please let me know the software. Thanks in advance !!!

TinkerCAD from Autodesk. It's free and you can do quite a bit and very easy to use. I like fritzing, but I prefer the simplicity of TinkerCAD.

Thanks Outsider! It worked like a charm, very much appreciated :slight_smile:

I was able to get multiple servos working by building on the sketch you provided Outsider, but the servos are very slow to rotate. Any ideas on how to speed that up? Power supply set to 5.4V 5A

#include <Servo.h>
Servo myservo;
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;

void setup()
{
  pinMode(2,INPUT_PULLUP);
  myservo.write(0);  // set servo to 0
  myservo.attach(9);
  myservo1.write(0);
  myservo1.attach(3);
  myservo2.write(0);
  myservo2.attach(5);
  myservo3.write(0);
  myservo3.attach(6);
  myservo4.write(0);
  myservo4.attach(10);
  myservo5.write(0);
  myservo5.attach(11);
  
}

void loop()
  {
    if(digitalRead(2))
      myservo.write(0);
  	else
      myservo.write(90);
   if(digitalRead(2))
      myservo1.write(0);
  	else
      myservo1.write(100);
   if(digitalRead(2))
      myservo2.write(0);
  	else
      myservo2.write(60);
   if(digitalRead(2))
      myservo3.write(0);
  	else
      myservo3.write(125);
   if(digitalRead(2))
      myservo4.write(0);
  	else
      myservo4.write(125);
   if(digitalRead(2))
      myservo5.write(0);
  	else
      myservo5.write(125);
}

Looks like they're all moving at the same time, pulling more current than your power supply can provide, try just one by itself, if it works right, you may have to move them in sequence with a 100 mS delay between each move.