Other Arduino Codes and Videos by Robojax

Control Stepper Motor with L298N module

Patrons Early Access

Control Stepper Motor with L298N red module with Arduino

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

Resources for this sketch


  /*
 * MotorKnob
 * Modefied by Ahmad Shamshiri for Robojax on July 20, 2019
 * in Ajax, Ontario, Canada
 * Watch video instruction for this code: https://youtu.be/cYTICj4DWYc
 * 
 * A stepper motor follows the turns of a potentiometer
 * (or other sensor) on analog input 0.
 *
 * http://www.arduino.cc/en/Reference/Stepper
 * 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
  stepper.setSpeed(5);
  Serial.begin(9600);
}

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 tutorial helpful, please support me so I can continue creating content like this. You can support me on Patreon ( Launched June 14, 2019): http://bit.ly/2ZnITLz

Or support me via PayPal