Help us understand the problem. What is going on with this article?

ESP32 Servoを使ってみる

More than 1 year has passed since last update.

IPUアドベントカレンダーですがラスト空いてたので予備で取ってたネタを差し込みます。

先日の記事を書いたら、コメントをもらいました。

ということで調べてみました。

確かにライブラリマネージャにありますね。

ということでインストールして使ってみます。

利用するサーボはGroveのサーボモータです。

servo.ino
#include <ESP32Servo.h>

Servo servo1; // create four servo objects 
int servo1Pin = D2;

// Published values for SG90 servos; adjust if needed
int minUs = 500;
int maxUs = 2400;

int pos = 0;      // position in degrees

void setup() {
  servo1.setPeriodHertz(50);      // Standard 50hz servo
  servo1.attach(servo1Pin, minUs, maxUs);
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // sweep from 0 degrees to 180 degrees
    // in steps of 1 degree
    servo1.write(pos);
    delay(2);             // waits 20ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // sweep from 180 degrees to 0 degrees
    servo1.write(pos);
    delay(2);
  }
}

0~180度でサーボのシャフトを回すサンプルです。
のはずなんだけど明らかに180度まわっていない苦笑

複数動かす

multiple-servo.ino
#include <ESP32Servo.h>

Servo servo1; // create four servo objects 
Servo servo2; // create four servo objects 

int servo1Pin = D0;
int servo2Pin = D2;

// Published values for SG90 servos; adjust if needed
int minUs = 500;
int maxUs = 2400;

int pos = 0;      // position in degrees

void setup() {
  servo1.setPeriodHertz(50);      // Standard 50hz servo
  servo2.setPeriodHertz(50);      // Standard 50hz servo
  servo1.attach(servo1Pin, minUs, maxUs);
  servo2.attach(servo2Pin, minUs, maxUs);
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // sweep from 0 degrees to 180 degrees
    // in steps of 1 degree
    servo1.write(pos);
    delay(2);             // waits 20ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // sweep from 180 degrees to 0 degrees
    servo1.write(pos);
    delay(2);
  }

  for (pos = 0; pos <= 180; pos += 1) { // sweep from 0 degrees to 180 degrees
    // in steps of 1 degree
    servo2.write(pos);
    delay(2);             // waits 20ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // sweep from 180 degrees to 0 degrees
    servo2.write(pos);
    delay(2);
  }
}

複数を同時に動かす

#include <ESP32Servo.h>

Servo servo1; // create four servo objects 
Servo servo2; // create four servo objects 

int servo1Pin = D0;
int servo2Pin = D2;

// Published values for SG90 servos; adjust if needed
int minUs = 500;
int maxUs = 2400;

int pos = 0;      // position in degrees

void setup() {
  servo1.setPeriodHertz(50);      // Standard 50hz servo
  servo2.setPeriodHertz(50);      // Standard 50hz servo
  servo1.attach(servo1Pin, minUs, maxUs);
  servo2.attach(servo2Pin, minUs, maxUs);
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // sweep from 0 degrees to 180 degrees
    // in steps of 1 degree
    servo1.write(pos);
    servo2.write(pos);
    delay(2);             // waits 20ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // sweep from 180 degrees to 0 degrees
    servo1.write(pos);
    servo2.write(pos);
    delay(2);
  }
}

所感

Bluetooth連携のコードを試したときにこっちの実装のほうがESP32ナイズされてるからか安定して動きました。

ESP32でサーボやるときはこっちのライブラリのほうが良いかもしれなせいです。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした