0% found this document useful (0 votes)
378 views7 pages

Worksheet 3 Quarter 3 Robotics and Electronics

This document contains a worksheet on basic Arduino sketch for robotics and electronics. It discusses the basic concepts of Arduino coding including Arduino IDE, sketches, functions like setup(), loop(), pinMode(), digitalWrite(), and delay. It provides examples of a basic blink code. It includes activities to test the understanding of the concepts through identifying terms and creating a sketch to control an LED.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
378 views7 pages

Worksheet 3 Quarter 3 Robotics and Electronics

This document contains a worksheet on basic Arduino sketch for robotics and electronics. It discusses the basic concepts of Arduino coding including Arduino IDE, sketches, functions like setup(), loop(), pinMode(), digitalWrite(), and delay. It provides examples of a basic blink code. It includes activities to test the understanding of the concepts through identifying terms and creating a sketch to control an LED.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Republic of the Philippines

DEPARTMENT OF EDUCATION
Region I
SCHOOLS DIVISION OFFICE
DAGUPAN CITY NATIONAL HIGH SCHOOL
Dagupan City

Worksheet
in
Robotics
and
Electronics
Basic Arduino Sketch
(Worksheet 3, Quarter 3)

Francisco C. Dacoco
Master Teacher I
Getting Started
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. The coding structures and concepts of Arduino is similar to other
common computer programming languages like Java or C++. So, a background in the fundamentals of computer
programming will make learning to program an Arduino very fast. But for the sake of those who are not acquainted in
programming, this module will help you learn the basic concept of programming Arduino.

In this worksheet you will:


a. Explain the basic Arduino code and syntax;
b. Discuss the basic Arduino sketch; and
c. Create your first Arduino sketch using the simulation Tinkercard.

In doing this worksheet, please follow these reminders…

1. Read the Lesson


2. Create a study notebook, preferably a digital one with 8.5 x 11 size
3. Perform the activities as suggested. Write your answer on your study
notebook
4. You may keep this worksheet for future consumption
5. Print and pass your study notebook together with the required output for
every worksheet

Have Fun and Enjoy Learning in the New Normal!

1
Study Time
Basic Arduino Sketch

Arduino Ide is like a text editor that allows you to write your code. The file generated or
saved using the Arduino IDE is called a sketch. It is saved as a plaintext 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 ca understand. If there is any error during the
compiling of the program, an error message will be displayed in the Status Bar, which is located at
the bottom of the IDE. Errors should first be fixed before the program starts executing, the pro-
cess of running or performing the task.

Arduino code is case sensitive, which means small letters (lowercase) are distinguished
from capital letters (uppercase) and typing the wrong case will result to a syntax error. Like other
languages, Arduino code should conform with the correct syntax– a set of rules, format,
statements, commands, or declaration in a programming language.

It is also important to remember that almost every line of code in Arduino must end with a
semicolon (;) just like other programming languages. 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

Upon launching the Arduino IDE, you will be prompted with the default sketch. The Arduino
sketch is divided into two functions: the void setup() and void loop(). The function (otherwise
known as a procedure or sub-routine) is a named piece of code that can be used from elsewhere
in a sketch. It is the part of a computer code that instructs the computer to perform a specific
task. For example, here's the definition of the setup() function from the Blink example:

Void setup()
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}

The first line provides information about the function, like its name, "setup". The text before
and after the name specify its return type and parameters: these will be explained later. The code
between the { and } is called the body of the function: what the function does.

You can call a function that's already been defined (either in your sketch or as part of
the Arduino language). For example, the line pinMode(ledPin, OUTPUT); calls the pinMode
() function, passing it two parameters: ledPin and OUTPUT. These parameters are used by
the pinMode() function to decide which pin and mode to set.

2
Comments

The first few lines of the Blink sketch are a comment:


