Servo control by push button switch

In this servo control push button project, the clockwise and anticlockwise movement of the servo shaft is controlled by two push buttons.

The servo motor is interfaced with the Arduino (How to use a servo with Arduino). The control switches are connected to the digital pin 2 and pin 3 of the Arduino. Two 1K resistors are connected to the inputs and the GND terminal. The positive supply taken from the +5V output is connected to the other terminal of the SPST push switch. So whenever the switch is pressed the digital pins read an active high state input.control servo motor with button

During active high state at pin 2, the position of the servo increments until it reaches a value of 180 degrees. And during the high state at pin3, the servo starts to decrement till reaches to 0 degrees.

The speed of the servo can be controlled by adjusting the time delay function. The speed of rotation is inversely proportional to the time delay value. In the delay() function, the time delay decimal value has given in milliseconds.

how to control a servo with a switchHere one switch turns the arm in a clockwise direction and the other in the anti-clockwise direction. The servo moves only when an input is an active high. Whenever the push button has released, the arm movement stops. And on next switch press, it starts the movement from the current position.

Code

#include<Servo.h>
int pos = 0;

Servo servo;
void setup() {
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  servo.attach(9);
}
void loop() {
  while (digitalRead(2) == HIGH && pos < 180) {
    pos++;
    servo.write(pos);
    delay(15);
  }
  while (digitalRead(3) == HIGH && pos > 0) {
    pos--;
    servo.write(pos);
    delay(15);
  }
}
1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 4.20 out of 5)
Loading...

You may also like...

