Skip to content

Instantly share code, notes, and snippets.

@gsampallo
Created March 31, 2020 19:22
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save gsampallo/81331043fb443fb627c2697ab53dd342 to your computer and use it in GitHub Desktop.
Control de un motor paso a paso ( 28BYJ-48) con Attiny85 y L293D
/*
* Control de un motor paso a paso ( 28BYJ-48) con Attiny85 y L293D
* Esquemas y documentación aqui: https://www.gsampallo.com/blog/?p=624
*/
const int motorPin1 = 0;
const int motorPin2 = 1;
const int motorPin3 = 2;
const int motorPin4 = 4;
int motorSpeed = 1200; //variable para fijar la velocidad
int stepCounter = 0; // contador para los pasos
int stepsPerRev = 512; // pasos para una vuelta completa
//secuencia media fase
const int numSteps = 8;
const int stepsLookup[8] = { B1000, B1100, B0100, B0110, B0010, B0011, B0001, B1001 };
void setup() {
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
}
void loop() {
for (int i = 0; i < 100; i++) {
clockwise();
delayMicroseconds(motorSpeed);
}
}
//girar en sentido derecho
void clockwise() {
stepCounter++;
if (stepCounter >= numSteps) stepCounter = 0;
setOutput(stepCounter);
}
//izquierda
void anticlockwise() {
stepCounter--;
if (stepCounter < 0) stepCounter = numSteps - 1;
setOutput(stepCounter);
}
void setOutput(int step) {
digitalWrite(motorPin1, bitRead(stepsLookup[step], 0));
digitalWrite(motorPin2, bitRead(stepsLookup[step], 1));
digitalWrite(motorPin3, bitRead(stepsLookup[step], 2));
digitalWrite(motorPin4, bitRead(stepsLookup[step], 3));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment