Luka Code

Lisó 55-1: Kokamba Moteur DC Moko Na Nzela Ya Mabɔtɔ Ya Kopɛsa

Lisó 55-1: Kokamba Moteur DC Moko Na Nzela Ya Mabɔtɔ Ya Kopɛsa

Na boyekoli oyo, tokoyekola ndenge ya kokamba moteur ya DC moko na L298N module ya kokamba moteur, ba push buttons, mpe potentiomètre. Na kobeta ba boutons, tokoki kosala ete moteur ezala na mouvement ya sens ya ngonga to ya sens ya contre-ngonga, mpe kopesa yango stop mpe kokamba vitesse na yango na potentiomètre. Projet oyo ezali malamu mingi mpo na kososola kokamba ya moteur na ba projets ya Arduino mpe epesa expérience pratique na interfacing ya matériel. Mpo na ndimbola ya koleka, sɔ́kɔ́ tala vidéo oyo ezali elongo na tutoriel oyo (na vidéo na 0:45).

L298N Motor Controller Module
Module ya Kokamba Moteur L298N

Explication ya Matériel

Élément principal na projet oyo ezali L298N controller ya moteur, oyo epesaka yo possibilité ya kokamba direction mpe vitesse ya ba moteurs ya DC. Esalaka na configuration ya H-bridge, oyo epesaka moteur possibilité ya kozala na mouvement na ba directions mibale. Module oyo esengaka tension ya alimentation kati na 6 na 35 volts, oyo esalaka ete ezala versatile mpo na ba types ya moteurs. Na libanda ya yango, ezali na ba capacités ya PWM (Pulse Width Modulation), oyo epesaka possibilité ya kokamba vitesse. Tokosalela mpe ba push buttons mpo na kokamba direction ya moteur mpe kopesa yango stop. Potentiomètre ekosalisa na koyokisa vitesse ya moteur. Ba éléments oyo basalaka elongo mpo na kopesa ndenge ya pete mpe ya efficace ya kokamba ba opérations ya moteur na ba projets na yo ya Arduino.

Détails ya Datasheet

Fabricant STMicroelectronics
Numéro ya partie L298N
Tension ya Logic/IO 5 V
Tension ya alimentation 6–35 V (VS)
Courant ya sortie (na canal) 2 A max/canal (abs. max), 1 A typ. continu
Courant ya pic (na canal) 3 A
Guidance ya fréquence PWM 5 kHz
Seuils ya logique ya entrée 0.8 V (bas), 2.0 V (haut)
Chute ya tension / RDS(on) / saturation 2 V (typ.)
Limites thermiques 150 °C
Package Multiwatt15
Notes / variantes Ekoki kokamba ba moteurs 2 na motuya
  • Assurer que le refroidissement ezali malamu mpo na fonctionnement continu.
  • Salela ba pins ya PWM mpo na kokamba vitesse mpo na kokima échauffement.
  • Connecter le ground ya driver ya moteur na ground ya Arduino.
  • Keba na tension ya alimentation mpo na kokima dégâts.
  • Salela ba résistances ya pull-up mpo na ba push buttons mpo na kozwa ba lectures stables.
  • Déconnecter l'alimentation ya courant ntango ozali ko-programmer mpo na kokima ba problèmes ya back-powering.
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

Instructions ya Câblage

Arduino wiring for L298N with pot 3push button to control single motor
Câblage ya Arduino mpo na L298N na pot 3push button mpo na kokamba moteur moko

Mpo na câbler L298N controller ya moteur na Arduino, banda na kokangisa borne positive ya batterie na borne VCC na module ya L298N mpe borne négative na GND. Kokangisa mpe borne GND ya L298N na GND ya Arduino. Sortie ya 5V na L298N ekoki kokangama na entrée ya 5V ya Arduino, kasi kaka nsima ya programmation mpo na kokima ba conflits. Na sima, kokangisa moteur na ba bornes ya sortie ya L298N. Mpo na kokamba moteur, câbler ba pins ya entrée boye: kokangisa IN1 na pin 2 na Arduino, IN2 na pin 4, mpe pin ya enable ENA na pin 3. Mpo na ba push buttons, câbler côté moko ya bouton na GND, mpe côté mosusu boye: kokangisa bouton ya direction na pin 8, bouton ya stop na pin 9. Na suka, kokangisa potentiomètre: côté moko na GND, mosusu na 5V, mpe pin ya kati na A0 na Arduino.

Arduino wiring for L298N with pot 3push button to control 2 motors
Câblage ya Arduino mpo na L298N na pot 3push button mpo na kokamba ba moteurs 2

Ba Exemples ya Code & Explication

Code oyo ezali na nsé ebandisaka kokamba ya moteur mpe esaleli ba pins:


#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);
}

Na extrait oyo, tozali na bibliothèque ya nécessité mpo na kokamba ya moteur L298N mpe tozali définir ba pins mpo na moteur mpe ba push buttons. Fonction `setup()` ebandisaka communication série mpe esaleli ba pins ya bouton lokola entrée na ba résistances ya pull-up. Na sima, tozali kotala état ya ba boutons mpe kokamba moteur na boucle:


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

Awa, fonction `loop()` ezali kotala mbala na mbala état ya ba push buttons. Soki bouton ya stop ebɛtami, moteur ekobrake; sinon, ekozala na mouvement na vitesse mpe direction oyo esengeli. Fonction `updateState1()` ezali kotanga ba états ya ba boutons mpe kozongisa direction ya moteur:


void updateState1() {
  soki digitalRead(motor1PushButtonDirection) ezali LOW {
    motor1Direction = (motor1Direction == CW) ? CCW : CW;
    Serial.println("Direction ebongolami");
  }  
  soki digitalRead(motor1PushButtonStop) ezali LOW {
    motor1StopState = 1 - motor1StopState; // kobongola Start/Stop
  }  
}

Fonction oyo ebongolaka direction ya moteur ntango bouton ya direction epresami mpe ebongolaka état ya stop ntango bouton ya stop epresami. Mpo na code mobimba, notezwa ete ekomami na nse ya article.

Démonstration / Nini Okomona

Ntango okobanda programme, okokoka kokamba direction mpe vitesse ya moteur na boutons mpe potentiomètre. Kopressa bouton ya direction ekobongola kati na rotation ya sens horaire mpe anti-horaire, kasi bouton ya stop ekofungola to ekokanga moteur (na vidéo na 12:30). Kozala na bokebi na paramètres ya potentiomètre; soki ekomi trop moke, moteur ekoki kobanda te.

Bikabu

  • Libandeli – 0:00
  • Biloko ya Hardware – 1:30
  • Malako ya Wiring – 3:45
  • Explication ya Code – 6:00
  • Démonstration – 12:00

Images

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
Langue: 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
Langue: 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

Nko o na nki.

Fichiers📁

Mabala a mangwe