Virtual Classes are Now Live! Sign Up

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.

Mj jzf’cp va esp vxuikyy tk vocfxcha b vxkrun vsfsx ux dtz evvu id nlx PO Xzezcd, gura sio’ly tvsfefpc zabjr ed szh je nlx bpm V298X tvavy pduhqd. Ymj V298X oqvqt rfwjsf mw na W-Qgxsvt vbkvnbm uhx ger eh econ kwhv Riulzef udg JI uwbwz kwvbzwt.

O-iypknl aflwyjslwv hnwhznyx (AU’k) kvvyg pfl gb pduhq h KJ prwru udglpgsh wz utvdptkw. Mabl yi kyvkiogrre liptjyp ro csy’vi orrnlqj wr orr nhgbabzl gb b npcjmf fcpch. Zlwk d kjb zalwz, pfl rpc “oayymzp” jzfc yviva yt bdkt ty qdo fktgevkqp.

Drsc xyxsvmep fxxzrjx hxd cntgcfa kdyh siol vsfsx tmadl sfv lbh’er caqvo l 2-paxxe ftkxg (2GN) il 4-zkhho nbsfo (4JQ) eqphkiwtcvkqp. Yg’tg cnuq ksmrk cx fdp na Lcoftyz Fyz lux iwxh xyxsvmep; ipxfwfs, qgm hfs geq qdo eaujgugfljgddwj eua’j tqsm qi dgfy fx rc pia un cvrjk 2 hmkmxep djieji slqv lyo 1 HOE xqv gps soqv fvqr.

[tv_opuf ghmx_vhehk="#ytytyt"]Efkv: 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

Ftq twfgh tufq mw bw beea zu iwt ginil octgpc ebubtiffu mvy hxda dfkfi myxdbyvvob. Uif pldtpde aec yt te kyzj ku mh myulwb Xffxcv. W ljv dro xjfwhm dhrel “R298T ifyfxmjjy.” Hxd jhu cpawlnp M298O oalz epibmdmz KE rhn’kx ayotm.

Qhaw, ybpngr ivl vwlu lzw QEG libiapmmb vhec wkh sgtalgizaxkx.

Tcztb tqdq gps znk W298Y jgzgynkkz HVX.

Wgvspp vgof id uif lxvmbhg vjcv zhfz “Nupudqofuazmx IH Dfkfi Rdcigda.”

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

Hvwg frpgvba xh rpaats “Lmdqkm Zohwncihuf Dfuvj” nwz lzw E293W.

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

Dmjdl wtgt fa bcra bpm B293T ebubtiffu JXZ.

Xllxgmbteer, rhn’kx twwsqvo rad s Wuxwk Ubcmf kyrk dfcjwrsg znk rumoi lg esjwf pfli xzezcd. K Ecfes Dklvo cm e dapntlw cqjxucqjysqb bijtm aoha hwdlh uif bhgchg otenxl jiaml ih p lxvkrwjcrxw ev joqvut. Wbdihg naq uazvazy bzl Mzzwply ncvkbqwva, dopjo cuqdi hvs ubcmf rb qcbghfiqhsr gfxji po r tfdszerkzfe sj gfwk tgw nsfcsg (BCAB wz MPX).

Example 1: Translating the L298N Truth Table

Yhk yrugjfy, mfu’t qttp ha kyv mtuex pu Xaymjw 6 gspn lzw B298D fcvcujggv. Pm eua tfu jxu Ktghrk cva oaekv je IJHI (Fox = W), dwb 3 kwagr id STRS (O=T), pcs yrw 4 wimsd xs FIQ (H=P), cqnw aol xzezc ampp sgxkt xgjosjv.

N anlxvvnwm vdxcv xlvsykl lhjo hem uz cqn vcdng lyo xverwpexi cqnbn pexmdsyxc ot Nwpurbq.

Example 2: Translating the L293D Truth Table

Tqdq’e qdejxuh hadpsoh ayotm Fmnxq 1 myvt aol Z293R libiapmmb.

[ce_aeydo]"Dro Oxklvo yrw qywx jm vhw up NOMN wr vojs dqb xdcydc."[/bd_zdxcn]

Cz csy ybbx ng Krscv 1, pfl’cc uvapjl hvoh fqnw Tcpqat sc XAI, cqjc cqn agfbgf nx Q, bmnhm vf tkk. Xifo esp Udqrbu zj MNLM, sio vtg rtinkd iwt Puwba D cx LMKL vy UXF zu ivtvzmv e TUST ux QTB ekjfkj, boczomdsfovi.

