Stepper Motor SG90

Stepper Motor SG90

This page is to Setup and Config Stepper Motor SG90

Hardware and Software

Hardware

Stepper Motor SG90

Software

Arduino IDE

Sanki Notes

    • Stepper Motor SG90

    • DATA : D9 (Pink)

    • Extra V++ and GND (Not Connect to Arduino)

    • V++ : RED / GND : BROWEN

Examples

This example is for Arduino

Reference :

Examples

This example is for Digispark with Digispark Server Library

Reference :

            1. // Sweep

            2. // by BARRAGAN <http://barraganstudio.com>

            3. // This example code is in the public domain.

            4. #include <Servo.h>

            5. Servo myservo; // create servo object to control a servo

            6. // a maximum of eight servo objects can be created

            7. int pos = 0; // variable to store the servo position

            8. void setup()

            9. {

            10. myservo.attach(9); // attaches the servo on pin 9 to the servo object

            11. }

            12. void loop()

            13. {

            14. for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees

            15. { // in steps of 1 degree

            16. myservo.write(pos); // tell servo to go to position in variable 'pos'

            17. delay(15); // waits 15ms for the servo to reach the position

            18. }

            19. for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees

            20. {

            21. myservo.write(pos); // tell servo to go to position in variable 'pos'

            22. delay(15); // waits 15ms for the servo to reach the position

            23. }

            24. }

            25. // This SoftwareServo library example sketch was initially delivered without any comments.

            26. // Below my own comments for SoftRcPulseOut library: by RC Navy (http://p.loussouarn.free.fr)

            27. // Controlling the position of 2 servos using the Arduino built-in hardware UART (Arduino Serial object).

            28. // This sketch do NOT work with an ATtinyX4 and ATtinyX5 since they do not have a built-in harware UART (no Arduino Serial object).

            29. // The command (issued in the Arduino Serial Console or in a Terminal) is:

            30. // S=P with:

            31. // S=A for Servo1 and S=B for Servo2

            32. // P=Position number x 20° (Possible positions are from 0 to 9 which correspond to from 0° to 180°)

            33. // Ex:

            34. // A=7 sets Servo1 at 7 x 20 =140°

            35. // B=3 sets Servo2 at 3 x 20 =60°

            36. #include <SoftRcPulseOut.h>

            37. SoftRcPulseOut servo1;

            38. SoftRcPulseOut servo2;

            39. void setup()

            40. {

            41. pinMode(13,OUTPUT);

            42. servo1.attach(2);

            43. servo1.setMaximumPulse(2200);

            44. servo2.attach(4);

            45. servo2.setMaximumPulse(2200);

            46. Serial.begin(9600);

            47. Serial.print("Ready");

            48. }

            49. void loop()

            50. {

            51. static int value = 20;

            52. static char CurrentServo = 'A';

            53. /*

            54. if ( Serial.available()) {

            55. char ch = Serial.read();

            56. switch(ch) {

            57. case 'A':

            58. CurrentServo='A';

            59. digitalWrite(13,LOW);

            60. break;

            61. case 'B':

            62. CurrentServo='B';

            63. digitalWrite(13,HIGH);

            64. break;

            65. case '0' ... '9':

            66. value=(ch-'0')*20;

            67. if (CurrentServo=='A')

            68. {

            69. servo1.write(value);

            70. }

            71. else if (CurrentServo=='B')

            72. {

            73. servo2.write(value);

            74. }

            75. break;

            76. }

            77. }

            78. */

            79. int angle = 180;

            80. for (int i=0;i<=angle;i++) {

            81. value=i;

            82. servo1.write(value);

            83. //servo2.write(value);

            84. delay(100);

            85. SoftRcPulseOut::refresh();

            86. }

            87. for (int i=angle;i>=0;i--) {

            88. value=i;

            89. // servo2.write(value);

            90. servo1.write(value);

            91. delay(100);

            92. SoftRcPulseOut::refresh();

            93. }

            94. }