M5Stack Servo operating
- 2018/09/20
- 23:53
M5Stack Servo operating
Hello everyone! I’m RoboTakao.
Servo is indispensable as an element part for robot. I tried to move the servo easily with M5Stack.

Connection

Description of the sketch
A general servo angle is specified with a pulse width of 0 to 180 degrees every 20 ms.
(Pulse width varies depending on servo)
When it is 0 degrees, 544 ms
When it is 180 degrees, 2400 ms
In this example, delaymicroseconds( ) is used to specify the basic period of 20 ms and the pulse width of angle designation.
Operation check
I was able to move the servo from 45 degrees to 135 degrees.
Sketch
#include
int servoPin = 5;
void servoSet(int Angle,int Time) {
int pulseWidth;
int TimeCount;
TimeCount = Time/20;
for(int i=0; i <= TimeCount; i++){
pulseWidth = map(Angle, 0, 180, 544, 2400);
digitalWrite(servoPin, HIGH);
delayMicroseconds(pulseWidth);
digitalWrite(servoPin, LOW);
delayMicroseconds(20000 - pulseWidth);
}
}
void setup() {
m5.begin();
M5.Lcd.printf("M5Stack Servo test:\r\n");
pinMode(servoPin, OUTPUT);
}
void loop() {
M5.Lcd.printf("Angle 45deg\r\n");
servoSet(45,1000); //45deg
M5.Lcd.printf("Angle 135deg\r\n");
servoSet(135,1000); //135deg
}
Good bye! Thank you!
M5Stack related posts
1) M5Stack Arduino IDE initial setting (macOS)
2) M5Stack I tried Arduino IDE sample sketches
3) M5Stack I treid Avatar(Arduino IDE)
4) M5Stack LED Blink
スポンサードリンク