Control 4 wire Stepper Motor with L298N module

Project Video
Project Details

This is code is one of 4 examples shown in this video.

Project Images


Project Code




 * MotorKnob
 * Modefied by Ahmad Shamshiri for Robojax on July 20, 2019
 * in Ajax, Ontario, Canada
 * Watch video instruction for this code:
 * A stepper motor follows the turns of a potentiometer
 * (or other sensor) on analog input 0.
 * This example code is in the public domain.

#include <Stepper.h>

// change this to the number of steps on your motor
#define STEPS 200

// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to
Stepper stepper(STEPS, 8, 9, 10, 11);

// the previous reading from the analog input
int previous = 0;

void setup() {
  // set the speed of the motor to 30 RPMs

void loop() {
  // get the sensor value
  int val = analogRead(0);

  int step=map(val, 0, 1023, 0, STEPS);

  // move a number of steps equal to the change in the
  // sensor reading
  stepper.step(val - previous);

  // remember the previous value of the sensor
  previous = val;


If you found this Arduino course helpful, please support me so I can continue creating content like this. support me via PayPal