# MG90S Servo Motor Rotation

**URL:** <https://forum.arduino.cc/t/mg90s-servo-motor-rotation/618008>\
**Category:** Motors, Mechanics, Power and CNC\
**Created:** [October 30, 2019, 11:38am UTC](https://forum.arduino.cc/t/mg90s-servo-motor-rotation/618008 "2019-10-30T11:38:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ti9bet](https://avatars.discourse-cdn.com/v4/letter/t/5f8ce5/32.png) [@ti9bet](https://forum.arduino.cc/u/ti9bet)\
**Post date:** [October 30, 2019, 11:38am UTC](https://forum.arduino.cc/t/mg90s-servo-motor-rotation/618008/1 "2019-10-30T11:38:18Z")

</div>

Hello, I'm using arduino nano and I wish to have a push button that when it is pressed at the first time, it will rotate 90 degrees. And for the 2nd time, it will go back to 0 degrees. I'm having trouble with this.

> #include \<Servo.h\>  
> int servoPin = 3;  
> int button = 4;  
> int buttonread = 0;  
> int status = 0;  
> Servo Servo1;  
> void setup() {  
> Servo1.attach(servoPin);  
> pinMode(button, INPUT);  
> }  
> void loop{  
> buttonread = digitalRead(button);  
> if(buttonread != 0){  
> if(status == 0){  
> rotate();  
> delay(500);  
> }else{  
> rotate1();  
> delay(500);  
> }  
> }  
> void rotate(){  
> Servo1.attach(servoPin);  
> Servo1.write(90);  
> delay(500);  
> Servo1.detach();  
> status = 90;  
> delay(1000);  
> }  
> void rotate1(){  
> Servo1.attach(servoPin);  
> Servo1.write(0);  
> delay(500);  
> Servo1.detach();  
> status = 0;  
> delay(1000);  
> }

---

<div class="post-metadata">

**Author:** ![slipstick](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@slipstick](https://forum.arduino.cc/u/slipstick)\
**Post date:** [October 30, 2019, 5:33pm UTC](https://forum.arduino.cc/t/mg90s-servo-motor-rotation/618008/2 "2019-10-30T17:33:13Z")

</div>

"I'm having trouble" is not really a very useful problem report. What does it do or not do?

It's conventional to test the result of a digitalRead against LOW or HIGH rather than "!=0". And it's pointless to keep continuously attaching and detaching the servo. But who knows if either of those have anything to do with whatever the trouble is?

Details of how the servo is powered and how the button is wired might also help.

Steve

---

<div class="post-metadata">

**Author:** ![IoT\_lover](https://avatars.discourse-cdn.com/v4/letter/i/94ad74/32.png) [@IoT\_lover](https://forum.arduino.cc/u/IoT_lover)\
**Post date:** [October 31, 2019, 3:53am UTC](https://forum.arduino.cc/t/mg90s-servo-motor-rotation/618008/3 "2019-10-31T03:53:05Z")

</div>

There is three issue in your code:

1. You MUST use pull-up for the button =\> See [Arduino Button Pull-up](https://arduinogetstarted.com/tutorials/arduino-button)
2. You SHOULD detect button pressed and released =\> See [Arduino Detect Button Press and Released](https://arduinogetstarted.com/tutorials/arduino-button#content_modifying_arduino_code)
3. You MUST NOT use delay when reading the button =\> See [Avoid using delay function](https://arduinogetstarted.com/tutorials/arduino-led-blink-without-delay)

---

<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:** [May 6, 2021, 9:51am UTC](https://forum.arduino.cc/t/mg90s-servo-motor-rotation/618008/4 "2021-05-06T09:51:43Z")

</div>

Closed on May 6, 2021, 9:51am UTC


