Skip to content

Instantly share code, notes, and snippets.

@battis
Last active April 3, 2020 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save battis/79c69de577647ef739571190c21fefa5 to your computer and use it in GitHub Desktop.
Save battis/79c69de577647ef739571190c21fefa5 to your computer and use it in GitHub Desktop.
Using Arduino Stepper library to control 28BYJ-48 stepper motor with L293D H-bridge
/*
* Example code for controlling a 28BYJ-48 stepper motor through an L293D H-Bridge
*/
// Include the Arduino Stepper.h library
#include <Stepper.h>
/*
* Motor outputs from the 28BYj-48:
* 1 - blue ------|
* 2 - pink ---| |-- coils A and A'
* 3 - yellow -|--|
* 4 - orange _|_____ coils B and B'
* 5 - red .......... reversible power
*/
const int STEPS_PER_REVOLUTION = 2048;
const int MAX_SPEED = 14; // revolutions per minute
/*
* L293D H-bridge wiring
* ----------\_/---------
* 5V (NOT arduino, if possible) -|Enable 1 Vss |- 5V (NOT arduino if possible)
* digital I/O pin 12 -|Input 1 Input 4 |- digital I/O pin 9
* motor pin 1 (blue) -|Output 1 Output 4 |- motor pin 4 (orange)
* Ground -|Ground Ground |-
* -|Ground Ground |-
* motor pin 3 (yellow) -|Output 2 Output 3 |- motor pin 2 (pink)
* digital I/O pin 11 -|Input 2 Input 3 |- digital I/O pin 10
* 5V (NOT arduino, if possible) -|Vs Enable 2 |- 5V (NOT arduino if possible)
* ---------------------
*/
const int INPUT1 = 12;
const int INPUT2 = 11;
const int INPUT3 = 10;
const int INPUT4 = 9;
Stepper myStepper = Stepper(STEPS_PER_REVOLUTION, INPUT1, INPUT2, INPUT3, INPUT4);
void setup() {
myStepper.setSpeed(MAX_SPEED);
Serial.begin(9600);
}
void loop() {
// Step one revolution in one direction:
Serial.println("revolving clockwise");
myStepper.step(STEPS_PER_REVOLUTION);
delay(2000);
// Step one revolution in the other direction:
Serial.println("revolvingg counter-clockwise");
myStepper.step(-STEPS_PER_REVOLUTION);
delay(2000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment