Darasi na 55-1: Sarrafa Motar DC Guda ɗaya ta Amfani da Maɓallan Turawa
A cikin wannan koyawa, za mu koyi yadda ake sarrafa motar DC guda ɗaya ta amfani da na'urar sarrafa motar L298N, maɓallan turawa, da potentiometer. Ta hanyar danna maɓallan, za mu iya sa motar ta juya agogo baya ko agogo, da kuma tsayar da ita da sarrafa saurinta ta amfani da potentiometer. Wannan aikin yana da kyau sosai don fahimtar sarrafa mota a cikin ayyukan Arduino kuma yana ba da ƙwarewa ta hannu tare da haɗin kayan aiki. Don ƙarin bayani, tabbatar da duba bidiyon da ke tare da wannan koyawa (a cikin bidiyo a 0:45).
Bayanin Kayan Aiki
Babban ɓangaren wannan aikin shine mai sarrafa motar L298N, wanda ke ba ka damar sarrafa alkibla da saurin motocin DC. Yana aiki ta amfani da tsarin H-bridge, wanda ke ba da damar motar ta juya a dukkan alkibla biyu. Na'urar tana buƙatar ƙarfin wutar lantarki tsakanin 6 zuwa 35 volts, wanda ke sa ta dace da nau'ikan motoci daban-daban. Bugu da ƙari, tana da ikon PWM (Pulse Width Modulation), wanda ke ba da damar sarrafa sauri. Za mu kuma yi amfani da maɓallan turawa don sarrafa alkiblar motar da tsayar da ita. Potentiometer zai taimaka wajen daidaita saurin motar. Waɗannan kayan aikin suna aiki tare don samar da hanya mai sauƙi amma ingantacciya don sarrafa ayyukan mota a cikin ayyukan Arduino.
Cikakkun Bayanai na Datasheet
| Mai Ƙirƙira | STMicroelectronics |
|---|---|
| Lambar ɓangare | L298N |
| Ƙarfin Logic/IO | 5 V |
| Ƙarfin wutar lantarki | 6–35 V (VS) |
| Ƙarfin fitarwa (kowane tashar) | 2 A max/tashar (abs. max), 1 A typ. ci gaba |
| Ƙarfin kololuwa (kowane tashar) | 3 A |
| Jagorar mitar PWM | 5 kHz |
| Ƙofofin shigar da logic | 0.8 V (ƙasa), 2.0 V (sama) |
| Rage ƙarfin lantarki / RDS(on) / saturation | 2 V (typ.) |
| Iyakokin zafi | 150 °C |
| Kunshin | Multiwatt15 |
| Bayanan kula / bambance-bambance | Yana iya sarrafa har zuwa motoci 2 |
- Tabbatar da ingantaccen zubar da zafi don ci gaba da aiki.
- Yi amfani da fil ɗin PWM don sarrafa sauri don guje wa zafi mai yawa.
- Haɗa ƙasan direban motar zuwa ƙasan Arduino.
- Yi hattara da ƙarfin wutar lantarki don hana lalacewa.
- Yi amfani da resistors na pull-up don maɓallan turawa don tabbatar da ingantaccen karatu.
- Cire wutar lantarki yayin shirye-shirye don guje wa matsalolin wutar baya.


Umarnin Haɗin Waya
Don haɗa mai sarrafa motar L298N da Arduino, fara da haɗa ƙarshen tabbataccen baturi zuwa tashar VCC akan na'urar L298N da ƙarshen mara kyau zuwa GND. Haɗa tashar GND na L298N zuwa GND na Arduino kuma. Ƙarfin fitarwa na 5V daga L298N za a iya haɗa shi zuwa shigar 5V na Arduino, amma kawai bayan shirye-shirye don guje wa rikice-rikice. Na gaba, haɗa motar zuwa tashoshin fitarwa na L298N. Don sarrafa motar, haɗa fil ɗin shigar kamar haka: haɗa IN1 zuwa fil 2 akan Arduino, IN2 zuwa fil 4, da fil ɗin kunna ENA zuwa fil 3. Don maɓallan turawa, haɗa gefe ɗaya na kowane maɓallin zuwa GND, da ɗayan gefen kamar haka: haɗa maɓallin alkibla zuwa fil 8, maɓallin tsayawa zuwa fil 9. A ƙarshe, haɗa potentiometer: gefe ɗaya zuwa GND, ɗayan zuwa 5V, da fil ɗin tsakiya zuwa A0 akan Arduino.
Misalan Lamba & Bayani
Ƙararren lamba mai zuwa yana fara sarrafa motar da saita fil ɗin:
#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);
}
A cikin wannan ɓangaren, mun haɗa ɗakin karatu da ake buƙata don sarrafa motar L298N kuma mun ayyana fil ɗin don motar da maɓallan turawa. Aikin `setup()` yana fara sadarwar serial kuma yana saita fil ɗin maɓallin azaman shigar tare da resistors na pull-up. Na gaba, muna duba yanayin maɓallan kuma mu sarrafa motar a cikin madauki:
void loop() {
updateState1();
if(motor1StopState == HIGH) {
motor.brake(motor1);
} else {
motor.rotate(motor1, motor1Speed, motor1Direction);
}
delay(pushButtonDelayTime);
}
A nan, aikin `loop()` yana ci gaba da duba yanayin maɓallan turawa. Idan an danna maɓallin tsayawa, motar za ta tsaya; in ba haka ba, za ta juya a ƙayyadaddun sauri da alkibla. Aikin `updateState1()` yana karanta yanayin maɓallan kuma yana sabunta alkiblar motar:
void updateState1() {
if(digitalRead(motor1PushButtonDirection) == LOW) {
motor1Direction = (motor1Direction == CW) ? CCW : CW;
Serial.println("An canza alkibla");
}
if(digitalRead(motor1PushButtonStop) == LOW) {
motor1StopState = 1 - motor1StopState; // kunna/kashe
}
}
Wannan aikin yana canza alkiblar motar idan an danna maɓallin alkibla kuma yana canza yanayin tsayawa idan an danna maɓallin tsayawa. Don cikakken lambar, don Allah a lura cewa tana ɗauka a ƙasa da labarin.
Nuni / Abin da za a Yi Tsammani
Lokacin da kuka kunna shirin, ya kamata ku iya sarrafa alkiblar motar da saurinta ta amfani da maɓallan turaw da potentiometer. Danna maɓallin alkibla zai canza tsakanin juyawa agogo baya da agogo, yayin da maɓallin tsayawa zai fara ko tsayar da motar (a cikin bidiyo a 12:30). Yi hattara da saitunan potentiometer; idan an saita ƙasa da yawa, motar ba za ta iya farawa ba.
Babuka
- Gabatarwa – 0:00
- Duba Kayan aiki – 1:30
- Umarnin Wayoyi – 3:45
- Binciken Lambar – 6:00
- Nuni – 12:00
/*
* 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
/*
* 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
Abin da zaka iya bukata
-
eBay
-
AliExpressPurchase L298N Module from AliExpresss.click.aliexpress.com
-
AliExpressPurchase L298N Module from AliExpress (2)s.click.aliexpress.com
-
AliExpressPurchase L298N Module from eBayebay.us
Albarkatun da kuma shafukan da za a duba
-
Waje
-
Waje
Fayiloli📁
Sauran fayiloli
-
Robojax L298N DC Motor Library
robojax_L298N-DC-Motor_library.zip0.10 MB