Plusivo Nano Starter Kit Guide
Plusivo Nano Starter Kit Guide
Nano R3
Super Starter Kit
Table of Contents
Lesson 1: Installing the Arduino IDE 5
1.1 Introduction 5
1.2 Installing Arduino (Windows) 6
1.3 Installing Arduino (Linux) 9
1
2
3
4
1.1 Introduction
The software of the Arduino device is called The Arduino Integrated Development
Environment (IDE).
First, you will learn how to properly setup your computer to use your Arduino device and
install the software. The IDE that will be used to program your Arduino is available for Windows,
Linux and Mac, but the installation is not the same for all three.
5
Click on JUST DOWNLOAD.
6
Click on Next.
7
Click on Install.
When the setup is finished, click on Close.
8
Click on JUST DOWNLOAD.
9
Find the file that you downloaded, Right Click on it, and extract the files in a folder of your
choice.
Right click on the folder and containing the files extracted and go to Properties.
10
You can close the terminal.
11
12
When the library manager opens, a list of installed libraries will be displayed as well as
those libraries that are ready for installation. Take for example the installation of the Bridge
library. Look for it in the list and then install the version of the library that you prefer. There are
times that version selection menu does not appear, but that is normal.
13
Click “Install” and wait for the new library to be installed. Once the process has finished, you can
see an INSTALLED tag next to the Bridge library.
The new library can now be located inside the Include Library menu. You can open a new
issue on Github, if you wish to add your own library.
14
In the Arduino IDE, click Sketch menu > Include Library. Select "Add .ZIP Library''.
Next step is you will need to select the library you would like to add. Go to the .zip file's
location and open it.
15
Go back to the Sketch > Import Library menu. The library can now be seen at the bottom
of the menu, indicating that it is ready to be used. The zip file will be present in the libraries
folder in your Arduino sketches directory.
NB: The Library will be available to use in sketches, but examples will be displayed in the
File > Examples only after the IDE restarts..
Those two are the most common method, with MAC and Linux systems being handled
similarly. You can also proceed to manual installation, as presented below, but it is seldom used
and you may skip it.
16
My Documents\Arduino\libraries\ArduinoLove\ArduinoLove.cpp
My Documents\Arduino\libraries\ArduinoLove\ArduinoLove.h
My Documents\Arduino\libraries\ArduinoLove\examples
Documents/Arduino/libraries/ArduinoLove/ArduinoLove.cpp
Documents/Arduino/libraries/ArduinoLove/ArduinoLove.h
Documents/Arduino/libraries/ArduinoLove/examples
There are often more files other than the .cpp and .h, you just have to ensure that they can
be found there (if you put the .cpp and .h files directly into the libraries folder, the library may
not work or if they're inside an extra folder).
For example:
Documents\Arduino\libraries\ArduinoLove.cpp and
Documents\Arduino\libraries\ArduinoLove\ArduinoLove\ArduinoLove.cpp won't work.
Restart the Arduino application. The new library should now appear in the
Sketch->Import Library menu item of the software. Congratulations, you’ve installed a library!
17
18
Choosing which port to open in the Serial Monitor is very similar to choosing a port for
uploading your code. To do that, go to Tools -> Serial Port, and select the adequate port.
Tips: Select the same COM port that you have in your Device Manager.
19
2.8 Settings
There are limited settings on the Serial Monitor, but enough to handle most of serial
communication needs.. The baud rate is the first setting you can change. Choose the correct
baud rate (9600 baud) using the drop-down menu on selecting baud rate.
You can also set the terminal to Autoscroll by checking the box beside the Autoscroll
option.
Pros
The Serial Monitor is a quick and easy way to create a serial connection with your Arduino.
But if you are already working in the Arduino IDE, there’s no need to set up a separate terminal
to display data.
Cons
There is a lack of settings in the Serial Monitor, so it is not suitable for advanced serial
communications.
20
Lesson 3: Blink
3.1 Overview
This chapter will teach you to program the Nano R3 controller board to blink the built-in
LED in your Arduino and the fundamental steps in downloading programs.
3.3 Principle
You can find in the Nano R3 board, along both sides, the rows of connectors. These are
used to connect to several electronic devices and plug-in 'shields' that extend its capability. It
also has a single LED, built onto the board, that you can control from your sketches,. This is
often referred to as the 'L' LED, same as how it is labelled on the board.
When you connect your board to a USB plug, you should see that the 'L' LED already
blinks, as the 'Blink' sketch is generally pre-installed on the board.
In this chapter, we will rewrite our own Blink sketch and then change the rate at which it
blinks.
In the first lesson, we covered how to set up your Arduino IDE and ensure that you will be
able to find the right serial port for it to connect to your Nano R3 board. Now it is time to test that
connection and program your Nano R3 board.
21
The Arduino IDE consists of a large collection of example sketches that you can load up
and use, including an example sketch on how to make the 'L' LED blink.
In the IDE’s menu system folder, go to File >Examples > 01.Basics and load the 'Blink'
sketch.
22
The example sketches are 'read-only', you can change them but cannot be saved as the
same file. Thus, first you have to save your own copy as a new file since we are going to
change this sketch.
23
On the Arduino IDE File menu, select 'Save As..', and save it with filename 'MyBlink'.
24
You have just saved your copy for blinking the ‘L’ LED as ‘MyBlink’ in your sketchbook,
and when you want to use it you can just open it from the File > Sketchbook menu option.
Using USB cable, connect your Arduino board to your computer and check that both the
'Board Type' and 'Serial Port' are set correctly.
25
Note: The Board Type and Serial Port shown here may appear differently as the Serial
Port displayed for everyone is different. COM110 that is shown here may be COM3 or COM4 on
your computer. The right COM port should be COMX (arduino XXX), chosen according to the
certification criteria. Also, if you are using 168, the Board type you will have to choose is Mega
168, or choose the type adequate to your situation.
26
The current settings for the board are shown at the bottom corner of the window.
On the status area of the IDE, a progress bar and a series of messages will be shown. It
will say 'Compiling Sketch...' in the beginning, indicating that it is converting the sketch into a
suitable format for uploading to the board.
Next, the status will become 'Uploading'. During this time, the sketch is being transferred
and it would cause the LEDs on the Arduino to start flickering
There is also a message that says that the sketch is using 1,052 bytes of the 30,720 bytes
available. After the 'Compiling Sketch..' stage, the following error message may appear:
27
This can mean that your board is not properly connected, that the necessary drivers have
not been installed or maybe you selected a wrong serial port. If you encounter this error, you
should revisit Lesson 10. Once the upload is complete, the board should restart and start
blinking.
Please note that a big part of this sketch is composed of comments. These are not actual
program instructions, but an explanation on how the program works. A block comment is
everything between /* and /*, and a single line comment is the one that starts with //. Everything
up until the end of that line is considered a comment.
Below is the first line of code:
This code is giving a name to the pin where the LED is attached to. On most Arduinos,
including the Nano, UNO and Leonardo, this is pin 13. Then, we have the 'setup' function. This
is executed when the reset button is pressed, and also whenever the board resets for any
reason or after a sketch has been uploaded.
28
Inside the loop function, the commands first turn the LED pin on (HIGH), then 'delay' for
1000 milliseconds (1 second), then turn the LED pin off (LOW) and pause for another second.
If you want your LED to blink twice as fast, change the value from 1000 to 500
milliseconds. The setting would then pause for half a second each delay rather than a whole
second.
Upload the sketch again and your LED should now start to blink more quickly.
29
Lesson 4: LED
4.1 Overview
Here, you will learn how to change the brightness of LED using different values of resistors.
In a breadboard, you can prototype your circuits quickly without the need to solder the connections. You
can see below an example of a breadboard.
30
Some breadboards have two strips of holes (also called rails) running along the long edges of the board
separated from the main grid, with strips running down the length of the board inside that enable you to
connect at a common voltage. They are usually for +5 volts and ground.
While breadboards are great for prototyping, they have some limitations due to potential poor
connections. Because the connections are temporary, they are not as reliable as soldered connections.
LED
LEDs are great for making indicator lights as they use very little electricity and they last longer. In this
lesson, the most common of all LEDs will be used: a 5mm-diameter red LED (5mm LED). You cannot connect
it directly to a battery or voltage source, as the LED has a positive and a negative lead and won’t light if
connected the wrong way and a resistor must be used with it to limit the current flowing through it so that it
won’t burn out.
Not using a resistor with LED will instantly destroyed it, as the excessive current flowing through will harm
the 'junction' where the light is produced.
There are two ways to identify the two leads (positive and negative) of the LED. First, the longer lead is
the positive lead. Second, you can see a flat edge to the case of the LED where the negative lead enters the
body of the LED. If you have LED with flat side next to the longer lead, it can be assumed that the longer lead
is positive.
31
RESISTORS
Resistors resist the flow of electricity, which means higher value of the resistor means that the more it can
resist and less electrical current will flow through it. We use these to control how much electricity flows through
the LED and eventually, how brightly the LED will shine.
In this lesson, three different values of resistor will be used: 220Ω, 1kΩ and 10kΩ. These have the same
appearance but have different colored stripes on them, which indicates the value of the resistor. The color code
of a resistor is a three-colored stripes and a gold stripe at one end as seen below.
32
Resistors, unlike LEDs, can be connected either way as they don’t have a positive and negative lead. If you
find this colored lines method too complicated, you can use a digital multimeter to determine the resistance
value of a resistor.
4.4 Connection
Schematic
33
Wiring diagram
Plug your Nano (a convenient source of 5 volts) into your computer as this will be used to provide power
to the LED and the resistor. The LED should be quite bright with the 220 Ω resistor in place. Alternatively, by
using a 1kΩ resistor, the LED will appear a little dimmer. Finally, when you use the 10 kΩ resistor, the LED will
be barely visible. To notice the difference, you can use the red jumber as a switch by pulling the red jumper
lead out of the breadboard, touching it into the hole and immediately removing it.
Now, you have the 5V going to one leg of the resistor, then the other leg of the resistor going to the
positive leg of the LED and the other leg of the LED going to GND. If we reposition the resistor and place it
after the LED, as shown below, the LED will still light, because it won’t matter where we put the resistor, on
either side of the LED, as long as it is there.
34
5.1 Overview
These RGB LEDs are a fun and easy way to add some color to your projects. Using them is easy and
connecting them is pretty much the same because they are just like 3 regular LEDs in one and they mostly
come in Common Anode or Common Cathode versions.
If you take a look at RGB (Red, Green and Blue) LEDs, they look just like regular LEDs. But there are
actually three LEDs inside the usual LED package, one of each of the primary colours (red, green, blue). You
can mix any color that you want by controlling the brightness of each of the individual LEDs.
How we mix colors of the LED is by adjusting the brightness of each of the three LEDs, just like how you
mix paint on a palette. Or to use different value resistors or variable resistors, which is the harder way and a lot
of work. Fortunately, the analogWrite function of Nano R3 board can be used to output a variable amount of
power to the appropriate LEDs.
35
COLOR
By varying the quantities of red, green and blue light, you can mix any color that you like. Your eye has
three types of light receptors (red, green and blue) and together with your brain, they can process the amounts
36
of red, green and blue and convert it into a color of the spectrum.
In a way, we are playing a trick on the eye by using the three LEDs. This is also the mechanism used in
TVs, where the red, green and blue color dots (in LCD) next to each other make a pixel.
Black is the absence of light, thus we can have black when we turn off all three colors of LED.
Common Cathode is straightforward, meaning, having a higher current means a brighter corresponding
LED. Here, the current is flowing from the board to the LED and is called Current Sourcing.
Common Anode is a bit different, which means, if the current is lower, it will result in a brighter
corresponding LED. Here, the current is flowing from the LED to the board. and is called Current Sinking.
37
There is no visible difference between the common anode and common cathode, thus, to determine
which is which is by testing it.
You may follow these steps to identify if it is a common anode or common cathode:
38
The PWM output will produce a pulse at roughly every 1/500 of a second. The 'analogWrite' function
controls the length of this pulse, thus, specifying a value between 0 and 255 won’t produce any pulse at all at
'analogWrite(0)' and a pulse that lasts all the way until the next pulse is due will be at 'analogWrite(255)'.
5.7 Connection
Schematic
Common Anode
You can see from the diagram that the longest leg directly connects to the 5 V and the other pins
connects (in series with a 220Ω resistor each) to 3 digital pins (capable of PWM).
39
Common Cathode
You can see from the diagram that the longest leg directly connects to the GND and the other pins
connects (in series with a 220Ω resistor each) to 3 digital pins (capable of PWM).
40
Wiring diagram
Common Anode
Common Cathode
5.8 Code
After wiring, find and open the program located in the folder - Lesson 5 RGB LED, and UPLOAD to
upload the code. If there are any errors, see Lesson 3 for details about program uploading
FOR loops will be used in our code to be able to cycle through the colors. The first will go from RED to
GREEN, then GREEN to BLUE and the third is from BLUE to RED.
The sketch starts by identifying pins that are going to be used for each of the colors:
// Define Pins
#define BLUE 3
#define GREEN 5
#define RED 6
41
Then we will write the 'setup' function. This function only runs once after the Arduino has reset as shown
in the previous lesson. Here, the function just need to define the three pins we are using as being outputs.
void setup()
{
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
digitalWrite(RED, HIGH);
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, LOW);
}
Let’s have a look at the last function in the sketch and define variables.
redValue = 255; // choose a value between 1 and 255 to change the color.
greenValue = 0;
blueValue = 0;
There are three arguments in this function, one argument is for the brightness of each of the red, green
and blue LEDs, with values between 0 (means off) and 255 (maximum brightness). The 'analogWrite' argument
of the function will then be used to set the brightness of each LED.
Looking at the 'loop' function, it can be seen that we are specifying the amount of red, green and blue
light that we want to produce, then pause for a second before moving on to the next color.
Now, try to add few colors of your own to the sketch and observe the effect on your LED.
42
43
6.1 Overview
In this lesson, it will be discussed how to use push buttons containing digital inputs to turn the LED on
and off. Pressing the button will turn the LED on and off.
Push buttons are simple components that works in the following manner: when a button is pressed or a
lever is flipped, it causes two contacts to connected together resulting to flow of electricity through them. The
push buttons in this lesson have four connections.
44
There are only two electrical connections inside the switch package: pins B – C and A - D that are
connected together.
6.4 Connection
Schematic
Wiring diagram
45
The pins of switches protrude from opposite sides of the square bodies of the component. Thus the
distance between the pins will only be enough when placed correctly on the breadboard. Keep in mind that the
LED has to have the shorter negative lead to the left.
6.5 Code
After wiring, find and open the program located in the folder - Lesson 6 Digital Inputs, and UPLOAD the
code. If there are any errors, see Lesson 3 for details about program uploading.
Load the sketch. The code tells us the LED will turn ON when left button is pressed, and it will turn OFF
when the right button is pressed.
In the sketch, three variables are defined for the three pins that will be used. The output is the 'ledPin', the
'buttonApin' is the switch closer to the top of the breadboard and other switch is 'buttonBpin'.
The ledPin is the OUTPUT defined by the 'setup' function, but we have the two inputs to set up. In this
case, we use the pinMode as 'INPUT_PULLUP' as below:
pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);
The INPUT_PULLUP pin mode tells us that the pin is used as an input, and it should then be 'pulled up'
to HIGH if nothing else is connected. That basically means that the default value is HIGH for the input, unless it
is pulled LOW by a press of the button. Thus, switches are usually connected to GND. Pressing a switch will
connect the input pin to GND, and it will no longer be HIGH.
Since default value is HIGH unless it is pulled LOW by a press of the button, the logic is a bit backwards.
This is resolved in the 'loop' function.
void loop()
{
if (digitalRead(buttonApin) == LOW)
{
digitalWrite(ledPin, HIGH);
}
if (digitalRead(buttonBpin) == LOW)
{
digitalWrite(ledPin, LOW);
}
}
46
We have two 'if' statements in the 'loop' function, one for each button. Each of the statements does a
'digitalRead', verifying the appropriate input. Keep in mind that pressing a button will result in LOW input. Thus,
if we have button A that is low, the 'digitalWrite' on the ledPin will turn it on. Likewise, a LOW is written to the
ledPin if button B is pressed.
47
48
7.1 Overview
This lesson will teach you the basics about using a passive buzzer. The goal of this chapter is to produce
eight different sounds of 0.5 seconds each: from Alto Do (523Hz), Re (587Hz), Mi (659Hz), Fa (698Hz), So
(784Hz), La (880Hz), Si (988Hz) to Treble Do (1047Hz).
How a passive buzzer works is by using PWM generating audio to make the air vibrate and generate
different sounds. For instance, sending a pulse of 523Hz generates Alto Do, a pulse of 587Hz generates
midrange Re, and so on. Thus, you can even play a song using the buzzer.
This time we do not use the Arduino board analog Write () function to generate a pulse to the buzzer, as
the function will always output 500Hz.
49
7.4 Connection
Schematic
Wiring diagram
50
In wiring the buzzer connected to the Arduino board, we put the red wire (+) going to the pin 12 and the
black wire (-) going to the GND.
7.5 Code
After wiring, find and open the program located in the folder – Lesson 7 Passive Buzzer, and UPLOAD
the code. If there are any errors, see Lesson 3 for details about program uploading.
51
8.1 Overview
This lesson will teach you the basics on using a tilt ball switch to identify a small inclination angle.
Tilt sensors are used to detect inclination or orientation. They are reliable, low-power, long-lasting and
very inexpensive. Because they are so simple, they are quite popular for appliances, gadgets and toys. They
are also known as "mercury switches", "tilt switches" or "rolling ball sensors".
They usually consist of a cavity (most often of cylindrical shape) with a conductive free mass inside, like a
blob of mercury or rolling ball. When the end of the cavity, which contains two poles, is pointed downwards, the
sensor can act as a switch throw as the mass rolls onto the poles and shorts them.
Tilt sensors can detect motion or orientation, but they are not as accurate or flexible as a full
accelerometer. The big ones have the capacity of switching power independently, whereas accelerometers
require extra circuit components for evaluating output digital or analog voltage.
52
8.4 Connection
Schematic
Wiring diagram
53
8.5 Code
After wiring, find and open the program located in the folder – Lesson 8 Ball Switch, and UPLOAD the
code. If there are any errors, see Lesson 3 for details about program uploading.
54
In this lesson, a Library designed for HC-SR04 sensor, which is a cheap and easy to use sensor, will be
used.
The ultrasonic sensor module HC-SR04 has a capacity of 2cm to 400cm indirect measurement, with a
ranging precision of up to 3mm. Ultrasonic transmitters, receivers and control circuits are included in the
module. The fundamental working formula is:
55
You can observe from the Timing Diagram below. As you can see, only a short 10ms pulse is necessary
to trigger the device to start the ranging. Afterwards, the module will send out 8-cycle bursts of ultrasound at 40
kHz and raise its echo, thus the range through the time interval between sending trigger signal and receiving
echo signal can be measured. The formula is: ms/58 in centimeters or ms/148 in inches or the range = high
level time * velocity (340M/S) / 2. We recommend using measurement cycles over 60ms.
56
9.4 Connection
Schematic
Wiring diagram
57
9.5 Code
Our code will be short and simple if we use a Library designed for these sensors. The Library will be
included at the start of the code and by using simple commands we can control the behavior of the sensor.
Open the monitor so we can see the following values:
Press the Serial Monitor button to turn it on. The serial monitor is comprehensively introduced in Lesson
2.
58
10.1 Overview
Wireless control on your projects can be easily achieved using an Infrared (IR) Remote, as they are
simple to understand and use. In this lesson we will be learning to program the IR receiver using a Library
designed specifically for it.
In our code, we will have access to all the IR Hexadecimal codes available on this remote, we will check if
the code was identified and whether we are pressing a key.
IR detectors are essentially small microchips that contains a photocell. This photocell’s function is to
detect infrared light. These detectors are commonly used for remote control detection, especially in TVs and
DVD players. TV commands are given by the infrared light inside the remote control: to power on, change the
volume, etc. This light can not be seen by the human eye, which makes it harder for us to test a setup.
59
IR detectors are not suitable for visible light, as they are especially created for IR light. On the other hand,
photocells are the opposite: good at detecting yellow/green light, but not for IR light.
Photocells do not have demodulators and they can distinguish any frequency within the speed of the
photocell’s response (which is about 1KHz). On the contrary, IR detectors have a demodulator inside that
searches for modulated IR at 38 KHz.
60
10.4 Connection
Schematic
Wiring diagram
61
There are 3 connections to the IR Receiver: Signal, Voltage and Ground. The “-” is the Ground, “S” is
signal, and middle pin is Voltage 5V.
10.5 Code
After wiring, find and open the program located in the folder – Lesson 10 IR Receiver Module, and
UPLOAD the code. If there are any errors, see Lesson 3 for details about program uploading. To be able to run
this, have the < IRremote > library installed or re-install it, if necessary, for the code to work. For details about
loading the library file, see Lesson 2.
Now, we will move the <RobotIRremote> out of the Library folder, to avoid problems the one we are going
to be utilizing. Just put it back after you are done with this lesson. After installing the library, restart your IDE.
62
Open the monitor, so you can see the following figure:
Press the Serial Monitor button to turn it on. The serial monitor is comprehensively introduced in Lesson
2.
63
11.1 Overview
In the previous lesson, we used the IR remote to see which button is pressed on the serial monitor, in this
lesson we will learn how to use it to control 5 LEDs. we will use the numbers on the remote from 0 to 9 to
control them.
64
11.3 Connection
Schematic
Wiring diagram
65
11.4 Code
After wiring, find and open the program located in the folder – Lesson 14 IR Receiver Module, and
UPLOAD the code. If there are any errors, see Lesson 3 for details about program uploading. To be able to run
this, have the < IRremote > library installed or re-install it, if necessary, for the code to work. For details about
loading the library file, see Lesson 2.
66
12.1 Overview
This lesson will be teaching you the basics on how to control a small DC motor using L293D IC and an
Arduino.
Product Specifications
67
Description/ordering information
The purpose of the L293 is to supply bidirectional currents of a maximum of 1A, at voltages between 4.5V
and 36V, whereas the L293D can handle a bidirectional current of up to 600mA using the same voltage range.
Both of them are called quadruple high-current half-H drivers, and they are both devised with the purpose of
driving inductive parts: DC and bipolar stepping motors, relays, solenoids, and any other high-current and
voltage loads in positive-supply operations.
Each and every one of the outputs are complete totem-pole drive circuits, with a Darlington transistor sink
and a pseudo-Darlington source, while all the inputs are TTL compatible. Drivers 1 and 2 are enabled by
1,2EN, while drivers 3 and 4 are enabled by 3,4EN, as they are set up in pairs. They are enabled when an
enable input is high, and the corresponding outputs are active and in phase with their inputs. Alternatively,
those drivers are disabled when the enable input is low, and the corresponding outputs are off and have high
impedance. Each pair of drivers creates a full-H, also called a bridge, reversible drive suited for solenoid or
motor usage with the appropriate data inputs.
Block diagram
68
12.4 Connection
Schematic
Wiring diagram
69
The code below does not require a separate power supply or a battery, it actually uses the 5V power from
the Arduino. This can only be done because the L293D is controlling it and would be risky without it.
DO NOT connect a motor directly to the device, because if you will get an electrical feedback when you
turn the motor off. If you have a small motor, this will damage your Nano, but with a large one, you can even
see a flame and sparks effect.
12.5 Code
After wiring, find and open the program located in the folder Lesson 12 DC Motors, and UPLOAD the
code. If there are any errors, see Lesson 3 for details about program uploading.
Once the program loads, power ON all the power switches. There will slightly rotation of the motor
clockwise and counterclockwise for 5 times. Afterwards, it will continue to dramatically rotate clockwise and
counterclockwise. Then after a short pause, it will dramatically rotate counterclockwise. Then, to drive the
motor, the controller board will send a PWM, so that the motor will gradually lower its maximum RPM to the
minimum and increase to the maximum again. Finally, it stops for 10 seconds until the next cycle starts.
70
71
13.1 Overview
In this lesson, you will learn the basics for using a relay.
A relay is an electrically operated switch, generally using an electromagnet to mechanically operate a
switch, but sometimes its operating principles are also used as in solid-state relays. Relays are used where it is
necessary to control a circuit using a low-power signal (with complete electrical isolation between control and
controlled circuits), or where several circuits must be controlled by one signal. The first relays were used in
long-distance telegraph circuits as amplifiers where they repeated the signal coming in from one circuit and
re-transmitted it on another circuit. As early computers, what they do is to perform logical operations further
72
down the line. The way they are working is that they repeated the signal coming in from one circuit and
re-transmitted it on another circuit.
A contactor is a type of relay that can handle the high power needed to directly control an electric motor
or other loads. Solid-state relays control power circuits using a semiconductor device to perform the switching.
Relays with calibrated operating characteristics and sometimes multiple operating coils are utilized to
safeguard electrical circuits from overload or faults. In modern electric power systems, these functions are
performed by digital instruments called "protective relays".
You can find the schematic showing how to drive a relay with our device below.
If you are unsure about how to insert the relay into the breadboard. As you can see in the photo below,
you will need to slightly bend one of the pins of the relay so you can insert it into the breadboard.
73
13.4 Connection
Schematic
Wiring diagram
74
13.5 Code
After wiring, find and open the program located in the folder - Lesson 13 Relay, and UPLOAD the code. If
there are any errors, see Lesson 3 for details about program uploading.
After the program loads. The relay will pick up with a ringing sound. Then, the motor will rotate and
eventually, the relay will be released, and the motor stops.
75
14.1 Overview
In previous lesson we controlled LEDs with remote, in this lesson we will learn how to control DC motor
with remote, using 0 and 1 buttons.
Transistor
The transistor is a circuit element that can be used as an electrically operated switch. It is made of
semiconductor materials, has three terminals and can interrupt or close the circuit between two terminals
depending on the command applied on the control pin.
76
14.4 Connection
Schematic
Wiring diagram
77
14.5 Code
After wiring, find and open the program located in the folder Lesson 12 DC Motors, and UPLOAD the
code. If there are any errors, see Lesson 3 for details about program uploading.
78
15.1 Overview
This lesson will show you how to use eight LEDs with an Arduino without using up 8 output pins! Even
though you could wire up eight LEDs with a resistor for each of them to a pin, you would quickly run out of pins
for other components, as we usually have an array of devices such as buttons, sensors, servos, etc. for most
projects. Instead, we are using a chip called the 74HC595 Serial to Parallel Converter, which has eight outputs
and three inputs that you could use to give data to.
79
Additionally, the chip has an output enable (OE) pin, utilized to weather enable or disable the outputs
collectively. This can be linked to a PWM-capable Arduino pin, allowing the use of 'analogWrite' to the light
intensity of the LEDs. We connect this pin to GND since it is active low.
80
15.4 Connection
Schematic
Wiring diagram
81
We have eight LEDs and eight resistors to connect, so let us begin.
We recommend you place first the 74HC595 chip as almost everything else connects to it. Place it in
such a way that towards the top of the breadboard is the U-shaped notch. Located at the left of this notch is the
Pin 1 of the chip.
Digital 12 from the Nano goes to pin #14 of the shift register
Digital 11 from the Nano goes to pin #12 of the shift register
Digital 9 from the Nano goes to pin #11 of the shift register
All except one of the outputs is placed on the left side of the chip, where the LEDs can be found as well,
to make the connection effortless.
Then, place the resistors on the breadboard. You should check that the leads of the resistors are not
touching each other before you connect the power to your Nano. You can try to shorten the leads so that they
are closer to the surface of the breadboard, if ever you are encountering any difficulties
Afterwards, put the LEDs in place. The longer positive leads must all be in the direction of the chip,
irrespective of which side of the breadboard they are on.
Connect the jumper wires, but remember the one that goes from pin 8 of the IC to the GND column.
15.5 Code
After wiring, find and open the program located in the folder Lesson 15 Eight LED with 74HC595, and
UPLOAD the code. If there are any errors, see Lesson 3 for details about program uploading.
Then, we define a variable called ‘leds’, which will hold the order of the
LEDs being turned on or off. The data 'byte' represents numbers using eight bits, each being on or off, ideal for
monitoring which of the eight LEDs are powered on or off.
void setup()
{
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
82
At first, the 'loop' function turns all of the LEDs off by setting the variable ‘leds' to 0. Afterwards, it calls the
'updateShiftRegister' function, which sends the 'leds' sequence to the shift register in order to turn all the LEDs
off.
There will be a half second pause for the loop function and using the ‘for’ loop and the variable ‘i’, it
counts through the number of leds (from 0 to 7). Every time, the 'bitSet' function is utilized to set the bit that
controls that LED in the variable 'leds'. Then it calls the ‘updateShiftRegister' to update the LEDs. Afterwards,
there is a half second delay before the incrementation of 'i' and the next LED is turned on.
void loop()
{
leds = 0;
updateShiftRegister();
delay(500);
for (int i = 0; i < 8; i++)
{
bitSet(leds, i);
updateShiftRegister();
delay(500);
}
}
First of all, latchPin is set to low by 'updateShiftRegister' function followed with a call of the
'shiftOut' function before bringing back the ‘latchPin’ to high. We have four parameters here: the first two are for
Data and Clock, the third is to identify which end of the data you wish to begin at, and the last is for the actual
data to be shifted into the shift register. We are
beginning with the rightmost bit, also called the 'Least Significant Bit' (LSB).
void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}
In order to turn one of the LEDs off, you would call the ‘bitClear’ function with the 'leds' variable, which will
have a setting of ‘0’, and then followed with calling the 'updateShiftRegister' to have the actual LEDs updated.
83
84
16.1 Overview
This lesson will be based on the previous lesson, with the option of managing the LEDs from your
computer. We will be utilizing the Serial Monitor – the connection between the computer and the Arduino,
enabling you to send and receive text messages for debugging or controlling from keyboard. After this lesson,
you can send commands from your computer to turn the LEDs on, using the same exact parts and a similar
breadboard layout.
85
This window will open.
Press the Serial Monitor button to turn on the serial monitor. We discussed the serial monitor in detail in
Lesson 2.
The message that appeared, “Enter LED Number 0 to 7 or 'x' to clear” has been sent by the device, which
informs us of the commands we can give to the Arduino: 'x' to turn all the LEDs off or the number of the LED
you want to turn on (where 0 to7 is from bottom LED to top LED). You can test the following commands into
the top area of the Serial Monitor and press 'Send': x 0 3 5.
If the LEDs are already turned off, typing ‘x’ will not do anything, but upon entering each number, the
matching LED will turn on and a confirmation message will appear, like the one below.
86
Typing ‘x’ one more time and clicking ‘Send’ will turn off all the LEDs.
16.3 Code
After wiring, find and open the program located in the folder Lesson 16 The Serial Monitor, and UPLOAD
the code. If there are any errors, see Lesson 3 for details about program uploading.
As expected, this sketch is largely built on the one used in Lesson 16, so we only need to explain the new
lines of code. You may deem it useful to refer to the complete sketch in your IDE. As you can see, there are
three new lines at the end in the 'setup' function:
void setup()
{
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
updateShiftRegister();
Serial.begin(9600);
while (! Serial); // Wait until Serial is ready - Leonardo
Serial.println("Enter LED Number 0 to 7 or 'x' to clear");
}
The 'while' loop assures us that there is something on the other side of the USB connection for the device
to communicate with, or else the message cannot be displayed even though it is sent. This line is only needed
if you are using a Leonardo board because the Arduino board routinely resets itself when you open the Serial
87
Monitor.
The final new lines in 'setup' send out the message we see at the top of the Serial Monitor. Let’s take a
look at the ‘loop’ function:
void loop()
{
if (Serial.available())
{
char ch = Serial.read();
if (ch >= '0' && ch <= '7')
{
int led = ch - '0';
bitSet(leds, led);
updateShiftRegister();
Serial.print("Turned on LED ");
Serial.println(led);
}
if (ch == 'x')
{
leds = 0;
updateShiftRegister();
Serial.println("Cleared");
}
}
}
The ‘if’ statement tells us everything that happens inside the loop and nothing else will happen if the call
to the built-in function 'Serial.available()' is 'true'. If data is sent to the Nano and is ready to be processed, then
Serial.available() will return 'true'. Meaning, the Serial.available() will return 'true' if the buffer, where incoming
messages are held, is not empty. If a message has been accepted, then we move on to the next part of the
code:
char ch = Serial.read();
This extracts the next character from the buffer and assigns it to the variable 'ch'. The variable 'ch' is of
type 'char', which holds a single character. This variable will either be a number between 0 and 7 or the letter
'x'.
The 'if' statement in the following line checks if it is a single digit by confirming that the value of 'ch' is
between '0' and '7'. While it may seem peculiar comparing characters like this, it is perfectly reliable, as each
character is illustrated by a unique number, called an ASCII value. Thus, when we compare characters using
the operators <= and >=, we are actually comparing the ASCII values. In the case that the ‘if’ result is positive,
we proceed on to the next line:
Next, we are subtracting the digit '0' from any digit entered. Thus, typing '0' then '0' - '0' will equal 0,
whereas typing '7' then '7' - '0' will equal the number 7 as the ASCII values are actually the ones being used in
the operation. Because we know the number of the LED we want to power on, we just need to store that in the
variable 'leds' and have the shift register updated.
bitSet(leds, led);
updateShiftRegister();1
The following lines send a confirmation message back to the Serial Monitor.
88
Serial.print("Turned on LED ");
Serial.println(led);
You can see from the first line that it uses Serial.print rather than Serial.println, as we do not want to
begin a new line after printing. We will print the message in two parts: 'Turned on LED ' and the number of the
LED as an ‘int’ variable. Serial.print can take any type of variable, either a text string enclosed in
double-quotes, or an 'int' or any type of variable. Ensuing the 'if' statement that handles the single digit case,
there is a second 'if', one that checks whether 'ch' is the letter 'x'.
if (ch == 'x')
{
leds = 0;
updateShiftRegister();
Serial.println("Cleared");
}
In that is the case, it then clears each one of the LEDs and sends a message of confirmation.
89
17.1 Overview
This lesson will be showing you the basics on how to measure brightness using Analog Input or how to
check the intensity of light and how to use that value to manage the number of LEDs being lit using a photocell.
The photocell used here is a light dependent resistor (‘LDR’) type. From the name, we draw the
conclusion that these components behave like a resistor, except that the resistance changes according to the
level of light they are exposed to, with values between 50 kΩ in near darkness and 500 Ω in bright light. To
transform this fluctuating value of resistance into something that the Arduino’s analog input can measure, it
has to be converted into a voltage. The most basic way to do so is to integrate it with a fixed resistor.
90
Alternatively, when the photocell is in dull light, the resistance becomes greater than the fixed 1 kΩ
resistor and it behaves as if the pot is being turned towards GND. Use the code found in the next section and
try to cover the photocell or hold it near a light source to see how the values change.
17.4 Connection
Schematic
91
Wiring diagram
17.5 Code
After wiring, find and open the program located in the folder Lesson 17 Photocell, and UPLOAD the code.
If there are any errors, see Lesson 3 for details about program uploading.
In this situation, we will divide the raw reading by 57 instead of 114, half as much as we did previously
with the pot to split it into nine areas, from no LEDs lit to all eight lit. We also have to note the fixed 1 kΩ
resistor. Thus, when the photocell has the same resistance as the resistor, the raw reading will be 1023 / 2 =
511, which means that all the LEDs are being lit and that ‘numLEDSLit’ will be 8.
92
93
18.1 Overview
This lesson will teach you how to show the temperature in Serial Monitor.
94
18.4 Connection
Schematic
Wiring diagram
95
18.5 Code
After wiring, find and open the program located in the folder Lesson 18 Thermometer, and UPLOAD the
code. If there are any errors, see Lesson 3 for details about program uploading.
Open the monitor so we can see the following values:
Press the Serial Monitor button to turn it on. The serial monitor is comprehensively introduced in Lesson
2.
96
19.1 Overview
In this lesson we learn how to use LED Bar and how to increase it one cell by one.
19.4 Connection
Schematic
97
Wiring diagram
19.5 Code
After wiring, find and open the program located in the folder Lesson 19: LED Bar with 10 Segments, and
UPLOAD the code. If there are any errors, see Lesson 3 for details about program uploading.
98
20.1 Overview
This lesson is the same as the previous one, but we are going to add 74hc595 IC so we will use only 5 Outputs
from Nano.
20.3 Connection
Schematic
99
Wiring diagram
20.4 Code
After wiring, find and open the program located in the folder Lesson 20: LED Bar with 74hc595, and UPLOAD
the code. If there are any errors, see Lesson 3 for details about program uploading.
100
21.1 Overview
In this lesson we are going to use 10K Potentiometer to light up the LEDs.
21.3 Connection
Schematic
101
Wiring diagram
102
21.4 Code
After wiring, find and open the program located in the folder Lesson 21: LED Bar with Potentiometer, and
UPLOAD the code. If there are any errors, see Lesson 3 for details about program uploading.
103
22.1 Overview
To understand this lesson, let us take a push button off and take a look inside of it.
There is a metal domed disk inside of it so when you press on it, the disk makes a contact between the legs.
As you can see below, the first contact is red, and the second one is blue.
104
The disk is boncy, so when you press on it, it will not make contact directly, indeed it will make the contact on
and off many times before setting down.
In this lesson we are going to use code to debounce a push button.
22.3 Connection
Schematic
105
Wiring diagram
106
22.4 Code
After wiring, find and open the program located in the folder Lesson 22: Debouncing, and UPLOAD the code. If
there are any errors, see Lesson 3 for details about program uploading.
23.1 Overview
In this lesson we are going to use 2 push buttons to make a counter with the LED Bar.
23.3 Connection
Schematic
107
Wiring diagram
108
23.4 Code
After wiring, find and open the program located in the folder Lesson 23: LED Bar with 2 Push buttons, and
UPLOAD the code. If there are any errors, see Lesson 3 for details about program uploading.
24.1 Overview
This lesson will be teaching you the basics on using a 4-digit 7-segment display. For 1-digit 7-segment
display, please note that common cathode pin connects to ground while common anode pin connects to the
power source.
For this display, the one that controls which digit to show is the common anode or common cathode pin.
Although just one digit is working at a time, you are capable of seeing all the numbers displayed because the
flashing is so quick that you barely see the interruptions, according to the principle of Persistence of Vision.
110
111
24.4 Connection
Schematic
Wiring diagram
112
24.5 Code
After wiring, find and open the program located in the folder Lesson 24 Four Digits Seven Segments
Display, and UPLOAD the code. If there are any errors, see Lesson 3 for details about program uploading.
113
25.1 Overview
This lesson based on the previous lesson, instead of showing the voltage on 4 digit seven segment, we
will show the temperature using thermistor.
114
25.3 Connection
Schematic
g
Wiring diagram
115
25.4 Code
After wiring, find and open the program located in the folder Lesson 25 Thermometer Display on 4 Digit
Seven Segment, and UPLOAD the code. If there are any errors, see Lesson 3 for details about program
uploading.
116