Built-in LED
rduinoincorporatesaledontheboardconnectedtopin13througharesistor.Thuswe
A
can make afirsttestoftheoperationoftheboardwithouthavingtoconnectanyother
component to the Arduino board.
Built-inLED equivalent circuit:
ractice 1: Built-in LED blink
P
Pin 13 will work as an output. The program makes the built-in LED,
so that it remains ON for a time and another time OFF. Delay time
will determine the blink rate, you can try different values to see the difference in
blink speed. This process is repeated infinitely within the main "loop".
ractice 2: LED block
P
The “Led” block of the actuators works exactly the same as the “Write Digital” block.
We can use it in the same way.
hefollowingprogrammaketheledconnectedtopin13blinkusingavariabletosetthe
T
delay time and using the “Led” block.
LED sequence
ewillconnect5ledstotheArduinoboardonpins2,3,4,5,6byconnectinga220Ω
W
resistor before the ledanode(longpin)andallthecathodesoftheleds(shortpin)will
be joined directly to GND.
ractice 3: odd and even
P
Switch on even leds and then odd leds after a while and start over again.
ractice 4: move a light
P
Switchonaledconsecutivelyoneaftertheotherfromlefttoright(onlyoneledonata
time). When the last one is reached, the sequence begins again.
ractice 5: all on one by one and all off one by one
P
Switch on the leds consecutively one after the other fromlefttorightuntiltheyareall
on.Thentheygooffconsecutivelyinthereverseorderthattheywereswitchedonuntil
they are all off and start over.
ractice 6:A single led lights from side to side
P
A single led lights from side to side.
ractice 7:from the outside to the inside
P
Make a sequence of leds so that the leds light up from the outside to the inside and
then vice versa. The speed can be adjusted by changing the value of a variable.
ractice 8:functions
P
Functions allow us to group blocks under a singlenameandtobeabletoexecuteall
the blocks simply by adding the block to call the function.
Definition of a function: Function calling:
reate a function “all off” to start switching off all the leds, another function for
C
“sequence 1”, another for “sequence 2” and one for “sequence 3”. In the main loop
execute 3 times the sequence 1, 4 times sequence 2 and 5 times sequence 3
continuously.
PWM outputs
ulse width modulation (also known as PWM) of a signal or power source is a
P
techniqueinwhichthedutycycleofaperiodic(cyclic)signalismodified,tocontrolthe
amount of energy sent to a load. With this technique we can control the intensity of
LEDs, speed of motors, etc.
This is the way Arduino generates a pseudo-analog signal on its outputs from digital
pulses to vary the energy that it sends through the relevant pin.
Charts of PWM from 0% to 100% of intensity Pins that allow PWM operation are marked with the symbol ~
Connect a LED to pin 3 as follows:
The values that we can write to the pin using PWM are from 0 to 255 (from 0 to 100%):
ractice 9:Switching on a led in several steps ofintensity
P
Switching on a led in several steps of intensity.
Calculate the table with the values to write to the PWM output pin
ractice 10:Switching on and off a Led slowly
P
SwitchingonandoffaLedslowly:Thevaluesfrom0to255willbewrittenonpin3so
that theLEDilluminatesslowly.Whenreaching100%(255)theprocesswillbecarried
out in reverse until reaching 0.
Modify the delay value to speed up or slow down the process.
ractice 11: Random intensity
P
Make a program that changes the intensity of the led randomly every second.
ractice 12: Contrary leds
P
Make a program with two leds connected to PWM pins, when the intensity of one
increases the other decreases. Leds connected to pins 3 and 5.
ractice 13: light increases and decreases progressively
P
Connect 3 leds to pins 9,10 and 11.
Carry out a sequence where intensity of the LEDs increases and then decreases
progressively.
RGB LEDs
RGB LED is actually the union of three LEDs of the basic colors, in a common
A
encapsulation, sharing the cathode (-) or the anode (+).
Example of connection of a common cathode RGB LED to pins 9~, 10~ and 11~:
e can control the RGB led in several different ways from ArduinoBlocks:
W
-Digital outputs (ON/OFF):
-Analogue outputs (0…255):
-RGB Led block (actuators):
ractice 14: on/off control of RGB Led
P
Withtheuseofblockstocontroldigitaloutputs(ON/OFF)wewillshowthecolorgreen,
red, blue, yellow (R+G), white (R+G+B) and black (all off) for 1 second each color.
(RGB LEDs of common cathode).
ractice 15: PWM control of RGB Led
P
We will progressively increase the intensity of Red, then Green and finally Blue
(Common cathode RGB LED)
ractice 16:RGB Led blockcontrol
P
Sequential change of colours.
ractice 17:Random colour
P
Random colour change every 2 seconds.
ractice 18:Random colour with smooth transition
P
GeneratesrandomRGBcoloursandsmoothlytransitionsfromthecurrentcolourtothe
next.
Digital inputs
rduino incorporates multiple pins that canfunctionasinputoroutput.Let'strytouse
A
them as input to read data from external sensors.
ThedigitalinputsallowreadinganON/OFFvaluedependingonthevoltageappliedto
the relevant pin.
If a voltage less than 2v is applied, a LOW value will be read (a logical "0")
If a value greater than 3v is applied, a HIGH value will be read (a logical "1")
The diagram to connect a push-button/switch to an Arduino digital input is:
In most cases we can use a push-button module that incorporates the resistor and
simplifies the connections:
ome of these modules are connected inaninvertedwaytothepreviousscheme,so
S
thattheinputwillbeactive(HIGH/ON)atrestandwillbedeactivated(LOW/OFF)when
pressed.
The blocks used to read the value of a digital input or a button are:
ush button or sensor with HIGH/ON output
P ush button or sensor with LOW/OFF output
P
when pressed or active when pressed or active
ractice 19: LED on by pressing button
P
We will turn on a led while the button is pressed, otherwise the led will remain off.
ractice 20: switch LED by pressing button
P
Turn a led on and off with a single button.
hat is the "repeat while..." block for?
W
What happens if we remove it?
ractice 21:Two-button intensity control
P
Wewillconnecttwobuttonsandaled.Onebuttonwillincreasetheintensityoftheled
and another will decrease it.
ractice 22:led control with a clap
P
We will use a sound sensor with digital output (0/OFF without sound and1/ONwhen
sound is detected).
The program will turn on the led for 5s when a loud sound is detected.
Check the sensor pins. You must connect the digital output D0 of the sensor to pin 8.
ractice 23:PIR motion detector
P
ThePIR(PassiveInfrared)motionsensorreactsonlytocertainenergysourcessuchas
humanoranimalbodyheat.Theyreceivethevariationoftheinfraredradiationfromthe
environment it covers. It is called passive because it does not emit radiation, but
receives them. These capture the presence by detecting the difference between the
heat emitted by the human body and the space around it.
reate a program that turns on a led connected to pin 3 for 5s when it detects
C
movement.
We will connect the PIR motion sensor to pin 7.
Serial communication
rduino incorporates a serial connection that allows connection with the PC (or with
A
many other devices).
This is the same connection used to upload the program to the Arduino. Using this
connection we can send information from Arduino to the PC and vice versa.
odisplaytheinformationreceivedonthePCandbeabletosenddatatotheArduino
T
the easiest is to use a serial terminal or serial console.
erialconsoleallowsdatatobesentfromthePCtotheArduinoboard.Andviceversa,
S
it allows to receive and visualise the data received from the Arduino board.
ractice 24:Sending messages from Arduino
P
The program will send text messages from Arduino, to displaythedatareceivedfrom
the serial connection we will use the console that ArduinoBlocks incorporates.
ractice 25:Display the value of a variable
P
We are going to see how to send the value of a variable. In this case, the variable
increases and its value is shown in the serial console.
ractice 26: Switch on a LED from the PC
P
Upon receiving the value 1 from the console we will turn off the led, upon receiving
value 2 we will turn it on.
ractice 27:LED intensity regulation from PC
P
Connect a led to pin 3. Receive a number from the computer through the serial port.
The number received must be between 0 and 255 and will be written to the analog
output (PWM) pin 3.
ractice 28: Guess the number
P
We willplayagamewheretheArduinoboardwill"think"arandomnumberbetween1
and 100.
From theconsolewewillsaynumbersanditwilltellusifthesecretnumberisgreater
or less until it is guessed and shows us the total number of guesses we've used.
Analog inputs
rduino incorporates 6 pins that can function as analog inputs. Let's try how we can
A
read data from external sensors through them.
The analog inputs allow you to read the voltage that is applied to itasaninput.That
voltage may varybetween0and5v.Thevoltagevaluereadisconvertedtoanumeric
value between 0 and 1023.
Blocks to read an analog input:
The reading blocks of an analog input will return a value between 0...1023.
Input voltage Read value
0 V 0
2.5 V 512
5 V 1023
nother interesting block is“map”,whichallowschangingtherangeofthevalueread.
A
Forexample,ifIwanttochangetherangereadfrom0...1023to0..100Ican"map"itas
follows:
ractice 29: Read the position of a potentiometer
P
Wewillreadthevalueoftheanaloginputwherethepotentiometerisconnectedandwe
will send it to the serial console to be able to visualize it on the PC.
ractice 30:Regulate the intensity of led withapotentiometer
P
We will read thevalueofapotentiometer(0...1023)andmapittoaproportionalvalue
between 0 and 255 to regulate a led connected to pin 3 as a PWM output.
ractice 31:Measure the ambient light
P
WewillconnectanLDRresistortotheanaloginputA0,throughwhichwewillmeasure
the level of ambient light detected.
Wewillsendthevaluereadthroughtheserialconnectiontoviewitontheserialconsole
every 2s.
ractice 32: Joystick
P
The“joystick”typemodulesforArduinoaremadeupoftwopotentiometers,oneforthe
movementoftheXaxisandanotherforthemovementoftheYaxis.Alsointhesame
module they usually incorporate a button.
GND / - GND = 0V
VCC / 5V / + VCC = 5V
Vrx / X X axis potentiometer (to analog pin)
Vry / Y Y axis potentiometer (to analog pin)
SW Button (to digital pin)
ractice 33: Control of two leds with joystick
P
Connectajoystick(X=>A0/Y=>A1)andtwoledstopins6and7respectively.Aled
willvaryitsintensitywiththeXaxisandanotherwiththeYaxis.Thetwoledsmustbelit
at about half intensity when the joystick is at rest..
Relay
relay is an electromagnetic device. It works like a switch controlled by an electric
A
circuit in which, by means of a coil and an electromagnet, a set of one or more
contacts can open or close other independent circuit.
he connection of the relay will be made through a digitaloutput,sinceweonlyhave
T
two states ON/OFF (relay active or not).
The most common is to connect a relay module thatsimplifiestheconnectionsinceit
alreadyhasalltheelementsnecessaryfordirectconnectionoftherelaytoArduino.We
only have to connect 5V (VCC), GND and the signalrelayactivationinputthatwillbe
connected to the corresponding Arduino pin.
Ifweuseanordinaryrelay,connectoneofthepinsofthecoiltoGNDandtheotherpin
of the coil to the corresponding Arduino pin to control the relay.
ToactivatetherelayintheArduinoprogramwewillsimplyusetheinstructiontoactivate
the pin digitally connected to the relay module (ON/OFF)
Intheoutputsectionwehavearelaymodulethatinternallyperformsthesamefunction
as the “write digital” instruction,
These two instructions perform the same function: Activate the output of pin 2.
ractice 34: intermittent relay
P
Carry out an assembly with the relay module connected to pin 8, we will carry out a
program that activates and deactivates the relay in periods of a 1s.
ractice 35: Control of relay from PC
P
We will turn on/off the relay connected to pin 8 through the serial port console.
1 = turn on / 2 = turn off / 3 = blink