steppingモータ使ってみた

# はじめに

amazonで購入した[こちら](https://www.amazon.co.jp/gp/product/B07FL2ZYXS/ref=ppx_yo_dt_b_asin_title_o06_s00?ie=UTF8&psc=1)のsteppingモータをarduinoを使って動かしてみました．

![Screenshot from 2023-02-15 02-32-39.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/589906/c31f3b0e-2dec-0dfe-d5c9-e48fe156213b.png)

# 使用方法

使い方についてはカスタマーQ&Aに取扱説明書のリンクがあったので,そちらを参考にしました．

https://mega.nz/#F!5MMTET5S!FBYjHDamQSCvKLxKRDyPfg

# 配線

以下の用に配線します．

![Screenshot from 2023-02-15 02-50-27.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/589906/0649b0c2-b3a3-f81a-3a14-98dab0e9f2c7.png)

# プログラム

下記サンプルプログラムが用意されていたので，そちらを実行します．

```cpp:code.txt

#include <Arduino.h>

/*
TB6600 module connected with the ARDUINO:
EN +, DIR + : PUL + (total Yang connection mode)
EN-  5
DIR-  6
PUL-  7
*/
int EN = 5;
int DIR = 6;
int PUL = 7;

boolean  dir = true;

void setup()
{
  pinMode(EN,OUTPUT);
  pinMode(DIR,OUTPUT);
  pinMode(PUL,OUTPUT);
}
void clockwiseRotation(unsigned char rotationspeed)  //
{
    digitalWrite(DIR,LOW);
    digitalWrite(EN,HIGH);  
    digitalWrite(PUL,LOW);
    delay(rotationspeed);
    digitalWrite(PUL,HIGH);
    delay(rotationspeed);
}

void negativeRotation(unsigned char rotationspeed)
{
    digitalWrite(DIR,HIGH);
    digitalWrite(EN,HIGH);
    digitalWrite(PUL,LOW);
    delay(rotationspeed);
    digitalWrite(PUL,HIGH);
    delay(rotationspeed);
}

/*
Multiple_Of_45_Degrees:Stepper motor rotation multiples of 45 degrees;
Speed:Adjust the speed of stepper motor 
*/
void stepByStepClockwiseRotation(unsigned int Multiple_Of_45_Degrees,unsigned int Speed)   //+45.
{
  unsigned int n=0;
  for(n = 0;n < 50 * Multiple_Of_45_Degrees;n++)
  {
    clockwiseRotation(Speed); //The Speed value, the greater the stepper motor rotate Speed is slow
  }
  dir = true;
}

void stepByStepNegativeRotation(unsigned int Multiple_Of_45_Degrees,unsigned int Speed)   //-45.
{
  unsigned int n=0;
  for(n = 0;n < 50 * Multiple_Of_45_Degrees ;n++)
  {
    negativeRotation(Speed);
  }
  dir = false;
}
void loop()
{
    //clockwiseRotation();  
    if(dir)
    {
    delay(5000);
    stepByStepNegativeRotation(35,5);
    delay(1000);
    }
    else
   {
    stepByStepClockwiseRotation(35,5) ;
    delay(1000);
    }
}
```

# 実行結果

stepperがゆっくり旋回します．

![IMG_7361_AdobeExpress (1).gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/589906/a0f6dd3f-491f-c3e6-f7b0-a744b326c674.gif)

# 参考

https://burariweb.info/electronic-work/arduino-learning/arduino-stepping-motor-control.html

# 早く回す

- モタドラ設定

![IMG_7521.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/589906/d64c8978-e3c9-0dce-c59a-a38c17140d55.jpeg)


- プログラム
```

int step_pin = 7;
int dir_pin = 6;
int relay_pin = 5;

void setup()
{
  pinMode(step_pin, OUTPUT);
  pinMode(dir_pin, OUTPUT);
  pinMode(relay_pin, OUTPUT);
  digitalWrite(dir_pin, HIGH);
  digitalWrite(relay_pin, LOW);

  Serial.begin( 9600 );
}


void spinMotor(int direction, int num){
  digitalWrite(relay_pin, HIGH);
  if(direction == 0){
    digitalWrite(dir_pin, LOW);
  }else{
    digitalWrite(dir_pin, HIGH);
  }

  for(int i=0;i<num;i++)
  {
    digitalWrite(step_pin, HIGH);
    delayMicroseconds(30);
    digitalWrite(step_pin, LOW);
    delayMicroseconds(30);
  }
  digitalWrite(relay_pin, LOW);
}

void loop()
{
  spinMotor(0,200*32);
  delay(3000);

  spinMotor(1,200*32);
  delay(3000);

}
```

- 動作

https://youtu.be/UTyUa7fkI7Q