In the previous article on getting started with Raspberry Pi (Part 3), I introduced how to drive a stepper motor. At that time, the stepper motor driven was a simpler one with lower precision. Today, we will drive a more complex stepper motor. Of course, I will still use Arduino as an example, but I will provide the Raspberry Pi code as well.
This drawing software is called (fritzing), which can only be used for drawing, not for simulation.
The explanation of the jumpers MS1, MS2, MS3 (in the example, they are low-level, floating, or grounded, using full-step mode) are full-step, 1/2 step, 1/4 step, 1/8 step, and 1/16 step modes.
A stepper motor moves one step at 1.8 degrees, which means 200 steps for one revolution. For example, using 1/16 stepping, it requires 3200 steps to complete one revolution.
The three floating wires in the above image are MS1, MS2, MS3.
The image below is of the A4988 driver; some may be green, but it does not matter.
Next, I will provide the Arduino code, which looks quite simple. Interested readers can check more information online.
int x; void setup(){ pinMode(6,OUTPUT); // Enable pinMode(5,OUTPUT); // Step pinMode(4,OUTPUT); // Dir digitalWrite(6,LOW); // Set Enable low} void loop(){ digitalWrite(4,HIGH); // Set Dir high for(x = 0; x < 200; x++) // Loop 200 times { digitalWrite(5,HIGH); // Output high delayMicroseconds(800); // Wait 1/2 a ms digitalWrite(5,LOW); // Output low delayMicroseconds(800); // Wait 1/2 a ms } delay(1000); // pause one second digitalWrite(4,LOW); // Set Dir low for(x = 0; x < 200; x++) // Loop 2000 times { digitalWrite(5,HIGH); // Output high delayMicroseconds(800); // Wait 1/2 a ms digitalWrite(5,LOW); // Output low delayMicroseconds(800); // Wait 1/2 a ms } delay(1000); // pause one second}
Next, I will provide the Raspberry Pi code.
#include <stdio.h>#include <wiringPi.h>
void init();void loop();int main(){ init(); loop(); return 0;} void init(){ wiringPiSetup(); pinMode(6,OUTPUT); // Enable pinMode(5,OUTPUT); // Step pinMode(4,OUTPUT); // Dir digitalWrite(6,LOW); // Set Enable low} void loop(){ digitalWrite(4,HIGH); // Set Dir high for(x = 0; x < 200; x++) // Loop 200 times { digitalWrite(5,HIGH); // Output high delayMicroseconds(800); // Wait 1/2 a ms digitalWrite(5,LOW); // Output low delayMicroseconds(800); // Wait 1/2 a ms } delay(1000); // pause one second digitalWrite(4,LOW); // Set Dir low for(x = 0; x < 200; x++) // Loop 2000 times { digitalWrite(5,HIGH); // Output high delayMicroseconds(800); // Wait 1/2 a ms digitalWrite(5,LOW); // Output low delayMicroseconds(800); // Wait 1/2 a ms } delay(1000); // pause one second}
The Raspberry Pi code is quite similar to the Arduino code, indeed. It is worth noting that the pins used for the Raspberry Pi are arbitrary, so please pay attention to which pins you are using when experimenting, and do not blindly follow my example.
Feel free to follow our public account. My knowledge is limited, and if there are any errors in the article, please feel free to give me feedback or leave a message. Thank you very much!