Arduino Nano - Button - Servo Motor

In this tutorial, you will learn how to control a servo motor using an Arduino Nano and a button. When the button is pressed, the servo motor will rotate 90 degrees. If the button is pressed again, the servo motor will return to 0 degrees. This action will repeat continuously.

This tutorial has two sections:

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Push Button
1×(Optional) Panel-mount Push Button
1×Servo Motor
1×Breadboard
1×Jumper Wires
1×(Optional) DC Power Jack
1×(Optional) 9V Power Adapter for Arduino Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of Servo Motor and Button

If you are unfamiliar with servo motors and buttons (including pinouts, how they work, and how to program them), the following tutorials can help:

Wiring Diagram

The wiring diagram between Arduino Nano and Button Servo Motor

This image is created using Fritzing. Click to enlarge image

It should be noted that the wiring diagram shown above is only suitable for a servo motor with low torque. In case the motor vibrates instead of rotating, an external power source must be utilized to operate the servo motor. The following wiring diagram demonstrates how to connect the servo motor to an external power source.

The wiring diagram between Arduino Nano and Servo Motor Button

This image is created using Fritzing. Click to enlarge image

Please do not forget to connect GND of the external power to GND of Arduino.

Arduino Nano Code - Button Controls Servo Motor Without Debouncing

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-button-servo-motor */ #include <Servo.h> const int BUTTON_PIN = 2; // The Arduino Nano pin connected to button's pin const int SERVO_PIN = 9; // The Arduino Nano pin connected to servo motor's pin Servo servo; // create servo object to control a servo int angle = 0; // The current angle of servo motor int prev_button_state; // The previous state of button int button_state; // The current state of button void setup() { Serial.begin(9600); // Initialize the Serial to communicate with the Serial Monitor. pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object servo.write(angle); button_state = digitalRead(BUTTON_PIN); } void loop() { prev_button_state = button_state; // save the last state button_state = digitalRead(BUTTON_PIN); // read new state if(prev_button_state == HIGH && button_state == LOW) { Serial.println("The button is pressed"); // change angle of servo motor if(angle == 0) angle = 90; else if(angle == 90) angle = 0; // rotate the servo motor to the angle position servo.write(angle); } }

Detailed Instructions

  • Plug a USB cable into your Arduino Nano and PC.
  • Launch the Arduino IDE, select the correct board and port.
  • Paste the code into the IDE and open it.
  • Click the Upload button in the IDE to send the code to the Arduino Nano.
  • Push the button multiple times.
  • Check out the servo motor's movement.

※ NOTE THAT:

In practice, the code mentioned above does not always work correctly. To ensure it functions properly, we need to debounce for the button. Debouncing the button can be difficult for those new to the topic. Fortunately, with the help of the ezButton library, this task can be made much simpler.

Arduino Nano Code - Button Controls Servo Motor With Debouncing

Why is debouncing necessary? See the Arduino Nano - Button Debounce tutorial for more information.

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-button-servo-motor */ #include <Servo.h> #include <ezButton.h> const int BUTTON_PIN = 2; // The Arduino Nano pin connected to button's pin const int SERVO_PIN = 9; // The Arduino Nano pin connected to servo motor's pin ezButton button(BUTTON_PIN); // create ezButton object for pin 7; Servo servo; // create servo object to control a servo int angle = 0; // The current angle of servo motor void setup() { Serial.begin(9600); // Initialize the Serial to communicate with the Serial Monitor. button.setDebounceTime(50); // set debounce time to 50 milliseconds servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object servo.write(angle); } void loop() { button.loop(); // MUST call the loop() function first if(button.isPressed()) { Serial.println("The button is pressed"); // change angle of servo motor if(angle == 0) angle = 90; else if(angle == 90) angle = 0; // rotate the servo motor to the angle position servo.write(angle); } }

Detailed Instructions

  • Install the ezButton library. Refer to How To for instructions.
  • Open the code in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to upload the code to the Arduino Nano.
  • Push the button multiple times.
  • Check out the changes in the servo motor.

Video Tutorial