5
5

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.

ESP-WROOM-02でI2Cモータードライバ制御

5
Posted at

やりたいこと

ESP-WROOM-02で、I2CモータードライバのDRV8830でモーターの無段階変速制御。

機材

コード

//esp8266でWire.hでI2C通信テスト。1コだけ。
# include <Wire.h>;
const int address = 0x60;

void writeRegister(int adr,byte reg,byte vset,byte data){
  int vdata = vset << 2 | data;
  Wire.beginTransmission(adr);
  Wire.write(reg);
  Wire.write(vdata);
  Wire.endTransmission(true);
  Serial.print(adr,HEX);
  Serial.print(reg,HEX);
  Serial.print(vdata,HEX);
  delay(12);
}
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Wire.begin(4,14);
  Serial.println(address,HEX);
  Serial.println("Hello");
  delay(500);
}

void loop() {
  // put your main code here, to run repeatedly:
  writeRegister(address,0x00,0x25,0x01);//正転
  delay(1000);
  writeRegister(address,0x00,0x25,0x00);//静止
  delay(1000);
  writeRegister(address,0x00,0x25,0x02);//逆転
  delay(1000);
  writeRegister(address,0x00,0x25,0x00);//静止
  delay(1000);

  Serial.println("sendOK");
}

解説

  • 2行目:Wireライブラリ読み込み。
  • 3行目:DRV8830のアドレスを16進数で変数に代入。
  • void writeRegister(){}内:I2C書き込み関数。引数はアドレス、レジスタ(モード?モーター回すなら0x00)、電圧設定、レジスタ(正逆転静止制御。詳しくはデータシート8ページの表)。
  • void setup(){}内:セットアップ系とデバック用処理。
  • void loop(){}内:writeRegister()関数を呼び出すことで、以下の処理
    正転1秒、静止1秒、逆転1秒、静止1秒。(ループ)

DRV8830に渡すデータについて詳しくは、データーシート(下記)を見てください。

参考

参考にさせていただきました。ありがとうございました。

5
5
0

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
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?