/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-dc-motor */ // constants won't change const int IN1_PIN = 4; // the Arduino pin connected to the IN1 pin L298N // the setup function runs once when you press reset or power the board void setup() { // initialize digital pins as outputs. pinMode(IN1_PIN, OUTPUT); // pinMode(IN2_PIN, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(IN1_PIN, HIGH); // control motor A spins clockwise for (int speed = 0; speed <= 255; speed++) { delay(10); } delay(1000); // rotate at maximum speed 1 seconds in in clockwise direction // change direction digitalWrite(IN1_PIN, LOW); // control motor A spins anti-clockwise delay(1000); // rotate at maximum speed 1 seconds in in anti-clockwise direction for (int speed = 255; speed >= 0; speed--) { delay(10); } delay(1000); // stop motor 1 second }