อุปกรณ์
28BYJ-48 Stepper Motor
ULN2003 Motor Driver
ESP8266
Jumper Wires
5V Power Supply เช่น ถ่าน 9v สี่เหลี่ยม หรือ ถ่าน BRC18650 3.7v. 2 ก้อน (ถ่านชาร์จ)
ขา IN1 ต่อกับ D2 (GPIO4)
ขา IN2 ต่อกับ D5 (GPIO14)
ขา IN3 ต่อกับ D6 (GPIO12)
ขา IN4 ต่อกับ D8 (GPIO15)
โหลด library เพิ่ม ที่ library manager ค้นหา accelstepper
#include <AccelStepper.h>
const int stepsPerRevolution = 2048; // change this to fit the number of steps per revolution
// ULN2003 Motor Driver Pins
#define IN1 5
#define IN2 4
#define IN3 14
#define IN4 12
// initialize the stepper library
AccelStepper stepper(AccelStepper::HALF4WIRE, IN1, IN3, IN2, IN4);
void setup() {
// initialize the serial port
Serial.begin(115200);
// set the speed and acceleration
stepper.setMaxSpeed(500);
stepper.setAcceleration(100);
// set target position
stepper.moveTo(stepsPerRevolution);
}
void loop() {
// check current stepper motor position to invert direction
if (stepper.distanceToGo() == 0){
stepper.moveTo(-stepper.currentPosition());
Serial.println("Changing direction");
}
// move the stepper motor (one step at a time)
stepper.run();
}