Arduino UNO R4 - Button - Servo Motor

In this tutorial, we'll learn how to control a servo motor with a button using an Arduino Uno R4. Pressing the button will rotate the servo motor to 90 degrees, and pressing it again will move it back to 0 degrees. This process repeats each time the button is pressed.

The tutorial is divided into two sections:

This project is great for beginners to understand servo motor control, button inputs, and debouncing techniques with Arduino Uno R4.

Overview of Servo Motor and Button

If you are not familiar with servo motors and buttons, including their pinout, how they operate, or how to program them, please refer to the following tutorials for more information.

Wiring Diagram

The wiring diagram between Arduino UNO R4 Button Servo Motor

This image is created using Fritzing. Click to enlarge image

See The best way to supply power to the Arduino Uno R4 and other components.

Arduino UNO R4 Code - Button Controls Servo Motor Without Debouncing

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-button-servo-motor */ #include <Servo.h> #define BUTTON_PIN 7 // The Arduino Uno R4 pin connected to button's pin #define SERVO_PIN 9 // The Arduino Uno R4 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 lastButtonState; // the previous state of button int currentButtonState; // the current state of button void setup() { Serial.begin(9600); // initialize serial 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); currentButtonState = digitalRead(BUTTON_PIN); } void loop() { lastButtonState = currentButtonState; // save the last state currentButtonState = digitalRead(BUTTON_PIN); // read new state if(lastButtonState == HIGH && currentButtonState == LOW) { Serial.println("The button is pressed"); // change angle of servo motor if(angle == 0) angle = 90; else if(angle == 90) angle = 0; // control servo motor arccoding to the angle servo.write(angle); } }

Detailed Instructions

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Wire the components according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Copy and paste the above code into the Arduino IDE.
  • Click the Upload button in the Arduino IDE to transfer the code to the Arduino UNO R4.
  • Press the button multiple times.
  • Observe the movement of the servo motor.

※ NOTE THAT:

Sometimes the given code doesn't work properly. To ensure it always functions correctly, we need to use button debouncing. Button debouncing can be challenging for beginners. However, with the help of the ezButton library, it becomes much easier.

Arduino UNO R4 Code - Button Controls Servo Motor With Debouncing

Why is debouncing important? ⇒ see Arduino UNO R4 - Button Debounce tutorial

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-button-servo-motor */ #include <Servo.h> #include <ezButton.h> #define BUTTON_PIN 7 // The Arduino Uno R4 pin connected to button's pin #define SERVO_PIN 9 // The Arduino Uno R4 pin connected to servo motor's pin ezButton button(BUTTON_PIN); // create ezButton object that attach to 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 serial 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; // control servo motor arccoding to the angle servo.write(angle); } }

Detailed Instructions

  • Install the ezButton library. See How To
  • Copy the code and open it in Arduino IDE
  • Click the Upload button in Arduino IDE to transfer the code to Arduino UNO R4
  • Press the button multiple times
  • Observe how the servo motor moves

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!