22
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ESP32 Servoを使ってみる

Last updated at Posted at 2018-12-25

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でサーボやるときはこっちのライブラリのほうが良いかもしれなせいです。

22
14
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?