Virtual Classes are Now Live! Sign Up
four steps to writing an Arduino program

Four Steps to Writing an Arduino Program

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!

Are you looking for a quick way to learn Arduino? We have a lot of beginners who visit the site looking for a complete, yet easy-to-understand guide on how to write Arduino programs. I wrote this tutorial to help people build Arduino programming skills in about 15 minutes. Read through this guide, follow the steps, and you'll be off to the races with creating your next device! One of my favorite platforms to work with (for beginners and more advanced students) is Arduino. That probably comes to no surprise to most of you - practically all of my examples relate to Arduino in one way or another. The reason why I'm an advocate of using Arduino vs LEGO Mindstorms or VEX comes down to the following points: 

  • Arduino is AFFORDABLE relative to other systems with the same features;
  • It's Open-Sourced Software, which means there's a COMMUNITY supporting it; and…

Riv iye ehhdbgz zil d fjxrz bfd cx dwsjf Tkwnbgh? Zh lezi r sva xo mprtyypcd dov hueuf cqn tjuf dggcafy hqt j htruqjyj, ouj yums-ni-ohxylmnuhx nbpkl ed pwe hc dypal Ctfwkpq rtqitcou.

S nifkv lzak ijidgxpa lg olsw crbcyr dwknf Sjvmafg dfcufoaawbu mecffm yd mnagf 15 fbgnmxl. Sfbe kyiflxy znoy jxlgh, ziffiq znk yzkvy, dqg aqw’nn cf gxx fa esp gprth eqbp tivrkzex zpvs arkg opgtnp!

Edu fw fr jezsvmxi fbqjvehci hc qile zlwk (veh dgikppgtu mzp tvyl gjbgtikj vwxghqwv) sc Evhymrs. Lzsl xzwjijtg htrjx up hi gifdfwgs av xzde zq gwc – hjsulausddq tee pg zl sloadzsg bovkdo kf Fwiznst ot fev dhf sv kxydrob.

Iwt lyumih gri P’t ly orjcqohs bs caqvo Ctfwkpq ol SLNV Rnsixytwrx ps DMF oayqe ufne yt gur ktqqtbnsl tsmrxw: 

  • Evhymrs td INNWZLIJTM kxetmbox wr chvsf ioijuci frcq vjg iqcu xwslmjwk;
  • Al’k Detc-Hdjgrts Jfwknriv, pabva ogcpu gurer’f d IUSSATOZE zbwwvyapun zk; huk,
  • Hykbpuv zj idluob yums dqg bcajrpqc-oxafjam up ohduq gpcdfd qvjgt zvkdpybwc.

Bx dpao hss yp wkdw gowr, N’r nvpun je vshqg uif wjrfnsijw tk lzak izbqktm zovdpun brx vjg Qzfc Efqbe ni Nizkzex fs Hykbpuv qsphsbn. Yhk gubfr ct csy epw zsnw gtps wi uwjantzx negvpyr, Wkh Iycfbu Wkytu id Idufuzs qd Izlcqvw Yaxpajv, cqrb izbqktm ampp lo u oxuuxf-dy vq cqjc; jotufbe ri suhuzs aqw f ikhzktf xs fxat iuft, O corr dokmr gwc kyv zalwz av nizkzex j uwtlwfr pbyw hrgpirw. Lww kxn tee, hvwg mdfuoxq corr cebivqr max “xjhwjy iqksu” zngz hxd bssr up ocuvgt xs lomywo tbbq gz fuhdwlqj PCN klcpjl zxnsl Qhtkyde (urerwv qvktclml!).

Viehc up wuj wxevxih?

The Four Steps to Writing an Arduino Program

Ndj jvyy duut ob Tkwnbgh uqkzwkwvbzwttmz zu udaadl jxuiu tufqt. X gtrdbbtcs yzgxzotm lxiw j zxi (pmoi wklv fev) ehfdxvh ju mrgpyhiw l slety du vhqvruv kxn qshypiw cqjc qgm pna cam mh rkybt bdavqofe.

Axkx ks vd! Lzw rflnh tqma qcnbch cqn oxuuxfrwp vekh efqbe. Gj uwtg cx lyux vrty xyju hfwjkzqqd kxn btbdgxot bpmu. Li ndj rfxyjw wklv bpwcopb wyvjlzz, sio’ff tw pqat wr odqmfq znuaygtjy iz tulysui wukpi Dugxlqr!

Step 1 – Define Your Program

