Comprehensive Arduino Guide
Comprehensive Arduino Guide
4. Electronic Components.......................................................... 25
4A. Microcontroller..................................................................25
4B. Protoboard.........................................................................26
4C. Resistance ........................................................................26
4D. Color code table for resistors............................................27
4E. Diode................................................................................28
4F. Transistor.......................................................................... 28
4G. Capacitor ...................................................................... 28
4H. LED ................................................................................. 29
4I. LED RGB ........................................................................... 29
[Link]............................................................................ 29
4K. Reed switch ....................................................................... 30
4L. Potentiometer.................................................................... 30
4M. Photocell......................................................... 30
4N. Bell or buzzer............................................................... 31
40. DC Motor............................................................... 31
5 Programming............................................................................ 33
6. Arduino .................................................................................. 43
7. Basic kit................................................................. 55
7A. Description........................................................................ 55
7B. Distribution........................................................................ 55
7C. Fotos ............................................................................... 56
8. Fritzing.................................................................................. 57
8A. Software............................................................................57
8B. Protoboard view...................................................................57
8C. View diagram .....................................................................58
8D. Vista PCB—Printed circuit .....................................................58
8E. Master exercise...................................................................59
9. Tutoriales............................................................................... 60
The purpose of this guide is to address the concept of physical computing that
it is the ability of a machine to interact and communicate with the
humans, using sensors and actuators. The decisions on this will be made by
Take a microcontroller that is located on the Arduino board.
The Arduino board is the heart of this guide.
9
WHAT AM I GOING TO LEARN?
In this guide, you will learn to imagine and ground all ideas into concepts.
tangible things you can be proud of, since it was your idea and you
you developed ;)
10
1. INTRODUCTION
The Robotics Store and the Mechatronics Stuff Team bring this guide
which addresses learning about the DIY (Do it yourself) concept
spanish - 'Do it yourself'. After a careful selection of components-
Electronic kits supported by the Arduino board are created.
Basic Arduino supported by this guide.
11
Physical computing
Physical computing means the construction of inter-
activos físicos mediante el uso de software y hardware que pueden
to feel and respond to the analog world. Although this definition is
sufficiently broad to encompass aspects such as systems
intelligent traffic control for cars or processes of
1A factory automation, in a broader sense, the computa-
Physical education is a creative framework for understanding the relationship
of human beings in the digital world. In practice, often
the term describes handmade art, DIY project design
the hobbies that use sensors and microcontrollers to
lead analog inputs to software-based systems, and/or with-
control electromechanical devices such as motors, servos, ilumi-
nation or other hardware.
12
2. TO TAKE INTO ACCOUNT
Robotics Store
The Robotica Store
in 2010 with the idea of
make available to you-
fronts, professionals and afi-
robotics the best used products in the world for
develop their projects.
The social responsibility that the world demands from us means that through
2A
Robotics store and the website Things of Mechatronics we can compare
news, events, tutorials, projects among others. The Store of
Robotics is located in the city of Bogotá D.C, Colombia and is
only virtual. We appreciate that you buy our products with the best
market price. Contact us to learn more about us.
13
Free hardware
It is called hardware that frees hardware devices.
whose specifications and schematic diagrams are of
public access, whether under some type of payment or for-
it's free. The philosophy of free software (the ideas
about the freedom of knowledge) is applicable to that of
free hardware. It must be remembered at all times that
2C Free is not synonymous with free of charge. Free hardware forms
part of free culture.
Free software
Free software (in English free software, although this
the term is sometimes confused with 'free'
due to the ambiguity of the term 'free' in the language
2D English, so "free software" is also used and
"logical free" is the name of the software that
respect the freedom of users regarding their product
acquired and, therefore, once obtained it can be used
done, copied, studied, modified, and redistributed
freely. According to the Free Software Foundation, the soft-
Free software refers to the freedom of the users.
rights to execute, copy, distribute, study, modify the software and
distribute it modified.
Creative Commons
Creative Commons (CC) is a non-profit organization
governmental non-profit that develops
plans to help reduce legal barriers
2E from creativity, through new legislation
and new technologies. Creative Com licenses
mons or CC are inspired by the GPL (General Public License) of the Free
Software Foundation, sharing a good part of its philosophy. The main idea
behind them is to enable a legal model supported by computer tools
case, in order to facilitate the distribution and use of content.
14
Arduino
Arduino is an electronics platform.
open for prototyping based-
from free software and hardware, flexible and
easy to use. It was created for artists, designed-
pain, enthusiasts, and anyone interested in
create interactive environments or objects.
The boards can be handmade or purchased assembled from the factory; the software
can be downloaded for free. The reference design files (CAD)
they are available under an open license, so you are free to adapt them to your
needs.
[Link]
Fritzing
Fritzing is a design automation program
free electronic that seeks to help designers and artists
so they can move from prototypes (using, for example,
test plates) to final products.
[Link]
15
License of the work
2H
[Link]/licenses/by-nc-sa/3.0/[Link]
16
3. ELECTRONICS
From the beginning of this chapter, you will learn the technical terms.
what electronics has and that at the end of this you will most likely...
to master expertly.
Concept of electronics
Study and employ systems whose function-
namiento is based on driving and the
control of the flow of electrons
other electrically charged particles
you.
Look around you: radio, television, PC, mobile phone, washing machine - all
they have electronics.
Voltage
17
DC Voltage
It is the continuous flow of electrons through
a conductor between two points of different potential. In
in direct current, electric charges flow continuously.
flowing in the same direction, the current is continuous
it always has the same polarity. In the system standard-
European tic the color black corresponds to the negative and the
AC voltage
It is the electric current in which the magnitude and
directions vary cyclically. The waveform of
the most commonly used alternating current is the
3D of a sine wave.
Current
It is the flow of electrons through a conductor or semiconductor.
in a sense. The unit of measurement for this is the ampere (A).
An electric current, since it is a movement of
3E charges produce a magnetic field, a phenomenon that can
take advantage of the electromagnet, this is the principle of operation
engine maintenance.
18
Resistance
Ohm's Law
The law states that the current (I) flowing through
an electrical conductor is directly proportional
to the voltage (V) and inversely proportional to
the resistance (R). 3G
The pyramid on the right is very useful to know
the formula to which the variable that you cover with is equal
the finger, for example: Cover the V with your finger
(voltage), then voltage will be equal to I
(current) through R (resistance), one more, cover I
(Current), I will be equal to V divided by R.
Electronic systems
An electronic system is a set of circuits that interact
join together to achieve a result. One way to understand
electronic systems consist of dividing them into inputs, outputs 3H
signal processing.
Entries
The inputs: They are sensors (or transducers) electronic-
or mechanics that take the signals (in the form of tempera-
pressure, humidity, contact, light, movement, pH, etc.
from the physical world and convert them into current signals or 3I
voltage.
19
Exits
Outputs: They are actuators or other devices (also
transducers) that convert current or voltage signals into
physically useful signals like movement, light, sound, force,
rotation among others.
3J
For example: a motor that turns, an LED or lighting system that
turns on automatically when it gets dark, a
buzzer that generates various tones.
Signal processing
20
Electronic signals
The inputs and outputs of an electronic system will be considered
variables such as variable signals. In electronics, one works with
variables that are taken in the form of voltage or current, these
They can commonly be referred to as signals. 3L
Signals can primarily be of two types
described below: Digital or analog
Digital variable
Analog variable
21
Digital input / output
30
Entry Exit Entrance
Button LED Reed switch
Analog input/output
3P
Voltage divider
A voltage divider is a circuit configuration
trick that distributes the voltage of a source (Vin) between a
or more resistances (R1, R2) connected in series (one to
continuation of another).
Thank you
22
Pulse Width Modulation PWM
Pulse Width Modulation (also known as PWM, an acronym in English for
Pulse-width modulation of a signal or power source is a technique in which
the duty cycle of a periodic signal (a sinusoidal or a square wave) is modified
for example), whether to transmit information through a communication channel 3S
or to control the amount of energy sent to a load.
For example, if we apply PWM to an LED, we can vary its brightness intensity and if
we apply a PWM to a DC motor to vary its speed with it
characteristic of maintaining its torque (force) constant.
Serial communication
It is a communications interface of
digital data, frequently used
clad by computers and peripherals,
where the information is transmitted 3T
of sending a single bit to the
once. One of its uses is to monitor
through the computer screen
the condition of the beltway
connected, for example, when pressing the letter A on the keyboard it should turn on
a LED connected remotely to the computer.
SEARCH T03AB
LIKE THIS
23
Page left intentionally blank
24
4. ELECTRONIC COMPONENTS
Microcontroller
A microcontroller (abbreviated μC, UC, or MCU) is a circuit
programmable integrated circuit, capable of executing the recorded commands
in its memory. It consists of several functional blocks,
which perform a specific task. A microcontroller
includes inside the three main functional units:
central processing unit (CPU), memory, and input and output peripherals
(I/O).
25
Breadboard
It is a reusable board used to build prototypes of electronic circuits without
welding. Composed of perforated plastic blocks and numerous sheets
thin wires of an alloy of copper, tin, and phosphorus
4B
Resistance
4C
Symbol Component
26
Color code chart for resistors
4D
Example
27
Diode
4E
Symbol Component
A diode is a two-terminal electronic component that allows the flow.
of the electric current through it in one direction. It has two parts: the cat-
and the anode.
Transistor
4F
Symbol Component
The transistor is a semiconductor electronic device that performs functions of
amplifier, oscillator, switch or rectifier. It has three parts: the base (B), the
emitter (E) and collector (C).
Capacitor
4G
Symbol Component
A capacitor is a device used in electronics, capable of
storing energy by sustaining an electric field. It is formed by a pair of
conductive surfaces, usually in the form of sheets or plates, in situation
of total influence separated by a dielectric material or by vacuum.
The plates, subjected to a potential difference, acquire a certain
charged electric, positive on one of them and negative on the other.
28
LED
Symbol Component
4H
A LED (Light Emitting Diode, also 'luminous diode') is a semiconductor diode.
that emit light. They are used as indicators in many devices,
and increasingly more frequently in lighting. The LEDs
they offer many advantages over incandescent light sources
it has a much lower energy consumption, longer time of
life, smaller size, great durability and reliability.
The LED has a polarity, a connection order, and when connected the other way around
it can be burned, check the drawings at the top to get to know
what corresponds to the positive and the negative.
LED RGB
4I
Symbol Component
An RGB LED is an LED that incorporates three within its own encapsulation.
LEDs, it is RGB because R (red), G (green), and B (blue)
this way thousands of colors can be formed by adjusting individually
Each color has two. The three LEDs are connected by the negative or cathode.
Button
Symbol Component
4J
A button or switch is used to activate a function. Buttons are for
general activated when pressed, usually with a finger. A button on a device-
an electronic switch generally works like an electrical switch, that is to say in
its interior has two contacts, if it is a NO (normally open) device
to) NC (normally closed), so when pressed it will activate the fun-
reverse action of the one I am currently performing.
29
Reed switch
4K
Symbol Component
Potentiometer
4L
Symbol Component
A potentiometer is a resistor whose resistance value is variable. From this
way, indirectly, the intensity of current flowing through can be controlled
A circuit can connect in parallel, or control the voltage when connecting it in series.
They are suitable for use as a control element in the devices.
electronics. The user acts upon them to vary the parameters
operating standards. For example, the volume of a radio.
Photocell
4M
Symbol Component
It is a resistor, whose value in ohms varies with changes in the incident light.
A photocell exhibits a low resistance value in the presence of light and
a high resistance value in the absence of light.
30
Buzzer
4N
Symbol Component
The buzzer, or 'buzzer' in English, is an electroacoustic transducer that produces a
continuous or intermittent sound or buzz of the same tone. It serves as a mechanism
of signaling or warning, and are used in multiple systems such as in cars
or in household appliances. Initially, this device was based on a system
electromechanical that was similar to an electric bell but without the metal clapper
which imitated the sound of a bell.
DC Motor
40
Symbol Component
The direct current (DC) motor is a machine that converts electrical energy
gear in mechanics, causing a rotational movement. This current machine
continua is one of the most versatile. Its easy control of position, stop, and speed.
they have turned it into one of the best options in con- applications.
troll and process automation. For example, you can find them in
the traction of battery-operated toy cars or on the wheels of robots.
SEARCH T05AB
LIKE THIS
31
Page left blank intentionally
32
5. PROGRAMMING
Concept of programming
To create a program that the computer interprets and executes the instructions.
escritas en él, debe usarse unLenguaje de programación. En sus inicios los compu-
taders only interpreted instructions in a specific language, of the lowest
level known as machine code, being this excessively complicated for
programming. In fact, it only consists of strings of numbers 1 and 0 (Binary system).
33
Programming language
A programming language is an artificial language.
designed to express operations that can be
carried out by machines such as computers.
They can be used to create programs that control the
5B physical and logical behavior of a machine, for
express algorithms precisely, or as a way of
human communication.
Machine language
Machine code is the system of codes
directly interpretable by a micro-circuit
programmable, like the microprocessor of a com-
computer or a microcontroller. This language is
5C composed of a set of instructions that
determine actions to be taken by the machine
No. These instructions are normally executed
this in sequence, with potential flow changes caused by the program itself
the external events.
Machine language works with two voltage levels. These levels, by abstraction,
action, they are symbolized by zero (0) and one (1), which is why machine language only
use these signs. This allows the use of algebra theories
Boolean and binary system in the design of this type of circuits and in
its programming.
Assembly language
Assembly language or assembler is a language of
low-level programming for computers, my-
microcontrollers, and other integrated circuits program
blessings. Implement a symbolic representation of the
5D machine codes. This representation is usually
defined by the hardware manufacturer, and is based on
mnemonic codes that symbolize the steps of
processing (the instructions).
34
High-level language
A high-level programming language is characterized by
to express the algorithms in a manner suitable for the
human cognitive ability, instead of ability
executor of the machines. 5E
For high-level languages, certain requirements are needed.
programming knowledge to carry out the sequences
logical instruction circuits. High-level languages
were created so that the average user could
solve a processing problem of
data in an easier and faster way.
Algorithm
Structures 5G
Variables
Functions
35
Structures
There are two main functions that every Arduino program must have:
setup() {
5H }
Initial configuration code, it only runs once.
loop() {
}
This function runs after setup(), it keeps running until it
de-energize or disconnect the Arduino.
Variables
It is a piece of data or set of data that changes its value with the execution of the program.
5I
true or false Integer value Store an ASCII
These are some types of variables and the most used in this guide. For more types
visit
[Link]/en/Reference/HomePage
Boolean operators
Generally used within the conditional If
5J &&(y)
If (a||b)
(o)
not
Comparison operators
Usually used within the conditional If and on the For and While
If (a==b)
36
Mathematical operators
They apply to the handling of variables, conditionals, and loops
assign remaining
% multiplication 5L
sum division
int value=value+5
Control structures
They are instructions that allow us to make decisions and perform various repetitions.
according to some parameters, among the most important we can highlight:
If
5M
Switch/case
For
While
Conditionals
Ideal for making decisions after evaluating the logical conditions:
5N
if (input < 500) switch (var) {
{ case1:
// action A // action A
} else break;
{ case2:
// action B // action B
} break;
default:
// action C
}
Cycles
Ideal for repeating what is found within them
for While
50
for(int a=0; a<10; a++ ) while (var<200) {
{ action to repeat
action to repeat var++;
} }
37
Analogous functions
Ideal for reading and writing analog values
EXERCISES
1
Relate the following terms, each letter has three associated terms:
SEARCH T04AB
LIKE THIS
39
5S Quick reference for programming
The Arduino program can be divided into three main parts: the
structure, variables (values and constants) and functions.
V +Constants +Utilities
A byte -sizeof()
- HIGH | LOW
- int
R - INPUT | OUTPUT +Conversion
word
- true | false char()
I long
- Integer constants byte()
A Floating constants
unsigned long
float int()
B Data types word()
double
L void string - char array long()
E boolean - String - object float()
char array
S
40
F
Digital I/O
pinMode() U
digitalWrite() N
digitalRead() C
Random numbers
randomSeed() I
+Analog I/O random()
analogReference() O
analogRead() N
analogWrite() - PWM E
Bits and Bytes
lowByte() S
+Advanced I/O
highByte()
- tone()
bitRead()
noTone()
bitWrite()
shiftOut()
bitSet()
shiftIn()
bitClear()
pulseIn()
bit()
L
EEPROM - read and write I
Ethernet - connect to the Internet B
Liquid crystal - LCD control
SD - reading and writing of SD cards
R
Servo - servo motor control E
- SPI - communication via the SPI bus R
- Step by step - motor control
Í
Wire - send and receive TWI/I2C data
A
... and many more visit S
[Link]
41
Blank page left intentionally
42
6. ARDUINO
Arduino Project
The project started in Ivrea, Italy (the site of the company)
Olivetti computers), in the year 2005 with the aim of creating a
device for students for the integrated control of projects
design and interaction, with the aim of making it more
cheaper than the available prototyping creation systems in
That was then. Starting in May 2011, more than 300,000 units
The Arduino boards have been distributed. The founders Massimo
Banzi and David Cuartielles named the project as Arduino of Ivrea, a prota- 6A
historical nothing of the city. Firstly, 'Arduino' is a masculine term.
Liano, which means 'great friend'.
1 Interview with Ben Fry and Casey Reas by Shiffman Daniel (Sep 23, 2009)see more
43
Arduino Family
Like any respected family, we have members from the oldest to the youngest.
small ones, we invite you to get to know them:
44
Expand Arduino with shields
An Shield is a board that allows expanding functionalities to your Arduino.
with which you can connect motors, or to the cellular network, to a WiFi network, to a network
Ethernet or having an MP3 on the Arduino, among many more just look:
6C
Cell phone Ethernet Proto GPS
SEARCH T01AB
THIS WAY
45
6D Arduino Uno board and its parts
Front view
2 3
5
6 4
1
8
10
11 9
12
13 14
Auxiliary views
46
D
E
1 USB connector for Type AB cable
S
C
2 Reset Button
R
I
3 Digital I/O and PWM pins P
C
4 Green LED on board is on I
Ó
5 Orange LED connected to pin 13 N
14 Entradas análogas
SEARCH T02AB
LIKE THIS
47
6E Installing drivers
3 Connect the Arduino Uno board to the PC and see this warning. We should not pre-
to occupy
48
WINDOWS 7, Vista and XP
6
Access the Find driver software option on the device
49
We received the confirmation of the assigned COM port, this port number
9
COM is very important to keep in mind when programming.
SEARCH T07AB
LIKE THIS
50
Getting to know Arduino software
6F
1 Puerto COM
51
2 Select the board to work on
3 Serial console
6G
1
52
Loading my first program
Before proceeding to the next step, make sure to configure it correctly.
ta:
A–Puerto COM, check Step 1 of section 6F, remember that the value of the
We obtained the COM port in Step 9 of section 6E.
B-Board, review Step 2 of section 6F, remember that for this case the
Arduino UNO board
53
Exercise
Based on the example loaded in the previous section, we now propose that you modify-
fix the program a little, for this in the two lines of code where it says:
delay(1000);
Change the value of 1000 to 2000 and reload the program to your Arduino board.
One, what do you observe?
54
7. BASIC KIT
It is the central resource of this guide, we suggest you get to know it.
so that you have proper management of its components.
Description
The Robotics Store brings the Basic Kit
Arduino includes a careful selection
selection of the best components to get started
in the world of Arduino. It is a tool
designed for beginners that includes
7A
with everything necessary to develop progra-
more that contribute to learning and management
of Arduino.
BASIC KIT 7 6
2 5 7B
1
3 4
1 3 5
1 xArduino Uno 5 x 5mm LEDs 5 xResistance 220Ω
7
1 x USB Type AB Cable
10 x Fast connectors MM
55
Photos
7C
56
8. FRITZING
Software
Fritzing allows designers, artists, researchers, and enthusiasts
document their Arduino-based prototypes and create circuit diagrams
printed circuits for their subsequent manufacturing.
8A
Prototyping board
8B
57
View scheme
8C
8D
58
MASTER EXERCISE
1
In the following exercise, we want you to strengthen your
mounting capabilities in Fritzing, it is an exercise that
requires you to put your abilities to the test
recognition of electronic assemblies and wiring
between the Arduino and a protoboard, it is a summary
practical from the previous chapters. To develop it
we recommend that you work alongside the software and
from the image presented below. You can find the software
en:
[Link]/download/ 8E
DIY
59
9. TUTORIALS
These tutorials are designed with practical exercises that allow for
understand the concept and is constantly verifying what has been learned
through questions. Finally, exercises are proposed that involve ca-
sos of real life.
A
E
F
C
G
D
A Tutorial Title
E Assembly plan
F Assembly diagram
60
G Test your learning
H Programming code
step by step
H
I Very useful tips
J Applied exercises
61
T1 Hello world—flashing LED
Materials
1 1
SEARCH
THIS WAY
62
P
L
A
N
O
E
S
Q
U
E
M
A
P
1—What is the value What does this do? 3—Complete R
of this resistance? function? E
G
digitalRead()
U
_______________ ?
I = ___ N
_____Ω
_______________ R T
_____KΩ
A
63
C /*
Ó -----------------------------------
D Hello World
I -----------------------------------
G Turn on a LED for one second and turn it off for the same amount of time.
0 */
//------------------------------------
D
Main function
E
//------------------------------------
void setup() Executed every time the Arduino starts
{
P
pinMode(13,OUTPUT);// Initializes pin 13 as an output
R
}
O
G
R
//------------------------------------
A
Cyclic function
M
//------------------------------------
A
voidloop() This function keeps running
C
{ when the Arduino is powered
I
digitalWrite(13,HIGH);// Turns on the LED
Ó
delay(1000); Temporizes for a second (1s = 1000ms)
N
digitalWrite(13,LOW);// Turn off the LED
delay(1000); // Waits for one second (1s = 1000ms)
}
64
EXERCISES
1
Starting from the programming code explained above, we want that
a LED lights up intermittently in the following manner:
Tip: 1s = 1000ms
2
The automation company NRJ Inc. hires you to do an assembly of
a blinking LED on an Arduino UNO board, with the only requirement that
the LED must be located on Pin 5, they kindly provided you with
Make the outline, your task is:
A) Carry out the assembly and the respective programming for 2 seconds on.
1 second off
MY NOTEBOOK
65
T2 Turn on an LED with a button
Materials
1
1
1
Yellow LED
Arduino UNO 1
Breadboard
Push button
1
1 4
SEARCH T10AB
LIKE THIS
66
P
L
A
N
O
E
S
Q
U
E
M
A
P
1—What is the value? What is this doing? 3—An example of R
of this resistance? function? a high-level language E
level G
digitalWrite()
U
_______________ _______________ N
_____Ω
_____KΩ _______________ T
A
67
C /*
-----------------------------------
Ó Turn on LED with a push button
-----------------------------------
D
I Press a button and while it is held down
a LED lights up
G
Mechatronics Things and Robotics Store
0
*/
//------------------------------------
D //Declare input and output ports
//------------------------------------
E intpulsador=2; //Pin where the button is located, input
intled=13; //Pin where the LED is located, output
//------------------------------------
P //Main function
//------------------------------------
R void setup() // Executes every time the Arduino starts
{
O pinMode(button, INPUT); // Set the button as an input
G set pin mode for led to OUTPUT;
Configure the LED as an output
}
R
A //------------------------------------
//Cyclic function
M //------------------------------------
A voidloop() // This function keeps running
{ when the Arduino is powered on
C
Conditional to know the state of the button
I if(digitalRead(button)==HIGH)
{
Ó Button pressed
N digitalWrite(led,HIGH);//Turns on the LED
}
else
{
// Normally open push button not pressed
digitalWrite(led, LOW);//Turn off the LED
}
}
//End program
T 1–When you are programming in the Arduino Software, many things that
I you write are reserved words of the language, all reserved words the
P You can find them in the 5S section, when you write them they are placed in a different color.
68
EXERCISES
1
The French multinational Flante, expert in ma-
industrial machinery has hired you to auto-
you are a machine for cutting paper. The condi-
the main action is that the machine operator
when making the cut always keep
both hands occupied, this is a rule of follow-
Industrial safety measures to prevent accidents. The operator must press both buttons.
you do one with each hand and the cutting blade must go down and make the cut. The
The following setup simulates the control of the machine, the two buttons (S1 and S2)
and the red LED simulates the cutting blade.
S1 S2
MY NOTEBOOK
69
T3 Serial reading of a digital input
Materials
1
1
Arduino UNO
Button Breadboard
1
1
4
1K Resistance
USB Type AB Cable
MM connectors
SEARCH T11AB
LIKE THIS
70
P
L
A
N
O
E
S
Q
U
E
M
A
P
1—What is the value? What does this do? 3—This symbol to R
from this resistance? function? what corresponds E
G
[Link]()
U
_______________ N
_____Ω
_____KΩ _______________ _______________ T
A
71
C /*
--------------------------------------
Ó Serial reading of a digital input
--------------------------------------
D Read a digital entry and display on the screen of
I computer (serial console) the state of the button
when oppressed
G Mechatronics Things and Robotics Store
0 */
//------------------------------------
//Declare input and output ports
D //------------------------------------
intboton=2; Pin where the button is located, input
E
//------------------------------------
//Main function
//------------------------------------
voidsetup() // Executes every time the Arduino starts
{
P Configuration
pinMode(button, INPUT); //Set the button as an input
R [Link](9600); Start serial communication
}
O
G //------------------------------------
//Funcion ciclicla
R //------------------------------------
voidloop()// This function keeps running
A { when the Arduino is powered
M Store in an integer variable the value of button 0 or 1
intestado=digitalRead(boton);
A
//Conditional to know the state of the button
C if(state==1)
{
I Pulsed
Pressed //Prints to the serial console
Ó } Pressed
else
N {
Not pressed
[Link]("NO Pressed"); //Prints to the serial console
} NO Pressed
delay(100);//Delay for data visualization in the console
}
//End program
4 100
To remember
5 101
6 110
7 111
To read a digital signal use: digitalRead(pinNumber);
72
EXERCISES
1
A soda company has a system with two
sensors, one of them indicates if the bottle is full-
with the liquid and the other sensor indicates if the bottle
it already has the lid. For this case, we will simulate both.
sensors through two buttons (S1 and S2).
MY NOTEBOOK
73
T4 Serial reading of an analog input
Materials
1
1
1
Arduino UNO
Breadboard
1
10K potentiometer
3
SEARCH T12AB
LIKE THIS
74
P
L
A
N
O
E
S
Q
U
E
M
A
P
1—What is the value? 2—What does this do? 3—This symbol to R
of this resistance? function? what corresponds E
G
analogRead()
U
_______________ N
_____Ω _______________
_____KΩ _______________ T
A
75
C /*
--------------------------------------
Ó Serial reading of analog input
D --------------------------------------
I Read an analogous entry and display it on the screen.
G computer (serial console) the value after turning
the potentiometer
0
Mechatronics Things and Robotics Store
*/
D
E //------------------------------------
Main function
//------------------------------------
void setup() // Executes every time the Arduino starts
{
P [Link](9600);//Starts serial communication
R }
O //------------------------------------
//Cyclic function
G //------------------------------------
R voidloop()// This function keeps running
{ When the Arduino is powered on
A
M Store in an integer variable the value of the potentiometer 0 to 1024
intvalor=analogRead(A0);
A
C //Prints the value of the variable to the serial console
[Link](value);
I
Ó Delay for data display in the console
delay(100);
N
}
//End program
2–To remember
76
EXERCISES
MY NOTEBOOK
77
T5 Serial writing
Materials
1 1
SEARCH T13AB
LIKE THIS
78
P
L
A
N
O
E
S
Q
U
E
M
A
P
1—What is the value What is this doing? 3—Name 4 types R
of this resistance? function? of variables E
G
[Link]() a._______
U
_______________ b._______ N
_____Ω
_______________ c._______ T
_____KΩ
d._______ A
79
C /*
--------------------------------------
Ó Serial writing
--------------------------------------
D It consists of writing on the computer screen (serial console)
I a predetermined letter, the first time it is written is
a LED lights up, if it is rewritten for the second time
G the LED turns off.
//--------------------------------------------------
D //Declares input and output ports and variables
//--------------------------------------------------
E intled=13; //Pin where the LED is located, output
charleer //Variable where the lyrics are stored
boolean isOn=false;//LED state the first time, off
//------------------------------------
P Main function
//------------------------------------
R void setup() // Runs every time the Arduino starts
{
O [Link](9600); Start serial communication
pinMode(led, OUTPUT);//Set the LED as an output
G }
R
//------------------------------------
A //Cyclic function
//------------------------------------
M voidloop()// This function keeps running
{ when the Arduino is powered
A
Save the value of the serial console in a variable
C read=[Link]();
I If it is the letter 'a' and the LED is off
if ((read == 'a') && (turnedOn == false))
Ó {
digitalWrite(led,HIGH);// Turns on the LED
N prendido=true; Update the LED status
}
If it is the letter 'a' and the LED is also on
else if( (read=='a')&&(turnedOn==true) )
{
digitalWrite(led,LOW);// Turn off the LED
prendido=false; Update the LED status
}
}
//End program
T 1–Boolean tables are very useful for understanding the logic of programs.
I Do you know what the AND boolean table is?&&),OR(||)yNOT(!)?
P
S
80
EXERCISES
MY NOTEBOOK
81
T6 Turn on an LED by PWM
Materials
1
1
1
Yellow LED
Arduino UNO
Breadboard
1
1
SEARCH T14AB
LIKE THIS
82
P
L
A
N
O
E
S
Q
U
E
M
A
P
What is the value? What does this do? 3—What are you running to-R
of this resistance? function? put these ope- E
timers G
analogWrite()
&&_______ U
_______________ N
_____Ω ||_______
_____KΩ _______________ T
==_______
A
!=_______
83
C /*
---------------------------------------------
Ó Turn on/Off an LED proportionally
---------------------------------------------
D Program that proportionally turns on a LED when
I it reaches its maximum brightness and starts to fade
proportionally.
G Mechatronics Stuff and Robotics Store
0 */
//--------------------------------------------------
//Declare input and output ports and variables
D //--------------------------------------------------
intbrillo=0; //Brightness variable starts at 0
E intvariacion=5; Increment variable set to 5
intled=9; //Pin where the LED is located, output
//------------------------------------
//Main function
P //------------------------------------
voidsetup()// Se ejecuta cada vez que el Arduino se inicia
R {
pinMode(led, OUTPUT);//Set the LED as an output
O }
G
//------------------------------------
R Cyclic function
//------------------------------------
A voidloop()// This function keeps executing
{ when the Arduino is powered on
M
Analog writing (PWM) to the LED, I write the brightness value
A analogWrite(led, brightness);
C I increase the brightness variable by 5 each time.
brightness=brightness+variation;
I
// Note: PWM ----> 0 - 255
Ó
If the brightness is 0 or 255
N if(brightness==0||brightness==255)
variation = -variation; // The variation becomes negative
delay(30);//Time to increase brightness
}
//End of program
PWM
84
EXERCISES
1 Your mom wants to have adjustable intensity lights in the living room.
from your home, wants low light for the moments when the family
shares to maintain harmony but requires quite a bit of light
when she makes her fabrics, she wants to have control over the intensity-
control the light through a potentiometer and the LED bulb will
light up according to what she graduated. Your task is to carry out the
program that reads an analog input from a potentiometer (0—1023) and
adjust the PWM value (0-255) of an LED, remember to scale the value...map()
MY NOTEBOOK
85
T7 ON/OFF control with potentiometer
Materials
1
Green LED 1
1
Arduino UNO
Breadboard
1
10K potentiometer
1
3
SEARCH T15AB
LIKE THIS
86
P
L
A
N
O
E
S
Q
U
E
M
A
P
1—What is the value 2—What is it for? 3—This symbol a R
of this resistance? the operator? what corresponds E
G
++ U
_______________ N
_____Ω
_____KΩ _______________ _______________ T
A
87
C /*
---------------------------------------------
Ó ON/OFF control with potentiometer
---------------------------------------------
D
I Programa que enciende un LED cuando el valor de la entrada
analog controlled by the potentiometer is at a certain value,
G when this value changes the LED turns off, it is a system with
control ON/OFF
0
Mechatronics Stuff and Robotics Store
*/
D //------------------------------------
Main function
E //------------------------------------
void setup() // Runs every time the Arduino starts
{
[Link](9600);//Starts serial communication
pinMode(13, OUTPUT); // Set pin 13 as an output
P }
R //------------------------------------
O Cyclic function
//------------------------------------
G void loop() // This function keeps executing
{ when the Arduino is powered on
R
Save the value of the analog reading in a variable
A intvalor=analogRead(A0);
M [Link](value);//Prints the value to the console
A If the value is greater than or equal to 500
C if(value>=500)
{
I digitalWrite(13,HIGH);//Turns on the LED on pin 13
Ó }
If the value is less than 500
N else
{
digitalWrite(13, LOW);//Turn off the LED on pin 13
}
delay(100);//Delay of 100ms to see the console data
}
//End program
T
I
You must be very careful when
P
moment when your Arduino board is
S energized, if you look at the palca by the
the bottom has all its points
air welding equipment, if you have it
feed the plate and place it on top
from a metallic surface, for example-
I set a table, it is possible that the plate
it gets damaged because it shorts out. If you are going to
88
EXERCISES
89
T8 Control of an LED with a photocell
Materials
1 1
1
Green LED
Arduino UNO
1 1 Breadboard
Photocell
USB Type AB Cable
1 5
1
SEARCH T16AB
THIS WAY
90
P
L
A
N
O
E
S
Q
U
E
M
A
P
1—What is the value 2—The letter A 3—This symbol a R
of this resistance? keeps in a va- what corresponds E
variable of type: G
U
_______________ N
_____Ω
_____KΩ
T
A
_______________
91
C /*
---------------------------------------------
Ó LED intensity control
---------------------------------------------
D Program that lights up an LED proportionally from
I according to the amount of light that strikes a
photoelectric cell
G Mechatronics Things and Robotics Store
0 */
//------------------------------------
//Main function
//------------------------------------
void setup() // Runs every time the Arduino starts
D {
[Link](9600);//Starts serial communication
E pinMode(9,OUTPUT);//Configure pin 9 as a PWM output
}
//------------------------------------
//Cyclic function
//------------------------------------
P voidloop()// This function keeps running
{ when the Arduino is powered on
R
Store the value of the analog reading in a variable
O photoelectric cell
intfoto=analogRead(A0);
G
Check the maximum value and perform a conversion
R intconversion=780-foto;
A //Conditional to establish an absolute value
if(conversion<0)
M conversion = conversion * -1; // Multiply by -1 because it is negative
T 1–Think for a moment about where you can find a photocell, these are
I some examples:
P
S
Calculator Digital camera
Charge battery and turn off Check if there is a need
of the automatic dad with flash photo
92
EXERCISES
We propose that you create a sensor to measure the light radiated by a body.
1
bright. We are going to have two teams, A and B. Team A is going to have a
LED that varies its brightness according to the position of a potentiometer (See
T6) y en el equipo B va estar comandado por una fotocelda. La idea es que
bend the LED and the photoresistor at 90°, place them as close together as possible and the
you face each other, as if both were going to look at each other head-on. You must show through the serial console-
Read the light of the photocell, this reading should be in percentage from 0% to 100%. A
Light concentration 57%
LED 1
LED 2
Light sensor
S1 S2
93
T9 Pulse counter
Materials
1
1
1
Yellow LED
Arduino UNO 1
Breadboard
Button
1
1 4
SEARCH T17AB
LIKE THIS
94
P
L
A
N
O
E
S
Q
U
E
M
A
P
1—What is the value 2—What are the 3—This symbol a R
of this resistance? states of a what corresponds E
boolean variable? G
U
a._____________ N
_____Ω _________
_____KΩ b._____________ T
A
95
C /*
---------------------------------------------
Ó Pulse counter
---------------------------------------------
D Program that displays the number on the screen (serial console)
I the times the button has been pressed, an action is performed
process that lights up according to the number of pulses
G a LED,
[Link]/forum
96
EXERCISES
B1 B2
Button
97
T10 Magnetic switch for a visual alarm
Materials
1
1
1
Green LED
Arduino UNO 1
Breadboard
Reed switch
1
1 4
SEARCH T18AB
THIS WAY
98
P
L
A
N
O
E
S
Q
U
E
M
A
P
1—What is the value 2—What does it receive the 3—This symbol to R
of this resistance? functiondelay()? what corresponds E
G
U
_______________ N
_____Ω
_____KΩ ____________ T
A
99
C /*
---------------------------------------------
Ó Magnetic switch - Thief in the house
---------------------------------------------
D Program that generates a visual alarm with an LED
I from a magnetic field generated from a
reed switch.
G Mechatronics Stuff and Robotics Store
0 */
//--------------------------------------------------
//Declare input and output ports and variables
//--------------------------------------------------
D
intcontacto=2;//Pin asignado al reed switch
E intled=13; //Assigned pin to the LED
//------------------------------------
//Main function
//------------------------------------
void setup() // Executes every time the Arduino starts
P {
pinMode(contacto, INPUT);//The reed switch as an input
R pinMode(led, OUTPUT); The LED as an output
O }
G //------------------------------------
//Circular function
R //------------------------------------
voidloop()// This function keeps running
A { when the Arduino is powered on
M If the magnet approaches the reed switch
if(digitalRead(contact)==LOW){
A
//For loop that goes from 0 to 50, the counter is
C in variable a, it repeats while a is
less than 50
I for(int a=0; a<50; a++) {
Ó digitalWrite(led,HIGH);//Turns on the LED
delay(50);//Time
N digitalWrite(led, LOW);//Turn off the LED
delay(50);//Time
}
If the magnet is far from the reed switch
else {
digitalWrite(led,LOW);//Keeps the LED off
}//End of if
}
End of the program
100
EXERCISES
Siren
Buzzer
Reed
MY NOTEBOOK
101
T11 RGB LED supported by color table
Materials
1 1 1
Arduino UNO
LED RGB Breadboard
1 3 5
SEARCH T19AB
LIKE THIS
102
P
L
A
N
O
E
S
Q
U
E
M
A
P
1—What is the value? 2—The word 'pulse' 3—This symbol to R
of this resistance? it should be kept in what corresponds E
a variable of type: G
U
_____Ω _______________ ____________ N
_____KΩ
T
A
103
C /*
---------------------------------------------
Ó LED RGB - Color Table
---------------------------------------------
D Program that makes use of a function called
I color to generate various shades in
an RGB LED
G Mechatronics Stuff and Robotics Store
0 */
//--------------------------------------------------
//Declare input and output ports and variables
//--------------------------------------------------
D intledRojo=9;//Declara Pin LED Rojo
intledVerde=10;//Declara Pin LED Verde
E intledAzul=11;//Declara Pin LED Azul
//------------------------------------
//Main function
//------------------------------------
void setup() // Executes every time the Arduino starts
P {
pinMode(ledRojo,OUTPUT);//El LED Rojo como una salida
R pinMode(ledVerde, OUTPUT);//The Green LED as an output
pinMode(ledAzul,OUTPUT);//The Blue LED as an output
O }
G //------------------------------------
//Cyclic function
R //------------------------------------
voidloop()// This function keeps running
A { when the Arduino is powered on
M Call to the Color function that receives
1st position: Red Intensity
A 2nd position: Green Intensity
3rd position: Blue Intensity
C
color(255,0,0); Red
I delay(1000);
Ó color(0,255,0); Green
delay(1000);
N
color(0,0,255); Blue
delay(1000);
color(255,255,255);//White
delay(1000);
color(255,0,255);//Magenta
delay(1000);
color(255,128,0);//Naranja
delay(1000);
}
//------------------------------------
//Color function
//------------------------------------
void color(int red, int green, int blue) {
PWM writing of the Red color
analogWrite(ledRed, 255-red);
PWM writing of the Green color
analogWrite(ledVerde,255-verde);
Writing of PWM for the Blue color
analogWrite(blueLed, 255 - blue);
}
End of program
104
EXERCISES
1 The lighting of the reception of the Mancherie Hotel is something special because of
according to the mood of the owner Mr. Martin, the light should be configured in
a RGB scale. The light operator is very skilled as he is in charge of receiving
the colors desired by Mr. Martin, so the worker is responsible for writing
through Serial Console a RGB value separated by commas (,). So you must develop
a program that takes this information and applies it to an RGB LED, takes as
Outline page 103. For example, if it is sent:
2 The worker from the previous point is going on vacation, which is why Mr. Martin is very upset because not...
He understands very well how to write the color separated by commas. That's why the worker came up with a
great idea, place two buttons:
Button 1 (S1) allows you to choose a color from a menu (Red, Green, or Blue) each time it is pressed.
The text on the Serial Console that says the color must change, must have debounce so that for each
pulse given, just change a color. An example of the pulse given and the value displayed on the console:
Button 2 (S2) allows you to vary the color from 0 to 255 of the last assigned color name.
the Button 1 (S1). While S2 is pressed at intervals of 100ms (milliseconds), the variable of
that color should increase by one. The name of the Color should be printed (last state)
due to S1) and then the numerical value of the color, finally the result should progressively be reflected
I already in the RGB LED. For example, according to the last state of the Pulse 4 and then S2 is pressed:
Assistance to solve it: For each color, you need to create two variables, one to store the name of the color and
the other to store the numerical value that you have for that color.
LED RGB
S1 S2
105
T12 Motor ON/OFF control
Materials
1
1
1
NPN Transistor
Arduino UNO
1
1 Breadboard
Button
USB Type AB Cable
1 2 8
1K resistor MM Connectors
DC Motor
SEARCH T20AB
LIKE THIS
106
P
L
A
N
O
E
S
Q
U
E
M
A
P
1—What is the value? If I want to save 3—This symbol to R
of this resistance? the number π that what corresponds E
type of variable G
Should I use?
U
_____Ω N
_______________ _______________ T
_____MΩ
A
107
C /*
---------------------------------------------
Ó Motor ON/OFF control
---------------------------------------------
D Program that makes use of a motor and a button,
I while pressed, the motor must
be turned on (ON) otherwise it must be
G off
D //--------------------------------------------------
//Declares input and output ports and variables
E //--------------------------------------------------
intpulsador=7;//Declares Pin of the button
intmotor=3; //Declare Motor Pin
P //------------------------------------
Main function
R //------------------------------------
void setup() // Executes every time the Arduino starts
O {
pinMode(button, INPUT); //The button as an input
G pinMode(motor, OUTPUT);//The motor as an output
}
R
A
//------------------------------------
M //Cyclic function
//------------------------------------
A voidloop()// This function keeps running
{ when the Arduino is powered on
C
I If the button is pressed
if(digitalRead(button) == HIGH) {
Ó
digitalWrite(motor,HIGH);//Turns on the motor
N
}else{//if the button is not pressed
digitalWrite(motor, LOW); //Turns off the motor
}
}
// End program
108
EXERCISES
MY NOTEBOOK
109
T13 PWM control of a motor
Materials
1
1
1
NPN Transistor
Arduino UNO 1
Breadboard
DC Motor
1
1 5
SEARCH T21AB
LIKE THIS
110
P
L
A
N
O
E
S
Q
U
E
M
A
P
1—What is the value 2—How do you write- 3—This symbol to R
of this resistance? I am the following what corresponds E
numbers in binary? G
U
7=______ N
_____Ω
_____KΩ 5=______ _______________ T
2=______
A
111
C /*
---------------------------------------------
Ó PWM control of a motor
---------------------------------------------
D Program that uses an engine and the Console
I Arduino serial has the possibility of
set the engine to 5 different speeds,
G from the PC keyboard you can send him the
desired speed. The 5 speeds are configured
0 with 5 different PWMs.
Mechatronics Stuff and Robotics Store
*/
D //--------------------------------------------------
//Declare input and output ports and variables
E //--------------------------------------------------
intmotor=3; Declare Motor Pin
//------------------------------------
//Main function
//------------------------------------
P void setup() // Executes every time the Arduino starts
{
R [Link](9600);//Starts the serial communication Arduino-PC
}
O
//------------------------------------
G //Cyclic function
//------------------------------------
R voidloop() // This function keeps running
{ // cuando este energizado el Arduino
A
If there is any value in the Serial Console
M if([Link]()){
A //Variable where the character sent from the keyboard is stored
chara = [Link]();
C
If the entered character is between 0 and 5
I if(a>='0'&&a<='5'){
Ó Variable to scale the input value to PWM range
intspeed=map(a,'0','5',0,255);
N PWM writing to the motor
analogWrite(motor,speed);
//Mensaje para el usuario
The motor is turning at the speed
[Link](a);
}else{// If the entered character is NOT between 0 and 5
//Mensaje para el usuario
Invalid speed
[Link](a);
}
}
}
//End program
112
EXERCISES
The latest news from the Appliance Fair has left everyone greatly re-
2
name a blender that incorporates two buttons through which it is
It is possible to vary the 10 different speeds (0 to 9) at the time of blending.
All of this is supported from the serial console where it is possible to see the Speed
chosen, the buttons are adjusted as follows:
Button 1 (S1) allows you to choose a speed from a menu (Speed 0 … Speed 9), each time
when S1 is pressed, the text on the Serial Console that says the speed must change, it must have debouncing
so that for each pulse given only one speed changes. An example of the pulse given and the value shown
through the console:
Button 2 (S2) when pressing S2 for the first time, the motor should start spinning at the chosen speed.
In front of S1, if S2 is pressed a second time, the motor must stop. It should be displayed on the serial console.
Blender spinning at speed x or Blender stopped as appropriate.
S1 S2
113
T14 Generate tones with a buzzer
Materiales
1 1 1
1 1
SEARCH T22AB
LIKE THIS
114
P
L
A
N
O
E
S
Q
U
E
M
A
P
1—What is the value 2—Elswitch...case 3—This symbol to R
of this resistance? it's a type of what corresponds E
G
U
_____Ω N
_______________ _______________
_____MΩ
T
A
115
C /*
---------------------------------------------
Ó Generate tones with a buzzer
---------------------------------------------
D Program that uses a buzzer and a
I potentiometer, the idea is to generate various tones
in the buzzer from the analogous state of
G potentiometer. Additionally, the function is used
tone that is very useful for generating various melodies
0 Mechatronics Stuff and Robotics Store
*/
//--------------------------------------------------
D //Declare input and output ports and variables
//--------------------------------------------------
E intbuzzer=9;//Declara Pin del buzzer
inttono=0; //Declare potentiometer pin
//------------------------------------
Main function
P //------------------------------------
void setup() // Executes every time the Arduino starts
R {
No initial parameters are set, but they should be.
O // place the header of the setup() function
}
G
R //------------------------------------
Cyclic function
A //------------------------------------
voidloop() // This function keeps running
M { when the Arduino is powered
A Integer variable where the value of the potentiometer is stored
intsensor=analogRead(tono);
C
Variable where the frequency is scaled from 100 to 5000Hz
I intfrecuencia=map(sensor,0,1023,100,5000);
Ó Integer variable to store the desired time in ms
intduracion=250;
N //Function tone(), which receives:
1st position: Sound element pin
2nd position: Desired frequency in Hz
3rd position: Tone duration
tone(buzzer, frequency, duration);
Delay
delay(100);
}
//End program
T 1–If you are having difficulty learning the colors of the resistors, a little help
I online wouldn't hurt you at all ;) inWolfram Alphacan you do this:
P
S
116
EXERCISES
Buzzer
S1 S2
MY NOTEBOOK
117
A Hello, we want to know your
P
O opinions regarding this ma-
R material, they are very helpful with the
T the spirit of continuing to improve
E
S contents and making them
clearer.
Write to us at:
sales@[Link]
Thank you very much for your contributions :D
118
Basic Guide to Arduino
9 Chapters of content
Contáctenos
[Link]
ventas@[Link]
tdrobotics
tdrobotics