Home/Tutorials/Arduino Coding Basics

Arduino Coding Basics

There are many things to keep in mind when coding such as the syntax, functions, and keywords. It can be difficult to keep track of all these things when getting started, so here’s an Arduino programming cheat sheet to keep track of the basics!

Program Structure

void setup() { }

This is the void setup function that runs once the Arduino microcontroller first boots up.

Copy to Clipboard

void loop() { }

This is function that runs after the void setup has finished. It will continue to run over and over until the Arduino has been powered off.

Copy to Clipboard

Code Syntax

// (single line comment)

These double forward slashes create a single line comment. This is great for adding comments throughout your code to describe what each line does. Everything until the end of the line will not count as an actual part of the program

Copy to Clipboard

/* */ (multi-line comment)

If you have many lines that you would like to comment, you can use multiline comment. All text in between these symbols will be ignored from the program just line the single line comment.

Copy to Clipboard

{ } (curly brackets)

These curly bracket symbols are used to mark where a block of code begins and ends. This is used in functions and loops.

Copy to Clipboard

; (semicolon)

Just as you mark the end of a sentence with a period, every line of code must end with a semicolon. A semicolon that is missing will not allow the code to compile so it’s important they’re placed properly!

Copy to Clipboard

Variables

These are used to keep track of numbers in our program. They can be created by writing the data type followed by the name of the variable.

Note: Variable names cannot begin with a number.

int (integer)

One of the most popular variable data types. They are used to represent numbers. Integers can only store whole numbers and not decimals. They are 2 byte values and can store a number from -32,768 to 32,767.

Copy to Clipboard

long

Another data type used to store numbers. When an integer isn’t big enough, long variables are used as they are 4 byte values that can store a number from -2,147,483,648 to 2,147,483,647.

Copy to Clipboard

bool (boolean)

A simple data type that holds one of two values: true or false.

Copy to Clipboard

float

A useful data type as it can hold decimal numbers. Floating-point values can be as large as 3.4028235E+38 and as low as -3.4028235E+38. They are stored as 4 bytes of information.

Copy to Clipboard

char (character)

These variables are used to store single character values. These are declared in single quotes.

Copy to Clipboard

Math Operations

We can manipulate numbers in our program by perform the following math operations.

= (equals)

This single equal sign is the assignment operator. It is used to store the value of the right side of the equal sign to whatever variable is on the left side of the equal side.

Copy to Clipboard

+ (addition)

This adds two numbers.

Copy to Clipboard

(subtraction)

This subtracts two numbers.

Copy to Clipboard

* (multiplication)

This operation multiplies two numbers.

Copy to Clipboard

/ (division)

This divides two numbers.

Copy to Clipboard

Control Statements

In our program we can used certain statements to control the flow of execution. Here’s a few useful statements to keep in mind when programming.

If () { }

This is called an “if statement”. If statements will execute the code that’s in between the curly brackets if the condition in the parenthesis is true. When the “if” statement condition is false, then the “else if” code will trigger, but when that is also false, the “else” code will trigger.

Copy to Clipboard

for() { }

This is a function that loops a certain piece of code as many times as specified. It can count up using “i++” or count down using “i–“.

Copy to Clipboard

Digital Pins Input/Output

pinMode(pin, mode)

A function that sets a pin’s mode. It takes two parameters: pin and mode. Pin is the number of the pin to be used, and mode is set to INPUT or OUTPUT. On an Arduino microcontroller, pins 2-12 can easily be used as digital pins.

Copy to Clipboard

digitalWrite(pin, value)

When a digital pin is set to OUTPUT, then it can output a HIGH or LOW value. the digitalWrite() function takes in the “pin” number and “value” parameter which is either HIGH (which outputs +5 volts) or LOW (which pulls it to ground, or outputs 0 volts).

Copy to Clipboard

digitalRead(pin)

When a digital pin is set as an INPUT, you can read the voltage entering it whether it is HIGH (+5 volts) or LOW (0 volts).

Copy to Clipboard

Analog Pins Input/Output

Sometimes we want to deal with accurate values gained from analog sensors. This can’t be done with digital functions, so we have useful functions available to help us when working with analog.

analogWrite(pin, value)

This function produces pulse width modulation which allows a voltage output between +0 volts and +5 volts. This function takes the pin number and value between 0 and 255.

Copy to Clipboard

analogRead(pin)

Reads the value of an analog pin, the value received can be from 0 to 1024.

Copy to Clipboard
By |Published On: June 25th, 2021|Categories: Tutorials|

Share This Article

Leave a Comment

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments