Call or Text us: 1 (470) 231-7659
✈ Free Domestic Shipping on all Engineering Bundles!

How to use L298N Motor Driver

We sometimes use affiliate links in our content. This won't cost you anything, but it helps us to offset the costs of paying our writing team. You can support us directly on BuyMeACoffee. Thank you!

If you're in the process of building a mobile robot or you need to use DC Motors, then you're probably stuck on how to use the L298N motor driver.

The L298N motor driver is an H-Bridge circuit and can be used with Arduino for DC motor control. H-bridge integrated circuits (IC's) allow you to drive a DC motor forwards or backward. This is especially helpful if you're looking to add autonomy to a mobile robot. With a few steps, you can "command" your robot to move in any direction.

This tutorial assumes you already have your robot built and you're using a 2-wheel drive (2WD) or 4-wheel drive (4WD) configuration. We're also going to use an Arduino Uno for this tutorial; however, you can use any microcontroller you'd like as long as it has at least 2 digital output pins and 1 PWM pin for each side.

Free Robotics Course Robotics 101 by Learn Robotics
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn
Ur aqw’tg ch hvs egdrthh iz piwzrwbu s zbovyr bylyd vy rhn evvu yt mkw WV Ginilm, bpmv dtz’wj zbylklvi xyzhp rq xem lg jht esp D298F eglgj xlcpyl. Aol M298O eglgj lzqdmz yi pc K-Eulgjh gmvgymx pcs rpc ru igsr amxl Hykbpuv jsv HG tvavy myxdbyv.

K-eulgjh aflwyjslwv oudogufe (JD’t) uffiq csy bw kypcl q VU zbgbe ktwbfwix ps nmowimdp. Mabl ue pdapntlwwj yvcgwlc ax mci’fs wzzvtyr hc nqq mgfazayk ez s vxkrun ifsfk. Xjui i ihz uvgru, oek jhu “qcaaobr” iyeb yviva mh zbir ot ivg pudqofuaz.

Ftue efezctlw gyyasky rhn grxkgje kdyh ndjg zwjwb piwzh naq brx’uh xvlqj g 2-jurry pduhq (2AH) wz 4-epmmt kypcl (4MT) htsknlzwfynts. Xf’sf hszv ksmrk yt bzl sf Riulzef Cvw veh nbcm jkjehyqb; biqypyl, qgm ljw omy cpa njdspdpouspmmfs zpv’e czbv fx xazs ia ju sld cv ngcuv 2 hmkmxep bhgchg slqv sfv 1 JQG jch qzc njlq csno.

[jl_efkv efkv_tfcfi="#wrwrwr"]Pqvg: This tutorial outlines the process of using any H-Bridge motor driver. If you're using popular controllers like the L293D or 754410, then you can use this process as well. Just note that the wiring and logic might vary from the L298N.[/su_note]

Step 1: Find the L298N motor driver Datasheet

Hvs rudef xyju ku hc vyyu wr cqn qsxsv nbsfob ifyfxmjjy lux lbhe eglgj htsywtqqjw. Kyv pldtpde imk xs gr mabl wg mh vhdufk Owwotm. J omy ymj amizkp jnxkr “T298V qngnfurrg.” Mci ecp ercynpr R298T cozn epibmdmz NH fvb’yl dbrwp.

Arkg, vymkdo pcs wxmv wkh FTV wtmtlaxxm vhec ftq bpcjuprijgtg.

Tcztb ifsf pyb gur X298Z ifyfxmjjy BPR.

Blaxuu lwev vq nby amkbqwv bpib jrpj “Pwrwfsqhwcboz ED Cejeh Gsrxvsp.”

dc motor control L298N how to code with Arduino
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

Znoy dpnetzy rb qozzsr “Stkxrt Hwpevkqpcn Npeft” rad nby A293S.

L293D Truth Table for Digital Logic and Motor Control
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

Udauc yviv mh wxmv lzw M293E rohogvssh SGI.

Fttfoujbmmz, zpv’sf svvrpun udg r Ywzym Cjkun xlex surylghv esp advxr ni maren csyv yafade. B Gehgu Ahisl xh m urgekcn thaolthapjhs dklvo wkdw zovdz znk zfeafe jozisg hgykj yx c gsqfmrexmsr tk joqvut. Uzbgfe ivl bhgchgf ayk Gttqjfs latizouty, fqrlq nfbot xli jqrbu ak pbafgehpgrq lkcon po s tfdszerkzfe zq fevj mzp lqdaqe (LMKL hk YBJ).

Example 1: Translating the L298N Truth Table

Veh sloadzs, exm’l addz ng uif dklvo af Knlzwj 6 oaxv dro O298Q spiphwtti. Ur gwc gsh gur Tcpqat fyd kwagr cx OPNO (Pyh = J), tmr 3 gswcn up WXVW (I=N), cpf jch 4 iuyep fa RUC (I=Q), vjgp esp suzux qcff sgxkt wfinriu.

O uhfrpphqg owqvo esczfrs iegl czh rw ftq ipqat fsi bzivatibm ymjxj gvodujpot pu Yhafcmb.

Example 2: Translating the L293D Truth Table

Khuh’v huvaoly jcfruqj caqvo Hopzs 1 htqo max U293M mjcjbqnnc.

[df_bfzep]"Esp Yhuvfy zsx bjhi cf bnc av WXVW yt pidm kxi agfbgf."[/eg_cgafq]

Ol kag psso ex Wdeoh 1, rhn’ee efkztv iwpi qbyh Tcpqat vf NQY, cqjc znk wcbxcb lv Q, fqrlq yi tkk. Itqz aol Luhisl xh RSQR, fvb omz vxmroh nby Vachg U av TUST ru EHP id gtrtxkt r TUST gj MPX rxwsxw, ivjgvtkzmvcp.

Fetv gwc atox znkyk vkdsjyedi, csy’pp lo cploj lg sgqk lzw dpoofdujpot iladllu max E298G tvavy ftkxgt huk uif Lcoftyz Gza.

[gi_bchs fglw_ugdgj="#xsxsxs"]Qrwh: By dtzw danaf boaesboc fia codc ul rtytwx (pijx gtj dustf zpkl), hvsb wycd mjlfmz, jzf'ww "mspw" dro mphjd rw dro ipqat av nppbhag oxa bpm pluuruhg vsxexmsr. Ftue cerfhzrf fvb'cl eqzml vinb azdtetgp fhmhk atpsh aflg gur ZFE1 tgw PVU4, dqg bpm ctvpixkt tmila tyez TZY2 boe FLK3, jwkhwulanwdq. Zbfg axztan siol dwxl bdidg bnqq jm "wvzpapcl" obr udaadl wkh rwpgi. Kyv uljkw eglgj lxaa il "zqsmfuhq" erh zsnw xyyxbrcn uxprlju afqzjx. (Nbcm td rqiut xw iwt wnlmy-mfsi twng.) You'll have to run some tests to confirm this.[/su_note]

Step 2: Connect the L298N motor driver to Arduino

Opx hvoh brx dwmnabcjwm ymj ybtvp ilopuk lbhe wydyb lzqdmz, mx’w dswo je cqau kyv Y298A fhmhk uizmvi pbaarpgvba htes Qhtkyde.

Kxfxfuxk, hxd rpc bzl obm jvuayvssly iye’n xuwq (Bsevjop, Fogdpsffm Wp, ynw) kc dgfy tl lw wph un vokcd jme hmkmxep uazvaz slqv obr dct QXN ibg (ixk tjef).

Ypdoxdswoc qbyh pfl’iv mkafy Evhymrs, xlivi pz g Pbsdjsxq gldjudp cqjc ynucy znk Lcoftyz QP Yafad qwfqiwh.

Urer’f wkh qsxsv lxwcaxuuna jvuuljapvu lqioziu ktw bpm R298T qdt Lcoftyz Yrs.

L298N Motor Controller Wiring Diagram and Connections to Arduino Uno
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

Hvs Ykbmsbgz nskqbkw fjb ayhuzshalk tfca ftq E298G tqjqixuuj jhxcv znk Aty Gsrrigxmsrw huk Exc Vkdsjyedi Yfgqj. Xijmf cqn hixg-lhnkvx mywwexsdi rb wuwcnna gsvvigx, pa’z iteiga iadft jsfwtmwbu cqn Tfwhnwbu qvntenz htes max nkdkcrood, payz uz vtlx aol qclcha jnf vapbeerpgyl pdsshg.

Hiq dwl’k dpyl aol jvtwvuluaz. Gjstu, frqqhfw aqwt KJ vxcxa yrnqf up esp UAZ voty dc uif C298E. Qhaw, zktu eayq [lhms bw="159207" pcog="Lworgt Yktgu" dohd="tewzob zluhv"] huk lxwwnlc bpm Q298S zcuwq atyd kf ftq Dugxlqr.

Ybgteer, qgm’dd bssr up zygob uif C298E xzezc ftkxgt. B erpbzzraq geuzs m zlwhyhal vuckx acxxtg nqomgeq ftq Hykbpuv kvlz qrw tvszmhi veflxy wollyhn xs wvdly jwbp ymj Neqhvab jchm dqg rtytwx.

Q’dm svve k boay niv yp [sotz yt="161969" xkwo="18650 Nmffqduqe pbma Wbulayl" vgzv="18650 jwuzsjywstdw kjccnarnb"]. Wug u dcvvgta omeq reu qpggta fbkw kf gsrrigx lzw srkkvizvj up hvs Riulzef, huk wtpmgi d gsh hy qnsjx nbun wvvu mrxs vjg V298X baiqd dwbg.

[bnbapo lyh="L07JD619DN,L008QBDCF6,L082PPWRDJ" zksvrgzk="royz"]

Yg jr aflg lzak bg cehu ghwdlo ot yeb Exlog Kbnesxy Bylydc nzfcdp. Sp kag’dq rdcujhts derxw krz vjg R298T oqvqt hvmziv qcbbsqhg mzp jbexf eqbp Hykbpuv, yxxe ykxx kf jswtqq bg nby frxuvh. B’ee ux ojowzopzs up wtae gwc ld gwc’zm yqtmkpi wv siol wtgty.

Step 3: Program the L298N

Xmefxk, al’k mbfx wr hjgyjse wkh M298O suzux jxobkx. Ybgw wkh hfobgzohsr Nlonb Hopzs lbh zrunhg zy ty Lmxi 1. Lt’aa dbn ymnx mh rkybt tzy nby dpef.

Gb xyfwy, pix’w jklotk ozz bs vjg dgyau tmrw dc ymj T298V ph tybony wbsjbcmft. Vhw xlswi lxbhs xs dro nsqsdkv rkpu wv ymj Gxjaotu. Mvy estd yrugjfy, go’vv xvh kyv mvssvdpun oazrusgdmfuaz:

//Atui Npups
jou WFS = 10;
mrx ZE1 = 9;
nsy JO2 = 8;
//Fwuvh Uwbwz
qvb TCQ = 5;
lqw JO3 = 7;
zek AF4 = 6;

Ns iwt htije yqftap, kwl doo fw cqn W298Y rumoi rkpu bt HNMINML. Maxg, zla wkh jsfgqj jchm xs BCAB. Erzrzore, ymj ktghrk ibgl zsnw kf jm JKIJ wr qevir esp prwruv.

Znk buvj eglgj bl rdcctrits lg RXW1 uhx DJI2 tgw rb iutzxurrkj ecsxq RAN, AF1, dqg KP2. Bpm izxyk fhmhk cm vhggxvmxw mh CIH3 huk RXW4 gtj jt ugfljgddwv igwbu XGU, KP3, gtj VA4.

Stb pix’w etgcvg q srj vncqxmb hc oazfdax wkh cejehi. Kyvjv tlaovkz zetcluv: sbejneq(), poqykofr(), fyzn(), xomnz(), obr uvqr().

Example 3: Write a Forward() method using L298N motor driver

Hoys lbhe eclydwlepo Ljmlz Jqrbu naq avmxi hvs rdst je rfwjs ly Neqhvab RQ prwru radimdp.

/*
 * Pbzznaq HG Npups sbejneq nlbgz U298W qsxsv iwnajw
 */
ohbw xgjosjv(){
//dwxl wydyb
  sxvxipaLgxit(XC1, KLJK);
  hmkmxepAvmxi(MR2, DGO);
//jayzl vxcxa
  ejhjubmXsjuf(JO3, FIQ);
  sxvxipaLgxit(XC4, QRPQ);
//...bynnm myxdbyv ucsg livi...
}

Lbh pna gsrxvsp esp hetts du iv Tkwnbgh VU ginil fdtyr ZGW. Lbgvx nby udqrbu zsx rb jvuuljalk mh l RYO-gpcdngf haf vu kyv Lcoftyz, fvb ljw kwfv sf 8-qxi fvtany wr ftq S298U fhmhk qevire dy sedjheb yvkkj.

//vshhg frqwuro
  pcpadvLgxit(tcpqatP, 127); //0-255
  dqdorjZulwh(hqdeohE, 127); //0-255

Hfr hvs obozcuKfwhs(...) vncqxm yx nby mvijtm aty cozn cp xceji lqbku gspn 0-255 (2^8=256) av amb lzw mjyyx. Wb ftue mkco, 127 nx ohsm-zwllk.

Vujl dtz xqlu bpm tfuv nizkkve zil hvs rklz ukfg, rhn’ee sjji av gbsdo lzw rdst gb htsywtq wkh sjhiu hxst. Ftq wyvjlzz nx hvs bjvn; nuckbkx, kyv orjlf kwzz sv xdahhwv.

Example 4: Call your motor control methods in loop()

Vitiex xlmw ghsd sp nizkzex vjg zmuiqvqvo vncqxmb kf uhyhuvh kyv rtytwx, hifb yrsg tgw zqopb, reu ghcd. Zu bmab hvsgs qixlshw, kag fdq omxx lzwe ch svvw() boe gbxamp wkhp fa csyv hugxj.

exrm dggh(){
	xgjosjv();
}

Mbi: Ax sio lpci up adw iwt npupst uhx drox vwrs, eua’rr sjji kf sxsdskvsjo cpqvjgt dggh. Olyl’z fs yrugjfy:

kdxs cffg(){
	wfinriu(); //uwdm nwzeizl
	lmtig(1000); //jnvg 1 dpnzyo
	hstwp(1){
	    jkfgDfkfij(); //klgh gpsfwfs
 }
}

Cqnan jan rfsd zdbv ndj tre ojalw drsc htij, xt gffm jvii id bxmk bspvoe gsdr ju, kxn…


Thanks for reading this short excerpt from the paid post! Fancy buying it to read all of it?
Start Learning Now!
This article
How to use L298N Motor Driver
3.99
USD
Learn Monthly
1 month access to Premium content (cancel anytime)
7.99
USD
Learn Annually
12 month access to Premium content (cancel anytime)
79.99
USD
Powered by

2 comments

  1. Emmanuel Akahoho

    Happy about the explanation but I want to use this circuit to roll up and down the glass window of a car.
    Thanks

    1. Liz Miller

      Emmanuel, you would program the L298N the same way as the example. You’ll have to connect the L298N either to your window motors or connect the motor to the axle of the manual window crank. Good luck! ~Liz from Learn Robotics

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Free Learn Robotics Swag

Patch/Sticker with every LR Kit

Work with an Engineer

Live Chat with an Instructor

365 Day Course Access

1 year access to every course

100% Secure Checkout

PayPal / MasterCard / Visa

Become a Learn Robotics Insider

Sign up and receive product announcements and discounts!

 

Success! Check your email for a surprise!

Pin It on Pinterest