کوډ ولټوئ

درس ۵۵-۱: کنټرول کول د یو واحد DC موټور په کارولو سره د فشار تڼیو

درس ۵۵-۱: کنټرول کول د یو واحد DC موټور په کارولو سره د فشار تڼیو

په دې ټیوټوریل کې به موږ زده کړو چې څنګه د L298N موټر کنټرولر ماډل، پش بټنونو او پوټینشیومیټر په کارولو سره یو واحد DC موټر کنټرول کړو. د بټنونو په فشارولو سره، موږ کولی شو موټر په ساعت ګرد یا د ساعت په مخالف لوري وګرځوو، او همدارنګه یې ودروو او د پوټینشیومیټر په کارولو سره د هغه سرعت کنټرول کړو. دا پروژه د Arduino پروژو کې د موټر کنټرول د پوهیدو لپاره غوره ده او د هارډویر سره د اړیکې عملي تجربه برابروي. د نورو روښانتیا لپاره، ډاډ ترلاسه کړئ چې د دې ټیوټوریل سره مل ویډیو وګورئ (په ویډیو کې په 0:45).

L298N Motor Controller Module
L298N موټر کنټرولر ماډل

هارډویر تشریح شوی

په دې پروژه کې اصلي برخه د L298N موټر کنټرولر دی، کوم چې تاسو ته اجازه درکوي د DC موټرو لوري او سرعت کنټرول کړئ. دا د H-برج ترتیب په کارولو سره کار کوي، چې موټر ته اجازه ورکوي په دواړو لوریو وګرځي. ماډل د 6 څخه تر 35 ولټو پورې د بریښنا ولټاژ ته اړتیا لري، چې دا د مختلفو موټرو ډولونو لپاره څو اړخیز کوي. سربیره پردې، دا د PWM (د پلن ماډلولیشن) وړتیاوې لري، چې د سرعت کنټرول ته اجازه ورکوي. موږ به د موټر د لوري کنټرول او د هغه د درولو لپاره پش بټنونه هم وکاروو. پوټینشیومیټر به د موټر د سرعت تنظیم کولو کې مرسته وکړي. دا برخې یوځای کار کوي ترڅو ستاسو د Arduino پروژو کې د موټر عملیاتو اداره کولو لپاره یوه ساده مګر اغیزمنه لاره چمتو کړي.

ډیټاشیټ تفصیلات

جوړونکی STMicroelectronics
د برخې شمېره L298N
منطق/IO ولټاژ 5 V
د بریښنا ولټاژ 6–35 V (VS)
د تولید جریان (په هر چینل) 2 A اعظمي/چینل (مطلق اعظمي)، 1 A عادي دوامداره
د لوړ جریان (په هر چینل) 3 A
د PWM فریکونسي لارښود 5 kHz
د ننوتلو منطق حدونه 0.8 V (ټیټ)، 2.0 V (لوړ)
د ولټاژ کمښت / RDS(on) / سنتریشن 2 V (عادي)
حرارتي حدونه 150 °C
بسته Multiwatt15
یادښتونه / ډولونه کولی شي تر 2 موټرو پورې کنټرول کړي
  • د دوامداره عملیاتو لپاره مناسب حرارتي سنک ډاډمن کړئ.
  • د ډیر تودوخې څخه مخنیوي لپاره د سرعت کنټرول لپاره PWM پنونه وکاروئ.
  • د موټر ډرایور ځمکه د Arduino ځمکې سره وصل کړئ.
  • د زیان مخنیوي لپاره د بریښنا ولټاژ په اړه محتاط اوسئ.
  • د مستحکم لوستلو ډاډ ترلاسه کولو لپاره د پش بټنونو لپاره پل-اپ ریزسټرونه وکاروئ.
  • د شاته بریښنا کولو مسلو څخه مخنیوي لپاره د پروګرام کولو پرمهال د بریښنا رسولو جلا کړئ.
Arduino wiring for L298N with pot 3push button to control 2 motors
Arduino wiring for L298N with pot 3push button to control 2 motors
Arduino wiring for L298N with pot 3push button to control single motor
Arduino wiring for L298N with pot 3push button to control single motor

د تارونو لارښوونې

Arduino wiring for L298N with pot 3push button to control single motor
د L298N لپاره د Arduino تارونه د پوټ او 3 پش بټن سره د واحد موټر کنټرول لپاره