/*
* Blink
*
* The basic Arduino example. Turns on an LED on for one second,
* then off for one second, and so on... We use pin 13 because,
* depending on your Arduino board, it has either a built-in LED
* or a built-in resistor so that you need only an LED.
*
* http://www.arduino.cc/en/Tutorial/Blink
*/
Everything between the /* and */ is ignored by the Arduino when it runs the sketch (the * at the
start of each line is only there to make the comment look pretty, and isn't required). It's there for
people reading the code: to explain what the program does, how it works, or why it's written the
way it is. It's a good practice to comment your sketches, and to keep the comments up-to-date
when you modify the code. This helps other people to learn from or modify your code.
There's another style for short, single-line comments. These start with // and continue to the end of
the line. For example, in the line:

int ledPin = 13; // LED connected to digital pin 13

the message "LED connected to digital pin 13" is a comment.

setup() and loop() Functions


There are two special functions that are a part of every Arduino sketch: setup() and loop().
The setup() is called once, when the sketch starts. It's a good place to do setup tasks like setting
pin modes or initializing libraries. The loop() function is called over and over and is heart of most
sketches. You need to include both functions in your sketch, even if you don't need them for
anything.

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.

pinMode(), digitalWrite(), and delay() Functions


The pinMode() function configures a pin as either an input or an output. To use it, you pass it the
number of the pin to configure and the constant INPUT or OUTPUT. When configured as an input,
a pin can detect the state of a sensor like a pushbutton. As an output, it can drive an actuator like
an LED.

Syntax

pinMode(LED_BUILTIN, OUTPUT);

Parameters

pin: specific number of the pin it is connected to the board

Mode: INPUT, OUTPUT

3
The digitalWrite() functions outputs a value on a pin. Writes either a HIGH or LOW value to a digi-
tal pin. If the pin has been set up as an OUTPUT with pinMode(), the voltage will be set to 5 V (or
3.3 V for 3.3 boards) for HIGH and ) V (ground) for LOW. For example, the line:

Syntax

digitalWrite(ledPin, HIGH);

Parameters:

pin: the pin number


VALUE: HIGH, LOW

set the ledPin (pin 13) to HIGH, or 5 volts. Writing a LOW to pin connects it to ground, or 0 volts.

The delay() causes the Arduino to wait for the specified number of milliseconds before continuing
on to the next line. There are 1000 milliseconds in a second, so the line:

delay(1000);

creates a delay of one second.

4
Activity Time Activity 1: Identify me!

Directions: Write the answer that matches the description below.

1. A set of instructions that enable the computer or electronic device to perform its task.

2. Refers to the file generated using the Arduino IDE.

3. Process of translating the code written in a programming language into machine language.

4. Set of rules on how letters and characters are formed to represent a command/code.

5. Function that prepares the pin to be used in the circuit and sketch.

6. Represents a block of code in the program.

7. Function that controls digital output devices.

8. Function that enables the microcontroller to wait until it process the next command/code.

9. Represent a single line comment.

10. Ignored by the compiler but is included in the code to serve as a note or reminder to the programmer while editing
the code.

Performance Task
Create a sketch that will control the LED in the Arduino microcontroller connected to digital pin 13. The LED will
light up for half a second and will turn off for half a second. This sequence will only be done for the first three seconds
when the program is uploaded.

After three seconds, the LED will change the on-off pattern; light up for one second and turn off for one second.
This pattern will continuously executed.

Write or copy paste your sketch below:

5
Rubrics: Basic Arduino Sketch

Indicator 10 points 7 points 5 point 3 points 1 point Point/s

Light Emit- For the first For the first For the first The LED light up The LED on pin
ting Diodes three seconds, three seconds, three seconds, and turns off 13 does not
the light the light the light after three sec- light up and
emitting diode emitting diode emitting diode ond or more. turns off.
lights up and lights up and lights up and
turns off for turns off for turns off but
half a second. half a second; not with the
The LED’s on- but turns on-off specified time
off pattern every second allotment.
changes to one without inter-
second continu- val.
ously.

Time allot- The prototype The prototype The prototype The prototype The prototype
ment was submitted was submitted was submitted was submitted was submitted
on time. one meeting two meetings three meetings more than
late. late. late. three meetings
late.

Total Points

Note: for simulations of your output you may visit the Tinkercard or watch

https://www.youtube.com/watch?v=yyG0koj9nNY&list=PLV6cmKvnKRs5geApVORPW79U6s3wpa0Ht

for video tutorial.

You might also like