Pretraži kod

Lekcija 55-1: Upravljanje jednim DC motorom pomoću tipkala

Lekcija 55-1: Upravljanje jednim DC motorom pomoću tipkala

U ovom tutorijalu naučit ćemo kako upravljati jednim DC motorom koristeći L298N modul za upravljanje motorom, tipkala i potenciometar. Pritiskanjem tipkala možemo natjerati motor da se rotira u smjeru kazaljke na satu ili suprotno od kazaljke na satu, kao i zaustaviti ga i kontrolirati njegovu brzinu pomoću potenciometra. Ovaj projekat je odličan za razumijevanje upravljanja motorom u Arduino projektima i pruža praktično iskustvo s povezivanjem hardvera. Za dodatna pojašnjenja, svakako pogledajte video koji prati ovaj tutorijal (u videu na 0:45).

L298N Motor Controller Module
L298N modul za upravljanje motorom

Objašnjenje hardvera

Primarna komponenta u ovom projektu je L298N kontroler motora, koji vam omogućava da kontrolirate smjer i brzinu DC motora. Radi koristeći H-most konfiguraciju, omogućavajući motoru da se rotira u oba smjera. Modul zahtijeva napon napajanja između 6 i 35 volti, što ga čini svestranim za različite tipove motora. Osim toga, ima PWM (Pulse Width Modulation) mogućnosti, omogućavajući kontrolu brzine. Također ćemo koristiti tipkala za kontrolu smjera motora i njegovo zaustavljanje. Potenciometar će pomoći u podešavanju brzine motora. Ove komponente rade zajedno kako bi pružile jednostavan, ali efikasan način upravljanja radom motora u vašim Arduino projektima.

Detalji iz tehničke dokumentacije

Proizvođač STMicroelectronics
Broj dijela L298N
Logički/IO napon 5 V
Napon napajanja 6–35 V (VS)
Izlazna struja (po kanalu) 2 A max/kanalu (aps. max), 1 A tip. kontinuirano
Vršna struja (po kanalu) 3 A
Smjernice za PWM frekvenciju 5 kHz
Ulazni logički pragovi 0,8 V (nisko), 2,0 V (visoko)
Pad napona / RDS(on) / zasićenje 2 V (tip.)
Termička ograničenja 150 °C
Paket Multiwatt15
Napomene / varijante Može upravljati do 2 motora
  • Osigurajte odgovarajuće hlađenje za kontinuirani rad.
  • Koristite PWM pinove za kontrolu brzine kako biste izbjegli pregrijavanje.
  • Spojite uzemljenje drajvera motora na uzemljenje Arduina.
  • Budite oprezni s naponom napajanja kako biste spriječili oštećenja.
  • Koristite pull-up otpornike za tipkala kako biste osigurali stabilna očitavanja.
  • Isključite napajanje dok programirate kako biste izbjegli probleme s povratnim napajanjem.
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

Upute za povezivanje

Arduino povezivanje za L298N s pot i 3 tipkala za upravljanje jednim motorom
Arduino povezivanje za L298N s pot i 3 tipkala za upravljanje jednim motorom

Za povezivanje L298N kontrolera motora s Arduinom, počnite spajanjem pozitivnog terminala baterije na VCC terminal na L298N modulu i negativnog terminala na GND. Spojite GND terminal L298N-a na Arduino GND također. 5V izlaz s L298N-a može se spojiti na Arduino 5V ulaz, ali samo nakon programiranja kako biste izbjegli konflikte. Zatim spojite motor na izlazne terminale L298N-a. Za kontrolu motora, povežite ulazne pinove na sljedeći način: spojite IN1 na pin 2 na Arduinu, IN2 na pin 4, a enable pin ENA na pin 3. Za tipkala, spojite jednu stranu svakog tipkala na GND, a drugu stranu na sljedeći način: spojite tipkalo za smjer na pin 8, tipkalo za zaustavljanje na pin 9. Konačno, spojite potenciometar: jednu stranu na GND, drugu na 5V, a srednji pin na A0 na Arduinu.

Arduino povezivanje za L298N s pot i 3 tipkala za upravljanje 2 motora
Arduino povezivanje za L298N s pot i 3 tipkala za upravljanje 2 motora

Primjeri koda i objašnjenje

Sljedeći isječak koda inicijalizira kontrolu motora i postavlja pinove:


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

U ovom izvatku, uključujemo potrebnu biblioteku za L298N kontrolu motora i definiramo pinove za motor i tipkala. Funkcija `setup()` inicijalizira serijsku komunikaciju i postavlja pinove tipkala kao ulaz s pull-up otpornicima. Zatim provjeravamo stanje tipkala i kontroliramo motor u petlji:


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

Ovdje, funkcija `loop()` kontinuirano provjerava stanje tipkala. Ako je tipkalo za zaustavljanje pritisnuto, motor će kočiti; u suprotnom, rotirat će se specificiranom brzinom i smjerom. Funkcija `updateState1()` čita stanja tipkala i ažurira smjer motora:


void updateState1() {
  if(digitalRead(motor1PushButtonDirection) == LOW) {
    motor1Direction = (motor1Direction == CW) ? CCW : CW;
    Serial.println("Smjer promijenjen");
  }  
  if(digitalRead(motor1PushButtonStop) == LOW) {
    motor1StopState = 1 - motor1StopState; // prebaci Start/Stop
  }  
}

Ova funkcija mijenja smjer motora kada se pritisne tipka za smjer i prebacuje stanje zaustavljanja kada se pritisne tipka za stop. Za puni kod, imajte na umu da se učitava ispod članka.

Demonstracija / Šta očekivati

Kada pokrenete program, trebali biste moći kontrolisati smjer i brzinu motora pomoću tipki i potenciometra. Pritiskanje tipke za smjer će prebacivati između rotacije u smjeru kazaljke na satu i suprotnom smjeru, dok će tipka za stop pokrenuti ili zaustaviti motor (u videu na 12:30). Budite oprezni s postavkama potenciometra; ako je postavljen prenisko, motor se možda neće pokrenuti.

Poglavlja

  • Uvod – 0:00
  • Pregled hardvera – 1:30
  • Upute za povezivanje – 3:45
  • Pregled koda – 6:00
  • Demonstracija – 12:00

Slike

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

Stvari koje bi vam mogle zatrebati

Datoteke📁

Ostale datoteke