د L298N موټر کنټرولر د Arduino سره د تارونو لپاره، د بیټرۍ مثبت ترمینل د L298N ماډل په VCC ترمینل سره او منفي ترمینل د GND سره وصل کړئ. د L298N GND ترمینل د Arduino GND سره هم وصل کړئ. د L298N څخه 5V تولید د Arduino 5V انپټ سره وصل کیدی شي، مګر یوازې د پروګرام کولو وروسته ترڅو شخړو څخه مخنیوی وشي. بیا، موټر د L298N د تولید ترمینلونو سره وصل کړئ. د موټر کنټرول لپاره، انپټ پنونه په لاندې ډول تار کړئ: IN1 د Arduino پن 2 سره، IN2 پن 4 سره، او د فعالولو پن ENA پن 3 سره وصل کړئ. د پش بټنونو لپاره، د هر بټن یو اړخ GND سره وصل کړئ، او بل اړخ په لاندې ډول: د لوري بټن پن 8 سره، د درولو بټن پن 9 سره وصل کړئ. په پای کې، پوټینشیومیټر وصل کړئ: یو اړخ GND ته، بل 5V ته، او منځنی پن د Arduino A0 ته.

Arduino wiring for L298N with pot 3push button to control 2 motors
د L298N لپاره د Arduino تارونه د پوټ او 3 پش بټن سره د 2 موټرو کنټرول لپاره

د کوډ مثالونه او واک ترو

لاندې کوډ ټوټه د موټر کنټرول پیل کوي او پنونه تنظیموي:


#include 
#define IN1 2
#define IN2 4
#define ENA 3 

const int motor1PushButtonDirection = 8;
const int motor1PushButtonStop = 9;

void setup() {
  Serial.begin(115200);
  motor.begin();
  pinMode(motor1PushButtonDirection, INPUT_PULLUP);
  pinMode(motor1PushButtonStop, INPUT_PULLUP);
}

په دې اقتباس کې، موږ د L298N موټر کنټرول لپاره اړین کتابتون شامل کوو او د موټر او پش بټنونو لپاره پنونه تعریف کوو. د `setup()` فنکشن سیریل اړیکه پیل کوي او د بټن پنونه د پل-اپ ریزسټرونو سره د انپټ په توګه ټاکي. بیا، موږ د بټنونو حالت چیک کوو او په لوپ کې موټر کنټرول کوو:


void loop() {
  updateState1();
  if(motor1StopState == HIGH) {
    motor.brake(motor1);     
  } else {
    motor.rotate(motor1, motor1Speed, motor1Direction);    
  }
  delay(pushButtonDelayTime);
}

دلته، د `loop()` فنکشن په دوامداره توګه د پش بټنونو حالت چیک کوي. که د درولو بټن فشار شوی وي، موټر به ودریږي؛ که نه، دا به په ټاکل شوي سرعت او لوري وګرځي. د `updateState1()` فنکشن د بټن حالتونه لوستل کوي او د موټر لوری تازه کوي:


void updateState1() {
  if(digitalRead(motor1PushButtonDirection) == LOW) {
    motor1Direction = (motor1Direction == CW) ? CCW : CW;
    Serial.println("لور لار بدله شو");
  }  
  if(digitalRead(motor1PushButtonStop) == LOW) {
    motor1StopState = 1 - motor1StopState; // پیل/بند toggle
  }  
}

دا فنکشن هغه وخت د موټور لور بدلوي کله چې د لور تڼۍ فشارول شي، او هغه وخت د بند حالت بدلوي کله چې د بند تڼۍ فشارول شي. د بشپړ کوډ لپاره، مهرباني وکړئ په یاد ولرئ چې دا د مقالې لاندې بارېږي.

ښودنه / څه تمه وکړو

کله چې تاسو پروګرام پرمخ وړئ، تاسو باید وکولای شئ چې د تڼیو او پوټینشیومیټر په کارولو سره د موټور لور او سرعت کنټرول کړئ. د لور تڼۍ فشارول به د ساعت په لور او د ساعت خلاف څرخېدلو ترمنځ بدلون راولي، پداسې حال کې چې د بند تڼۍ به موټور پیل یا بند کړي (په ویډیو کې په 12:30). د پوټینشیومیټر تنظیماتو څخه محتاط اوسئ؛ که ډیر ټیټ وټاکل شي، موټور ممکن پیل نشي.

فصلونه

  • پیژندنه – 0:00
  • د هارډویر کتنه – 1:30
  • د تارونو لارښوونې – 3:45
  • د کوډ کتنه – 6:00
  • ښودنه – 12:00

انځورونه

