# Nano with Servo

**URL:** <https://forum.arduino.cc/t/nano-with-servo/1337362>\
**Category:** Motors, Mechanics, Power and CNC\
**Created:** [December 30, 2024, 9:35pm UTC](https://forum.arduino.cc/t/nano-with-servo/1337362 "2024-12-30T21:35:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![2nanos](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/2nanos/32/1147120_2.png) [@2nanos](https://forum.arduino.cc/u/2nanos)\
**Post date:** [December 30, 2024, 9:35pm UTC](https://forum.arduino.cc/t/nano-with-servo/1337362/1 "2024-12-30T21:35:52Z")

</div>

So i would just like some help getting my arduino nano and its shield to make a servo motor to spin 90 degrees after i press a button, and go back counter clockwise after i press the button again. My servo is connected to pin 9, and button is pin 4.

The script right now works by when button is pressed the servo motor goes indifinetly until i press it again which it stops.

```arduino
#include <Servo.h>

#define BUTTON_PIN 4 // Button connected to D4
#define SERVO_PIN 9 // Servo motor connected to D9

Servo myservo; // Create Servo object to control the servo

bool buttonState = HIGH; // Current button state
bool lastButtonState = HIGH; // Previous button state
bool servoPosition = false; // Tracks servo position (false = 0 degrees, true = 90 degrees)

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP); // Button as input with pull-up resistor
  myservo.attach(SERVO_PIN); // Attach the servo to pin 9
  myservo.write(0); // Start with the servo at 0 degrees
}

void loop() {
  buttonState = digitalRead(BUTTON_PIN); // Read the button state

  // Detect button press (transition from HIGH to LOW)
  if (buttonState == LOW && lastButtonState == HIGH) {
    toggleServoPosition(); // Toggle the servo position
    delay(300); // Debounce delay
  }
  lastButtonState = buttonState; // Update the last button state
}

// Function to toggle the servo position
void toggleServoPosition() {
  if (servoPosition) {
    myservo.write(0); // Move servo to 0 degrees
  } else {
    myservo.write(90); // Move servo to 90 degrees
  }
  servoPosition = !servoPosition; // Toggle the position state
}

```

Thanks for the help.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [December 30, 2024, 9:39pm UTC](https://forum.arduino.cc/t/nano-with-servo/1337362/2 "2024-12-30T21:39:49Z")

</div>

Welcome to the forum

> [@2nanos](#):
>
> the servo motor goes indifinetly

Does the servo rotate continuously through 360 degrees ?

If so, then you do not have a servo, you have an electronically controlled motor and can only control its direction of movement and its speed. Please post a link to where you got the servo

---

<div class="post-metadata">

**Author:** ![2nanos](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/2nanos/32/1147120_2.png) [@2nanos](https://forum.arduino.cc/u/2nanos)\
**Post date:** [December 30, 2024, 9:42pm UTC](https://forum.arduino.cc/t/nano-with-servo/1337362/3 "2024-12-30T21:42:48Z")

</div>

yeah it rotates after 360 degrees, its the FIIEC continuous servo fs90r

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [December 30, 2024, 9:45pm UTC](https://forum.arduino.cc/t/nano-with-servo/1337362/4 "2024-12-30T21:45:25Z")

</div>

> [@2nanos](#):
>
> continuous

As I suspected, that is not really a servo at all

---

<div class="post-metadata">

**Author:** ![2nanos](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/2nanos/32/1147120_2.png) [@2nanos](https://forum.arduino.cc/u/2nanos)\
**Post date:** [December 30, 2024, 9:45pm UTC](https://forum.arduino.cc/t/nano-with-servo/1337362/5 "2024-12-30T21:45:31Z")

</div>

sorry forgot the link: [Pololu - FEETECH FS90R Micro Continuous Rotation Servo](https://www.pololu.com/product/2820)

---

<div class="post-metadata">

**Author:** ![2nanos](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/2nanos/32/1147120_2.png) [@2nanos](https://forum.arduino.cc/u/2nanos)\
**Post date:** [December 30, 2024, 9:46pm UTC](https://forum.arduino.cc/t/nano-with-servo/1337362/6 "2024-12-30T21:46:22Z")

</div>

wait so what does that mean? is it still possible for my script to work?

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [December 30, 2024, 9:49pm UTC](https://forum.arduino.cc/t/nano-with-servo/1337362/7 "2024-12-30T21:49:27Z")

</div>

Your sketch is doing what you tell it to but the so called "servo" is not able to do what you want

> [@UKHeliBob](#):
>
> you have an electronically controlled motor and can only control its direction of movement and its speed.

You cannot control its angle, which is what you want to do

---

<div class="post-metadata">

**Author:** ![2nanos](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/2nanos/32/1147120_2.png) [@2nanos](https://forum.arduino.cc/u/2nanos)\
**Post date:** [December 30, 2024, 9:51pm UTC](https://forum.arduino.cc/t/nano-with-servo/1337362/8 "2024-12-30T21:51:21Z")

</div>

ok well thanks for you help, it really helped. Thanks once again

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [June 28, 2025, 9:51pm UTC](https://forum.arduino.cc/t/nano-with-servo/1337362/9 "2025-06-28T21:51:48Z")

</div>

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