117 Responses

  1. Pat says:

    Hi I have copied and pasted this in my uno but it will not upload I get an error 302 message HELP

    • admin says:

      This is due to the hidden invalid characters in the code. It can occur while you directly copy paste the code.
      Try: Tools > Fix Encoding & Reload in the Arduino IDE OR check and remove every extra spaces and character in the code.

  2. Pat says:

    Got it thank you much

  3. Pat says:

    Hi I have a question I would like to use 1 button push it once to move the servo from 0 to 180 and keep going until I then push it again to stop it. Can this be done??
    Pat

  4. Arun prabu says:

    Is it possible to control this program for three servo in the same uno board

  5. Yule says:

    I got an issue. The hole thing work but if im click the right button in he goes to right but if i click nothing the servo goes automatically trough the left.

    Can you help me

    • admin says:

      Probably it might due to loss contact or wrong connection. Make sure the pull down resistors and wires are connected properly.

  6. Yule says:

    Can you make the servo go faster. And what do i have to change in my code for that

  7. andre says:

    what is the resistors’ function in it? Is it possible to make it work without them?

    • admin says:

      It is a pull-down resistor, to hold logic low level when there is no input. It avoids a floating input and prevents an undefined input state.

  8. akuler says:

    Hi guys. really needs help. I try to make one project which can sweep the servo motor from 0 to 180 degree and back to 0 degree once triggered by one push button. Do anyone had the example of the circuit construction and the coding? Thanks in advance.

    • admin says:

      Try this code,

      if (digitalRead(Switch) == HIGH) {
        //sweep from 0 to 180 degree
        for (pos = 0; pos <= 180; pos += 1) {
          myservo.write(pos);
          delay(15);
        }
        //sweep from 180 to 0 degree
        for (pos = 180; pos >= 0; pos -= 1) {
          myservo.write(pos);
          delay(15);
        }
      }
      
      
  9. Alex says:

    is there a way to go from 90 to 180 with one button and 90 to 0 with another??? if so please respond soon

  10. Alex says:

    New question, which resistor is it i couldn’t make out the numbers

    • admin says:

      1KΩ, commonly a value of 1-10 kΩ can be used.

    • admin says:
      while (digitalRead(2) == HIGH && pos < 180) {
        servo.write(90);
        pos++;
        servo.write(pos);
        delay(15);  
      }
      while (digitalRead(3) == HIGH && pos > 0) {
        servo.write(90);
        pos--;
        servo.write(pos);
        delay(15);
      }
      

      Here whenever you press the button the servo first comes to 90 degrees position, then only it can increments or decrements.

  11. Alex says:

    #include
    int pos = 0;
    Servo servo;
    void setup() {
    pinMode(2, INPUT);
    pinMode(3, INPUT);
    servo.attach(9);
    }
    void loop() {
    while (digitalRead(2) == HIGH && pos 0) {
    pos–;
    servo.write(pos);
    delay(15);
    }
    }

    int potPin = A0;
    int motorPin = 9;
    int potValue = 0;
    int motorValue = 0;
    void setup() {
    Serial.begin(9600);
    }
    void loop() {
    potValue = analogRead(potPin);
    motorValue = map(potValue, 0, 1023, 0, 255);
    analogWrite(motorPin, motorValue);
    Serial.print(“potentiometer = ” );
    Serial.print(potValue);
    Serial.print(“t motor = “);
    Serial.println(motorValue);
    delay(2);
    }

  12. Alex says:

    is there a way too combined these codes if

    • admin says:
      #include <Servo.h>
      Servo servo;
      int pos = 0;
      /* The motor pin has to be changed from 9 to any of the PWM pins,
      because servo is already attached in that pin.*/
      int motorPin = 3; // pin is changed from 9 to 3.
      int potValue = 0;
      int motorValue = 0;
      int potPin = A0;
      
      void setup() {
        pinMode(2, INPUT);
        pinMode(3, INPUT);
        servo.attach(9);
        Serial.begin(9600);
      }
      void loop() {
        while (digitalRead(2) == HIGH && pos > 0) {
          pos--;
          servo.write(pos);
          delay(15);
        }
        potValue = analogRead(potPin);
        motorValue = map(potValue, 0, 1023, 0, 255);
        analogWrite(motorPin, motorValue);
        Serial.print("potentiometer =");
        Serial.print(potValue);
        Serial.print("t motor =");
        Serial.println(motorValue);
        delay(2);
      }
      
  13. Alex says:

    Im not going to copy you but thank you a ton this will be great as something too look off of and be useful as a advice kinda thing.

  14. Alex says:

    sorry if i’m asking too much questions but what is a pot pin, pot value and motor pin and motor value, i assume its a pin of the potentiometer and motor

    • admin says:

      Your code is to control the speed of a motor using a potentiometer. So these variables (pot pin, pot value and motor pin and motor value) are required to connect potentiometer and motor to the arduino and read and write the values respectively. Here, the pot pin is the analog pin A0 which connects to the potentiometer terminal C. And pot value is the analog value read by the input A0, which is a decimal value ranging 0 to 1023 for the corresponding input voltage range of 0-5V.
      The motor pin is the pin number which attaches the motor and motor value is the value to vary the motor speed using PWM. Here code the motor value is just the mapped value of pot value from 0-1023 to 0-255.

  15. Alex says:

    thanks that was needed alot (you are helping me alot for understanding code thank you)

  16. murali says:

    hi, admin, i need your help,
    i am using a continuous servo motor with one push button, for this if i press push button, i need 360 rotation (1 circular rotation) of the servo motor and then stop. so i need code for it as well connections also …thank you.

    • admin says:

      Unlike from Position rotation servo motors, continuous rotation servos rotates clockwise and anti-clockwise with a speed control.
      Its direction and speed are controlled by the pulse width signals between 1000us and 2000us. When the value increases from 1000us to 1500us the clockwise speed decreases and the motor stops at 1500us and on after 1500us the motor speed increases in counterclockwise direction till it reaches maximum speed at 2000us.
      1000microseconds – motor rotates clockwise at maximum speed.
      1500 microseconds – motor stops.
      2000microseconds – motor rotates counterclockwise speed at maximum speed.
      Set the delay value by measuring the time required to complete 1 rotation for the given speed.

      #include<Servo.h>
      int time_delay = 1000 ; // Set time delay value.
      
      Servo servo;
      void setup() {
         pinMode(2, INPUT);
         servo.attach(9);
      }
      void loop() {
         if (digitalRead(2) == HIGH) {
          servo.writeMicroseconds(1000); // rotates clockwise at full speed
          delay(time_delay);
          servo.writeMicroseconds(1500); // rotation stops  
        }
      }
      

      Just ignore the arrangement for Input at pin3, except the connection is same as in the post.

      • murali says:

        thank you, but
        the problem with speed ,the motor is rotating continuously not stoping and initially with out pressing push button the motor is rotating continuously , but i need when we prees the push button that time should start the servo motor and 2 rotations of the motor shaft and then stop the motor automatically….thank you i am waiting for the code…

        • murali says:

          AND one more situation if pushbutton press continuously the motor is also rotating continuously for this i need some delay after pressing the push button.

        • admin says:

          The problem may be related to the connection, make sure the input has a proper connection with a pull-down resistor.

  17. nate says:

    hi how would i write a default position in to the code so that when a button is released the servo goes back to a set degree like 90.

  18. nate says:

    hi can you use this code to control multiple servos at once.

  19. Murali says:

    Ultrasonic sensor with continuous servomotor for Arduino I need code with connections where to give but I need 2sec motor rotate and then stop

    • admin says:

      Refer : Ultrasonic sensor with servo motor.
      For 2 second rotation use this code,

      servo.writeMicroseconds(1000); //clockwise at full speed
      delay(2000);
      servo.writeMicroseconds(1500); //stop
      
      • murali says:

        sir, i need full code i am new to Arduino please.

        • admin says:

          For an ultrasonic scanner with servo, what type of motion you need? Does it should rotate with a code only or controlled by a push button.

          • murali says:

            i am not using push button, i am using the ultrasonic sensor with the continuous servo motor in Arduino, i need only one direction rotation with some (time 2 or 3 seconds) time and then stop .please sir i need code and connections. here input is the ultrasonic sensor.

          • admin says:

            For connection and ultrasonic sensor mounting, refer the link of “object scanner with Ultrasonic Servo rover“.
            Try this,
            Replace the code inside the void loop of Ultrasonic servo rover with the below code.

            for (int i = 0; i <= 100; i++) {
              // rotates clockwise at full speed
              servo.writeMicroseconds(1000);
              delay(20);
              // rotation stops
              servo.writeMicroseconds(1500);
              // Serial print Ultrasonic sensor value
              Serial.println(radar());
            }
            
  20. nate says:

    hi i’m hoping you can help again, I’ve modified a bit of code so that a continuous servo can rotate and the return but, i’m struggling to be able to flip it so it does the opposite ( ie wanting to control a single continuous servo, so that one button sends it one way and another button sends it the other way

    if (digitalRead(2) == HIGH) {
    servo1.writeMicroseconds(2000);
    delay(time_delay);
    servo1.writeMicroseconds(1500);

    if (digitalRead(2) == LOW) {
    servo1.writeMicroseconds(1000);
    delay(time_delay2);
    servo1.writeMicroseconds(1500);

    can you help ????

    • admin says:
      // Button at input pin 2
      if (digitalRead(2) == HIGH) {
        servo1.writeMicroseconds(2000);
        delay(time_delay);
        servo1.writeMicroseconds(1500);
      }
      // Button at input pin 3
      if (digitalRead(3) == HIGH) {
        servo1.writeMicroseconds(1000);
        delay(time_delay2);
        servo1.writeMicroseconds(1500);
      }
      
  21. murali says:

    this is my last question, SIR,
    actually, i am developing sugar feeding system in my house, i am using screw conveyor 3d printing part .it will feed the sugar.in that i am using the ultrasonic sensor as the input signal when the signal is coming the continuous servo motor will start the rotating 2sec and then stop because giving the signal once i will get some quantity sugar. for this, i need the code. and thank you very much sir…

    • admin says:

      Try this. Here the servo operates when the object infront of the ultasonic sensor comes closer than the preset distance value.

      #include<Servo.h>
      const int  trig = 4, echo = 5 ;
      int Distance = 10; //Set preset distance value.
      Servo servo;
      void setup() {
        servo.attach(9);
      }
      void loop() {
        if (radar() < Distance) {
          // rotates clockwise at full speed
          servo.writeMicroseconds(1000);
          delay(20);
          // rotation stops
          servo.writeMicroseconds(1500);
        }
      }
      
      long radar(void)
      {
        digitalWrite(trig, HIGH);
        delayMicroseconds(15);
        digitalWrite(trig, LOW);
        long  dur = pulseIn(echo, HIGH);
        dur = dur / 58;
        return dur;
      }
      
  22. Lol says:

    Hello sir , i would like to control my servo , whereby the angld is controlled by potentiometer and the speed is controlled by pushbutton. Everytime the button is pushed , and after we release the button, when we play with potentiometer the speed will increase. My question is , for each speed , do i have to increase delay or decrease the delay? Because when i try increasing the delay, the speed increased

    • admin says:

      Actually, the value of delay between each servo values (increment or decrement) is inversely related to its speed. The speed increases with the decrease in delay. But in your case, you are controlling the servo position with a potentiometer, so in the code where you have added the delay..?
      Can you share the Arduino code.

  23. glenn says:

    Good day. Whats the code for. 1 servo controlled by two switches.First switch is going to 90′ while the 2nd switch will return it to its original position.

    • admin says:

      To move the servo through each angular degree, then Just change the position increment limit in the code; pos < 180 to pos < 90. Or the servo needs to move only two angles use,

      if (digitalRead(2) == HIGH ) {
       servo.write(90);
       delay(15);
      }
      if (digitalRead(3) == HIGH ) {
       servo.write(0);
       delay(15);
      }

  24. L1F3 says:

    good day sir.. just need some help.. whats the code for controlling a servo by a pushbutton manually. like 2 pushbutton. pb1 for left. and pb2 for right . then if i press pb1 it will move towards left but dont reach its maximum movement.. but only a microstep.. if i release it will stop. and only continue towards left if i press pb1 again.. then it will go towards right if anytime i press pb2 .. same as pb1

  25. Shruthi ns says:

    Sir I use three push buttons for controlling servomotor in 3 different angle how can I wrote this code please help me

    • admin says:

      you mean, to drive the servo position to just three angles while pushing the respective 3 switches.
      Eg:- switch 1 -> 45°.
      switch 2 -> 90°.
      switch 2 -> 135°.

  26. Manfred says:

    Hello Sir

    your code is magnificent for use on my model railroad thank you for that ,but i have a question can i moddefy this code so when it reaches 0 degree a green led wil turn on en wen it reaches 90 degree a red led turns ond and how can i do that

    kind regards from the netherlands

    • admin says:

      In this code, the Green LED ON when the servo position reaches 0 and the RED OFF. When 90 degree is reached the Green OFF and RED ON.

          //Place inside the end of first while loop;incrementing loop.
          if (pos == 90) {
            digitalWrite(REDLED, HIGH);
            digitalWrite(GreenLED, LOW);
          }
          //Place inside the end of second while loop;decrementing loop.
          if (pos == 0) {
            digitalWrite(GreenLED, HIGH);
            digitalWrite(REDLED, LOW);
          }
      
      • Manfred says:

        thank you very much

        • Manfred says:

          another question

          on what pin i should put de led on that i can`t find in this code

          thank you

          • admin says:

            You have to assign the variable REDLED and GreenLED with any of the digital pin numbers; the respective pin of arduino connected to the LEDs.

            const int REDLED = 4, GreenLED = 5; 
            
            void setup() {
              // set the digital pin as output:
              pinMode(REDLED, OUTPUT);
              pinMode(GreenLED, OUTPUT);
            }
            
  27. Manfred says:

    Dear Sir

    allready found the connection for the green and red led it works perfect

    THANK YOU

  28. Rathod Ronak says:

    Hello sir can you help me To make the code of TX of servo and Rx of servo it means use 2 push button as transmitter ( works as remote) and servo attached to the receiver portion of the prototype. This all system is made using Bluetooth HC-05.

    • admin says:

      Using a Bluetooth module, you have to use two Bluetooth modules and Arduino. One at the Transmitter and the other at the receiver end. Then using serial communication (TTL interface) you have to transmit the switch state or a value from the transmitter end to receiver end.
      First code (Tx) : switch press to serial data
      Second code (Rx) : serial to servo posistion.

  29. Rathod Ronak says:

    Yes sir I know how to connect Bluetooth as master and slave to each other but I m totally capable to write a code of servo using 2 push button as transmitter and 1 servo as receiver portion can u give me code of TX and Rx of my application. As per yr code of 2 push button

  30. Rathod Ronak says:

    Sorry I am NOT totally capable to write code of…………………..

    • admin says:

      Interface the arduino with hc-05 for bluetooth serial communication.

      //Reading push switch
      while (digitalRead(2) == HIGH && pos < 180) {
        pos++;
        // write value of variable pos to serial
        Serial.write(pos);
        delay(15);
      }
      while (digitalRead(3) == HIGH && pos > 0) {
        pos--;
        Serial.write(pos);
        delay(15);
      }
      
  31. Rathod Ronak says:

    Sorry sir but I can’t understand in the code of TX because there are potentiometer and I want to add 2 push button instead of potentiometer. And in your code of Rx you put 1 push button and with the help of this push button control the led. That’s I don’t understand.
    Now I just I request u is that please tell me wherever I will have to change in Code of TX and Rx as per my application. otherwise will you make the code of TX and Rx for me as per my application?

    Application :
    Servo rotate in CCW and CW using 2 push button (0-180___180-0). With the help of serial communication Bluetooth HC05.

    • admin says:

      Connect Tx and Rx of the HC-05 to the RX and Tx of the arduino respectively.

      //HC 05 Bluetooth module Receiver code
      #include <Servo.h>
      Servo servo;
      int Position = 0;
      
      void setup() {
        //Attach servo control pin to arduino pin 9
        servo.attach(9);
        Serial.begin(38400);
      }
      void loop() {
       if(Serial.available() > 0){ 
        // Read position value.
          Position = Serial.read();
       }
       // write values to servo
       servo.write(Position);
       delay(15); 
      }
      
      //HC-05 Bluetooth module Transmitter code
      int Position = 0;
      void setup() {
        Serial.begin(38400); 
      }
      void loop() {
        //connect push button to pin 2
       while (digitalRead(2) == HIGH && Position < 180) {
          Position++;
          //send 0 to 180
          Serial.write(Position);
          delay(15);
        }
        //connect push button to pin 3
        while (digitalRead(3) == HIGH && Position > 0) {
          Position--;
          //send 180 to 0
          Serial.write(Position);
          delay(15);
        }
      }
      
  32. Rathod Ronak says:

    OK thx sir

  33. Rathod Ronak says:

    Hello sir, I have motor driver = L298N, l293d, and A4988 this 3 drivers which one is most easy to control the stepper motor using 2 push button. And how to use these selected driver. Can you please explain with the help of code.

    Applications ;
    And I also control stepper motor using 2 push button using Bluetooth HC05

  34. Arthur says:

    Hello sir, i am totally new in programming a servo, i have a lilypad which i want to control 2 servos and push button as switch, i have make it to move bu using sweep code, but when change the angles it doea not do anything, after a few moves the servk stop and i hear a sound of servo still running and then servo feels hot,
    I onky want to achieve that my servo goes to go up 90 degrees by pressing the button switch and back to 0 by pressing again the switch.
    I really appreaciate all help i can get a coreect code. Thanks si much

    • admin says:

      Probably it can be due to gear damage or similar servo complaints. The heating is due to the excessive current drawn by the motors when it stuck.

  35. Arthur says:

    Btw sir my servos are attached to pin 7 and 9, i alao intend to use 6 volts battery pack, i qas told that i need a separate skurce for the servos i want them to move at the same time same angle and back.

  36. Rathod Ronak says:

    Can YOU make this code as per my application Rx and TX for me
    Sir I req you.

    Application:
    Control the stepper using 2 push button using Bluetooth HC05

  37. Rathod Ronak says:

    hello sir i make this code to contorl stepper motor using 2 pushbotton with help of l298n driver.
    so i request i is that can you make this BELOW code for me into RX and TX for BLUETOOTH HC-05.

    i hope you help me.

    #include

    int forward = 2;
    int reverse = 3;

    Stepper motor(200, 11,10,9,8);

    void setup() {
    pinMode(forward,INPUT);
    pinMode(reverse,INPUT);
    Serial.begin(9600);
    }

    void loop() {
    int Speed = analogRead(A0);
    int RPM = map(Speed, 0, 1023, 0, 500);
    int f = digitalRead(forward);
    int r = digitalRead(reverse);
    if(f == 1 && r == 0 && RPM > 1)
    {
    motor.step(1);
    motor.setSpeed(RPM);
    delay(.01);
    }
    if(r == 1 && f== 0 && RPM > 1)
    {
    motor.step(-1);
    motor.setSpeed(RPM);
    delay(.01);
    }
    delay(5);
    Serial.println(RPM);
    }

    • admin says:

      I think this is the code you are expecting.

      //Receiver code
      #include <Stepper.h>
      Stepper motor(200, 11, 10, 9, 8);
      void setup() {
        Serial.begin(38400);
      }
      void loop() {
         while (Serial.available() > 0)
            {
          int pos = Serial.parseInt();   
          int RPM = Serial.parseInt();   
        }
        motor.step(pos);
        motor.setSpeed(RPM);
        delay(10);
      }
      
      //Transmitter code
      int forward = 2;
      int reverse = 3;
      void setup() {
        pinMode(forward, INPUT);
        pinMode(reverse, INPUT);
        Serial.begin(38400);
      }
      void loop() {
        int Speed = analogRead(A0);
        int RPM = map(Speed, 0, 1023, 0, 500);
        int f = digitalRead(forward);
        int r = digitalRead(reverse);
        if (f == 1 && r == 0 && RPM > 1)
        {
          Serial.print(1);
          Serial.print(",");
          Serial.println(RPM);
          delay(10);
        }
        if (r == 1 && f == 0 && RPM > 1)
        {
          Serial.print(-1);
          Serial.print(",");
          Serial.println(RPM);
          delay(10);
        }
        delay(5);
        Serial.println(RPM);
      }
      
  38. Rathod Ronak says:

    OK thx sir if you don’t mind can u please give me mail I’d or contact number

  39. Rathod Ronak says:

    hello sir my name is ronak rathod .
    i mail you to solve my query. i request you please check out your mail and help me .

  40. Swanith says:

    Hello I want to make servo move from 90 to 140 if first button is pressed and come back to 90 if second button is pressed and it should move from 90 to 40 if third button is pressed and again if second button is pressed it should go back to 90. I want connection and code. Pls HELLLLPPPPPP. HHHHHEEEEELLLLLLLPPPPPP

  41. Rathod Ronak says:

    //Receiver code
    #include
    Stepper motor(200, 11, 10, 9, 8);
    void setup() {
    Serial.begin(38400);
    }
    void loop() {
    while (Serial.available() > 0)
    {
    int pos = Serial.parseInt();
    int RPM = Serial.parseInt();
    }
    motor.step(pos);
    motor.setSpeed(RPM);
    delay(10);
    }
    //Transmitter code
    int forward = 2;
    int reverse = 3;
    void setup() {
    pinMode(forward, INPUT);
    pinMode(reverse, INPUT);
    Serial.begin(38400);
    }
    void loop() {
    int Speed = analogRead(A0);
    int RPM = map(Speed, 0, 1023, 0, 500);
    int f = digitalRead(forward);
    int r = digitalRead(reverse);
    if (f == 1 && r == 0 && RPM > 1)
    {
    Serial.print(1);
    Serial.print(“,”);
    Serial.println(RPM);
    delay(10);
    }
    if (r == 1 && f == 0 && RPM > 1)
    {
    Serial.print(-1);
    Serial.print(“,”);
    Serial.println(RPM);
    delay(10);
    }
    delay(5);
    Serial.println(RPM);
    }ગ

    Hello Sir this above code is not properly work. This code is made by you. I think ‘ Serial. Read ()’ is not write in the TX code. Is this problem to do not communicate .

  42. Rathod Ronak says:

    Sir please please sir help me. ‘ગ’ word write by mistake at the and of code.

  43. Rathod Ronak says:

    No sir but signal not approach at the receiver portion. And in the transmitter portion only show the value of ‘A0’ when I open serial monitor. There is no problems in the Bluetooth I checked perfectly.

    • admin says:

      I think you need to sort out the issue step by step.
      Check the communication using the Bluetooth module by directly sending data. And make sure it is properly communicating.
      Then step by step build the code by adding you sensor reading etc. Separately check each portion.

  44. Rathod Ronak says:

    Hello sir can you you please help me? As per my above application or solve this problem

  45. Rathod Ronak says:

    Hello sir can you you please help me? To solve my this problem

  46. Jerry Piaskowy says:

    how is the code different on an arduino mega 2560? thanks!!!

  47. Rathod Ronak says:

    Hello sir I want to control 2 servo motor using 4 push button, each servo control by 2 push button. It means 1st servo control by 2 push button and 2nd servo control by other 2 push button. This all system is control by Bluetooth HC05 then I request u plz help me

    This code was u gave me before months ago. Then can u plz give me code as per my above application.

    //HC 05 Bluetooth module Receiver code

    #include
    Servo servo;
    int Position = 0;

    void setup() {
    //Attach servo control pin to arduino pin 9
    servo.attach(9);
    Serial.begin(38400);
    }
    void loop() {
    if(Serial.available() > 0){
    // Read position value.
    Position = Serial.read();
    }
    // write values to servo
    servo.write(Position);
    delay(15);
    }

    //HC-05 Bluetooth module Transmitter code

    int Position = 0;
    void setup() {
    Serial.begin(38400);
    }
    void loop() {
    //connect push button to pin 2
    while (digitalRead(2) == HIGH && Position < 180) {
    Position++;
    //send 0 to 180
    Serial.write(Position);
    delay(15);

  48. Rathod Ronak says:

    //HC 05 Bluetooth module Receiver code
    #include
    Servo servo;
    int Position = 0;

    void setup() {
    //Attach servo control pin to arduino pin 9
    servo.attach(9);
    Serial.begin(38400);
    }
    void loop() {
    if(Serial.available() > 0){
    // Read position value.
    Position = Serial.read();
    }
    // write values to servo
    servo.write(Position);
    delay(15);
    }
    //HC-05 Bluetooth module Transmitter code
    int Position = 0;
    void setup() {
    Serial.begin(38400);
    }
    void loop() {
    //connect push button to pin 2
    while (digitalRead(2) == HIGH && Position 0) {
    Position–;
    //send 180 to 0
    Serial.write(Position);
    delay(15);
    }
    }

    Sorry in the previous comment I send u half code of TX but in this comment I give you full code of TX and Rx.

  49. XomenX says:

    Hallo admin, can youbplease help me.
    I have project rfid servo door lock, please help cooding for it.
    If i touch the rfid, servo will going from 0 to 90 but it will stay in ther, and if touch the rfid again, servo will going from 90 to 0.
    I need your support please

    • admin says:

      For controlling a servo with RFID you need to modify the code given above to check whether the RFID code given is correct, instead of checking the state of the digital input to drive the servo. Do you have the remaining code for reading and verifying UID?

  50. Rathod Ronak says:

    Hello sir I want to control 2 servo motor using 4 pushbutton each servo motor is controlled by 2 pushbutton with the help of HC-05 BLUETOOTH module. Sir can u please help to make a code

  51. crocker says:

    How do I add 2 LED’S to the circuit . All I have is a 1k ohm resistor and my Servo makes a humming noise sometimes. When I make Servo go clockwise LED turn red on. Then when press button 2 counterclockwise and turn green LED on.

    • admin says:
      //Declare Pin 4 & 5 as output, in void setup.
      while (digitalRead(2) == HIGH && pos < 180) {
        // Red LED to pin 4
        digitalWrite(4, HIGH); // RED ON
        ...
        ...
      }
      digitalWrite(4, LOW); // RED OFF
      while (digitalRead(3) == HIGH && pos > 0) {
        // Green LED to pin 5
        digitalWrite(5, HIGH); //Green ON
        ...
        ...
      }
      digitalWrite(5, LOW); //Green OFF
      
  52. crocker says:

    I have the code I. How do I add 2 LED’S to the circuit . All I have is a 1k ohm resistor and my Servo makes a humming noise sometimes. When I make Servo go clockwise LED turn red on. Then when press button 2 counterclockwise and turn green LED on?

    • admin says:

      As two LEDs are not ON at a time, you can use either one of the circuits.
      arduino parallel LED one resistorMultiple LEDs on a Single Resistor
      The servo buzzing can be due to positioning error or any mechanical issues.

  53. crocker says:

    l’m new
    I still cannot get it to work can you make a whole new diagram like the one above

  54. crocker says:

    like the original one

  55. RR says:

    can i use 2 push buttons which have 2 pins under to connect to the breadboard? if can, how do i connect it?

  56. chinta jyothi says:

    HELLO,
    i am using micro servo motor 9g A0090 and i need to modify it to turn 360 so i removed the knob inside the servo and it is rotating 360.but the problem is the servo is rotating continuously with out stop.i need angle control with push button .plz help me to solve this

Leave a Reply

Your email address will not be published. Required fields are marked *