L298N Motor Controller Module
L298N Motor Controller MOdule
Arduino wiring for L298N with pot 3push button to control single motor
Arduino wiring for L298N with pot 3push button to control single motor
Arduino wiring for L298N with pot 3push button to control 2 motors
Arduino wiring for L298N with pot 3push button to control 2 motors
485-Lesson 55: Controlling a DC motor using push buttons
ژبه: C++
/*
 * Lesson 55: Controlling DC Motors with 2 push buttons with L298N
 * Library Example for L298N Module to control with 2 push buttons
 * One Push button is used for CW/CCW
 * and one push button is used to stop the motor
 Download and resource page https://robojax.com/RJT506
 Watch video for full details of this code: https://youtu.be/diS_WkUdU-8
 * 
 * Written by Ahmad Shamshiri on October 20, 2019 at 14:58
 * in Ajax, Ontario, Canada. www.robojax.com
 * 
 * 
 * 
 * Get this code and other Arduino codes from Robojax.com

If you found this tutorial helpful, please support me so I can continue creating
content like this. 

or make a donation using PayPal http://robojax.com/L/?id=64

 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
 * This code has been downloaded from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <Robojax_L298N_DC_motor.h>
// motor 1 settings
#define IN1 2
#define IN2 4
#define ENA 3 // ~this pin must be PWM enabled pin

const int CCW = 2; // do not change
const int CW  = 1; // do not change
#define motor1 1 // do not change
#define motor2 2 // do not change
const int pushButtonDelayTime =200;


//*** Push buttons for motor 1 started
int motor1Speed =60;// speed of motor 1 in % (60 means 60%)
const int motor1PushButtonDirection = 8;// push button for direction change
const int motor1PushButtonStop = 9;// push button for START/STOP
const int motor1Minimum =15;//30% is minimum for motor 1
// do not change below this line
int motor1Direction =CCW;//
int motor1StopState=HIGH;//Stope state of motor 1
//*** Push buttons for motor 1 started  ended
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



// use the line below for single motor
Robojax_L298N_DC_motor motor(IN1, IN2, ENA, true);

// use the line below for two motors
//Robojax_L298N_DC_motor motor(IN1, IN2, ENA, IN3, IN4, ENB, true);

void setup() {
  Serial.begin(115200);
  motor.begin();

  //L298N DC Motor by Robojax.com
  pinMode(motor1PushButtonDirection, INPUT_PULLUP);
  pinMode(motor1PushButtonStop, INPUT_PULLUP);

   
}


void loop() {
  updateState1();//read all push 1 buttons

  if(motor1StopState ==HIGH)
  {
    motor.brake(motor1);     
  }else{
    motor.rotate(motor1, motor1Speed, motor1Direction);    
  }
  //motor.demo(1);

  //motor.rotate(motor1, motor1Speed, CW);//run motor1 at 60% speed in CW direction
 delay(pushButtonDelayTime);
  
}//

/*
 * 
 * updateState1()
 * @brief reads push buttons and updates values
 * @param none
 * @return no return
 * Written by Ahmad Shamshiri for robojax.com
 * on October 13, 2019 in Ajax, Ontario, Canada
 */