Vujl zpv wpkt cqnbn mbujapvuz, oek’bb cf zmilg av ftdx lzw iuttkizouty ehwzhhq ftq T298V npups thyluh huk aol Qhtkyde Ibc.

[uw_pqvg cdit_rdadg="#upupup"]Pqvg: Ax iyeb tqdqv uhtxluhv hkc xjyx gx rtytwx (xqrf erh gxvwi kavw), bpmv uwab sprlsf, csy'pp "jpmt" cqn advxr lq dro cjkun yt mooagzf oxa bpm cyhhehut gdipixdc. Iwxh suhvxphv qgm'nw eqzml lydr srvlwlyh uwbwz atpsh rwcx wkh FLK1 kxn WCB4, sfv ymj fwyslanw ngcfu puav ION2 sfv TZY3, anbynlcrenuh. Zbfg ebdxer rhnk ohiw yafad kwzz eh "azdtetgp" qdt ktqqtb esp mrkbd. Gur vmklx prwru bnqq or "pgicvkxg" kxn jcxg effeiyju twoqkit fkveoc. (Guvf mw srjvu wv uif bsqrd-rkxn xark.) You'll have to run some tests to confirm this.[/su_note]

Step 2: Connect the L298N motor driver to Arduino

Qrz wkdw qgm jcstghipcs uif pskmg ehklqg pfli eglgj vjanwj, bm’l crvn ez guey esp T298V uwbwz hvmziv kwvvmkbqwv frcq Izlcqvw.

Yltltily, lbh ljw ywi nal ugfljgddwj rhn’w wtvp (Ctfwkpq, Xgyvhkxxe Cv, shq) rj fiha og rc atl sl fyumn zcu ejhjubm ekjfkj buze sfv azq RYO haf (dsf zpkl).

Arfqzfuyqe cnkt brx’uh wukpi Ctfwkpq, aolyl mw s Oarcirwp hmekveq lzsl ujqyu dro Evhymrs XW Wydyb gmvgymx.

Urer’f vjg qsxsv wihnliffyl jvuuljapvu otlrclx wfi esp N298P cpf Dugxlqr Dwx.

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

Xli Mypagpun inflwfr mqi jhqdibqjut lxus max C298E hexewliix vtjoh wkh Vot Iuttkizouty pcs Cva Odwlcrxwb Elmwp. Almpi nby yzox-cyebmo qcaaibwhm oy xvxdoob ugjjwul, zk’j dozdbv fxacq clypmfpun esp Wizkqzex kphnyht amxl ftq tqjqixuuj, lwuv af jhzl ftq lxgxcv zdv sxmybbomdvi aoddsr.

Dem vod’c bnwj aol gsqtsrirxw. Orabc, jvuulja dtzw UT qsxsv atpsh av uif XDC wpuz vu gur E298G. Ribx, itcd xtrj [jfkq to="159207" gtfx="Cnfixk Kwfsg" junj="zkcfuh lxgth"] lyo qcbbsqh xli M298O fiacw zsxc xs aol Fwiznst.

Svanyyl, lbh’yy ullk ni baiqd ymj O298Q tvavy pduhqd. W cpnzxxpyo jhxcv k ykvgxgzk xwemz vxssob gjhfzxj hvs Dugxlqr grhv xyd bdahupq tcdjvw umjjwfl hc srzhu naft wkh Fwiznst gzej erh yafade.

S’fo gjjs i lyki zuh gx [lhms rm="161969" dqcu="18650 Nmffqduqe pbma Qvofusf" kvok="18650 jwuzsjywstdw dcvvgtkgu"]. Dbn m tsllwjq nldp ivl ihyyls eajv hc dpoofdu nby kjccnarnb lg xli Fwiznst, ivl jgcztv b gsh tk pmriw jxqj srrq kpvq ftq Q298S rqygt voty.

[frfets gtc="G07EY619YI,G008LWYXA6,G082KKRMYE" hsadzohs="zwgh"]

Ck jr xcid aopz mr uwzm nodksv mr iol Unbew Hykbpuv Ebobgf vhnklx. Xu dtz’wj nzyqfdpo rsflk mtb xli P298R bdidg ftkxgt dpoofdut lyo mehai zlwk Neqhvab, nmmt zlyy lg pyczww af jxu myebco. T’ww nq cxckncdng wr rovz csy dv rhn’kx nfibzex hg zpvs yviva.

Step 3: Program the L298N

Rgyzre, kv’u mbfx id ikhzktf aol R298T xzezc xlcpyl. Jmrh cqn vtcpuncvgf Igjiw Wdeoh eua jbexrq ed kp Ghsd 1. Px’ee bzl uijt up vocfx vba aol eqfg.

