Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Basic Arduino Code and Syntax

Code/Source
A code or source code is a computer program or set of instructions written by a programmer
to enable the computer or electronic device to perform its task.

Sketch
-It is saved as a plain text file with the extension name .ino. The written code is in human-
readable format, which means data presented can be naturally and easily read by humans.
-Arduino IDE also allows you to compile the code. Compiling refers to the process of
translating the code written in a programming language into machine-readable language, a
language that a computer can understand
-Errors should first be fixed before the program starts executing, the process of running or
performing the task.

Syntax
-a set of rules, format, statements, commands, or declaration in a programming language.
-Every line of code in Arduino must end with a semicolon (;) It is sometimes referred to as
program terminator and indicates the end of the statement in a program. Failure to end in
this manner can also cause a syntax error.

Setup () Function
Two Functions of Arduino Sketch:
- void setup()
- void loop()

-The function is the part of a computer code that instructs the computer to perform a specific
task.
-Once the sketch starts, it initially calls the setup function. This only runs once to set up your
program by initializing the variables and values, pin modes, calling libraries, etc.
-The pair of open and close curly brackets { } in the sketch identifies a line of code to be
executed when a function is called.

Comment
-is generally included anywhere in the program, though not required but recommended to write
a description or notes in the program. It will not affect the execution of the program since it
will be ignored by the compiler. This serves as a reminder for the programmer while editing the
code.

Single Line Comment - It starts with a double slash (//) symbol and anything typed within
that line will be ignored by the compiler.

Multiline Comment -It starts with a slash and asterisk (/*) and ends with an asterisk and
slash (*/). Anything in between these symbols will be considered as part of the comment.

Loop() Function
Looping is among the powerful basic concepts in computer programming. It is a programming
function that repeatedly executes a sequence of instructions until a certain condition is
reached. The loop function in Arduino code performs the same way by consecutively looping a
certain line of code inside the function. This function is generally used to control the Arduino
board.

Controlling a Digital Output Device


The light emitting diode (LED) is the only built-in digital output device in the Arduino Uno
microcontroller unit. It is directly connected to digital pin 13.

Arduino Sketch – LED on Pin 13

Working with the Arduino Sketch


PinMode ()
The pinMode ( ) function is used to set up a specific pin as either digital input or digital
output.
Syntax

Parameters
pin: specific number of the pin it is connected to the board
MODE: INPUT, OUTPUT

digitalWrite() Function
The digitalWrite() function writes either a HIGH or LOW value to a digital pin. If the pin has
been set up as an OUTPUT with pinMode( ) similar to the previous example, the voltage will be
set to 5 V (or 3.3 V for 3.3 V boards) for HIGH and 0 V (ground) for LOW.
If you do not declare the pinMode, the default mode is "input."
Syntax

Parameters
pin: the pin number
VALUE: HIGH, LOW

delay() Function
The delay( ) function causes the program to pause for a specific period of time in milliseconds
before executing the next line of code. One second is equivalent to 1000 milliseconds.
Syntax

Parameters
ms: the number of milliseconds
to pause (unsigned long)
What is a code or source code in programming?
a) A computer program b) A document containing programmer's notes
c) A hardware component d) A type of code used in music

What file extension is commonly used for Arduino sketches?


a) .txt b) .ard
c) .ino d) .code

What does the term "compiling" refer to in Arduino programming?


a) Debugging the code b) Translating code into machine-readable language
c) Writing comments in the code d) Running the program

How should every line of code in Arduino end?


a) With a period (.) b) With a comma (,)
c) With a semicolon (;) d) With an exclamation mark (!)

Which function is called only once at the beginning of an Arduino sketch?


a) loop() b) start()
c) initialize() d) setup()
What is the primary purpose of the setup() function in Arduino?
a) To control loops b) To print data to the serial monitor
c) To initialize and set up the program d) To create variables

How are comments marked in Arduino code for a single-line comment?


a) [comment] your comment here [/comment] b) <!-- your comment here -->
c) // your comment here d) /* your comment here */

What is the syntax for a multi-line comment in Arduino code?


a) /// your comment here /// b) /* your comment here */
c) <!-- your comment here --> d) // your comment here

Which function is responsible for repeatedly executing a sequence of instructions in Arduino?


a) repeat() b) execute()
c) loop() d) start()

What is the default digital pin for the built-in LED on an Arduino Uno?
a) Pin 1 b) Pin 5
c) Pin 9 d) Pin 13

What function is used to set up a specific pin as either digital input or digital output?
a) pinMode() b) setPin()
c) digitalPin() d) configurePin()

What does the digitalWrite() function do?


a) Reads the value of a digital pin b) Writes analog values to a pin
c) Writes a HIGH or LOW value to a digital pin d) Changes the mode of a pin

How many milliseconds are in one second?


a) 10 b) 100
c) 1000 d) 10000
\\Which function is used to pause the program for a specific period of time in milliseconds?
a) wait() b) halt()
c) stop() d) delay()
What is the syntax for the delay() function in Arduino?
a) delay(seconds) b) delay(minutes)
c) delay(milliseconds) d) delay(hours)

Which Arduino function is used to make a digital pin output a HIGH voltage?
a) setHigh() b) digitalWrite(HIGH)
c) highVoltage() d) digitalWrite(pin, HIGH)

What is the syntax for setting a pin as an input in Arduino using pinMode()?
a) pinMode(pin, INPUT) b) pinMode(INPUT, pin)
c) setInput(pin) d) setPinMode(pin, INPUT)

Which of the following is not a valid value for the pinMode() function in Arduino?
a) INPUT b) OUTPUT
c) HIGH d) INPUT_PULLUP

What is the purpose of comments in Arduino code?


a) To make the code run faster b) To add decorative elements to the code
c) To provide explanations and notes for programmers d) To hide parts of the code from
execution

What is the primary role of the loop() function in an Arduino sketch?


a) Initializing variables b) Setting up pin modes
c) Repeatedly executing a sequence of instructions d) Writing comments in the code

Answers:

a) A computer program
c) .ino
b) Translating code into machine-readable language
c) With a semicolon (;)
d) setup()
c) To initialize and set up the program
c) // your comment here
d) /* your comment here */
c) loop()
d) Pin 13
a) pinMode()
c) Writes a HIGH or LOW value to a digital pin
c) 1000
d) delay()
c) delay(milliseconds)
d) digitalWrite(pin, HIGH)
a) pinMode(pin, INPUT)
c) HIGH
c) To provide explanations and notes for programmers
c) Repeatedly executing a sequence of instructions

You might also like