3 Elec 1b - Arduino Programming
3 Elec 1b - Arduino Programming
Elective 1
(Embedded System)
This is a property of
PRESIDENT RAMON MAGSAYSAY STATE UNIVERSITY
NOT FOR SALE
Elec 1 – Technical Elective
First Edition, 2021
Copyright. Republic Act 8293 Section 176 provides that “No copyright shall subsist in any work of
the Government of the Philippines. However, prior approval of the government agency or office
wherein the work is created shall be necessary for exploitation of such work for profit. Such agency or
office may, among other things, impose as a condition the payment of royalties.
Borrowed materials included in this module are owned by their respective copyright holders. Every
effort has been exerted to reach and seek permission to use these materials from their respective
copyright owners. The University and authors do not claim ownership over them.
Assigned
Title Author
Chapter
Chapter 1: Introduction to Arduino
Chapter 2: Arduino Development Environment Dionisio M. Martin Jr.
Chapter 3: Arduino Programming
Chapter 3: Arithmetic Operators
Evaluators:
At the end of the semester, 85% of the students have attained 90% level of understanding for
being aware in the latest technology through the Arduino technology, both in research-based
and project-based.
Course Details:
The University LMS will be used for asynchronous learning and assessment. The link and class
code for LMS will be provided at the start of class through the class’ official Facebook Group.
Edmodo
Google Classroom
University LMS
Major examinations will be given as scheduled. The scope and coverage of the examination
will be based on the lessons/topics as plotted in the course syllabus.
0323
Module Overview
Introduction
This module aims to introduce the concepts of modular approach in designing and developing
projects using Arduino technology.
The students will learn how to program using operators and operators, conditional statements,
iterative statements and instruction necessary in using modules and library.
Table of Contents
Chapter 3
Arduino
Programming
Chapter 3
Arduino Programming
Introduction
Specific Objectives
Duration
_____________________________________________
SKETCHES
Basic Structure
The basic code structure of Arduino programming is simple and is composed of two parts:
1. Setup() function
2. Loop() function
Every sketch needs two void type functions, setup() and loop(). A void type function doesn’t
return any value.
The setup() method is ran once, just after the Arduino is powered up, and the loop() method is
ran continuously afterwards.
The setup() is where you want to do any initialization steps, and in loop() you want to run the
code you want to run over and over again.
What is a Function?
Function – is a block of code that executes a specific task.
All functions must have a unique name, setup is one example of a unique function name
(setup and loop are special functions in Arduino programming and form part of the
structure of a basic sketch).
The function name is followed by opening and closing parentheses () that may or may
not contain something.
All functions must have a return type. Both setup and loop have a void return type.
The body of a function consists of an opening and closing brace ({ and }).
Parts of a Sketch
The image below shows the parts of an Arduino sketch. Statements are lines of code that
are executed as the program runs. Each statement is terminated with a semicolon.
All Arduino sketches have a standardized code structure. This keeps the code easy to read
and modify later. The compiler doesn’t care what order you put these things in, but by
convention, people tend to structure their Arduino code in the following order:
1.) Comments: You describe what the code does.
2.) Libraries: You specify which libraries you want to use in your code. All libraries must
be in your Arduino libraries folder or your compiler will complain.
3.) Variable declarations: You specify what variables you are going to use in your code
and what their initial values are. You learn more about the different kinds of variables
you can use, as you build the projects in this book.
4.) Setup: You define how your Arduino will be used and set up any pins and
communications you will be using. This section is indicated by the instruction setup(){
and it is always concluded with a closing curly bracket:}. Code in your setup is executed
first and one time only.
5.) Loop: You place the main instructions for your code. The loop is executed after setup
and for as long as your Arduino is powered up. Any code between the curly brackets {}
is processed sequentially and then repeated forever. The loop is processed as fast as the
Arduino can go —around 16 million calculations per second.
6.) User-defined functions: You create your own reusable functions that describe how to
do.
7.) something useful. If you need to repeat an operation or calculation many times as your
Arduino is running, I recommend that you create a function to do this. This modular way
of coding makes it easy to make a change to how your code works.
Libraries – are used to expand the functionality of Arduino programming just like most
programming platforms. It provides extra functionality for use in sketches, e.g. working
with hardware or manipulating data.
– are commonly installed with the IDE.
– common functions are:
a. communication
b. data processing
c. data storage
d. device control
e. display
f. sensors
g. signal input/output
h. timing
– declare in the sketch using #include. This gives the programmer access to a large
group of standard C libraries (groups of pre-made functions), and also libraries written
especially for Arduino.
Syntax:
#include <LibraryFile.h>
Standard Libraries
EEPROM – reading and writing to "permanent" storage
Ethernet – for connecting to the internet using the Arduino Ethernet Shield.
Firmata – for communicating with applications on the computer using a standard serial
protocol.
GSM – for connecting to a GSM/GRPS network with the GSM shield.
LiquidCrystal – for controlling liquid crystal displays (LCDs)
SD – for reading and writing SD cards
Servo – for controlling servo motors
SPI – for communicating with devices using the Serial Peripheral Interface (SPI) Bus
SoftwareSerial – for serial communication on any digital pins.
Stepper – for controlling stepper motors
TFT – for drawing text, images, and shapes on the Arduino TFT screen
WiFi – for connecting to the internet using the Arduino WiFi shield
Wire – Two Wire Interface/Inter Integrated Circuit (TWI/I2C) for sending and
receiving data over a net of devices or sensors.
Comments – are lines in the program that are used to inform yourself or others about the way
the program works. Its' only purpose is to help you understand (or remember), or to inform
others about how your program works.
– are ignored by the compiler, and not exported to the processor, so it doesn’t take up
any space in the microcontroller’s flash memory.
– has two kinds:
a. Block comment or a multi-line comment is marked by the symbol /* and
the symbol */ marks its end.
b. Single line comment begins with // (two adjacent slashes) and the comment
ends automatically at the end of a line.
For example:
/*
This program continuously displays “Hello World”
at serial monitor of Arduino IDE.
*/
Serial – is used for communication between the Arduino board and a computer or other
devices. All Arduino boards have at least one serial port (also known as a UART or
USART), and some have several.
Serial.begin( ) – opens up the USB port for serial communication and also sets the baud rate.
Typical baud rate for serial communication with computer is 9600bps.
Serial.print( ) and Serial.println( ) – prints the value passed into the brackets on to the Serial
Monitor.
print( ) Function – is called when the invisible cursor must stay on the same line so text
printed in the println( ) statement that follows will be printed on the same line as well.
println( ) Function – is used when text must be printed and then the invisible cursor
moved to the next line so that the next print( ) statement will print text on a new line.
_____________________________________________
References/Additional Resources/Readings
Direction: Match the items in column A to their descriptions in column B. Write only the letter
of your choice on the space provided.
A B
_____ 1. Ethernet a. Controlling servo motors
_____ 2. Servo b. Connecting to the internet using the Arduino Ethernet shield
_____ 3. LiquidCrystal c. Connecting to the internet using the Arduino WiFi shield
_____ 4. WiFi d. Controlling LCDs
_____ 5. EEPROM e. Reading and writing to "permanent" storage
Direction: Give the complete terms for the following abbreviated words.
1. SPI
2. SD
3. TFT
4. LCD
5. GSM
6. TWI
7. I2C
8. EEPROM
9. IDE
10. TX
In what particular portion of this learning packet, you feel that you are struggling or lost?
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
To further improve this learning packet, what part do you think should be enhanced?
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
NOTE: This is an essential part of course module. This must be submitted to the subject
teacher (within the 1st week of the class).