Landing : Athabascau University

Arduino IDE Code Reference: Revision

Last updated May 7, 2019 - 6:35pm by Darren Hurren

This Wiki page lists some of the Arduino Integrated Development Environment (IDE) software's language adn code examples for reference. I made this Wiki page to assist with my own understanding of the Arduino language. This information has been copied from the Sparkfun inventors Package (SIK) Guide (v4.0a) or the online Arduino Language Reference ( .  

Arduino IDE Code Reference:

void setup(){code to run at start} & void loop(){code to after in a continuous loop}

Every Arduino program needs these two functions. Code that goes in between the curly brackets {} of setup() runs once. The code in between the loop()curly brackets {} runs over and over until the RedBoard is reset or powered off.


void function_name(){}

This is a definition of a simple function. When programmers want to use many lines of code over and over again, they write a function. The code inside the curly brackets “executes” whenever the function is “called” in the main program.


This line calls a function by that you have earlier created in: void function_name() { }. Functions are used for when you need to call the functions multiple times so it can reuse the code that was written inside the curly brackets of that function.

Comments: //this is a commented line OR /*this is a commented paragraph*/

pinMode(13, OUTPUT);

In order to use one of the digital pins, you need to tell the Arduino whether it is an INPUT or OUTPUT. The example above is a built-in “function” called pinMode() where you first specify the pin number and then hot it will function (INPUt or OUTPUT). The example above makes pin 13 a digital output. You can also use one of the arduino's built-in internal 20kOhm as an INPUT. Use this by writing the function: pinMode(pin, INPUT_PULLUP);

digitalWrite(13, HIGH);

When you are using a digital pin as an OUTPUT, you can command it to be HIGH (output 5 volts) or LOW (output 0 volts). The example above sets pin 13 to output 5 volts.


This function checks to see if an input pin is reading HIGH (5V) or LOW (0V). Returns TRUE (1) or FALSE (0) depending on the reading.

analogWrite(9, 100);

Use this analogWrite function to set a output voltage between 0 and 5V in 255 increments, where 0 woud be 0V and 255 woud be 5V. You can only write (or output) to one of several PWM analog pins (3, 5, 6, 9, 10, 11) or you can output to a variable (ex. redPin if redPin = one of the analog pins). These pins are marked with a ~ on the arduino board. Only these pins can switch on and off fast enough to vary the voltage output between the two voltage extremes (0 and 5V in this case).

potPosition = analogRead(A0);

The analogRead function is used to read the value on an analog pin. In this case, analogRead takes the value of what is read on analog pin A0, and returns a number between 0 (0V) and 1023 (5V), then assigns that number to the variable potPosition, to determine what value the potentiometer is set to.


Causes the program to wait on this line of code for the amount of time in between the brackets, represented in milliseconds (2000ms = 2s). After the time has passed, the program will continue to the next line of code.


This is the Baud rate, which determines how fast the Arduino board will transmit data to, and receive data from the PC. This line of code tells the arduino board to start a serial communication with the computer. The Baud rate that you set in the code (ex. 9600 is shown in the example above) must match in the serial monitor in the Arduino IDE in order for you to properly view the incoming data.


This function actually prints a line to the serial monitor that represents what value a potentiometer is set to. It takes the pot value then prints what vlue it is currently set to (0[0V] to 1023[5V]) at that moment in the loop(). the ln at the end of println tells the monitor to print a new line at the end of each value; otherwise the values would all run together on one line. 

tone(pin, frequency, duration);

The tone() function will output a specific frequency (in a PWM square wave) to a specific pin for a specified duration (in seconds?) (Note: a duration is not required, but if none is specified the square wave/tone will output contnuously until the function noTone() is called). The SIK says that tone() can be used on any PWM capable digital pin (marked with ~).


Turns off a pin that has been activated with the tone(pin, frequency, duration); command if no duration was seleccted in the tone(); command. 


Use the internal built-in clock in the Aruino board to see how many milliseconds have passed since the Arduino was powered on.







random (min, max);

This function takes a set of numbers (speified by min & max values in brackets) and generates a psuedo-random number from that set


