CuriousScientist

PWM DC Motor Control with Potentiometer and L298N

Oct 27th, 2019
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //If you found my video helpful, please SUBSCRIBE:  https://www.youtube.com/channel/UCKp1MzuAceJnDqAvsZl_46g
  2. //The code belongs to this tutorial video: https://youtu.be/8a5k_q0Bu7M
  3. /*
  4.  * Controlling a DC motor with a potmeter
  5.  * Starting the program and stopping the program with Serial
  6.  * Use 24V power supply at max; control power with potmeter for PWM
  7.  * Connect arduino GND to L298N GND -> Motor will stall otherwise
  8.  *  
  9.  */
  10.  
  11. /*
  12. You can also support me by buying the parts using the following links:
  13. L298N Dual H-bridge DC Motor Driver: https://www.banggood.com/custlink/vmmv0oOv4O
  14. DC Motor with Planetary Gear: https://www.banggood.com/custlink/KmD34jOG4A
  15. DC Motor 3500-9000 RPM: https://www.banggood.com/custlink/Dm3DBoO3gf
  16. Arduino UNO: https://www.banggood.com/custlink/33KKF85c3i
  17. Variable Power Supply: https://www.banggood.com/custlink/DGKvSpwFvQ
  18. Linear Slide Potentiometer:https://www.banggood.com/custlink/Gm3DiDNLtk
  19. */
  20.  
  21. int potPin = 0;                    // select the input pin for the potentiometer (A0)
  22. int val = 0;                       // variable to store the value coming from the sensor
  23. char receivedCommand;              // character for controlling the serial program
  24. //---------------Motor
  25. const int pinA = 8;               //Enable (Power)
  26. const int enA = 5;                //Enable (PWM) '~'
  27. //-------------------
  28. bool runningEnabled = false;
  29. bool newData = false;
  30.  
  31.  
  32. void setup()
  33. {
  34.   pinMode(pinA, OUTPUT);
  35.   pinMode(enA, OUTPUT);
  36.   Serial.begin(9600);  
  37.   Serial.println("DC-MOT-POT");
  38. }
  39.  
  40. void loop()
  41. {
  42. readSerialPort();  
  43. readPotmeter();
  44. }
  45.  
  46. void readSerialPort()
  47. {
  48.   if (Serial.available() > 0) //is there a data on the serial?
  49.      {
  50.         receivedCommand = Serial.read();
  51.         newData = true; //if we received a new command we set this bool to true
  52.      }
  53.      
  54.      if (newData == true)
  55.      {
  56.           if(receivedCommand == 's') //Start
  57.             {
  58.               runningEnabled = true; //start = enable running
  59.             }
  60.          
  61.           if(receivedCommand == 'n')//Stop
  62.             {
  63.               runningEnabled = false; //shut down everything
  64.               digitalWrite(pinA, LOW);
  65.             }
  66.       }
  67.         newData = false;  //reset newData to false so we can receive new command
  68.  
  69. }
  70.  
  71. void readPotmeter()
  72. {
  73.   if(runningEnabled == true) //is running is enabled?
  74.   {
  75.     val = analogRead(potPin);    // read the value from the pot (A0)
  76.     val = map(val, 0, 1023, 0, 255); //map 0-1023 to 0-255 ; |-----------| ---> |------|
  77.     analogWrite(enA, val); //write PWM (pin ~5)
  78.     digitalWrite(pinA, HIGH); //enable power
  79.     Serial.println(val); //print on serial
  80.   }
  81.   else
  82.   { //do nothing, disable power if it was enabled previously
  83.     runningEnabled = false;
  84.     digitalWrite(pinA, LOW);
  85.   }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment