Introduction To Arduino Projects The Best User Guide For Beginners
Introduction To Arduino Projects The Best User Guide For Beginners
com
INTRODUCTION TO
ARDUINO PROJECTS
BEGINNERS
BENJAMIN ISRAEL
Contents
INTRODUCTION .................................................................................... 1
www.electronic07.com
CHAPTER THREE: INTRODUCTION TO THE ARDUINO
PROGRAMMING
LANGUAGE .........................................................................................11
Time functions............................................................................19
Interrupts ...................................................................................23
Processor ....................................................................................27
Memory ..................................................................................... 27
Security ...................................................................................... 30
Price ........................................................................................... 30
AN ARDUINO ....................................................................................110
ARDUINO? ........................................................................................119
CONCLUSION ....................................................................................135
INTRODUCTION
www.electronic07.com
Arduino is a revolutionary technology in the electronics ecosystem.
I mean the makers movement. Before Arduino existed, electronics was not
interesting and easy to learn as it is today, and there was not so much of
resources available.
Arduino was developed in in a city called Ivrea in Italy. This is where the
company that created the first personal
For instance, the diagrams of the latest version Arduino Uno board, the
Arduino Uno Wifi Rev2, can be found online. It’s cool because you can
build your own Arduino, if you want.
In fact, companies can build and sell their own Arduino clones, and many
are doing it.
Each board has its own use case. Arduino Nano and Arduino Micro for
instance are awesome for IoT, wearables and small devices. However,
Arduino Mega has more memory and I/O
Nonetheless, the Arduino Uno board is considered the best board for
learning so far, and it’s included in many toolkits and used in so many
tutorials today.
The Arduino MKR WiFi 1010 board is commonly used in IoT, as it has
buit-in WiFi and Bluetooth.
Arduino does not have its own operating system, and it simply runs a single
program at a time. So, you don’t have to worry about anything since there is
nothing else than your program running on the Arduino.
In fact, most Arduino boards do not even have network connection, out of
the box! Although some do, like the Arduino Uno WiFi rev 2 or the
Arduino MKR WiFi 1010.
Once you load a program, it boots any time the Arduino is powered, either
via USB or via the power port via a AC-to-DC power cable or a battery.
By inference, once you have loaded the program, you can put the Arduino
on a mountain with a solar panel and a battery, and it will keep running
until there’s power.
It only operates programs that were compiled for the Arduino platform,
which typically means programs written
in the Arduino Language, which is C++ with some suitable features that
make it easy for beginners to start with.
This is not to say you are restricted to it. If you don’t mind having the
Arduino attached to the USB port of the computer (or a Raspberry PI
driving it), you can run Node.js code on it using the Johnny Five project,
which is pretty cool. There are related tools for other languages, like
pyserial and Gobot .
a program for it, attach a battery or a power connector and put it somewhere
to run, and play around with sensors and some other really cool stuffs that
interface with the real world.
For instance, you could use an Arduino to power your self-watering plants
or track the temperature outside, or power some other home automation
stuff.
4
CHAPTER ONE: THE ARDUINO UNO REV
3 BOARD
This is a microcontroller board.
The Arduino Uno rev 3 possesses the same form factor of the Arduino 101,
Arduino Zero, Arduino Yún, Arduino Leonardo, Arduino Uno Wifi rev 2
and Arduino Ethernet.
As per I/O, the Arduino Uno has a USB-B port that can be
used by a computer to transfer new programs to run, a power input and a set
of I/O pins. It also has 20 I/O pins, 14
digital and 6 analog pins that have 10 bits, mapping values from 0 to 1023.
Looking at the above picture carefully, we’ll notice that at the top of it, we
have 14 digital I/O pins, which can be 6
configured
to
be
output
pins
or
input
pins
programmatically.
Then, at the bottom of it, we have the power pins, and the 6
There is also a built-in led, linked to pin 13, which offers us status
information.
In addition, the Arduino Uno comes with a USB port that
The Arduino does not have an operating system, and it runs one single
program at a time.
Once you load a program, it boots any time the Arduino is powered, either
via USB or via the power port via a AC-to-DC power cable or a battery. In
that case, once you load the program, you can put the Arduino on a
mountain with a solar panel and a battery, and it will keep running until
there’s power.
7
CHAPTER TWO: THE ARDUINO UNO
The Arduino Uno Wifi rev 2 possesses the same form factor as the Arduino
101, Arduino Zero, Arduino Yún, Arduino Leonardo, Arduino Uno and
Arduino Ethernet.
It
comes
along
with
an
8-bit microcontroller,
SRAM and 256 bytes of EEPROM. That’s a lot more memory than
previous Arduino Uno rev 3 boards with the
ATmega328P. Take note of this change if you have an older Arduino Uno
board.
www.electronic07.com
When it comes to I/O, the Arduino Uno has a USB-B port
pins, 14 digital and 6 analog pins that have 10 bits, mapping values from 0
to 1023.
W13 which comes with an integrated TCP/IP protocol stack that is capable
of giving access to a WiFi network, with hardware-based security.
Otherwise, you can let the Arduino act as an access point, so you can
connect to it.
The Arduino Uno has a USB port that lets you to connect it to the computer
and load a program on it.
The Arduino does not have an operating system, and it runs just one single
program at a time.
10
LANGUAGE
The
Arduino
Language
is
founded
upon
software available for all the major desktop platforms (macOS, Linux,
Windows), which offers us two things: firstly, a programming editor that
incorporates libraries support, and secondly, a means to easily compile and
load our Arduino programs to a board connected to the
computer.
11
The main difference from “normal” C or C++ is that you wrap all your code
into two major functions. You can have more than two anyway, of course,
but any Arduino program
One is called setup(), the other is called loop(). The former is called once,
when the program starts, the latter is repeatedly called while your program
is running.
There is no main() function like you are used to in C/C++ as the entry point
for a program. Once you have compiled your sketch, the IDE will make
sure the end result is a correct C++
program and will essentially add the missing glue by preprocessing it.
Every other thing is normal C++ code, and since C++ is a superset of C,
any valid C is also valid Arduino code.
12
One important difference that might cause you troubles while using
Arduino is that while you can spawn your program over multiple files,
those files must all be in the same folder. This might be a big limitation if
your program will grow very large, but at that point it will be easy to move
to a native C++ setup, which is possible.
The Arduino Programming Language has built-in libraries which allow you
to easily integrate with the functionality provided by the Arduino board.
Your first Arduino program will definitely include making a led turn on the
light, and then turn off. To do this, you will use
“Hello, World!”):
#define LED_PIN 13
void setup() {
pinMode(LED_PIN, OUTPUT);
13
void loop() {
digitalWrite(LED_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN, LOW);
// Wait 1 second
delay(1000);
This is all part of the Arduino Programming Language, or you may call it
suite or library.
14
It is important to note that you are not limited to using this language and
IDE to program an Arduino. There are projects to let you run Node.js code
on it using the Johnny
Five project, Python code using pyserial and Go code with Gobot, but the
Arduino Programming Language is certainly the one you’ll find in most
tutorials, as it’s the primary and official way to work with these devices.
Constants
Uno) LOW equates to a low level of voltage. Also, the exact value depends
on the board used
15
to the number of the on-board pin, which usually equates to the number 13.
16
Program lifecycle
▪ setup() This function is called only once, when the program starts, and
when the Arduino is shut down and
restarted.
Handling I/O
The following functions are used to handle input and output from your
Arduino device.
Digital I/O
▪ digitalRead() reads the value from a digital pin. Accepts a pin number as a
parameter, and returns
pin.
You
pass
the
pin
number
17
www.electronic07.com
▪ pinMode() sets a pin to be an input, or an output. You pass the pin number
and the INPUT or OUTPUT value
as parameters.
to LOW again,
or
pulse is detected. You specify the pin number and the kind of pulse you
want to detect (LHL or HLH). You can
pins.
Analog I/O
▪ analogReference() configures the value used for the top input range in the
analog input, by default 5V in 5V
and MKR)
and MKR)
Time functions
19
▪ millis() the number of milliseconds since the start of the program. Resets
after ~50 days due to overflow
Math functions
usage
usage
Note: There are more built-in mathematical functions the ones listed above.
These are the common ones you’re likely to make use of most at this level.
20
▪ isSpace() checks if a char is a space, form feed \f, newline \n, carriage
return \r, horizontal tab \t, or vertical tab \v.
21
Just like in most languages, it’s impossible to get really random numbers in
Arduino, and the sequence is always the same, so you seed it with the
current time or (in the case of Arduino) you can actually read the input from
an analog
port.
= 8…)
22
Interrupts
▪ detachInterrupt(): disables
an
interrupt
enabled
using attachInterrupt()
23
Take note of the order in which they’re used here to help you remember
what they mean: repeat it a few times, milli-micro-nano-pico and you’ll
remember them the next time.
24
CHPTER FOUR: THE ARDUINO MKR WIFI
1010
This is the first board that has WiFi and Bluetooth connectivity built-in, and
it’s quite exciting to talk about the opportunities and possibilities it enables.
Let’s have an overview of the board, and compare it to the Arduino Uno,
which is the one most commonly used by beginners.
Form factor
25
Arduino Uno Wifi
Power
It runs at 3.3V. This is a crucial difference with Arduino Uno, which runs at
5V. 3.3V, the maximum voltage the I/O pins
can support.
26
Processor
The Arduino MKR WiFi 1010 board goes with a low power
The processor is 32 bit. The Arduino Uno board presents the ATmega328
processor, which is 8 bit.
Memory
I/O Pins
www.electronic07.com
The Arduino MKR WiFi 1010 provides:
▪ 1 Analog Output pin (DAC 10 bit), the one recognized with DAC0/A0.
27
28
29
IoT connectivity
The Arduino MKR WiFi 1010 has the provision of a WiFi module, the
WiFi U-BLOX NINA-W10 Series Low Power
2.4GHz. It works with the protocol 802.11 b/g/n and also provides
Bluetooth Low Energy (BLE).
Security
The board has a crypto chip that allows SHA-256 secure connections, the
ATECC508.
When you’re charging the Arduino MKR WiFi 1010 board through the
Micro-USB port, it can charge an external battery through its Li-Po
charging circuit.
Once the USB power is disconnected, the device switches to the external
battery automatically.
Price
As per the price, the Arduino MKR WiFi 1010 is more expensive than an
Arduino Uno Rev3 board, but less expensive than an Arduino Uno WiFi
Rev2.
30
If we consider the fact that Arduino Uno Rev3 board does
not have WiFi / Bluetooth connectivity, we would not even want to compare
them – they are no match!
1010 is more economical than its Uno IoT counterpart, Arduino Uno WiFi
rev2, which can be considered its educational counterpart.
31
ELECTRONICS
In the last decade or so, electronics have become more advanced than ever.
We have moved from a society where
Building your own devices and tools and watching them actually work, in
the real world, is something that can make you really happy and feel very
proud of yourself.
32
We
can
divide
electronics
into
two
major
digital
representation
can
only
assume
two
states: on or off. 1 or 0.
33
Being able to represent basic values this way, that is, using only 0 and 1
values, made it possible to solve complex problems in a simple way, and
ultimately led us to producing things like our computers, smartphones and
even the Internet.
It is possible to combine multiple binary values to represent numbers that
have more than two states. For instance, if we have two numbers, we can
define four states, if we have three numbers, we can define eight states, and
if we have four numbers, we can define sixteen, and so on.
times zero.
00
01
10
11
35
Below is a simple conversion table for the first 8 digits: Decimal number
Binary number
000
001
010
011
100
5
101
110
111
Below is a simple conversion table for the first 16 digits: Decimal number
Binary number
0000
0001
0010
0011
0100
5
0101
0110
36
0111
1000
1001
10
1010
11
1011
12
1100
13
1101
14
1110
15
1111
Current simply refers to the flow of electrons between two points with
different voltage. Current is measured
in ampere (A).
There are 2 types of current, namely: alternating current (AC) and direct
current (DC)
In alternating current (AC), the current changes direction from time to time.
This is the type of current that powers our houses from a grid that we
connect to.
37
The two types of current are different in nature and have different
characteristics, and thus, they allow very different usages and applications.
The more the voltage, the more the electrons that flow in a circuit: that is
what we call current.
38
Think of having a circuit with a LED light. Let’s first of all power it with a
1.5V battery, lighting up the led a little bit. If you then add another 1.5V
battery to make a 3V battery, the light will shine more, because you now
have more current
Earth has a null electric potential, i.e, 0V (zero volts). We call it ground.
Usually, AA batteries provide 1.5V of power. This means the + pole of the
battery has 1.5V more voltage than the - pole. Some devices will use
several batteries connected with each other to provide more voltage, for
example 3V
39
In all the projects and explanations in this book, you will come across
various terms that mean the same thing. For instance, the terms -, 0V,
negative pole of the battery, ground, GND and Vss all mean the negative
pole of the battery that is at 0V.
On the other hand, +, Vcc, Vdd or positive pole refers to the positive pole
of the battery which precise value will be different based on the battery or
power source utilized, e.g. 9V, 5V, 3.3V, as the case may be.
• TTL (Transistor-Transistor
Logic)
components
• CMOS (Complementary
Metal-Oxide-
Even the wire has some resistance, but it’s extremely low.
That’s what is called the Ohm’s law: R = V / I, where R is the symbol for
resistance, V is the symbol for voltage and I is the symbol for current.
▪V=R*I
▪I=V/R
We have resistors of various values. The very common ones you will find
used in circuits are 220Ω, 1kΩ, 4.7kΩ, 10kΩ, and so on.
Having Omh’s law, we can calculate the current flowing in a circuit when
you know the voltage provided by the battery, 41
If the battery provides 5V and the circuit provides a 1kΩ resistance, the
current flowing in the circuit will be 5mA.
Let’s assume the voltage is 5V, and we a have a positive pole as well as a
negative pole.
Short circuits are dangerous and capable of causing serious damage to our
devices and components, making them
42
Even in the case of a 1.5V battery, the amount of current flowing between
the positive and negative poles can ignite a fire.
Later, we’ll talk about resistors and LEDs in details, but let’s create our first
circuit now.
circuit. In this book, we’re going to use Tinkercad, and is available for free
use at https://tinkercad.com.
43
printing.
Create free user account on the website, then from the dashboard choose the
Circuits menu:
44
Then click the Create new Circuit button to display the circuit builder
interface:
45
Here, you can drag and drop components from the right sidebar on the main
screen.
47
Pull the negative pole to meet one of the poles of the resistor:
Lastly, connect the anode, the right pin of the LED, to the positive pole of
the battery:
48
You can change the colors of the wires by clicking on them: 49
You can also alter the settings for each component by clicking on it. For
example, click the resistor to reveal and modify its resistance value, which
is 1kΩ by default: 50
www.electronic07.com
Double-click a wire to add a point to it for a nicer circuit: 51
After a little tidying up, you’ll have this result:
52
Now click the Start simulation button. You’ll see the LED
53
Now try changing the resistance to 220Ω:
54
Then run the simulation again, and you’ll see a warning on the LED,
notifying you the current flowing through is too much, and that the
recommended maximum amount of
55
While the simulation is running, you can as well change the value of the
resistor. If you use 1000Ω instead of 220, you’ll notice the light on the led
will be less bright.
Add 10000Ω, and the LED will not give any light at all.
The higher the resistance of the resistor, the lesser the current flowing in the
circuit, and consequently, the dimmer the light produced by the LED.
Recall Ohm’s law, I = V / R: the current flowing through the circuit with a
220Ω resistor is 9 / 220 = 40mA.
56
Take a look at the same circuit, but in the real world:
Electronics
Basics:
Prototyping
Using
Breadboards
Looking closely at the image below, you will see a simple circuit with a
battery, a resistor and a LED.
57
The elements are shown inside a tiny white box which is called the
breadboard:
Breadboard
58
Underneath the surface, the 5 holes in a set are connected with each other,
so as to enable us create electric connections very easily.
The principle is the same, except that we have more elements on the outer
border, the ones wrapped inside the red and blue lines:
59
In this instance, the items on the outer boarder are connected longitudinally,
and then, orthogonally to the 5-elements sets in the inside of the board (see
illustration below):
60
We use them to connect the positive and negative poles of the battery (or
any other power source) to the board, so your elements can have easy
access to them on the board.
Red wires are commonly for the + positive pole and black wires for the -
negative pole:
and once you’re set to move on, all you need is just to solder it into a
perforated board.
A digital multimeter is one handy tool, among the few you’ll need to start
with.
There are several types of multimeters, from very cheap ones (<10$) like
this one below which is quite ok for a beginner.
As you grow on to become a professional, you can get a great one for <
30$. Take a look at this:
62
63
If you compare these two, you’ll see there’s a big difference in size and
build quality:
You can also see one has a 10A port, and the other has a 20A port. That is
to say one can measure up to 20 Ampere of current before breaking its fuse,
the other half of that.
64
Again, one can measure temperature as well with a special cable. It has a
light, and so on.
65
CHAPTER SIX: HOW YOU CAN MEASURE
1. Take a battery
ground,
5. Connect
the
other
end
of
the
cables
to
67
It’s a 220Ω resistance.
68
Note: In the previous experiment, we didn’t have to set the scale, it was
automatically determined. But in this case, if you notice the resistance is too
low or too high for the scale, 69
you
need
to
adjust
it
between
the 200 2000 20k 200k 2000k points to see which one gives you a
satisfactory result.
For example, in this case, I chose the 20k scale, and I got 0.22 in the
display. 20k here means it’s able to measure up to 20kΩ and 0.22 means it’s
0.22 of 1kΩ:
70
Does that look confusing? Oh well, it’s not the best method anyway. I
would therefore recommend you get a multimeter
that can determine the scale automatically for you.
Right? I showed you those first because they work in a similar way: the
connectors are put in parallel to the item we want to measure. We call that a
parallel connection.
Also, depending on the multimeter you’re using, you might need to modify
the entry point for the cable. In this instance, we’re measuring volts and Ω
using the input #4, but we’ll measure current using input #1 (and high
amounts of current through input #2):
71
To measure current let’s build a little circuit. In this circumstance, we’ll use
a potentiometer that lights a LED.
Now, use the two cables connect the LED cathode to the lead that closes the
circuit to GND.
72
NOTE: You don’t measure the current flowing through an
On the multimeter, you can Select the scale for measuring the current. Here,
we’ll set it to mA, but you can try switching to uA and measure it in
microamperes:
73
There you have 753uA, which are equal to 0.753mA.
Let’s see the same measurement using the small, yellow multimeter, in this
case we use the same port to measure small currents as the one we used for
voltage and resistance: And what do you get?
74
AND ELECTRONICS
But when you’re starting out you need a lot of little components that when
you buy them individually might cost a lot. It’s possible to get kits that
come with many, many things at once.
Precisely, I would recommend a kit made by Elegoo. It’s available on
Amazon for $53.99 at the time of writing. The name is of the kit is called
ELEGOO UNO R3 Project Most Complete Starter Kit with Tutorial
Compatible with Arduino IDE (63 Items).
It’s a box containing an Arduino Uno rev 3 clone board, and several other
components, sensors and small parts you’ll find very useful:
76
77
It contains a lot of useful items: a breadboard, lots of wires, resistors, LEDs,
a battery, a shield, a power supply module, a joystick, a water level sensor,
an LCD screen, a keypad, buttons, a stepper motor and a servo motor, an IR
receiver and transmitter, a relay, an ultrasonic sensor, etc.
Another good one, though not as complete as the first, is the ELEGOO
UNO Project Super Starter Kit with
78
www.electronic07.com
79
There are other similar kits out there that you can buy, but make sure the
board is an Arduino Uno, which is better to start with.
You also need to buy a multimeter. Any one will suffice. You can find one
to buy at your local tools store or on any place on the Internet.
That’s all you need to start with. You need to try first with cheap kit, then if
you like tinkering with electronics, it’s endless possibilities!
80
It’s cool to get a device that communicates with WiFi and Bluetooth like the
Arduino MKR Wifi 1010 and the Arduino
MKR GPS shield to work with GPS, for example.
81
BLINK A LED
Introduction to Arduino.
Now, we’re going to build the first Arduino project. We’ll turn a LED light
ON and OFF.
Also, you will learn how to create your first Arduino program, upload it to
the Arduino board via USB, and how
It’s quite a simple project, but you’ll learn a lot of stuff about Arduino if it’s
your first time.
If you already have an Arduino board, you can still use it.
The most important thing to take note of is that the board should work at 5V
I/O pins.
82
We can power it with a USB-B port, or a battery (a 9V battery works
perfectly since the recommended input voltage is 7-12V):
We’ve got a set of power pins and analog I/O pins on one
side:
83
and another set of digital I/O pins on the other side:
Now let’s build a simple circuit that will light a LED. We use a 1kΩ
resistor, a yellow 5mm LED, and we connect it to - and + as usual:
84
We connect + and - to the power pins of the Arduino that serve 5V and
GND:
85
Note that in this circuit, Arduino doesn’t do anything useful yet other than
scaling the 9V in the input provided by the battery to 5V.
Now let’s make the LED blink by writing our first Arduino program.
To do that, we have to first of all install the Arduino IDE on our computer.
1.
Go
86
2.
(i) On macOS: You need to move the Arduino app to your Applications
folder.
(ii) Check
3.
87
NOTE: The setup() function is only executed once, just after the program
starts, and that’s where we commonly set up
88
#define LED_PIN 13
void setup() {
pinMode(LED_PIN, OUTPUT);
void loop() {
digitalWrite(LED_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN, LOW);
delay(1000);
89
NOTE: HIGH and LOW are constants available by default in our Arduino
programs.
> Port menu the first time you start the program.
First of all, you need to save the file before you can compile the program
and write it on the Arduino. You’re free to save it anywhere you like. So
let’s create an Arduino folder in our Documents folder, and there store all
the Arduino programs we write.
Click the Upload button (the one with a right arrow in it) and the program
will be compiled and installed on the Arduino. The LED should start
blinking.
90
www.electronic07.com
Should you disconnect the USB cable from the computer, you will see the
LED will turn off because the Arduino is no longer powered.
charger, the LED will blink. The computer is no longer needed. The
Arduino will run the program we’ve loaded, just this program, it won’t run
anything else.
91
Once we have powered it, the program will automatically start and run.
92
CHAPTER NINE: THE ARDUINO BUILT-IN
LED
Arduino boards usually come along with a little utility: the built-in LED.
pin:
93
This LED is connected to the digital I/O pin #13 in majority of the boards.
In some other boards, like the Arduino MKR
Regardless, you can also reference the precise pin using the LED_BUILTIN
constant, that is always correctly
In order to light up the LED, you need to first of all set the pin to be an
output in setup():
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
or
digitalWrite(LED_BUILTIN, 1);
94
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
95
SUPPLY MODULE
battery, because it’s very simple to start with and it’s quite easy to find.
But it’s not optimal. First because the battery does not last long as
commonly advertised.
There are many different ones available, but the one I’ll show you below is
part of the Elegoo kit, although there are those from other brands in this
same shape and
96
97
The input voltage allowed is 6.5V - 9V DC. The maximum
98
99
Then on each side, you can choose which tension you would apply to the
power lines in the breadboard, independently: 5V like in this case:
100
From there, you can now connect to a 12V to 220V adapter
Press the gray button to turn it on. The LED will turn on as well:
101
The USB port you see there is not to power the power supply module. It’s
meant to power an USB Device if needed (like the Arduino, for example).
You also have other output pins on top that provide GND,
102
www.electronic07.com
103
CHAPTER ELEVEN: THE ARDUINO
CREATE PLATFORM
This is a cloud editor that lets you write and compile code directly in your
browser.
With this, you don’t have to install anything on your computer to create
your Arduino programs, other than a 104
It’s absolutely free, with a substantial free stage. Being a cloud tool, it has
the advantage of providing automatic backups and cross-device usage, plus
it works on Windows, Mac and Linux.
It
also
lets
you
share
programs
on
Project
After signing up, you are asked to install the plugin I mentioned earlier, and
once you run it, you’re good to go!
105
Look there on top, and you’ll find it already recognized the Arduino
Uno
connected
to
the
USB
port
The Examples menu lists a lot of pre-made examples with source code,
instructions and in some instances, you may have schematics and pictures
of the circuit:
106
You also have a handy “From libraries” tab on the same Examples menu
that lists examples that are provided by the Arduino libraries.
The Libraries menu lists the official Arduino libraries, in addition to lots of
community contributed ones.
107
One beautiful thing is that you can mark libraries as favorites and find them
very easily.
The next item you’ll see in the sidebar is the serial monitor, which helps to
debug and get information from your Arduino programs (as well as send
information to your board):
108
Next you have an inline help, which contains some tutorials, the preferences
and your usage quota of features like storage compilation time and the
number of projects.
200s of compilation time per day, after which you can upgrade to a paid
plan for $6.99/month which also includes advanced IoT features.
109
First, the Arduino must have WiFi connectivity, for example the Arduino
MKR WiFi 1010, (that’s the one I use) or the Arduino Uno WiFi Rev2 and
others.
We’re going to use the library WiFiNINA. This library is very handy. It lets
you connect to a WiFi network, and also create a WiFi network if you want.
#include <SPI.h>
#include <WiFiNINA.h>
NOTE: The SPI library is used by the WiFiNINA library, so load it as well.
SPI stands for Serial Peripheral Interface
110
The WiFiNINA library gives us the right to use, plus other things, the WiFi
object that we’re going to use.
Now let’s work on the body of the setup() function. This is the only
Arduino core function we’re going to implement (we’ll leave loop() empty).
void setup() {
First of all, we’re going to define two strings to hold the network name, the
SSID, and the network password.
www.electronic07.com
Since we’re using Arduino Create, we’ll just use constants that we’ll fill
separately in the Secret tab:
Arduino Create:
111
Serial.begin(9600);
while (!Serial);
After that, we put WiFi.begin() inside a loop that will check if its return
value is WL_CONNECTED, and keep retrying every 2 seconds until it is:
Serial.print("Connecting to ");
Serial.println(ssid);
delay(2000);
112
Once we’ve ended this loop, we are connected and we can call the
WiFi.localIP() method to collect the device IP
Serial.println(WiFi.localIP());
#include <SPI.h>
#include <WiFiNINA.h>
void setup() {
Serial.begin(9600);
while (!Serial);
113
Serial.print("Connecting to ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(5000);
Serial.println(WiFi.localIP());
void loop() {
114
Now we have access to other information, such as the network SSID with
WiFi.SSID() and the signal strength using WiFi.RSSI():
Serial.print(WiFi.RSSI());
other tasks.
if
(WiFi.firmwareVersion()
<
WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Firmware upgrade available");
to confirm if that’s the case. If you’re sure, connect to the board using the
Arduino IDE and then load the example sketch File -> Examples ->
WiFiNINA -> Tools -> FirmwareUpdater
115
At that point, open the Tools -> WiFi101 / WiFiNINA Firmware Updater
menu:
116
and press the Update Firmware button:
117
118
We’re going to connect to an existing WiFi network, and then, we’re going
to interact with the Arduino from our browser via HTTP.
This is very fascinating for a range of applications. From very simple check
of the sensors data, to performing some actions based on the HTTP request
performed.
#include <SPI.h>
#include <WiFiNINA.h>
void setup() {
119
Serial.begin(9600);
while (!Serial);
Serial.print("Connecting to ");
Serial.println(ssid);
delay(5000);
Serial.println(WiFi.localIP());
void loop() {
120
to set a TCP server on port 80 and when you are done with the end of
setup() call
server.begin();
Note that this is a TCP server, not an HTTP server. However, HTTP (which
is a TCP/IP application protocol) is built on top of TCP (i.e. the transport
layer), Therefore, we can build the HTTP server on our own quite simply.
void loop() {
121
if (client) {
▪ call client.available() to get the number of bytes available for reading (this
helps to ensure there is data to read)
▪ call client.read() to read one byte from the incoming
122
Then we can now start printing to the serial interface each character sent by
the client, and in the end we close the connection:
void loop() {
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
client.stop();
123
Attempt to upload this program on the Arduino. Point your browser to the
IP address. You will see something like this (below) printed to the serial
interface. This is what is sent by the browser:
GET / HTTP/1.1
Host: 192.168.1.40
Upgrade-Insecure-Requests: 1
Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,*/*
;q=0.8
Version/14.0.2 Safari/605.1.15
Accept-Language: en-us
Connection: keep-alive
Take note of the ending empty line. This is the end of the HTTP request.
124
www.electronic07.com
Thus, the simple algorithm is going to work, we just have to memorize 2
characters prior to the current one, and then check if we identify the
sequence \n\r\n (the last 3
void loop() {
if (client) {
char prevprev;
char prev;
while (client.connected()) {
125
if (client.available()) {
char c = client.read();
Serial.write(c);
prevprev = prev;
prev = c;
}
client.stop();
126
So we can now send the response inside the if, we may use client.println()
for this, and we then add a simple response like thus:
HTTP/1.1 200 OK
Content-Type: text/html
Connection: close
<!DOCTYPE HTML>
<html>
test
</html>
In this way:
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE HTML>");
127
client.println("<html>"); client.println("test");
client.println("</html>");
break;
is
going
to
end
the while
(client.connected()) {} block.
#include <SPI.h>
#include <WiFiNINA.h>
WiFiServer server(80);
void setup() {
Serial.begin(9600);
128
while (!Serial);
Serial.print("Connecting to ");
Serial.println(ssid);
delay(5000);
Serial.println(WiFi.localIP());
server.begin();
void loop() {
129
char prevprev;
char prev;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
130
client.println("test");
client.println("</html>");
break;
prevprev = prev;
prev = c;
client.stop();
Attempt this, you should see test showing up in the browser: 131
www.electronic07.com
The method works until you need to discover how, what the client asked us.
In that circumstance, you want to read each line, so this alternative method
works even better:
void loop() {
if (client) {
132
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
line += c;
if (c == '\n') {
if (line.length() == 0) {
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
133
client.println("test"); client.println("</html>");
break;
} else {
line = "";
}
}
client.stop();
In the last else we can check the line because the line is ended, and acts
accordingly to our requests.
134
CONCLUSION
Congratulations for coming this far! Now that we have successfully gone
through this course together, I believe you have acquired the basic
knowledge and skills in the field of electronics in general, and Arduino in
particular to make you that pro you’d like to be.
However, you need to constantly practice with other projects applying the
knowledge and skills you have acquired, as well as exploring other ways of
improving on what you have learned. Why? Because practice, they say,
makes perfect.
135
www.electronic07.com