This math function rounds a number up or down to the nearest whole number.


Analog pin values on the microcontroller can vary from 0 to 1023 (1 Kilobyte?). A servo motor can only accept a value from 0 to 180 (degrees). In order to control a servo motor based on analog pin values you have to use the map function. The map function takes one range of values (i.e. 0,1023) and outputs a different range (i.e. 20,160) that can contain more or fewer values that the original. In the function example above, we are using analogRead(); of the potPosition variable (which, since it's analog, inputs values: 0 to 1023) and mapping this to 20,160 to control a servo from 20 to 160 degrees. NOTE: It is not recommended to write the values of 0 or 180 to a servo motor since these are the two extreme angles and forcing a servo to reach those limits can cause the motor to twitch and possibly become damaged. It is best to stick with angles of 20 to 160 instead of 0 to 180. 

Serial.readStringUntil(' ');

Reads a serial message until the first space and saves it as a string


If a number is stored in a stringg variable, this will convert it to an integer, which can be used in math equations.



A while loop will loop continuously until the expression inside the parentheses () becomes false. The expression is a boolean statement that evaluates to TRUE or FALSE. Something must change the tested variable or the while loop will never exit. Example below:

var = 0;
while(var < 200){
  // do something repetitive 200 times

for (int i = 0; i < 5; i++){};

This is an example of a for loop, which repeats a section of code a number of times using a counter that increases each loop iteration until it reaches a stop value. The for loop has three parameters: The first parameter is a start value. In this case i starts at 0. The second parameter is the stop condition. In this case the loop stops when i is no longer less than 5 (i < 5 is no longer true). The third parameter is an increment value. i++ is shorthand for increase i by 1 each time. You can also increment the value by different amounts. This would complete the loop 5 times.

 IF ELSE Statements:

if(logic statement using logical operators) {code to run if statement is true. Write nested if statements in here with their own curly brackets{}}

else {code to run if statement is false}

These IF ELSE statements work by first determining if the first IF logic statement (in the round brackets) is true and if that isn't true it will then run the code written beside the ELSE statement. You can use nested IF statements for even more options. In a nested IF statement, if the very first IF is NOT TRUE, then it jumps to the ELSE statement. You can also use ELSE IF statements to add another test condition. Where nested IF statements will automatically be skipped if the first IF statement is false, the ELSE IF statements will be tested after the initial IF statement is FALSE. If the ELSE IF statements are also FALSE the code will go to the next ELSE IF or ELSE statement (Note: that an else if block may be used with or without a terminating else block and vice versa. An unlimited number of such else if branches is allowed). Else if statements let you combine more than one logic statement. Arduino will test each one in order, run the contained code in that section if it's TRUE, then skip skip all of the other sections in the remaining statements.

Refer to example Arduino IDE code: SIK_CIRCUIT_1D-RGB NIGHT LIGHT for examples on how to use nested IF statements, SIK_CIRCUIT_2B-DigitalTrumpet for examples on how to use ELSE IF statements.

Example Code:

if (temperature >= 70) {
//Danger! Shut down the system
else if (temperature >= 60 && temperature < 70) {
//Warning! User attention required
else {
//Safe! Continue usual tasks...



 int variable_name;

 Integer Variable: A variable is a placeholder for values that may change in you code. You must "declare" a variable at the beginning of the code (before "void setup()"  before you can use them. Variables are case sensitive and good practice is to have the first word lowercase and the second word start with an uppercase letter to easily identify it as a variable (ex. int photoResistor).

char variable_name;

The char variable stores character values. When used along with the int variable is can be used as a shortcut so that you are only writing, for example a letter instead of a longer string of text or numbers. Examples can be foundd on the Arduino IDE in the code example: SIK_Circuit_2A-Buzzer

boolean variable_name;

This variable type is based on boolean logic where there is only two possible values; 1 or 0 (a.k.a. HIGH or LOW, ON or OFF, TRUE or FALSE). Using this variable helps save memory on your microcontroller if you only need to know if something is either TRUE or FALSE. Space in the microconntrollerr is reserved for each variable that is declared in the code and this depends on the variable type. 

long variable_name;


float variable_name;

This type of variable is a floating-point number which is similar to a int (integer) variable except it can represent numbers that contain a decimal point, which allows for more precision. For example, you can measure precise distances such as 9.33 inches instead of just 9 inches if a using an int variable.

const int variable_name = 3; 

Constant variables are marked as "Read Only" and cannot have their values changed by the program. They are great for declaring pin number variables that will not change throughout the program. 

Logical Operators:

Logical operators are used to make a comparison between two different values. Those values can be taken from variables (i.e. variables used to capture photoresistor values), digitalRead(pin) or numerical values.  Some examples of logical operators are: == (is equal to? This actualy means: Are these two values equal? Note: a single = means assigning a particular value to a variable), > (greater than), < (less then), >= (greater then OR equal to), <= (less than OR equall to) and && (AND; this can be used to combine logical operators). The logical operator compares the number or variable on the left to the number or variable on the right side of the operator (ex. '400 >= photoResistor' would be TRUE if the photoResistor value was 400 or less and FALSE if the value of photoResistor was less than 400).



An array in basically a way to store a list of values, variables or pin numbers. To declare an array, first give it a suitable name (array_name as shown in the example above). Next, write the size of / number of positions/elements in the array or assign a list of variables to the array. An array must contain all the same type of variables and be declared as such. An array index / number starts at 0, not 1.

To call one of the values in an array, type the name of the array followed by the index of the value: array_name[index_#); (ex. array_name[0]; will call the first element/index of the array: array_name. array_name[1]; will call the second element/index etc.) 

Storing a sequencce of pin numbers in an array (ex. int led[ ] = {3,5,7,9};) allows you touse a loop to easily cycle through each pin connected to the LED's. 

const char* array_name [array_length] = {"string1', "string2",...};

Make an array of strings. The strings are stored as constants, so they can't be changed once the program starts.

 Including Libraries:


The #include command adds a library to your Arduino program. After you include a library, you can use the commands in the library in your program. This line adds the built-in servo library (servo.h). Libraries are important because some code would require a lot of time and knowledge to write from scratch (like writing code to send precise PWM signals to control a servo), but the Arduino has hundreds of built-in or user-submitted code contained in libraries that allow anyone to use in their functions in just a few lines of code instead of many. One example is the servo.h library, which is a built-in library.

Useful Libraries:

servo.h - allows easy control of servo motors. You need to create an object for servos (see objects section)

LiquidCrystal.h - LCD displlay library. You have to create an object to use the included functions in the library.

 Objects; Servo controls:

Servo myServo; 

 The servo command creates a new servo object and assigns a name to it, myServo in this case. If you need more then one servo objet then you have to give them different names.


the .attach(); method tells the servo object which pin the signal wire is attached. It will send position signals to this pin. In this instance, pin 9 is used. You can only use digital pins that are capable of PWM (pins marked with ~)


The .write(); method moves the servo to a specified angle. In this example, the servo is being told to go to angle 90). NOTE: It is not recommended to write the values of 0 or 180 to a servo motor since these are the two extreme angles and forcing a servo to reach those limits can cause the motor to twitch and possibly become damaged. It is best to stick with angles of 20 to 160 instead of 0 to 180. 

Objects; LCD controls:

LiquidCrystal LCD_name(RS_pin, enable_pin, d4, d5, d6, d7);

As with servos, you need to create an LCD objecct and give it a name (you can make more then one). The numbers in the brackets are pins on the Arduino that connect to specific pins on the LCD (data lines d4 - d7).

lcd.begin(16, 2);

This line initializes the LCD object and tells the program the CD's dimensions (which in this case are 2 rows of 16 characters each)


This method clears all the pixels on the display.


Moves the cursor to point on the 16x2 grid of characters. Text that you writ to the LCD will start from the cursor. This line is starting back at position (0,0).

lcd.print("Hello, world!");

Prints the string of characters located within the quotation marks to the LCD starting at the cursor point. In this case: Hello, world! 

lcd.write("text string");

lcd.write is more efficient for just printing text since lcd print has to call lcd.write to print text.