This week, I use WiFi to control a stepper motor remotely.
The Huzzah
For the WiFi portion of this project, I’m using an Adafruit Huzzah ESP32 board.
Controlling the stepper motor
The stepper motor
The stepper driver is powered via this 12v power supply.
Controlling the stepper motor with the Uno
The setup
I’m using the Uno as a middleman between the Huzzah and the driver because the Huzzah wouldn’t have enough ports and the driver expects a 5v input that the Huzzah can’t supply since it uses 3.3v output for its logic. A logic level converter/shift register combo would be a cleaner solution to this, however, I wanted to combine wired and wireless networking here just for fun.
The Uno code
Here’s the basic code I used to get the Uno to control the stepper.
// Include the Arduino Stepper Library
#include <Stepper.h>
// Number of steps per output rotation
const int stepsPerRevolution = 200;
// Create Instance of Stepper library
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
void setup()
{
// set the speed at 80 rpm:
myStepper.setSpeed(80);
// initialize the serial port:
Serial.begin(9600);
}
void loop()
{
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(500);
// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
delay(500);
}
I set the stepsPerRevolution to 200 because the (stepper motor)[https://www.omc-stepperonline.com/nema-17-stepper-motor/nema-17-bipolar-1-8deg-26ncm-36-8oz-in-0-4a-12v-42x42x34mm-4-wires.html] I am working with is rated for 1.8 degrees per step, and 360 degrees/1.8 = 200.
Controlling the direction of the stepper motor
The receiver Uno
I want the Uno to act as a receiver. It should wait for a signal from the Huzzah and use that to turn the stepper motor clockwise, counterclockwise, or off. I updated the code to do just this.
// Include the Arduino Stepper Library
#include <Stepper.h>
const int turnClockwisePin = 6;
const int turnCounterclockwisePin= 5;
const int offPin = 3;
// Number of steps per output rotation
const int stepsPerRevolution = 200;
// Create Instance of Stepper library
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
void setup()
{
// set the speed at 80 rpm:
myStepper.setSpeed(80);
// initialize the serial port:
Serial.begin(9600);
}
void loop()
{
if (digitalRead(turnClockwisePin)) {
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
}
if (digitalRead(turnCounterclockwisePin)) {
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
}
if (digitalRead(offPin)) {
Serial.println("off");
myStepper.step(0);
}
}
The Transmitter Huzzah
I set up the Huzzah as a simple local web server with 3 endpoints: clockwise, counterclockwise, and off. These buttons correspond to output pins on the huzzah that are connected to the Uno.
#include <WiFi.h>
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
const int clockwisePin = 15;
const int counterclockwisePin = 33;
const int offPin = 32;
WiFiServer server(80);
void setup()
{
Serial.begin(115200);
pinMode(clockwisePin, OUTPUT); // set the clockwise pin output
pinMode(counterclockwisePin, OUTPUT); // set counterclockwise pin output
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
int value = 0;
void loop(){
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("New Client."); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
client.print("Click <a href=\"/CLOCK\">here</a> to turn the motor clockwise.<br>");
client.print("Click <a href=\"/CCLOCK\">here</a> to turn the motor counterclockwise.<br>");
client.print("Click <a href=\"/OFF\">here</a> to turn the motor OFF.<br>");
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
} else { // if you got a newline, then clear currentLine:
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
if (currentLine.endsWith("GET /CLOCK")) {
digitalWrite(counterclockwisePin, LOW);
digitalWrite(clockwisePin, HIGH);
}
if (currentLine.endsWith("GET /CCLOCK")) {
digitalWrite(clockwisePin, LOW);
digitalWrite(counterclockwisePin, HIGH);
}
if (currentLine.endsWith("GET /OFF")) {
digitalWrite(clockwisePin, LOW);
digitalWrite(counterclockwisePin, LOW);
}
}
}
// close the connection:
client.stop();
Serial.println("Client Disconnected.");
}
}
All should be ready to go!