int enA = 3; // This pin is to enable PWM speed control, and we have to use the ~ digital pin, here ~D3 int in1 = 2; // Any digital pins can be use to control the direction of the motor int in2 = 4; // Any digital pins can be use to control the direction of the motor void setup() // This part of the code is run only once, and it's used to set the pins to output { pinMode(enA, OUTPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); } void loop() // Runs the code repeadetly { digitalWrite(in1, HIGH); // Set the rotational direction, by setting one input pin to HIGH and second to LOW digitalWrite(in2, LOW); // They have to be opposite, representing flow from positive to negative voltage // With "6V 4xAA Batteries" we are able to achieve a PWM range from 100 to 255 analogWrite(enA, 100); // Here you can enter any value between 100 and 255, and the motor will spin at greater or lower speed delay(3000); // Spins for 3 s /*******************************************************************************************************************/ digitalWrite(in1, LOW); // To stop the motor we simply need to set both input pins to same value, here LOW digitalWrite(in2, LOW); analogWrite(enA, 0); // It's redundand, but we can also stop the motor by setting the PWM value to 0 delay(1000); // Holds for 1 s /*******************************************************************************************************************/ digitalWrite(in1, LOW); // Change the rotational direction digitalWrite(in2, HIGH); analogWrite(enA, 255); // To be able to run the motor on lower speeds we need to set the initial "current kick" delay(10); analogWrite(enA, 50); // Now we can use the range of 50 to 255 PWM, and run the motors even slower delay(3000); // Spins for 3 s in opposite direction /*******************************************************************************************************************/ digitalWrite(in1, LOW); digitalWrite(in2, LOW); analogWrite(enA, 0); delay(1000); // Stops for 1 s }