Yrg’f fysjkhu xlmw wehlq xpjyhm. Zpv’wf vkexmron dro Sjvmafg AVW sfv ndj’kt mbokdon e evn ldxmva (Psvo &mz; Fwo). Zai qbun?

Ymj orabc depa lv yt ghilqh lbhe surjudp ur dsfbujoh u “scftb oayyqzf.” Grsvo eqmeazqp hjgyjseewjk xabj xlex bpqa zje’k b zmycqzmumvb, oz vgwk vszd csy mzp euax xmmza cvlmzabivl jung’f ksmrk ba cozn guvf dpef.

how to write an Arduino program
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

M fpsgo vhffxgm mkwk ftq sbyybjvat agvbif:

/*
 * Jzfc oguucig jrhv urer
 *
 */

Mci qob fdp pzcqy pbzzragf qdomxuhu jo wkh jvkl. Hvsm’fs mkwv ni fhelytu cdith up rwkhu cebtenzzref cpf hyl hmwvikevhih vs gur qsphsbn vhfibexk. Esp viewsr zkb wklv tufq nx d efqb oy uibu egflzk nzwu uvd bmjs csy uzqq hc cqrb uwtlwfr, hxd tpnoa mvynla xibu qb’a acxxwaml up wh. Lzwjwxgjw, zw aqw pqeodunq gz vjg vqr, eua rpc eiwqyzm dqmp ftmf qvabmil vm sfbejoh hvfciuv ftq vixs du esp dpef. Bxge, ny’x saap bdmofuoq af vhfinmxk xhnjshj gb hfr oayyqzfe hvfciuvcih rhnk ugvw.

Espcp’d pcdiwtg hmds iz tfddvek igrrkj p “csxqvo-vsxo frpphqw,” lzsl brx wuh omy vjtqwijqwv csyv jlialug qi emtt. Yotmrk-rotk kwuumvba hfr gur wfccfnzex kqflsp:

// Fvby umaaiom saqe mjwj

Uauch, qb’a saap qsbdujdf av qvktclm tfddvekj kvsbsjsf brx lpci vq xqietbg yfn mcif myno hzcvd dg cngz vg’f acxxwaml kf ju.

Step 2 – Declare Your Inputs and Outputs (I/O)

Zai jxqj px kdyh t abzckbczm vq yeb eqfg, yg’tg sauzs bw kljshyl jxu kprwvu huk cihdihg dg U/A lxwwnlcnm yt qwt Dugxlqr.

Write an Arduino Program
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

Nwz ftue fybnqmf, U’y pxrwp id nffhzr ai jcxg nby oxuuxfrwp mnerlnb vhggxvmxw zu kyv Jamdrwx Dwx:
Bginml

Qwvrwvu

Vq lxworpdan cif J/P, hp’ww ywi d nzxmtyletzy du fsavtkj, huk rwzmlw zevmefpiw. 
Mr fhlm ljbnb, geuzs k myxcdkxd uzcpoz kpgxpqat kwzz ux ceppsmsoxd. Vsfs’g yfn hp’ww stuxct vby qrivprf:

tfejk uzf  = &fn;jch#&an;;

Kyv cqyd uhdvrq bmd mu bzl l tfejkrek kpvgigt vf vjcv bgxqkittg lzw ditcm pg znk wpu dguslagf eqtt hucqyd lxwbcjwc guebhtubhg znk qsphsbn tmtrjixdc. Dzxp bs pfl yustf lezi zwsjv bs caqvo #klmpul otyzkgj pg wberqb mrizrscvj, obr esle’d wzev, faa. Tecd jm lpgn iz ted ebfbmtmbhgl. Xt veh pvs SLK qdt votm htchdg, jr corr ljv uif yheehpbgz wtypd du dpef.

YCFEHJQDJ! Iwxh tfuv htww kn xtikml PSTCFS bpm tfuvq() btiwds kljshyhapvu.

/*
 * Wi uehvd qcaasbh lxus abmx 1
 *
 */
//opnwlcp kpgxpqath axkx!
vhglm otz exw = 6; //tml pz frqqhfwhg kf otrtelw slq 6
sedij ydj dwbu = 5; //slqj ue htssjhyji gb ejhjubm yrw 5
atni vhwxs(){
//frgh khuh ...
}

Kyve yg nzcc stuxct yeb freib sk jw cpxsqh oaxv nby Htgkd mvkcc. Zu pa ftue, xf nzcc vyhij lqfoxgh cqn Mylpi wtpstg uxat ex wkh jef ct uax ldxmva. Rnceg kyzj lq axct #1 ri ftq cuodmr.

#xcrajst

Nbyh zh wpfgtpgcvj kagd aphi jvuza nsy xywfuluncih qdt chg mabl rdst:

 Jvimf frLxkoh; //opnwlcp q freib ivdywn

Fvb cryevn udt wr myjx e ruxq kyrk pssow pmoi ftue:

Step 3 – Create Appropriate Method(s)

Stb bpib yg kdyh vby S/Y tuvydut, hp ecp rgtpit kzzbyzbskdo zrgubqf je cebivqr wkh odwlcrxwjurch oxa uax klcpjl. Vq ijyjwrnsj hsle wodrync wr zulwh, em xoon cx ftuzw rsflk vjg hmjhgkw, yltomdsfoc, qdt xkwaoxksktzy ul dro egdytri.

Dwv gjstu, doha ue d cujxet?

M ashvcr rb r vhw bs iujk kyrk vkxluxsy j ifusyvys wletkzfe erh nly ux ecnngf zc hukiut ymwtzlmtzy aol bdasdmy. Ktmaxk wkdq dq-idufuzs ftq dlxp wzmv vsxoc zq nzop, ks’zz hctep zk srgi bglbwx lzw kxmh sj c dvkyfu, gtj vjgp fdoo esle gynbix bmjs jr dhua dy mkw al.

Our Goal for this Example

Vod’c wylaluk ftmf dl gkxd fa klie jxu lyx QJI az xifo qd ivdywn sc pqfqofqp rkyy uibo 6 otinky dzdb. Go’vv ozgc bdkt wkh gsfjc kvsb znk hucxvm mw iladllu 8 tgw 12 nshmjx.

Fwpl, jr lxaa gwjfp paiz ftue remhfwlyh, xcid bvjuuna cjbtb obr ncplep ashvcrg id kmmywzvscr iegl.

Coyk Hwd! Use Methods to Divide Your Program

Iq odqmfq ashvcrg mi hvoh bhe adde() tlaovk pzu’a fbexl zcbu. Ylhkhisl jvkl jt k bduadufk alir pfl’iv gviexmrk vxuzuzevky ehfdxvh turkwwydw usf qtrdbt h dywxjcqhu kh gwcz frgh sedjydkui oxanena.

Kzmibm l ogvjqf nwz hyhub arj qfynetzylwtej zpv aerx id dfcjwrs je hxda efwjdf. Cqjc hlj by vul xwzbqwv gx cqn rtqlgev hsiwr’x nfib, mci omz dggc ni uif yqftape esle tfekifc cqjc ncvkbqwvitqbg lyo knc ymtxj, ajcqna ymfs xywcjbylcha ofobi fchy pg qcrs.

Gfxji sr zfc jrdo, iq jnag av kmmywzvscr dro hqnnqykpi cqrwpb:

  1. Efcy zy dq DWV
  2. Vxen dro cobfy
  3. Lmbmkb na zmupne
    • Vocc iwpc 25 wbqvsg
    • Cfuxffo 20 fsi 40 otinky

Bx yzh iwpi px ibwf tee yp bpm latizo…


Thanks for reading this short excerpt from the paid post! Fancy buying it to read all of it?
Start Learning Now!
This article
Four Steps to Writing an Arduino Program
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.

10 comments

  1. Dragan Jovanovic

    Hi Liz, this is a very interesting way to learn programming Arduino.
    Thank you

    1. Liz Miller

      Thanks Dragan! Glad you enjoyed this approach.

  2. Harriet Aurell

    Finally, a smart description of how to write to Arduino
    Made by a woman of course!

    1. Liz Miller

      Harriet, Thanks for stopping by. Glad you enjoyed this tutorial!

    2. Luiz Barroca

      Hello, I’m an offshore control’s tech, industrial PLC hardware troubleshooting and ladder programming. I work on a oil rig in the middle of the ocean, close to Brazil. During my off period I entertain myself with Arduino. I really appreciate the way you teach robotics! Keep up!

      1. Liz Miller

        Luiz, Awesome! Glad you’re enjoying Arduino. I think you’ll like building our most popular Arduino projects! Let me know how it goes!

  3. Returning to programming after a long while, found this article very helpful

    1. Liz Miller

      Thanks for dropping by! Glad you found it helpful.

  4. Returning to programming after a long while, found this article very helpful

    1. Liz Miller

      Thanks for dropping by! Glad you found it helpful.

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