top of page

Writing Code

Although an Arduino can be programmed using different computer programming languages, the language used most often is C/C++.  It was invented back in the 70's and is still widely used today.  Code for an Arduino is written in C/C++ and is stored in a document called a 'sketch'.  C++ is just an improvement of the C language, and can be considered 1 type of language for use with an Arduino.   


 Every valid sketch will contain two major parts, as shown below- 

The two major parts are 'setup()' and 'loop()'. A sketch will not compile or run without both parts.  These parts are called Functions. The purpose of each of these two important functions is discussed below: 

The setup() function is called when a sketch starts. It is used to initialize variables, pin modes, start using libraries, etc. The setup function will only run once, after each powerup or reset of the Arduino board.

The loop() function does precisely what its name suggests, and loops consecutively, allowing your program to change and respond. An Arduino will run the setup function once, and then repeatedly run the loop function until power is removed or reset is pushed.  

Another important part of writing code for an Arduino is add meaningful Comments in useful places. Comments are not required, but make the code you write a lot easier for anyone to understand what is trying to be accomplished.


 A Comment is designated by the ' // ' character sequence.  Anything to the right of that character sequence is considered a comment and not executed by the processor.  They are usually human readable phrases or sentences that describe the authors intentions. Looking at the above code, we see a comment line in each of the two required Functions. These types of comments end at the end of the line of text. 


Another way to comment code is to use the ' /* ' and ' */ ' character sequences.  When these symbols are used, the starting point of the comment is anything after the ' /* ' character sequence, and the end does not occur until the ' */ ' character is reached.  This means that no matter how many lines of code are contained between the opening and closing comment character sequences, all the code in between is considered to be a comment and will not be executed by the processor. An example is given below-

void setup() {
  // put your setup code here, to run once:


void loop() {
  // put your main code here, to run repeatedly:


bottom of page