void updateState1()
{

  if(digitalRead(motor1PushButtonDirection) ==LOW){
      if(motor1Direction ==CW)
      {
        motor1Direction =CCW;// 
        Serial.println("*****Now CCW"); 
      }else{
        motor1Direction =CW;//
        Serial.println("*****Now CW");  
      }

  }  

 
  if(digitalRead(motor1PushButtonStop) ==LOW)
  {
   motor1StopState =1-motor1StopState;// toggle Star/stop
  }  
}//updateState end
486-Lesson 55: Controlling 2 DC Motors with push buttons with L298N
ژبه: C++
/*
 * Lesson 55: Controlling 2 DC Motors with push buttons with L298N 
 * Library Example for L298N Module to control with 2 push buttons
 * One push button is used for CW/CCW  
 and one push button is used to stop the motor
 * (this code is to control 2 motors). See the first code for a single motor
  Download and resource page https://robojax.com/RJT506

 Watch video for full details of this code: https://youtu.be/diS_WkUdU-8
 * 

 * 
 * Written by Ahmad Shamshiri on October 20, 2019   at 14:58
 * in Ajax, Ontario, Canada. www.robojax.com
 * 
 * 
 * 
 * Get this code and other Arduino codes from Robojax.com


If you found this tutorial helpful, please support me so I can continue creating 
content like this. 

or make a donation using PayPal http://robojax.com/L/?id=64

 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been downloaded from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <Robojax_L298N_DC_motor.h>
// motor 1 settings
#define IN1 2
#define IN2 4
#define ENA 3 // this pin must be PWM enabled pin

// motor 2 settings
#define IN3 7
#define IN4 6
#define ENB 5 // this pin must be PWM enabled pin

const int CCW = 2; // do not change
const int CW  = 1; // do not change
#define motor1 1 // do not change
#define motor2 2 // do not change
const int pushButtonDelayTime =200;


//*** Push buttons for motor 1 started 
int motor1Speed =60;// speed of motor 1 in % (60 means 60%)
const int motor1PushButtonDirection = 8;// push button for direction change
const int motor1PushButtonStop = 9;// push button for START/STOP
const int motor1Minimum =15;//30% is minimum for motor 1
// do not change below this line
int motor1Direction =CCW;//
int motor1StopState=HIGH;//Stop state of motor 1
//*** Push buttons for motor 1 started  ended
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

//*** Push buttons for motor 2 started 
int motor2Speed =60;// speed of motor 2 in % (60 means 60%)
const int motor2PushButtonDirection = 10;// push button for direction change
const int motor2PushButtonStop = 11;// push button for START/STOP
const int motor2Minimum =15;//30% is minimum for motor 2
// do not change below this line
int motor2Direction =CCW;//
int motor2StopState=HIGH;//Stop state of motor 2
//*** Push buttons for motor 2 started  ended
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


// use the line below for 1 motor
//Robojax_L298N_DC_motor motor(IN1, IN2, ENA,true);

// use the line below for two motors
Robojax_L298N_DC_motor motor(IN1, IN2, ENA, IN3, IN4, ENB, true);

void setup() {
  Serial.begin(115200);
  motor.begin();

  //L298N DC Motor by Robojax.com
  // push buttons for motor 1
  pinMode(motor1PushButtonDirection, INPUT_PULLUP);
  pinMode(motor1PushButtonStop, INPUT_PULLUP);


  //L298N DC Motor by Robojax.com
  // push buttons for motor 2
  pinMode(motor2PushButtonDirection, INPUT_PULLUP);
  pinMode(motor2PushButtonStop, INPUT_PULLUP);
   
}


void loop() {
  updateState1();//read push buttons value for motor 1
  updateState2();//read push buttons value for motor 2
  
  if(motor1StopState ==HIGH)
  {
    motor.brake(1);     
  }else{
    motor.rotate(motor1, motor1Speed, motor1Direction);    
  }

  if(motor2StopState ==HIGH)
  {
    motor.brake(2);     
  }else{
    motor.rotate(motor2, motor2Speed, motor2Direction);    
  }  
  //motor.demo(1);

  //motor.rotate(motor1, motor1Speed, CW);//run motor1 at 60% speed in CW direction
 delay(pushButtonDelayTime);
  
}//

/*
 * 
 * updateState1()
 * @brief reads push buttons and updates values
 * @param none
 * @return no return
 * Written by Ahmad Shamshiri for robojax.com
 * on Oct 13, 2019 in Ajax, Ontario, Canada
 */
void updateState1()
{

  if(digitalRead(motor1PushButtonDirection) ==LOW){
      if(motor1Direction ==CW)
      {
        motor1Direction =CCW;// 
        Serial.println("***** Motor 1 Now CCW"); 
      }else{
        motor1Direction =CW;//
        Serial.println("***** Motor 1 Now CW");  
      }

  } //if for direction push button

 
  if(digitalRead(motor1PushButtonStop) ==LOW)
  {
   motor1StopState =1-motor1StopState;// toggle Start/stop
  }// if for start/stop push button 
  
}//updateState1 end

/*
 * 
 * updateState2()
 * @brief reads push buttons and updates values
 * @param none
 * @return no return
 * Written by Ahmad Shamshiri for robojax.com
 * on Oct 13, 2019 in Ajax, Ontario, Canada
 */
void updateState2()
{

  if(digitalRead(motor2PushButtonDirection) ==LOW){
      if(motor2Direction ==CW)
      {
        motor2Direction =CCW;// 
        Serial.println("***** Motor 2 Now CCW"); 
      }else{
        motor2Direction =CW;//
        Serial.println("***** Motor 2 Now CW");  
      }

  } //if for direction push button 

 
  if(digitalRead(motor2PushButtonStop) ==LOW)
  {
   motor2StopState =1-motor2StopState;// toggle Start/stop
  }// if for start/stop push button 
    
}//updateState2 end

هغه شیان چې تاسو ورته اړتیا لرئ

فایلونه📁

نور فایلونه