2022年8月23日 星期二

步進電機與 NodeMCU 的接口

 

介紹

步進電機

步進電機

步進電機是一種無刷直流電機,可將 360° 的全旋轉角度分成多個相等的步長。

通過施加一定序列的控制信號來旋轉電機。可以通過改變施加控制信號的速率來改變旋轉速度。

有關步進電機、其控制序列以及如何使用它的更多信息,請參閱傳感器和模塊部分中的步進電機主題。

NodeMCU GPIO 可用於控制步進電機旋轉。我們可以在 NodeMCU 的 GPIO 引腳上生成一系列控制信號。要了解有關 NodeMCU GPIO 的更多信息,請參閱 帶有 ESPlorer IDENodeMCU GPIO 和帶有 Arduino IDE的 NodeMCU GPIO 。

 

接口圖

NodeMCU 與步進電機的接口

NodeMCU與步進電機接口

例子

讓我們以順時針和逆時針方向交替旋轉步進電機。

在這裡,我們使用的是六線單極步進電機。只需要四根線來控制這個步進電機。步進電機的兩條中心抽頭線連接到 5V 電源。

ULN2003驅動器用於驅動單極步進電機。

注意:要找到繞組線圈及其中心抽頭引線,請測量引線之間的電阻。與繞組端之間的電阻相比,從中心引線我們將獲得一半的電阻值。

我們可以用 Lua 腳本或 C/C++ 語言為 NodeMCU DevKit 編寫代碼。我們使用 ESPlorer IDE 用 Lua 腳本編寫代碼,使用 Arduino IDE 用 C/C++ 編寫代碼。要了解更多信息,請參閱 使用 ESPlorer IDE 開始使用 NodeMCU(對 NodeMCU 使用 Lua 腳本)和使用 Arduino IDE 開始使用 NodeMCU(對 NodeMCU 使用基於 C/C++ 語言的 Arduino 草圖)。

 

步進電機的 Lua 腳本

wire1 = 8
wire2 = 7
wire3 = 6
wire4 = 5

_delay = 50000 --delay in between two steps. minimum delay more the rotational speed

gpio.mode(wire1, gpio.OUTPUT)--set four wires as output
gpio.mode(wire2, gpio.OUTPUT)
gpio.mode(wire3, gpio.OUTPUT)
gpio.mode(wire4, gpio.OUTPUT)

function sequence(a, b, c, d)--four step sequence to stepper motor
    gpio.write(wire1, a)
    gpio.write(wire2, b)
    gpio.write(wire3, c)
    gpio.write(wire4, d)
    tmr.delay(_delay)
end

while true do
    for i = 1 ,12 do --Rotation in one direction
        sequence(gpio.HIGH, gpio.LOW, gpio.LOW, gpio.LOW)
        sequence(gpio.HIGH, gpio.HIGH, gpio.LOW, gpio.LOW)
        sequence(gpio.LOW, gpio.HIGH, gpio.LOW, gpio.LOW)
        sequence(gpio.LOW, gpio.HIGH, gpio.HIGH, gpio.LOW)
        sequence(gpio.LOW, gpio.LOW, gpio.HIGH, gpio.LOW)
        sequence(gpio.LOW, gpio.LOW, gpio.HIGH, gpio.HIGH)
        sequence(gpio.LOW, gpio.LOW, gpio.LOW, gpio.HIGH)
        sequence(gpio.HIGH, gpio.LOW, gpio.LOW, gpio.HIGH)
    end
    sequence(gpio.HIGH, gpio.LOW, gpio.LOW, gpio.LOW)
    for i = 1 ,12 do --Rotation in opposite direction
        sequence(gpio.LOW, gpio.LOW, gpio.LOW, gpio.HIGH)
        sequence(gpio.LOW, gpio.LOW, gpio.HIGH, gpio.HIGH)
        sequence(gpio.LOW, gpio.LOW, gpio.HIGH, gpio.LOW)
        sequence(gpio.LOW, gpio.HIGH, gpio.HIGH, gpio.LOW)
        sequence(gpio.LOW, gpio.HIGH, gpio.LOW, gpio.LOW)
        sequence(gpio.HIGH, gpio.HIGH, gpio.LOW, gpio.LOW)
        sequence(gpio.HIGH, gpio.LOW, gpio.LOW, gpio.LOW)
        sequence(gpio.HIGH, gpio.LOW, gpio.LOW, gpio.HIGH)
    end
    sequence(gpio.LOW, gpio.LOW, gpio.LOW, gpio.HIGH)
end 

步進電機的 Arduino 草圖

uint8_t wire1 = D8;
uint8_t wire2 = D7;
uint8_t wire3 = D6;
uint8_t wire4 = D5;

const uint16_t _delay = 50; /* delay in between two steps. minimum delay more the rotational speed */

void sequence(bool a, bool b, bool c, bool d){  /* four step sequence to stepper motor */
  digitalWrite(wire1, a);
  digitalWrite(wire2, b);
  digitalWrite(wire3, c);
  digitalWrite(wire4, d);
  delay(_delay);
}

void setup() {
  pinMode(wire1, OUTPUT); /* set four wires as output */
  pinMode(wire2, OUTPUT);
  pinMode(wire3, OUTPUT);
  pinMode(wire4, OUTPUT);
}

void loop() {
  /* Rotation in one direction */
  for(int i = 0; i<12; i++)
  {
    sequence(HIGH, LOW, LOW, LOW);
    sequence(HIGH, HIGH, LOW, LOW);
    sequence(LOW, HIGH, LOW, LOW);
    sequence(LOW, HIGH, HIGH, LOW);
    sequence(LOW, LOW, HIGH, LOW);
    sequence(LOW, LOW, HIGH, HIGH);
    sequence(LOW, LOW, LOW, HIGH);
    sequence(HIGH, LOW, LOW, HIGH);
  }
  sequence(HIGH, LOW, LOW, LOW);
  /* Rotation in opposite direction */
  for(int j = 0; j<12; j++)
  {
    sequence(LOW, LOW, LOW, HIGH);
    sequence(LOW, LOW, HIGH, HIGH);
    sequence(LOW, LOW, HIGH, LOW);
    sequence(LOW, HIGH, HIGH, LOW);
    sequence(LOW, HIGH, LOW, LOW);
    sequence(HIGH, HIGH, LOW, LOW);
    sequence(HIGH, LOW, LOW, LOW);
    sequence(HIGH, LOW, LOW, HIGH);
  }
  sequence(LOW, LOW, LOW, HIGH);
}

 

資料來源:https://www.electronicwings.com/nodemcu/stepper-motor-interfacing-with-nodemcu

沒有留言: