This page is to Setup and Config Stepper Motor SG90
Hardware and Software
Hardware
Stepper Motor SG90
Software
Arduino IDE
Stepper Motor SG90
DATA : D9 (Pink)
Extra V++ and GND (Not Connect to Arduino)
V++ : RED / GND : BROWEN
This example is for Arduino
Reference :
This example is for Digispark with Digispark Server Library
Reference :
// Sweep
// by BARRAGAN <http://barraganstudio.com>
// This example code is in the public domain.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
// This SoftwareServo library example sketch was initially delivered without any comments.
// Below my own comments for SoftRcPulseOut library: by RC Navy (http://p.loussouarn.free.fr)
// Controlling the position of 2 servos using the Arduino built-in hardware UART (Arduino Serial object).
// This sketch do NOT work with an ATtinyX4 and ATtinyX5 since they do not have a built-in harware UART (no Arduino Serial object).
// The command (issued in the Arduino Serial Console or in a Terminal) is:
// S=P with:
// S=A for Servo1 and S=B for Servo2
// P=Position number x 20° (Possible positions are from 0 to 9 which correspond to from 0° to 180°)
// Ex:
// A=7 sets Servo1 at 7 x 20 =140°
// B=3 sets Servo2 at 3 x 20 =60°
#include <SoftRcPulseOut.h>
SoftRcPulseOut servo1;
SoftRcPulseOut servo2;
void setup()
{
pinMode(13,OUTPUT);
servo1.attach(2);
servo1.setMaximumPulse(2200);
servo2.attach(4);
servo2.setMaximumPulse(2200);
Serial.begin(9600);
Serial.print("Ready");
}
void loop()
{
static int value = 20;
static char CurrentServo = 'A';
/*
if ( Serial.available()) {
char ch = Serial.read();
switch(ch) {
case 'A':
CurrentServo='A';
digitalWrite(13,LOW);
break;
case 'B':
CurrentServo='B';
digitalWrite(13,HIGH);
break;
case '0' ... '9':
value=(ch-'0')*20;
if (CurrentServo=='A')
{
servo1.write(value);
}
else if (CurrentServo=='B')
{
servo2.write(value);
}
break;
}
}
*/
int angle = 180;
for (int i=0;i<=angle;i++) {
value=i;
servo1.write(value);
//servo2.write(value);
delay(100);
SoftRcPulseOut::refresh();
}
for (int i=angle;i>=0;i--) {
value=i;
// servo2.write(value);
servo1.write(value);
delay(100);
SoftRcPulseOut::refresh();
}
}