Hi Everyone,
I’m new to coding on Arduino and i’m new to stepper motors in general.
- Objective: Detect physical resistance to “home” my 28BYJ48 stepper motor and UN2003 driver board.
- Cannot use a limit switch in my project which is why i’m relying on voltage drop.
- Using a ESP8266 (ESP-12E) wifi controller.
- I’ve tried Accelstepper but I can’t seem to understand it, I will eventually try to move towards it because of the built in homing feature.
Wiring: (The driver board is wired to the ESP-12E pins 16,4,0,5)
I was looking at some of SBright33s posts about connecting the input directly to one of the motor windings to detect a voltage drop and while that does work, the voltage fluctuates too much when it goes from one winding to the next. It’s been about 6 weeks since I’ve started this project and I haven’t been able to figure it out myself which is why i’m asking for help. Here’s what I have so far…
#include <Stepper.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
const char *ssid = "myESPNetwork";
const char *password = "password"; //the password should be 8 char or more
ESP8266WebServer server(80);
//setting up the webpage:
char HTML[ ] = "<html><body><h1><a href=\"\\\"><button>Refresh</button></a><a href=\"calibrate\"><button>Calibrate</button></a></body></html>";
void handleRoot() {
server.send(200, "text/html", HTML);
}
const int stepsPerRevolution = 514; //steps per revolution for the stepper motor
Stepper myStepper(stepsPerRevolution, 16, 4, 0, 5); //16,4,0,5 are the pins on the microcontroller
int steps;
int stepEnd;
void delaySeconds(int seconds) { //easier way to delay in seconds instead of milliseconds
for (int i = 0; i <= seconds * 10; i++) {
delay(100);
ESP.wdtFeed();
}
}
void setup() {
Serial.begin(9600);
Serial.println();
Serial.print("Configuring access point...");
/* You can remove the password parameter if you want the AP to be open. */
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.on("/", handleRoot);
server.on("/calibrate", calibrate);
server.begin();
Serial.println("HTTP server started"); ESP.wdtDisable(); //WDT (Watch Dog Timer) Disable is for the wifi board so it doesnt soft reset it because nothing's happening
}
void loop() {
server.handleClient();
ESP.wdtDisable();
}
void calibrate() {
server.send(200, "text/html", HTML);
Serial.println("Calibrating...");
delaySeconds(2);
for (int i = 0; i <= 400; i++) { //just to check the voltage
myStepper.step(-1);
int sensor = analogRead(A0);
float voltage = sensor * (1.6 / 1024.0); //for some reason 3.3/1024 isn't accurate
Serial.println(voltage);
}
boolean motorOn = true;
while (motorOn == true) { //spin counter-clockwise until voltage drops (hits a dead end)
int sensorValue = analogRead(A0);
float Voltage = sensorValue * (1.6 / 1024.0); //for some reason 3.3/1024 isn't accurate
myStepper.step(-1);
ESP.wdtDisable();
float uvoltage = Voltage;
if (Voltage > 1.1 && uvoltage < Voltage - .03) { //check if voltage is above 1.1 (while fluctuating) and if it drops .03V or more (when there's resistance) stop the motor
motorOn == false;
delaySeconds(2);
break;
Serial.println("Home Position Found!");
}
}
steps = 0;
Serial.print("Home Position:");
Serial.println(steps);
Serial.println("Looking for end position...");
boolean motorOn = true;
while (motorOn == true) { //spin clockwise until voltage drops (hits a dead end)
int sensorValue = analogRead(A0);
float Voltage = sensorValue * (1.6 / 1024.0);
myStepper.step(1);
steps++;
myStepper.setSpeed(60);
ESP.wdtDisable();
float uvoltage = Voltage;
if (Voltage > 1.1 && uvoltage < Voltage - .03) {
motorOn == false;
delaySeconds(2);
break;
Serial.println("End Position Found!");
}
}
int stepEnd = steps;
Serial.print("Ending Position:");
Serial.println(stepEnd);
Serial.println();
Serial.println();
}