0% found this document useful (0 votes)
8 views

3 Elec 1b - Arduino Programming

Uploaded by

Maoi Reyes
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

3 Elec 1b - Arduino Programming

Uploaded by

Maoi Reyes
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Elec 1 – Technical

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.

Learning Module Development Team

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:

(First Name, Middle Initial, Last Name), Position


(First Name, Middle Initial, Last Name), Position
(First Name, Middle Initial, Last Name), Position
Course Overview
Introduction

Technical Elective is a three-unit (two-unit lectures and one-unit laboratory) computer


engineering professional course. This elective is designed to the third year level computer
engineering students whose interest is in both hardware and software where the intended output
is the solution in engineering problems especially the real problem in the community using the
concept of automation and technology. This elective requires knowledge both in programming
and electronic circuit.

Course General Objectives

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.

1. Understand basic information technology concepts;


2. Use application software and the internet technology in learning new knowledge;
3. Acquire proficiency in programming and Arduino as the platform for circuit
programming development; and
4. Use high level language and programming and circuit programming applications in
solving engineering problems.

Course Details:

 Course Code: Elec 1


 Course Title: Technical Elective 1 – Embedded System
 No. of Units: 3-units (2-units lecture and 1-unit laboratory)
 Classification: Lecture with Laboratory-based
 Pre-requisite / Co-Requisite: 3rd Year Standing
 Semester and Academic Year: 1st Semester, AY 2021-2022
 Schedule: BSCpE 3B – Tuesday and Thursday, 2:00PM-4:30PM
 Name of Faculty: Dionisio M. Martin Jr.
 Contact Details
Email: dmmartinjr@yahoo.com
Mobile Number: 0939-906-0585
FB Account: Dionisio Martin Jr.
 Consultation
Day: TTH
Time: 8:00-9:30AM

Learning Management System

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

Assessment with Rubrics

Students will be assessed in a regular basis thru quizzes, assignments, activities,


individual/group outputs using synchronous and/or asynchronous modalities or submission of
SLM exercises. Rubrics are also provided for evaluation of individual/group outputs.

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.

Arduino board is basically an in-demand development board particularly to the electronic


enthusiast where integration of circuit and programs is no hassle. Thus, this module will feel
by the computer engineering student to program and used electronic circuit to designed project
that is related to automation and real-time application.

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 1: Introduction to Arduino


Chapter 2: Arduino Development Environment
Chapter 3: Arduino Programming
Chapter 3: Arithmetic Operators
Technical Elective 1

Chapter 3

Arduino
Programming
Chapter 3

Arduino Programming
Introduction

The Arduino programming language is based on a very simple hardware programming


language called processing, which is similar to the C language. After the sketch is written in
the Arduino IDE, it should be uploaded on the Arduino board for execution.

Specific Objectives

At the end of the lesson, the students should be able to:

- identify the two important function in Arduino programming.


- identify the parts of structure of Arduino sketch.
- identify the different libraries useful in basic Arduino programming.
- discuss the structure of Arduino program.
- program simple display messages using Arduino serial monitor.

Duration

Chapter 3: Arduino Programming = 3 hours


(2.5-hours discussion;
0.5-hour assessment)

_____________________________________________

SKETCHES

Sketch – is the term given to programs written in the Arduino IDE.


– is the unit of code that is uploaded to and run on an Arduino board.
– is written based on C++ programming language.

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.
*/

// the setup routine runs once when you press reset


void setup() {
// initialize serial communication at 9600 bits per second
Serial.begin(9600);
}

// the loop routine runs over and over again


void loop() {
// print out the message
Serial.println(“HELLO WORLD”);
delay(500); // delay in 0.5 second before it loop back
}

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

Hughes, J.M. (2016). Arduino: A Technical Reference. O’Reilly Media, Inc.


Perea, F. (2015). Arduino Essentials. Packt Publishing Ltd.
http://www.arduino.cc
https://www.i-programmer.info
Activity Sheet
ACTIVITY 3

Name: ______________________Course/Year/Section: ___________ Score: _________

Direction: Identify the following.


________________ 1. Used to expand the functionality of Arduino programming just like
most programming platforms.
________________ 2. Typical baud rate for serial communication with computer.
________________ 3. Used to inform yourself or others about the way the program works.
________________ 4. A block of code that executes a specific task.
________________ 5. Used for communication between the Arduino board and a computer
or other devices.

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

Direction: Give 5 each of the following:


1. Common functions of library
2. Standard libraries

3. Elements of code structure


Assignment
ASSIGNMENT 3

Name: ______________________Course/Year/Section: ___________ Score: _________

Direction. Answer the following:


1. Why Arduino programmers need to used library?

2. What is the difference between setup( ) and loop( )?

3. What is the importance of using comments in programming?

4. What is the difference between print( ) and println( )?

5. Why is do we need to place semicolon after each statements?


Learner’s Feedback Form

Name of Student: ___________________________________________________


Program : ___________________________________________________
Year Level : ___________ Section: ____________
Faculty : ___________________________________________________
Schedule : ___________________________________________________

Learning Module : Number: _________ Title : ______________________

How do you feel about the topic or concept presented?


□ I completely get it. □ I’m struggling.
□ I’ve almost got it. □ I’m lost.

In what particular portion of this learning packet, you feel that you are struggling or lost?
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________

Did you raise your concern to your instructor? □ Yes □ No

If Yes, what did he/she do to help you?


___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________

If No, state your reason?


___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________

To further improve this learning packet, what part do you think should be enhanced?
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________

How do you want it to 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).

You might also like