I had little to no experience driving stepper motors, so before designing, printing, assembling and programming the 'Antique' Auto Correcting Analog Clock (https://www.instructables.com/id/Antique-Auto-Correcting-Analog-Clock/) using a stepper motor, I decided to design and test the software using a much simpler test fixture. If you, as I, have little to no experience with stepper motors, then hopefully this short Instructable with source code will help.
The test fixture requires the following components:
- A prototyping board.
- An Adafruit Feather ESP32 with female headers.
- A ULN2003 based stepper controller board.
- A 28BYJ-48 5vdc stepper motor.
- Some male to female jumper wires.
- An Adafruit 3.7vdc lithium battery.
- A 3D printed indicator hand.
The stepper controller, stepper motor and jumper wires I used are included in a 5 pack which I purchased as a kit on line (search for "TIMESETL 5pcs DC 5V Stepper Motor 28BYJ-48 + 5pcs ULN2003 Driver Board + 40pcs Male Female Jumper Wire Cable").
The battery is optional. Note the battery outputs 3.7vdc, but the stepper controller board and stepper are 5vdc. The test fixture will operate on battery power only, even at the lower voltage.
I've included a video showing the steps required to download the software to the ESP32, wire the ESP32 to the stepper motor controller and plug in the stepper motor and battery.
Step 1: Wiring.
I used the male / female jumper wires included in the kit to wire the test fixture. Six wires are required, and are inserted as follows:
- ESP32 pin 14 (male) to stepper board pin IN4 (female).
- ESP32 pin 32 (male) to stepper board pin IN3 (female).
- ESP32 pin 15 (male) to stepper board pin IN2 (female).
- ESP32 pin 33 (male) to stepper board pin IN1 (female).
- ESP32 pin "GND" (male) to the stepper board pin "-" (female).
- ESP32 pin "USB" (male) for USB operation OR "BAT" (male) for battery operation, to the stepper board pin "+" (female).
Once the wires are inserted and double checked, plug the stepper motor cable into the stepper motor controller board connector. The connector is keyed and will only fit one way.
Finally, if using a battery, plug it into the ESP32 battery connector.
Step 2: Indicator.
For an indicator on the stepper motor, I designed and 3D printed an indicator hand "Hand.stl". I printed the indicator hand at .15mm layer height, 20% infill with no supports, then pressed it onto the stepper motor shaft.
As an alternative, tape, cardboard or other material can be used as an indicator.
Step 3: Software.
I wrote the stepper test software in the Arduino 1.8.5 environment. If you have not already done so, download the Arduino environment and necessary USB drivers onto your computer and install them. Also, visit the Adafruit website for any additional Adafruit ESP32 related software. I found this link to be very helpful: Adafruit ESP32 and the Arduino Environment.
With a USB cable connected between your computer and the ESP32, and "Stepper.ino" loaded into the Arduino environment, download "Stepper.ino" to the ESP32.
Once downloaded, the stepper should step 6 degrees once per second.
I wrote this test software for two reasons; first, to learn how to drive a stepper motor, and second, to convert the 4096 steps per rotation of the stepper motor into 60 one second 6 degree "ticks" for the clock.
The function "Step(nDirection)" drives the stepper motor. This function maintains a local (static) integer variable "nPhase", that is either incremented or decremented by one (each time the function is called), according to the sign of the function argument nDirection. This variable is limited in range to 0 through 7, which, when used along with the case switch, drives the motor phases in accordance with the manufacturers specifications for each step.
The function "Update()" determines when and how many steps to take for each tick to evenly space 60 ticks per 360 degrees of rotation. This function steps the stepper motor either 68 or 69 steps for each tick. For example, if the function only used 68 steps per tick, then (68 steps * 60 ticks) = 4080 steps would not be enough steps to complete the 360 degrees of rotation (remember the stepper requires 4096 steps for 360 degrees of rotation). And if the function used 69 steps per tick, then (69 steps * 60 ticks) = 4140 would be too many steps. The simple algorithm I wrote evenly distributes 68 and 69 step ticks throughout the 360 degree rotation, and can determine which direction of rotation is quickest to the desired second count (used in the clock).
And that is how I designed and tested the software for 'Antique' Auto Correcting Analog Clock.
If you have any suggestions and / or questions, please feel free to comment and I'll do my best to answer.