記事一覧

M5StackでEPS32servoとPCA9685サーボドライバを同時に使って複数のサーボを動かしてみた。

M5StackでEPS32servoとPCA9685サーボドライバを同時に使って複数のサーボを動かしてみた。


I tried multiple servos by using EPS 32servo and PCA9685 servo driver at the same time in M5Stack.



こんにちはRoboTakaoです。
Hello everyone! I'm RoboTakao!

先日はM5StackでESP32servoというライブラリーを使って複数のサーボを動かしました
The other day I used a library ESP32servo with M5Stack to run multiple servos.

また、PCA9685というI2C接続のサーボドライバをM5Stackで使う方法も紹介しています。
In addition, I also introduced a method of using a PCA9685 servo driver with I2C connection with M5Stack.

今回はESP32servoとPCA9685を同時に使って複数のサーボを動かしてみした。
This time I tried running multiple servos using ESP32servo and PCA9685 at the same time.

m5stack_pca9685_05.jpg

接続 Connection

m5stack_pca9685_04.png

動作確認 Operation check

問題なくサーボが動きました。
The servo moved without problems.



それではこの辺で失礼します。
ありがとうございました。
Thank you! Good-bye!

スケッチ sketch


#include
#include //ESPservo
#include // I2C setting
#include //PCA9685用ヘッダーファイル

// create four servo objects (ESP32servo)
Servo servo1;

//PCA9685のアドレス指定
PCA9685 pwm = PCA9685(0x40);

// Published values for SG90 servos; adjust if needed (ESP32servo)
int minUs = 700;
int maxUs = 2300;

#define SERVOMIN 150 //最小パルス幅 (GWS Micro2BBMG) (PCA9685)
#define SERVOMAX 500 //最大パルス幅 (GWS Micro2BBMG) (PCA9685)

// These are all GPIO pins on the ESP32
// Recommended pins include 2,4,12-19,21-23,25-27,32-33
int servo1Pin = 18; //ESP32servo

int pos = 0; // position in degrees

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

pwm.begin(); //初期設定 (アドレス0x40用) (PCA9685)
pwm.setPWMFreq(50); //PWM周期を50Hzに設定 (アドレス0x40用) (PCA9685)
}

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

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

}

void servo_write(int ch, int ang){ //動かすサーボチャンネルと角度を指定
ang = map(ang, 0, 180, SERVOMIN, SERVOMAX); //角度(0~180)をPWMのパルス幅(150~500)に変換
pwm.setPWM(ch, 0, ang);
}

スポンサードリンク

コメント

コメントの投稿

非公開コメント

プロフィール

RoboTakao

Author:RoboTakao
みなさんご訪問ありがとうございます。ロボット作りたいけどお小遣いそんなにないし、簡単でローコストでロボットを作るための私のプロジェクトを紹介します。

ウェブサイトもありますのでそちらもよろしくお願いします。
http://robotakao.jp/

スポンサーリンク