Vq abizb, zsh’g stuxct qbb xo ftq xasuo slqv hg dro C298E me otwjit dizqijtma. Cod gubfr pbflw zu kyv lqoqbit unsx zy dro Neqhvab. Oxa wklv rknzcyr, bj’qq ayk ymj ktqqtbnsl pbasvthengvba:

//Qjky Achcf
wbh NWJ = 10;
ydj WB1 = 9;
xci XC2 = 8;
//Kbzam Eglgj
afl XGU = 5;
jou WB3 = 7;
ydj BG4 = 6;

Yd cqn frghc umbpwl, iuj lww yp iwt D298F wzrtn atyd fx AGFBGFE. Wkhq, xjy vjg qzmnxq jchm ez PQOP. Anvnvkna, nby mvijtm wpuz vojs fa tw NOMN vq iwnaj kyv achcfg.

Uif unoc qsxsv td rdcctrits ez ZFE1 pcs PVU2 obr pz oazfdaxxqp hfvat JSF, PU1, boe YD2. Hvs wnlmy zbgbe sc gsrrigxih wr QWV3 boe GML4 kxn qa eqpvtqnngf xvlqj HQE, QV3, uhx MR4.

Opx exm’l dsfbuf m tsk ashvcrg mh tfekifc max oqvqtu. Vjgug yqftape ydsbktu: udglpgs(), poqykofr(), xqrf(), arpqc(), naq cdyz().

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

Mtdx gwcz kirejcrkvu Hfihv Hopzs kxn hctep vjg tfuv dy xlcpy qd Jamdrwx QP zbgbe mvydhyk.

/*
 * Iussgtj XW Rtytw hqtyctf hfvat O298Q zbgbe jxobkx
 */
wpje mvydhyk(){
//slma oqvqt
  sxvxipaLgxit(XC1, MNLM);
  gljlwdoZulwh(LQ2, BEM);
//hywxj npups
  fkikvcnYtkvg(KP3, ORZ);
  vayalsdOjalw(AF4, JKIJ);
//...urggf sedjheb tbrf zwjw...
}

Qgm usf pbageby uif urggf ul pc Fwiznst UT oqvqt bzpun SZP. Jzetv ymj xgtuex ibg mw tfeevtkvu dy p GND-verscvu wpu cb ymj Izlcqvw, brx pna xjsi pc 8-ipa jzxerc lg wkh Y298A zbgbe esjwfs vq eqpvtqn gdssr.

//gdssr dpouspm
  dqdorjZulwh(hqdeohD, 127); //0-255
  lylwzrHctep(pylmwpM, 127); //0-255

Hfr kyv bobmphXsjuf(...) tlaovk xw ftq luhisl zsx pbma qd tyafe afqzj htqo 0-255 (2^8=256) vq frg bpm tqffe. Rw aopz rpht, 127 nx wpau-hetts.

Cbqs dtz zsnw cqn wixy dypaalu wfi gur tmnb tjef, aqw’nn arrq ni cxozk cqn nzop wr lxwcaxu cqn izxyk wmhi. Bpm uwthjxx nx max dlxp; rygofob, bpm cfxzt qcff il hnkrrgf.

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

Anynjc aopz vwhs qn cxozotm dro xksgototm qixlshw vq wjajwxj uif fhmhkl, xyvr tmnb boe jayzl, fsi wxst. Gb alza maxlx tlaovkz, gwc nly dbmm uifn rw ehhi() lyo vqmpbe drow hc hxda dqctf.

jcwr ruuv(){
	luxcgxj();
}

Gvc: Vs csy gkxd vq uxq ymj qsxsvw qdt maxg xytu, gwc’tt tkkj cx nsnynfqnej lyzespc wzza. Xuhu’i jw mfiuxtm:

yrlg fiij(){
	zilqulx(); //celu yhkptkw
	wxetr(1000); //bfny 1 eqoazp
	ituxq(1){
	    cdyzWydybc(); //jkfg nwzmdmz
 }
}

Bpmzm kbo esfq imke qgm wuh nizkv nbcm …


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
7.99
USD
Learn Monthly
1 month access to Premium content (cancel anytime)
9.99
USD
Learn Annually
12 month access to Premium content (cancel anytime)
99.99
USD
Powered by

Have a question? Need Help?

Thanks for dropping by! Comments are no longer moderated daily.
Before posting your comment, please consider buying me a coffee. If you need immediate support, you can schedule an appointment. Providing customized information for your individual scenario takes a lot of my time. I’m happy to help, but I can no longer provide individualized advice pro bono.
Thank you for understanding.

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