
In this tutorial we will order a DC motor by the Arduino board in both directions:
Here is the program that allows to control a DC motor by the Arduino.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
// Constants const int enableBridge1 = 2; const int MotorForward1 = 3; const int MotorReverse1 = 5; // Variables int Power = 80; //Motor velocity between 0 and 255 void setup(){ pinMode(MotorForward1,OUTPUT); pinMode(MotorReverse1,OUTPUT); pinMode(enableBridge1,OUTPUT); } void loop(){ digitalWrite(enableBridge1,HIGH); // Active pont en H // Rotates in a direct direction for 3 seconds analogWrite(MotorForward1,Power); analogWrite(MotorReverse1,0); delay(3000); // Rotates in reverse direction for 3 seconds analogWrite(MotorForward1,0); analogWrite(MotorReverse1,Power); delay(3000); //Stops the DC motor for 1 second analogWrite(MotorForward1,0); analogWrite(MotorReverse1,0); digitalWrite(enableBridge1,LOW); delay(1000); } |