bruno_opaiva
Published © GPL3+

Controling servo motors with buttons and arduino

Hello there, in my first project I will show how to control a servo motor with two buttons using arduino UNO.

BeginnerShowcase (no instructions)21,639
Controling servo motors with buttons and arduino

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
Basic arduino board
×1
Breadboard (generic)
Breadboard (generic)
Generic breadboard, can be any size
×1
Jumper wires (generic)
Jumper wires (generic)
jumpers for wiring
×8
Resistor 10k ohm
Resistor 10k ohm
Resistors for the buttons
×2
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
Generic button
×2
SG90 Micro-servo motor
SG90 Micro-servo motor
Normal plastic gears servo motor
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Circuit diagram

I used TinkerCad to do this diagram

Code

Project code:

C/C++
I used arduino IDE to do the code
int button = 2;   //pin of the first button
int button1 = 3;  //pin of the second button
#include<Servo.h> //include the servo library
Servo servo; //create a servo object
int pos = 0;  //initial position of the servo
void setup() {
  // put your setup code here, to run once:
  servo.attach(9);  //pin used by the servo
  pinMode(button, INPUT_PULLUP);  //define first button as input pullup
  pinMode(button1, INPUT_PULLUP); //define second button as input pullup
  /*
  INPUT_PULLUP send to arduino LOW signal, so, when you press the button, you send a LOW signal to arduino
  */
}

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(button) == LOW) { //if Value read of the button ==LOW:
    pos++;  //increases the value of the "pos" variable each time the push button of the left is pressed
    delay(5); //5 milliseconds of delay
    servo.write(pos); //servo goes to variable pos
  }
  if (digitalRead(button1) == LOW) { //if Value read of the button ==LOW:
    pos--;  //decreases the value of the "pos" variable each time the push button of the right is pressed
    delay(5); //5 milliseconds of delay
    servo.write(pos); //servo goes to variable pos
  }
}

Credits

bruno_opaiva

bruno_opaiva

6 projects • 4 followers

Comments