100% found this document useful (2 votes)
238 views

Arduino 2021 Programming User Guide

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
238 views

Arduino 2021 Programming User Guide

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 163

ARDUINO 2021

PROGRAMMING
USER GUIDE

Learn How To Do Programming Using Arduino


And How To Build-Up Your Personal Arduino
With The Right Materials

John .A. Joseph

1
Table of Contents
Introduction to Arduino.................................................. 6
Chapter One ................................................................. 12
What Is the Use of Arduino? ........................................ 12
Arduino Uno Fire Up to Three Improvement Board ... 13
Arduino Uno Wifi Rev 2 Development Board ............ 16
Chapter Two ................................................................. 20
Introduction to Arduino Programming Language ........ 20
Backing for Other Languages ...................................... 24
Arduino Programming Language Built-In Constants .. 25
Arduino Numerical Constants ...................................... 26
The Arduino Programming Language Built-In Functions
...................................................................................... 27
Program Life Cycle ...................................................... 27
Dealing With I/O .......................................................... 27
Analog I/O .................................................................... 29
Time Functions ............................................................ 30
Mathematical Function................................................. 31
Use Alphanumeric Characters...................................... 32
Random Number Generation ....................................... 33
Use Bits and Bytes ....................................................... 34
Interrupts ...................................................................... 35
Chapter Three ............................................................... 36
Milli Micro Nano Pico ................................................. 36
Arduino MKR WiFi 1010 ............................................ 36

3
Form Factor ...................................................................37
Power ............................................................................38
Processor .......................................................................39
Memory .........................................................................40
IoT Connection .............................................................42
Security .........................................................................42
Li-Po Charging Circuit .................................................42
Price ..............................................................................43
Chapter Four .................................................................45
Introduction to Electronics ............................................45
Electronic Basics: Analog and Digital ..........................53
Basic Knowledge of Electronics: Current.....................59
Basic Knowledge of Electronics: Voltage ....................61
Basic Knowledge of Electronics: Vcc, Ground ............62
Basic Knowledge of Electronics: Resistance ................64
Basic Knowledge of Electronics: Short Circuit ............66
Basic Knowledge of Electronics: Your First Circuit ....67
Basic Knowledge of Electronics: Using A Multimeter 85
Chapter Five ..................................................................89
How to Measure Voltage ..............................................89
How to Measure Resistance ..........................................91
How to Measure Current ...............................................94
Using A Multimeter to Measure Voltage, Current, And
Resistance .....................................................................99
How to Measure Voltage ............................................101

4
How to Measure Resistance ....................................... 101
How to Measure Current ............................................ 102
Chapter Six ................................................................. 105
What to Buy to Start Using Arduino And Electronics 105
Arduino Project: Blink A LED .................................. 110
Chapter Seven ............................................................ 122
Arduino Built-In LED ................................................ 122
Breadboard Power Supply Module ............................ 125
Chapter Eight ............................................................. 136
Arduino Creation Platform......................................... 136
Chapter Nine .............................................................. 142
How to Connect to Wifi Network Using Arduino ..... 142
How to Run A Web Server on Arduino ..................... 149
Conclusion ................................................................. 164

5
Copyright 2021 © John .A. Joseph
All rights reserved. This book is copyright and no
part of it may be reproduced, stored or transmitted,
in any form or means, without the prior written
permission of the copyright owner.
Printed in the United States of America
Copyright 2021 © John .A. Joseph

6
Introduction to Arduino
Arduino is a breakthrough technology in the amateur
electronics ecosystem. Breakthrough does not mean
that it introduces a new powerful microprocessor or
something similar. It created an entire industry, a
movement, nothing.

I'm talking about the maker movement. Before the


birth of Arduino, electronic products were not as
interesting and easy to learn as they are today, and
there are not many resources around. I studied
electronics for many years at my technical high
school and then at the Polytechnic Institute of Milan,
but this is very serious, theoretical stuff, and there is
very little programming/fun.

Arduino was born in Ivrea, Italy, and the city is also


known as Olivetti. The company created the first
personal computer in 1965. Arduino was specially
designed as a learning device from the beginning.
This simple fact illustrates the initial design choices
made by the Arduino Core team. I will not go into the

7
historical details, but you can view them for free on
Wikipedia.

The key factor in Arduino's success is that it is


completely open-source and was the first to do so.
The hardware is open-source, and the software is
open source. For example, the schematic diagram of
the latest Arduino Uno development board Arduino
Uno WiFi Rev2 is available online.

This is cool because you can build your own Arduino


as needed. Organizations can construct and sell their
own Arduino clones, and they can do it. I got my first
Arduino from an organization called Elegoo. It
creates a great and cheap collection with many
components and tutorials to help you get started.

In addition, the entire ecosystem of tools, libraries,


and educational resources is all around, which has
made it a huge success. Prior to this, it was difficult
to obtain wood boards like this one because the
materials were too technical and aimed at
technicians rather than students. Arduino changed
8
all of this. Arduino has made an IoT cloud focus that
permits you to associate gadgets with the
organization.

After some time, the Arduino group delivered a few


diverse advancements loads up: Arduino Uno,
Arduino Mega, Arduino Diecimila, Arduino Robot,
Arduino Nano, Arduino Micro, Arduino Leonardo,
Arduino MKR, and so on.

Each board has its own utilization case. For instance,


Arduino Nano and Arduino Micro are truly
reasonable for IoT, wearable gadgets, and little
gadgets. Arduino Mega has more I/O pins and
memory than some other motherboard.

So far, the Arduino Uno board has been the best


board for learning. It is included in many kits and
used in many tutorials. The Arduino MKR WiFi 1010
development board has built-in WiFi and Bluetooth
functions, so it has been widely adopted in IoT.

9
As it stands, Arduino doesn’t have any operating
system, and it runs one program at a time. You don't
have to worry about anything because there is
nothing else but the program that runs on the
Arduino. Most Arduino development boards don't
even have a network connection. Some people do
this, such as Arduino Uno WiFi Rev 2 or Arduino
MKR WiFi 1010.

Once the program is fully loaded, you can then start


the program, don’t forget to supply power to the
Arduino so as to boost it through USB or via the
AC/DC power cord or you could simply use battery
from the power port. This therefore means that, as
soon as the program is loaded, it can simply be placed
on a mountain along with solar panels and of course
batteries, this will enable it to run until there is
power.

It can only run programs compiled for the Arduino


platform, which mainly refers to programs written in
the Arduino language, which is C++, with some

10
convenient functions, so that beginners can easily get
started.

However, you are not limited to this. If you could live


with the limitations of connecting the Arduino to the
USB port of your computer (or the Raspberry PI that
drives it), you can use the Johnny-Five project to run
Node.js code on it, which is pretty cool. Similar tools
are available in other languages, such as pyserial and
Gobot.

11
Chapter One
What Is the Use of Arduino?
First of all, learning electronics is great with Arduino.

Then, when you want to compile a program for it,


connect a battery or power connector and place it
where you want to run, and play with sensors and
other fun places to interact with the real world,
Arduino is incredible.

For example, I will use Arduino to power a self-


watering plant or track outdoor temperature, or
power some home automation equipment.

I will be chipping away at numerous undertakings


sooner rather than later, and I will present an
instructional exercise here to tell you the best way to
construct these things.

12
Arduino Uno Fire Up to Three
Improvement Board
Arduino Uno Rev 3 is a microcontroller board.

This is the reference Arduino version of the prototype


because many tutorials use this version of Arduino as
a reference board.

It has the same dimensions as Arduino 101, Arduino


Zero, Arduino Yún, Arduino Leonardo, Arduino Uno
WiFi rev 2, an Arduino Ethernet.
13
It has an 8-bit microcontroller ATmega328P with 32
KB of flash memory, 2 KB of SRAM, and 1 KB of
EEPROM.

The Arduino runs at a frequency of 16MHz under a


voltage of 5V and can be powered by the
recommended input range of 7V to 12V.

Regarding I/O, the Arduino Uno has a USB-B port,


which the computer can use to transfer new
programs to run, a set of I/O pins, and power input.
It has 14 digital pins, 20 I/O pins, and six analog pins
with 10-bits, with mapping values from 0 to 1023.

14
In the above picture, we can see 14 digital I/O pins on
the top, which can be configured as output pins or
input pins by programming. At the bottom, we have
power pins and six analog input pins.

You will notice that, there is a built-in LED pin that


is connected to pin 13, which can greatly help with
status information.

The Arduino Uno has a USB port that allows you to


connect it to a computer and load programs on the
computer. As we have said before, Arduino doesn’t
have any operating system but it runs one program
at a time.

Once the program is fully loaded, you can then start


the program, don’t forget to supply power to the
Arduino so as to boost it through USB or via the
AC/DC power cord or you could simply use battery
from the power port. This therefore means that, as
soon as the program is loaded, it can simply be placed
on a mountain along with solar panels and of course

15
batteries, this will enable it to run until there is
power.

Arduino Uno Wifi Rev 2


Development Board
Arduino Uno is a microcontroller board. It is right
now the reference variant of Arduino, and the most
recent rendition of the authority Arduino gadget is
Arduino Uno WiFi fire up 2.

16
It has a similar structure factor as Arduino 101,
Arduino Zero, Arduino Yún, Arduino Leonardo,
Arduino Uno, and Arduino Ethernet.

It has an 8-bit microcontroller ATmega4809 with 48


KB of flash memory, 6 KB of SRAM, and 256 bytes of
EEPROM. Compared with the previous ATmega328P
Arduino Uno rev three development board, its
memory is much larger. If there is an old Arduino
Uno development board around, please pay attention
to this change.

The Arduino runs at a recurrence of 16MHz under a


voltage of 5V and can be controlled by the prescribed
information scope of 7V to 12V.

As far as I/O, the Arduino Uno has a USB-B port,


which the PC can use to move new projects to run,
power input, and a bunch of I/O pins. It has 20 I/O
pins, 14 computerized pins, and six simple pins with
10-bits, with planning, esteems from 0 to 1023.

17
It has a WiFi module that underpins 802.11b/g/n,
specifically NINA-W13, which has an incorporated
TCP/IP convention stack that can give admittance to
WiFi networks dependent on equipment security.

Or on the other hand, you can allow the Arduino to


go about as a passageway with the goal that you can
associate with it.

In addition, it also comes with an integrated IMU


(Inertial Measurement Unit) that allows you to
implement some very cool motion sensor
applications, LSM6DS. This module provides a 3D
accelerometer and 3D gyroscope. Arduino Uno has
an integrated high-speed ADC analog-to-digital
converter.

The Arduino Uno has a USB port that allows you to


connect it to a computer and load programs on the
computer. Arduino doesn’t have any operating
system but runs one program after the other.

18
Once the program is fully loaded, you can then start
the program, don’t forget to supply power to the
Arduino so as to boost it through USB or via the
AC/DC power cord or you could simply use battery
from the power port. This therefore means that, as
soon as the program is loaded, it can simply be placed
on a mountain along with solar panels and of course
batteries, this will enable it to run until there is
power.

19
Chapter Two
Introduction to Arduino
Programming Language
How Do You Write A Program for The Arduino
Development Board?

Arduino itself supports a language called Arduino


programming language or Arduino language.

The language is based on the Wiring development


platform, and the Wiring development platform is
based on Processing. If you are not familiar with it, it
will be the basis of p5.js. There is a long history of
building projects on top of other projects in a very
open-source way. Arduino IDE is based on
processing IDE, and Wiring IDE is built on top of it.

When we use Arduino, we usually use Arduino IDE


(Integrated Development Environment). This
software can be used on all major desktop platforms
(macOS, Linux, Windows). It provides us with two
functions: programming with an integrated library

20
support Editor and a method to easily compile and
load our Arduino program onto the board connected
to the computer.

The Arduino programming language is a framework


built on C++. You can argue that it is not a real
programming language in traditional terms, but I
think it helps avoid confusing beginners.

The program written in the Arduino programming


language is called Sketch. Representations are
generally saved with the. ino expansion (from
Arduino).

The principal distinction from "ordinary" C or C++ is


that all code is wrapped into two fundamental
capacities. Obviously, you can have more than two,
yet any Arduino program should give in any event
two.

One is called arrangement (), and the other is called


circle (). Call the first once when the program is

21
begun, and the second is called over and over when
the program is running.

We don't have a main () function, just like you are


used to using it as the entry point of a program in
C/C++. After compiling the Sketch, the IDE will
ensure that the final result is the correct C++
program and will add the missing glue through
preprocessing.

Everything else is normal C++ code, and since C++ is


a superset of C, any valid C is also valid Arduino code.
One difference that can cause trouble is that
although the program can be built on multiple files,
these files must be in the same folder. If your
program becomes very large, it may become a
breakthrough limit, but by then, it can be easily
migrated to the native C++ setup.

Part of the Arduino programming language is a built-


in library that allows you to integrate with the
Arduino development board's functions easily.

22
Your first Arduino program will definitely involve
turning on the LED indicator first and then turning
it off. For this, you will use the pinMode (), delay (),
and digitalWrite () functions, as well as some
constants such as HIGH, LOW, OUTPUT.

In this way, the first standardized Arduino project


("Hello, World!”):

#Define LED_PIN 13

void setup () {

//Configure pin 13 as a digital output

pinMode (LED_PIN, output);

Invalid loop () {

//Turn on the LED

digitalWrite(LED_PIN, HIGH);

//Wait for 1 second (1000 milliseconds)

23
Delay (1000);

//Turn off the LED

digitalWrite(LED_PIN, LOW);

//Wait for 1 second

Delay (1000);

The preceding is all part of the Arduino


programming language, or we better call it a kit or
library.

Backing for Other Languages


As a reminder, I want to point out that you are not
limited to programming Arduino using this language
and IDE. There is a project, among other things; you
can use the Johnny-Five project to run Node.js code
on it, use pyserial and Gobot to do Python code with
Gobot. Still, the Arduino programming language is
undoubtedly the most of the tutorials you will see

24
based on that language Because this is the native and
standardized method of using these devices.

Arduino Programming Language


Built-In Constants
Arduino sets two constants that we can use as
constants

HIGH is equal to high voltage, and low voltage


depends on hardware (on 3.3V board, such as
Arduino Nano,> 2V; on 5V board, such as Arduino
Uno,> 3V), it depends on the hardware. LOW is at
low voltage. Again, the exact value depends on the
circuit board used

At that point, we can join three constants with the


pinMode () work:

• INPUT sets the pin as an info pin


• OUTPUT sets the pin as a yield pin
• INPUT_PULLUP sets the pin as an inside pull-
up resistor

25
Another steady we have is LED_BUILTIN, which
focuses on the pin's quantity on the board, which is
typically equivalent to 13.

Moreover, we additionally have C/C++ constants,


valid and bogus.

Arduino Numerical Constants


• M_PI steady pi (3.14159265358979323846)
• M_E steady e
• M_LN10 the normal logarithm of the number
10.
• M_LN2 the normal logarithm of the number
2.
• M_LOG10E e is the base-10 logarithm.
• M_LOG2E e is the base two logarithms.
• The square root 2 of M_SQRT2.
• NAN the NAN (not a number) steady.

26
The Arduino Programming
Language Built-In Functions
This part will give a reference to the implicit
capacities given by the Arduino programming
language.

Program Life Cycle


• Setup () This capacity is called once when the
program begins and when the Arduino is
closed down and restarted.
• circle () calls this capacity consistently while
the Arduino program is running.

Dealing With I/O


The accompanying capacities help to deal with the
info and yield of the Arduino gadget.

Digital I/O

• digitalRead () peruses the incentive from the


advanced pin. Acknowledge the PIN as a

27
boundary, and return the HIGH or LOW
consistent.
• digitalWrite () composes the HIGH or LOW
an incentive to the computerized yield pin.
You pass the PIN and HIGH or LOW as
boundaries.
• pinMode () sets the pin as input or output.
You pass the PIN and INPUT or OUTPUT
value as parameters.
• pulseIn () reads the digital pulse from LOW to
HIGH on the pin, and then reads the digital
pulse from LOW, or from HIGH to LOW, and
then reads the digital pulse from HIGH. The
program will block until a pulse is detected.
You can specify the PIN and pulse-type (LHL
or HLH) to be detected. You can specify an
optional timeout period to stop waiting for
the pulse.
• pulseInLong () is the same as pulseIn (), but
the implementation is different, and if the
interrupt is closed, you cannot use it. Usually,

28
turn off interrupts to get more accurate
results.
• shift () reads one byte of data from one pin at
a time.
• shiftout () writes data bytes one byte at a time
to one pin.
• Tone () sends a square wave on the pin for the
buzzer/speaker to play the tone. You can
specify the pin and frequency. It applies to
digital and analog pins.
• noTone () stops the waveform generated by
tone () on the pin.

Analog I/O
• analogRead () reads the value from the analog
pin.
• AnalogReference () configures the value of the
highest input range for analog input. By
default, it is 5V on the 5V board and 3.3V on
the 3.3V board.

29
• AnalogWrite () writes the analog value to the
pin
• AnalogReadResolution() allows you to change
the default analog bit resolution of
analogRead (), which is 10 bits by default. Only
applicable to specific devices (Arduino Due,
Zero, and MKR)
• AnalogWriteResolution () allows you to
change the default analog bit resolution of
analogWrite (), which is 10 bits by default.
Only applicable to specific devices (Arduino
Due, Zero, and MKR)

Time Functions
• delay () specifies the number of milliseconds
for the program to pause as a parameter
• delayMicroseconds () specifies the number of
microseconds for the program to pause as a
parameter

30
• Micros () The quantity of microseconds since
the program started. Reset due to overflow
after about 70 minutes
• millis () The number of milliseconds since the
program was started. Reset due to overflow
after about 50 days

Mathematical Function
• abs () the absolute value of a number
• Constrain () restricts the number to a range;
see usage
• Map () remaps numbers from one range to
another; see usage
• Max () the maximum of two numbers
• Min () the minimum of two numbers
• Pow () the value of the number raised to the
power
• sq () the square of a number
• sqrt () the square root of the number
• cos () the cosine of the angle
• Sin () the sine of the angle

31
• Tangent of a tan () angle

Note: If needed, there are more built-in math


functions; please record here.

Use Alphanumeric Characters


• isAlpha () checks whether the character is a
letter (letter)
• isAlphaNumeric () checks whether the
character is alphanumeric (letter or number)
• isAscii () checks whether the character is an
ASCII character
• isControl () checks whether char is a control
character
• isDigit () checks whether the character is a
number
• isGraph () checks whether char is a printable
ASCII character and contains content (for
example, it is not space)
• isHexadecimalDigit () checks whether the
character is a hexadecimal digit (A-F 0-9)

32
• isLowerCase () checks whether the character
is a lowercase letter
• isPrintable () checks whether the character is
a printable ASCII character
• isPunct () checks whether the character is a
punctuation mark (comma, semicolon,
exclamation point, etc.)
• isSpace () checks whether char is a space, form
feed \f, line feed \n, carriage return \r,
horizontal tab \t, or vertical tab \v.
• isUpperCase () checks whether the character
is an uppercase letter
• isWhitespace () checks whether char is a
space character or a horizontal tab \t

Random Number Generation


• Random () generates pseudo-random
numbers
• RandomSeed () initializes the pseudo-random
number generator with an arbitrary initial
number

33
In Arduino, like in most languages, it is impossible to
get a truly random number, and the sequence is
always the same so that you can use the current time
as the seed for the seed, or (for Arduino) you can read
from the analog port enter.

Use Bits and Bytes


• bit () calculates the value of a bit (0 = 1, 1 = 2, 2
= 4, 3 = 8...)
• bitClear () clears (sets to 0) a numeric variable.
Accepts a number, and the number of digits
starts from the right
• bitRead () reads a digital bit. Accept a number,
and the number of digits starts from the right
• bitSet () sets one bit of the number to 1.
Accepts a number, and the number of digits
starts from the right
• bitWrite () writes 1 or 0 to a specific bit of a
number to accept a number; the number of
the bit starts from the right and the value to
be written (0 or 1)

34
• highByte () Get the high (leftmost) byte of the
word variable (with 2 bytes)
• lowByte () gets the low (rightmost) byte of the
word variable (with 2 bytes)

Interrupts
• noInterrupts () disable interrupts
• interrupts () re-enables after disabling
interrupts
• attachInterrupt () allows digital input pins to
be interrupted. Different boards have
different allowed pins, and please check the
official documentation.
• detachInterrupt () disables interrupts enabled
with attachInterrupt ()

35
Chapter Three
Milli Micro Nano Pico
Through electronic and time measurement, you will
find frequently used terms: nano, micro, nano, pico.

This article is a quick reference to their respective


meanings.

• Milli = 10 ^ -3 = 0,001
• Micro = 10 ^ -6 = 0,000 001
• Nano = 10 ^ -9 = 0,000 000001
• Pico = 10 ^ -12 = 0, 000 000 000 001

I use the order to remember what they mean: repeat


it a couple of times, nano, and pico. Next time you
will remember them.

Arduino MKR WiFi 1010


This is the first motherboard with built-in WiFi and
Bluetooth connections. I am very excited about the
opportunities and possibilities it brings.

36
In this article, I want to give an overview of the
development board and compare it with the Arduino
Uno, which is another Arduino development board I
currently own and the one most commonly used by
beginners.

Form Factor
Let's start with the appearance.

It measures 61.4mm x 25mm and weighs only 32


grams.

37
Compared with the Arduino Uno development
board, the Arduino MKR WiFi 1010 is much smaller.
More than two times smaller.

Power
Arduino MKR WiFi 1010 can be powered by 5V input
or a Micro-USB connector.

38
Its working voltage is 3.3V. This is a very important
difference from the Arduino Uno, which works at 5V.
The maximum voltage that the I/O pins can support
is 3.3V.

Processor
The Arduino MKR WiFi 1010 development board is
equipped with a low-power SAMD21 Cortex-M0 + 32-
bit ARM processor.

The processor is 32 bits. The Arduino Uno


development board has an 8-bit ATmega328
processor.

39
Memory
The development board has 256KB flash memory and
32KB SRAM.

I/O Pins

Arduino MKR WiFi 1010 provides:

• 8 digital I/O pins, pins 0-7.


• 7 analog input pins (ADC 8/10/12 bits), pins A0
/ A6.
• 1 analog output pin (DAC 10-bit), this pin is
identified by DAC0/A0.
40
The DC current of each I/O pin is 7mA.

Pins 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 18, 19 are PWM pins.

41
The complete pin diagram can be found here.

IoT Connection
Arduino MKR WiFi 1010 provides a WiFi module,
namely WiFi U-BLOX NINA-W10 series low power
2.4GHz. It supports 802.11 b/g/n protocol and also
provides Bluetooth Low Energy (BLE).

Security
The evaluation board has an encryption chip, which
can realize the SHA-256 secure connection, namely
ATECC508.

Li-Po Charging Circuit


When charging the development board through the
Micro-USB port, the Arduino MKR WiFi 1010 can
charge an external battery through its Li-Po charging
circuit.

The battery must be a 3.7V lithium battery with a


minimum of 700mAh.

42
When the USB power is disconnected, the device will
automatically switch to an external battery.

Price
In terms of price, Arduino MKR WiFi 1010 is more
expensive than Arduino Uno Rev3 board but cheaper
than Arduino Uno WiFi Rev2.

At the time of writing, in the official Arduino store:

• Arduino MKR WiFi 1010 is €29.90


• Arduino Uno rev3 is €20.00
• Arduino Uno WiFi rev2 is €38.90

If we think that the Arduino Uno Rev3 development


board lacks WiFi/Bluetooth connection, it is not at
the same level-this is not an IoT device.

43
Therefore, we can say that the Arduino MKR WiFi
1010 is 9 euros cheaper than its Uno IoT counterpart,
the Arduino Uno WiFi rev2, which can be considered
as its educational version.

44
Chapter Four
Introduction to Electronics
This is a region that I am exceptionally enthusiastic
about, and I trust it resembles programming. We
change contributions to yields and make valuable
frameworks by joining straightforward segments into
more perplexing designs.

This is what we do in the programming software, but


it is more practical. We can stay on the screen for a
while to make a craft that allows us to create actual
real projects. When you add a programmable board
like Arduino, it is the actual embedded low-level
programming.

In the past, I only talked about electronic products,


but not much. I particularly talked about Arduino in
the introduction to Arduino and wrote an
introduction to the Arduino programming language.

Then, I introduced three popular boards:

45
• Arduino Uno version 3
• Arduino Uno WiFi version 2
• Arduino MKR WiFi 1010

Then I compared the Arduino with another very


popular device, the Raspberry Pi (but completely
different!).

This is the extent to which I cover electronic products


on this blog.

Everything is high. Arduino is a very easy-to-use


development board, but it also abstracts many tiny
details and provides many built-in functions in the
background.

Arduino is a very interesting starting point for all


electronic products. In any instance, there is as yet
far to go to follow the "Light up the LED"
instructional exercise to comprehend your work.

My work for this arrangement is to give a more top to


a bottom prologue to gadgets. It is unique in relation
to the standard method of perusing in books. I need
46
to be exceptionally down to earth and dodge all the
hypotheses and science behind it.

Theoretical the little subtleties, however, apply these


ideas to rehearse. For what reason did I begin
discussing hardware? All things considered, on the
grounds that I like to talk, read, watch recordings,
and on the grounds that I like to make with electronic
gadgets and electronic segments.

When you have an essential comprehension of


hardware and power, the entire world will be opened.

In the past ten years, we have gone from a society


where computers are installed on desks to a society
where everyone has powerful Internet-connected
devices in their pockets.

With the adoption of 5g, cheap mobile networks, and


the continuous development of renewable energy, we
will be completely immersed in more electronic
products than today, and understanding all the
working principles is a very valuable skill.

47
This skill can bring you a high salary, but it can also
make you very satisfied with DIY and handicrafts.

Building your equipment and tools and watching


them actually work can make you proud of yourself
in the real world. Maybe you will not learn how to
build the next iPhone, but maybe you will learn how
to create a simplified computer version. Or how to
develop circuits to do cool things.

Compared to other more expensive hobbies, jumping


on it is quite cheap.

I am a computer programmer and engineer. I studied


electronics in high school and university but starting
from this series. I want to relearn it with you.

First, we will learn more about Analog Electronics.

We will discuss the basics first:

• Analog and digital


• Current
• Voltage

48
• Vcc, ground
• Resistance
• Short circuit
• Your first circuit
• Use breadboard to make prototypes
• Use a multimeter to quantify resistance,
current, and voltage

Then, we will delve into electronic components:

• Resistance
• Light Emitting Diode (LED)
• Diode
• Button
• Potentiometer
• Capacitor
• Inductor
• Relay
• Transformers
• Transistor
• Thyristor
• 7-segment display
49
• LED display
• switch
• LCD Monitor
• Photodiode
• Buzzer and speaker

Then, we will explore the wonderful world of sensors,


allowing us to interact and obtain data from the
outside world, including:

• Thermistor
• Thermocouple
• Tilt sensor
• Accelerometer
• Force sensor
• Vibration sensor
• Humidity Sensor
• Sound sensor
• Photoresistance
• Photoresistance
• Phototransistor
• Magnetic sensor
50
• Proximity sensor
• Microphone
• Keyboard

We will learn how to use them and how to do some


interesting things with the Arduino development
board.

Then, we will study the method of using a motor to


animate objects:

• Servo motor
• Stepper motor

Then, we will delve into digital electronics.

We will learn:

• Binary arithmetic
• Boolean algebra
• Logic Gate
• Combinational logic
• Use integrated circuits
• Multiplexer and demultiplexer
51
• Encoder
• Flip flops
• Arithmetic logic unit
• Clock generator
• Counter
• Register
• Memories

We will learn how to connect the digital world with


the analog world through a converter.

At the same time, we will deal with simple projects


first. As time goes by, the complexity and fun will
increase.

When we add programmable devices to the mix, the


real fun begins, especially since we will talk a lot
about Arduino.

We shall learn how to use the Arduino language


(C++) and other languages (such as CircuitPython
and Johnny-Five) to program Arduino from beginner

52
to advanced level. We will work on very good projects
that use it.

Electronic Basics: Analog and


Digital
We divide electronics into two parts: analog and
digital.

Everything in the natural world is simulated.


Temperature, light, distance, speed, humidity, sound
is almost all measured with almost unlimited values
and accuracy.

The simulation is natural. However, digitization is


artificial. In the process of ancestors seeking to
understand nature and create man-made systems
and simulations, humans put forward digital
measurement and numerical value concepts.

The digital representation can only assume two


states: open or closed. 1 or 0.

53
Using only 0 and 1 values to represent basic values
can simply solve complex problems and ultimately
led us to create things such as computers,
smartphones, and the Internet.

We can combine multiple binary values to represent


numbers with more than two states. Using two
numbers, we can define four states, where three
numbers are 8, 4 numbers are 16, and so on.

Then, we use specific protocols and conventions to


express values.

For example, we can use a series of bits to represent


a decimal number:

One can be expressed as 1×20

10 can be expressed as 1×21 + 0×20

111 can be expressed as 1×22 + 1×21 + 1×20

54
You can delete or add the leading zeros in the
numbers because they do not mean that anything in
the upper left corner 1:1:1 can represent 0110 or
00000110 (if needed). It has the same exact meaning
because, as explained by the system above, we only
need to multiply the power of 2 by zero.

According to the value we need to represent, we need


to have enough numbers to represent enough
numbers.

If we want to have 16 values, then we can count from


0 to 15, and we need four digits (bits). Using five
digits, we can calculate 32 numbers. Thirty-two bits
will give us 4,294,967,296 possible numbers.

64 bits will provide us with 9,223,372,036,854,775,807


possible numbers.

This is a simple conversion table of the first four


digits. We only need to use two digits to generate:

55
Decimal number Binary number

0 00

1 01

2 10

3 11

This is a simple conversion table for the first 8 bits:

Decimal number Binary number

0 000

1 001

56
2 010

3 011

4 100

5 101

6 110

7 111

If you notice, I repeated the above sequence and


added 1 instead of 0 to the sequence from 4 to 7.

This is a simple conversion table for the first 16 bits:

Decimal number Binary number

57
0 0000

1 0001

2 0010

3 0011

4 0100

5 0101

6 0110

7 0111

8 1000

9 1001

58
10 1010

11 1011

12 1100

13 1101

14 1110

15 1111

Basic Knowledge of Electronics:


Current
Electric current actually is the flow of electrons
between two points with different voltages.

The unit is ampere (A).

59
We have two kinds of electric current: alternating
current (AC) and direct current (DC) In direct
current (DC), current flows in one direction, which is
the current type produced by a battery.

In alternating current (AC), the current will


periodically change direction. This is the current
provided by the grid that we connect to the house,
and sockets are added to the house's walls.

These two different current types have very different


characteristics, and they allow very different uses and
applications. For example, DC is not conducive to the
long-distance transmission of electric current. We
use AC instead. Most electronic equipment runs on
direct current.

This is why we use rectifiers to convert alternating


current to direct current.

We can also use devices called power inverters


commonly used in recreational vehicles to do the
opposite.

60
Basic Knowledge of Electronics:
Voltage
What is voltage?

We can simplify the voltage concept to the electric


potential at which electrons must flow from a higher
voltage area to a lower voltage area.

We measure in volts (V).

The more the voltage, the more electrons flow in the


circuit: this is what we call current.

Imagine a circuit with LED lights. You can use a 1.5V


battery to power it to make the LED light up. If you
add a 3V battery, more current will flow in the circuit
so that the lamp will emit more light.

The electric potential of the earth is zero, that is, 0V


(zero volts). We call it the ground.

61
Voltage is a relative measure. It measures the
potential difference between two points. So, say 5V,
which is relative to the ground.

Most commercial DC (direct current) electronic


equipment will operate at 5V or 12V.

AA batteries can provide 1.5V power. This means that


the + pole voltage ratio of the battery is 1.5V
extremely high. Some devices will use multiple
batteries connected to each other to provide a greater
voltage, for example, two batteries in series with 3V.

Your house uses 230V (Europe) or 110V (United


States) AC (Alternating Current) for wiring. Different
countries/regions may have different values.

Basic Knowledge of Electronics:


Vcc, Ground
In all projects and explanations, I will use various
terms to identify the same thing.

62
Especially worth mentioning are -0V, battery
negative, ground, GND, and Vss.

These all mean that the negative electrode of the


battery is 0V.

At the other end of the spectrum, I mentioned +, Vcc,


Vdd, or positive.

This refers to the battery's positive electrode, and its


exact value will vary depending on the battery or
power source used: 9V, 3.3V, 5V, etc.

Why are there so many words? Different series of


electronic components use different acronyms:

• TTL (transistor-transistor logic) components


use Vcc and GND.
• CMOS (Complementary Metal Oxide
Semiconductor) components use Vdd and
Vss).

63
Basic Knowledge of Electronics:
Resistance
If the + and-poles of the battery are connected to
each other, too much current will flow, and the
battery will be damaged. The current is too large to
handle. We need to introduce the concept of
resistance, that is, the limitation of current.

The resistance limits the current flowing in the


circuit. Each component in the circuit has a certain
resistance. Even the wires have resistance, but the
resistance is very low.

The resistance is measured in ohms (Ω).

1 ohm is clearly defined as 1 volt when it’s divided by


1 ampere:

1Ω = 1 V / 1 A

This is what we call Ohm's law: R = V / I, where R is


the symbol of resistance, V is the symbol of voltage,
and I is the symbol of current.

64
From this we can draw:

• V=R*I
• I=V/R

Resistors are components designed to provide a


certain amount of resistance.

We have various resistance values. You will find that


the common resistors used in circuits are 220Ω, 1kΩ,
4.7kΩ, 10kΩ, etc.

According to Ohm's law, when you know the


battery's voltage and the resistance provided by the
circuit components, we can calculate the current
flowing in the circuit.

If the battery provides 5V and the circuit provides a


1kΩ resistor, the current flowing will be 5mA.

65
Basic Knowledge of Electronics:
Short Circuit
In an electronic system, we can make different
components perform different tasks and combine
them to form a circuit.

We have a voltage source in a regular circuit, and our


gear likewise has a few employments.

The source of the voltage can be a battery or an


Arduino board that gives power. Assume that the
voltage is 5V. We have a positive electrode and a
negative electrode. If we directly connect these two
poles with wires, a short circuit will occur.

A short circuit is an exceptionally bad situation and


one of the most common causes of circuit problems.
We will take various precautions in practice because
it will seriously damage our equipment and
components and make them unusable.

Even with a 1.5V battery, the amount of current


flowing between the positive and negative electrodes

66
can cause a fire (I don't recommend trying, but it is a
cool ignition method when you forget the lighter in
camping.).

Imagine what happens when you use a larger battery.


If the battery is short-circuited, some batteries
(including those used in mobile phones) will explode.

To make a long story short, avoid short circuits in any


way possible and attract attention.

Basic Knowledge of Electronics:


Your First Circuit
One of the simplest circuits that work correctly that
we can build is the circuit that lights up the LED.

We will use a 9V battery, 470-ohm resistor and LED.

We will discuss resistors and LEDs in more detail


later, but now let's create the first circuit.

67
Tinkercad from Autodesk, the creator of the famous
AutoCAD, allows you not only to create and simulate
electronic circuits but also to create and simulate
electronic circuits and create 3D printing designs.
This is a very cool web application.

Create a free account on the website and select the


"Circuits" menu from the dashboard:

68
Click the Create New Circuit button, and you will see
the circuit builder interface:

69
Now you can drag and drop components in the right
sidebar of the main screen.

Choose a 9V battery, a resistor, and an LED:

Now, hovering your mouse over each item will show


you the connections you can create from or with
them. For example, a battery has a positive electrode
and a negative electrode:

70
Drag the negative pole to one of the resistors:

71
Now, connect the resistor to the cathode called the
LED on the left. The cathode is straight.

Finally, connect the anode (the right pin of the LED)


to the anode of the battery:

You can click on them to change the color of the


wires:

72
You can change the settings of each component with
a single click. For example, click on a resistor to find
out and change its resistance value, which is 1kΩ by
default:

73
Double-click the wire to add a point on it so that you
can make a better circuit:

74
After sorting it out, the results are as follows:

75
Now, click the "Start Simulation" button. Before
clicking "Stop Simulation", you will see the LED light
up:

Now try to change the resistance to 220Ω:

76
Run the simulation again, you will see a warning on
the LED, informing you that the current flowing
through is too large, and the recommended
maximum current that the LED can handle is 20mA:

77
It's possible to change the value of the resistor while
the simulation is running. If you write 1000Ω instead
of 220Ω, you will see a decrease in the LED's
brightness.

Add 10000Ω, and the LED will now display all light.

The higher the resistor's resistance, the less current


flows in the circuit, so the LED consumes less
current, thus showing bright light.

78
Remember Ohm's law, I = V / R: The current flowing
through the circuit using a 220Ω resistor is 9/220 =
40mA.

Using a 1kΩ resistor, the current is 9⁄1000 = 9mA.

This is the same circuit, but in the real world:

Basic Knowledge of Electronics: Prototyping


Using Breadboards

In this image, you can see a simple circuit with


batteries, resistors, and LEDs.

79
The elements are displayed in a small white box
called a breadboard:

80
The breadboard has 17 groups of 5 interconnected
elements on one side and 17 groups of interconnected
elements on the other side.

Under the surface, the five holes in a group are


connected to each other to establish electrical
connections easily.

This is a small circuit board that can be used for


simple prototyping.

This is a bigger board:

81
The principle is the same; we have more elements on
the outer boundary, these elements are wrapped in
red and blue lines:

82
In this case, these items are longitudinally
orthogonally connected to the five-element groups
inside the board:

They are used to connect the battery's positive and


negative poles (or any other power source) to the
board so the board's elements are easily accessible.

Usually, the red wire is used for the + positive pole,


and the black wire is used for the negative pole:

83
Usually, you use a breadboard to make a circuit
prototype:

84
Once you are set to move on, you can solder it to the
perforated board.

Basic Knowledge of Electronics:


Using A Multimeter
A digital multimeter is a convenient tool, one of the
few tools you need to use to get started.

There are different kinds of multimeters, from very


cheap multimeters (about 10 US dollars) to this price,
it is very cheap to buy, but there are also very cheap
feelings:

85
Unless you don't need a very professional service, you
can buy a great product for less than $30.

I paid the 30 dollars, and I did a good job:

There is a big difference in size and build quality


between these two:

86
You can also see that one has a 10A port, and the
other has a 20A port. This means that one person can
measure up to 20 amps before disconnecting the fuse,
and the other half can measure.

The larger one mA port can measure the maximum


current of 500mA, and the smaller one can measure
200mA.

Similarly, one can also use a special cable to measure


temperature. It has a light, and so on.

87
The digital multimeter can measure voltage
(voltmeter), current (amperemeter), resistance
(ohmmeter), capacity, frequency, etc.

It has many built-in tools.

I will show you how to measure the first three items.

88
Chapter Five
How to Measure Voltage
Let's start measuring the voltage. Take the battery,
connect and select the V symbol:

And connect the black connector to COM, common


ground, connect the red connector to the V symbol,
and then connect the other end of the cable to the +
and-battery terminals:

89
90
How to Measure Resistance
Now, let us see how to measure resistance.

Connect the two cables to both ends of the resistor,


and then select the Ω symbol on the multimeter:

This is a 220Ω resistor.

This is the same measurement made on a cheap


multimeter:

91
If it's not too much trouble, note that already we
didn't need to set the scale. It was resolved
consequently. Here, if the opposition is excessively
low or excessively high for the scale, you need to
change between 200 2000 20k 200k 2000k to see
which one can bring you significant outcomes.

92
For instance, here, I picked a size of 20k, and it is
shown as 0.22. 20k means it can quantify protections
up to 20kω. For this situation, 0.22 means 0.22 of 1kω:

It's somewhat confounding, correct? This isn't the


most ideal decision, so I recommend picking a
multimeter that can naturally decide the scale.

93
How to Measure Current
I told you the best way to gauge voltage and
opposition first since they work correspondingly: the
connector is put corresponding to the thing we need
to quantify.

The measured current is different. We need to


connect the multimeter in series so that the current
flows through it.

Also, depending on your multimeter, you may need


to change the entry point of the cable. In this case, I
use input #4 to measure volts and Ω, but use input #1
(and a lot of current through input #2) to measure
current:

94
To measure the current, we build a small circuit. In
this case, I have a potentiometer to light up the LED.

95
Two links associate the LED cathode to the lead that
shuts the circuit to GND. This is significant: don't
quantify the current moving through the component
by interfacing the multimeter straightforwardly to its
link. It's necessary to make the multimeter part of the
circuit.

96
The multimeter goes about as a wire.

On the multimeter, you can choose the size of the


current estimation. We set it to mA, yet attempt to
change to uA to quantify in microamperes rather
than milliamps:

97
You get 753uA, which is equal to 0.753mA.

This is the same measurement made with a yellow


multimeter. In this case, the ports used to measure
small currents are the same as the ports we used for
voltage and resistance:

98
A Multimeter Tool Can be used to
take the Measure Voltage, Current,
And Resistance
A digital multimeter is a convenient tool, one of the
few tools you need to use to get started.

There are various kinds of multimeters, from very


cheap multimeters (about 10 US dollars) to this price,
it is very cheap to buy, but there are also very cheap
feelings:

99
Unless you don't need a very professional service, you
can buy a great product for less than $30.

There is a big difference in size and build quality


between these two:

You can also see that one has a 10A port, and the
other has a 20A port. This means that one person can
measure up to 20 amps before disconnecting the fuse,
and the other half can measure.

The larger one mA port can measure the maximum


current of 500mA, and the smaller one can measure
200mA. Similarly, people can also use a special cable
to measure temperature. It has a light, and so on.

Digital multimeters can measure voltage (voltmeter),


current (amperemeter), resistance (ohmmeter),
capacity, frequency, etc.

It has many built-in tools. I will show you how to


measure the first three items.

100
How to Measure Voltage
Let's start measuring the voltage. Take the battery,
connect and select the V symbol:

And connect the black connector to COM, common


ground, connect the red connector to the V symbol,
and then connect the other end of the cable to the +
and-battery terminals:

How to Measure Resistance


Now, let us see how to measure resistance.

Connect the two cables to both ends of the resistor,


and then select the Ω symbol on the multimeter:

This is a 220Ω resistor. This is the same measurement


made on a cheap multimeter:

Please note that previously we didn't have to set the


scale. It was determined automatically. In the event
that the resistance is too high or too low for the scale,
you need to adjust between 200 2000 20k 200k 2000k

101
points to see which one can bring you meaningful
results.

For example, here, I chose a scale of 20k, and it is


displayed as 0.22. 20k means it can measure
resistances up to 20kΩ. In this case, 0.22 means 0.22
of 1kΩ:

It's a bit confusing, right? This is not the best choice,


so I suggest choosing a multimeter that can
automatically determine the scale.

How to Measure Current


I showed you how to measure resistance and voltage
first because they work similarly: the connector is
placed parallel to the item we want to measure.

The measured current is different. We need to


connect a multimeter in series to make the current
flow through it.

Also, depending on your multimeter, you may need


to change the entry point of the cable. In this case, I

102
use input #4 to measure volts and Ω, but use input #1
(and a lot of current through input #2) to measure
current:

To quantify the current, we fabricate a little circuit.


For this situation, I have a potentiometer that can
illuminate the LED.

Two links interface the LED cathode to the lead that


shuts the circuit to GND. This is significant: don't
gauge the current coursing through the component
by associating the multimeter straightforwardly to its
link. There is a need to make the multimeter part of
the circuit.

The multimeter goes about as a wire. On the


multimeter, you can choose the size of the current
estimation. We set it to mA, yet attempt to change to
uA to gauge in microamperes rather than milliamps:

You get 753uA, which is equal to 0.753mA. This is the


same measurement made with a yellow multimeter.
In this case, the ports used to measure small currents

103
are the same as the ports we used for voltage and
resistance:

104
Chapter Six
What to Buy to Start Using Arduino
And Electronics
In my e-tutorial series, I did use a set of tools that I
purchased after a while.

I can provide you with some buying suggestions to


help me start experimenting.

Arduino is an amazing project. I strongly recommend


that you purchase the original parts of Arduino to
support the project and the entire ecosystem.

However, in the beginning, you need to buy many


small parts that are purchased separately so that it
may cost a lot. You can find many kits that come with
many things at once.

In particular, I recommend using a kit made by


Elegoo. You can find it on Amazon for $53.99/45€ at
the time of writing. It is called the complete starter
kit for ELEGOO UNO R3 projects, with tutorials
compatible with Arduino IDE (63 projects).
105
It is a box containing an Arduino Uno rev three clone
board and many useful components, sensors, and
small parts:

106
It contains many things: breadboards, many wires,
resistors, LEDs, batteries, shields, power modules,
water level sensors, keyboards, LCD screens, buttons,
joysticks, stepper motors, servo motors, infrared
receivers and transmitters, relays, Ultrasonic sensors,
and so on.

Another very complete and cheap (but seems to have


fewer components and sensors) is the ELEGOO UNO
project super starter kit with tutorials and UNO R3
compatible with Arduino IDE.
107
108
I bought one of them for a friend of mine, and it was
so cool.

There are other similar kits, but please make sure the
development board is Arduino Uno. This is the best
place to start.

Then, I suggest you buy a multimeter. Any


multimeter can work normally. You can buy a
multimeter at your local tool store or anywhere on
the Internet.

That's it. You need to try to use cheap toolkits first


and then try to modify electronic products; this is the
limit.

For example, you can get devices that communicate


with WiFi and Bluetooth, such as Arduino MKR WiFi
1010 and Arduino MKR GPS shield, which can be used
with GPS.

109
Arduino Project: Blink A LED
I acquainted Arduino in Introduction with Arduino.

In this instructional exercise, I need to construct the


main Arduino project. We will kill on and the LED
lights.

You will figure out how to make your first Arduino


program, transfer it to the Arduino board by means
of USB, and keep in touch with computerized I/O
pins.

This is a straightforward undertaking; however, you


will become familiar with a ton about Arduino on the
off chance that you are the first run-through.

I will utilize the Arduino Uno fire up, three clone


sheets to finish this instructional exercise.

If you as of now have an Arduino improvement


board, you can use the current progression board.
The huge thing to note here is that the board should
manage 5V I/O pins.

110
This is the board:

We can control it through the USB-B port or battery


(9V battery functions admirably on the grounds that
the suggested input voltage is 7-12V):

111
We have a bunch of force pins and simple I/O pins
on one side:

112
There is also a set of digital I/O pins on the other side:

Let's build a simple circuit that lights up the LED. We


use a 1kΩ resistor, a 5mm yellow LED, and connect it
to-and + as usual:

113
+ And-are connected to the Arduino power pins for
5V and GND:

114
As you can see, when we use the battery to power the
Arduino, the LED will light up:

In this circuit, the Arduino never really, with the


exception of scaling the 9V in the info given by the
battery to 5V.

We should compose the primary Arduino program to


make the LED flicker. For this, we should initially
introduce the Arduino IDE on the PC.

115
Subsequent to downloading the product, you need to
move the Arduino application to the macOS's
Applications organizer. Check Windows and Linux
establishment directions.

Start the program, and you should see a


straightforward program:

116
As referenced in the Arduino programming language
instructional exercise presentation, the
arrangement(,) work is executed once following the
program begins. For instance, we normally set the
pin mode here. The circle () work is constantly
executed persistently on the up and up.

In our program, we initially set the advanced I/O PIN


to 13: void setup () {//Configure pin 13 as a digital
output pinMode (LED_PIN, output);}
117
Then in the loop (), we just give a command like
telling Arduino to help write a high-level tension
(5V), at this point, you need to wait one second, and
then write a low-level tension (0V = ground), and
thereafter, wait for another one second, when that is
done, you can repeat forever:

Invalid circle () {digitalWrite (LED_PIN, HIGH);


Deferral (1000);

DigitalWrite (LED_PIN, LOW); Deferral (1000);}

HIGH and LOW are constants accessible naturally in


the Arduino program.

DigitalWrite () composes the HIGH or LOW as an


incentive to a particularly advanced yield pin. You
pass the PIN and HIGH or LOW as boundaries.

Deferral () determines the quantity of milliseconds to


stop the program as a boundary. When beginning
the program interestingly, you may need to choose
the Arduino board on the "Devices" - > "Port" menu.

118
Ensure the Arduino is associated with the PC.

I utilize a MacBook Pro with a USB-C port, and the


Arduino has a USB-B port, so I utilized a connector.

Before accumulating the program and composing it


to the Arduino, you need to save the record. You can
save it anyplace. I made an Arduino organizer in the
Documents envelope and put away all the Arduino
programs I composed here.

Snap the "Transfer" button (the one with the correct


bolt). The program ought to be arranged and
introduced on the Arduino. The LED should begin
squinting.

119
On the off chance that you separate the USB link
from the PC, you will see the LED goes off in light of
the fact that the Arduino is done providing power.

In the event that you associate the USB link to an


ordinary USB charger, the LED will streak. No
requirement for a PC any longer. Arduino runs the
program we stacked, just this program; no other
programming is running.

120
When the program is controlled, it will begin and
run.

121
Chapter Seven
Arduino Built-In LED
The Arduino development board comes with a small
utility: built-in LED.

This is clearly identified with the letter L that is next


to it. On the Arduino Uno, it is close to pin #13:

On the Arduino MKR 1010 WiFi, it is close to the 5V


output pin:

122
This LED is connected to digital I/O pin #13 on most
boards. On some boards, such as the Arduino MKR
series, it is linked to pin 6.

In any case, you can use the LED_BUILTIN constant


to refer to the exact pin, which is always correctly
mapped to the correct pin by the Arduino IDE,
depending on the board to be compiled.

To light up the LED, you first need to set the pin as


an output in setup (): pinMode (LED_BUILTIN,
output);
123
Then, you can send a HIGH signal to it: digitalWrite
(LED_BUILTIN, HIGH); or digitalWrite
(LED_BUILTIN, 1);

This is a simple program to make the built-in LED


blink every second: void setup () {pinMode
(LED_BUILTIN, output);} Invalid loop ()
{digitalWrite (LED_BUILTIN, HIGH); Delay (1000);

digitalWrite (LED_BUILTIN, LOW); Delay (1000);

124
Breadboard Power Supply Module
I connected the circuit to a 9V battery in some of my
electronic tutorials because it is easy to get started
and easy to find.

But this is not the best choice. The first is because the
battery will deplete over time and cannot provide the
nominal 9V.

The breadboard power module accepts input from


the battery, 12V adapter, or any other power source
through the barrel jack and provides 5V or 3.3V to our
circuit according to our needs. We will introduce
Arduino into our circuit soon, and all our Arduino
circuits will work at these voltage levels.

There are many different modules. Here is the power


module I want to show you. It is part of the Elegoo
kit I recommend you to buy, but it is also sold by
other brands in the same shape and characteristics.
You can search for "Arduino power supply" in Check
out some alternatives on Amazon:

125
126
127
The accepted input voltage is 6.5V-9V DC. The
maximum output current is 700mA.

We connect it directly to the experiment board:

128
129
Then, you can independently determine the tension
to be applied to the breadboard power cord on each
side: 5V, in this case:

130
Or 3.3V, by changing the position of the jumper:

131
You can also close it by placing the jumper in the
middle:

Then, you can use the barrel jack input to connect to


an adapter from 12V to 220V:

132
Press the gray button to turn it on. The LED will also
light up:

133
The USB port does not exist and cannot supply power
to the power module. You can power USB devices
(such as Arduino) when needed.

There are other output pins on the top, which can


provide GND, 5V, and 3.3V when needed:

134
135
Chapter Eight
Arduino Creation Platform
Arduino Create is a cloud editor that allows you to
write and compile code directly in the browser.

The website is http://create.arduino.cc. This is a very


amazing tool.

In this article, the web editor will be explored.


136
Using it, you don't need to install anything on your
computer to create an Arduino program. You only
need a plug-in to "see" the circuit board connected to
the computer, but other than that, everything will be
automatically updated and set to zero. The best
choice to quickly use Arduino to speed up.

It is free, has a free tier, and is a cloud tool that can


be automatically backed up and used across devices,
and it can run on Windows, Mac, and Linux.

It also allows you to share programs on Project Hub


https://create.arduino.cc/projecthub, where you can
share your projects (and view what others have
created!).

After you have registered, the system will ask you to


install the plug-in I mentioned and then run the
plug-in; that is, the setup is complete.

Go to https://create.arduino.cc/editor, and then you


will immediately see a new sketch, the name given to
the program by Arduino:

137
At the top, you could see that it has recognized the
Arduino Uno connected to the USB port (the line
/dev/cu.usbmodem14301 represents the USB port). It
will automatically recognize the Arduino
development board.

On the left, you will see a sidebar.

138
The "Examples" menu lists many pre-made examples,
including source code, instructions, and circuit
schematics and pictures in some cases:

In the same "Examples" menu, there is a convenient


"From Library" tab, which lists the Arduino library's
examples.

The library's menu lists the official Arduino libraries


and many libraries contributed by the community.
139
Happily, you can mark libraries as favorites and find
them without any problem.

The fourth thing in the sidebar is the serial monitor,


which is a very convenient tool for debugging and
obtaining information from the Arduino program
(and also sending the information to the
development board):

140
Next, you will get inline help, which provides some
tutorials, feature preferences, and usage quotas, such
as storage compilation time and several projects.

There is 100MB of storage space per day, 100 sketches,


and a 200s compile-time limit. After that, you can pay
$6.99 a month to upgrade to a paid plan, which
includes advanced IoT features, which I will discuss
in another article.

141
Chapter Nine
How to Connect to Wifi Network
Using Arduino
When you connect electronic devices built with
Arduino to the network, they become very cool.

Let's see how to connect Arduino to WiFi network.


Arduino must have WiFi connectivity, such as the
Arduino MKR WiFi 1010 or Arduino Uno WiFi Rev2 I
used.

We will use the WiFiNINA library. The library is very


convenient, allowing us to connect to a WiFi
network, but we can also create a WiFi network if
necessary.

Start your Arduino sketch with the following


command:

#include <SPI.h>

#include <WiFiNINA.h>

142
The WiFiNINA library uses the SPI library, so it must
also be loaded. SPI stands for Serial Peripheral
Interface. The WiFiNINA library allows us to access
the WiFi objects that will be used.

Now, let's study the main body of the setup ()


function, which is the on void setup () {}

First, we will define two strings to save the network


name, SSID, and network password. I use Arduino
Create, so I only use the constants that will be filled
separately in the "Secret" tab:

char ssid [] = SECRET_SSID;

char pass [] = SECRET_PASS;

Next, we will initialize the serial interface to pass


information from the Arduino back to the Arduino
Create:

Serial.begin(9600);

We wait until it is ready to use

143
while(!Serial);

Next, we put WiFi. begin() into a loop that checks


whether its return value is WL_CONNECTED and
keeps retrying every 2 seconds until it reaches:

int status = WL_IDLE_STATUS;

while (status! = WL_CONNECTED) {

Serial.print("Connecting");

Serial.println(ssid);

Status = WiFi.begin(ssid, pass);

delay (2000);}

We first initialize it to WL_IDLE_STATUS, which is


another constant of WiFiNINA, but we can also
initialize it to an empty string.

After the end of this loop, we have established a


connection, we can call the WiFi.localIP() method to
get the device IP address and print it to the serial:

144
Serial.print("IP address:");

Serial.println(WiFi.localIP());

This is the complete program:

#include <SPI.h>

#include <WiFiNINA.h>

void setup () {

char ssid [] = SECRET_SSID;

char pass [] = SECRET_PASS;

Serial.begin(9600);

while (! Serial);

int status = WL_IDLE_STATUS;

while (status! = WL_CONNECTED) {

Serial.print("Connecting");

Serial.println(ssid);

145
Status = WiFi.begin(ssid, pass);

Delay (5000);}

Serial.print("IP address:");

Serial. println (WiFi.localIP());}

Invalid loop () {}

You can now access other information, such as the


network SSID using WiFi.SSID() and the signal
strength using WiFi.RSSI():

Serial.print("Signal strength (in dBm as unit:");

Serial.print(WiFi.RSSI());

At this point, you have established a connection and


can continue to perform other tasks.

Note: You may already be able to use the NINA


firmware upgrade. Add this Check to your program:

If (WiFi.firmwareVersion()
<WIFI_FIRMWARE_LATEST_VERSION) {

146
Serial.println("Firmware Upgrade Available");}

I know if this is the case. If so, please use the Arduino


IDE (I can't see how to perform the operation from
Arduino Create) to connect to the development
board, and then load the sample sketch file ->
Examples -> WiFiNINA -> Tools -> FirmwareUpdater

Then open Tools -> WiFi101 / WiFiNINA firmware


update program menu:
147
Then press the update firmware button:

148
How to Run A Web Server on
Arduino
This tutorial will show you how to start a web server
on an Arduino device with WiFi, such as my Arduino
MKR WiFi 1010.

We will connect to an existing WiFi network and be


able to interact with the Arduino through a browser
via HTTP.

149
This is very interesting for various applications, from
simply checking sensor data to performing
operations based on executed HTTP requests.

We will start by connecting to the program defined


in the WiFi network using the Arduino tutorial:

#include <SPI.h>

#include <WiFiNINA.h>

void setup (){

char ssid [] = SECRET_SSID;

char pass [] = SECRET_PASS;

Serial.begin(9600);

while (! Serial);

int status = WL_IDLE_STATUS;

while (status! = WL_CONNECTED) {

Serial.print("Connecting");

150
Serial.println(ssid);

Status = WiFi.begin(ssid, pass);

Delay (5000);}

Serial.print("IP address:");

Serial.println(WiFi.localIP());}

Invalid loop () {}

Before setup (), add the following line:

WiFiServer server (80);

Initialize port 80 and TCP server at the end of the


setup () call

server.begin();

Start the worker.

Presently, this is a TCP worker, not an HTTP worker.


But since HTTP (TCP/IP Application Protocol) is
based on top of TCP (Transport Layer), we can form

151
an HTTP worker without help from anyone else
without much of a stretch.

To begin with, we need to tune in for customer


associations. We do this in a circle():

Invalid loop () {

WiFiClient client = server. available ();

If (customer) {}}

The available () method of the server listens to


incoming clients.

In the if (client) {} check, we connected an HTTP


client. What we need to do is:

• Call client.connected () to check whether the


data is connected and there is data to be read
• Call client. available () to get the number of
bytes that can be read (to ensure that there is
data to be read)

152
• Call client. read () to read a byte from the
incoming data (HTTP request sent by the
client)
• Call client. printing () or client.print () to send
data to the client to establish an appropriate
HTTP response
• Call client. stop() to end the connection

We start to print each character sent by the client to


the serial interface and finally close the connection:

Invalid loop () {

WiFiClient client = server.available();

If (customer) {And (client.connected()) {If


(client.available()) {Character c = client.read();

Serial.write(c);}} client.stop();}}

Try to upload this program to Arduino. Point your


browser to the IP address. You will see something
similar on the serial interface. This is what the
browser sends:

153
GET / HTTP / 1.1

Host: 192.168.1.40

Unsafe upgrade request: 1

Accept: text / html, application / xhtml + xml,


application / xml; q = 0.9, * / *; q = 0.8

User agent: Mozilla / 5.0 (Macintosh; Intel Mac OS X


10_15_6) AppleWebKit / 605.1.15 (KHTML, such as
Gecko) version / 14.0.2 Safari / 605.1.15

Accepted language: en-us

Accept code: gzip, deflate.

Connection: keep-alive

Note the blank line at the end. This is the end of the
HTTP request.

We need to intercept this blank line.

Each line in the HTTP request is terminated with CR


carriage return (\r) and LF line feed (\n).

154
Therefore, the end of the request can be determined
by two sequences: \r\n\r\n.

This simple algorithm will work; we only remember


the two characters before the current character and
then check whether the sequence \n\r\n is identified
(the last three characters in the sequence are enough
to determine the last line):

Invalid loop () {

WiFiClient client = server.available();

If (customer) {

char prevprev;

Character prev;

And (client.connected()) {

If (client.available()) {

Character c = client.read();

Serial.write(c);

155
If (prevprev =='\ n'&& prev =='\ r'&& c =='\ n') {

//We can send a reply!}

prevprev = previous step;

prev = c;}} client.stop();}}

So now we can send a response in if we can use the


client. println() for this, and add a simple response as
shown below:

HTTP / 1.1 200 OK

Content type: text / html

Connection method: closed

<! DOCTYPE HTML>

<html>

test

</ html>

such:

156
client.println("HTTP / 1.1 200 OK");

client.println("Content-Type: text/html");

client.println("Connection:close");

client.println();

client.println("<!DOCTYPE HTML>");

client.println("<html>");

client.println("test");

client.println("</html>");

rest;

Rest; the statement ends while(client.connected()){}


block.

This is the complete program:

#include <SPI.h>

#include <WiFiNINA.h>

157
WiFiServer server (80);

void setup () {

char ssid [] = SECRET_SSID;

char pass [] = SECRET_PASS;

Serial.begin(9600);

while (! Serial);

int status = WL_IDLE_STATUS;

while (status! = WL_CONNECTED) {

Serial.print("Connecting");

Serial.println(ssid);

Status = WiFi.begin(ssid, pass);

Delay (5000);}

Serial.print("IP address:");

Serial.println(WiFi.localIP());

158
server.begin();}

Invalid loop () {WiFiClient client = server.available();

If (customer) {

char prevprev;

Character prev;

And (client.connected()) {

If (client.available()) {

Character c = client.read();

Serial.write(c);

If (prevprev =='\ n'&& prev =='\ r'&& c =='\ n') {

client.println("HTTP / 1.1 200 OK");

client.println("Content-Type: text/html");

client.println("Connection:close");

client.println();

159
client.println("<!DOCTYPE HTML>");

client.println("<html>");

client.println("test");

client.println(" </ html>");

rest;}

prevprev = previous step;

prev = c;}} client.stop();}}

Try it, and you should see the test display in your
browser:

160
This method is effective until you need to figure out
how customers ask us.

In this case, you want to read every line, so this


alternative method works better:

Invalid loop () {

WiFiClient client = server.available();

161
If (customer) {

String line="";

And (client.connected()) {

If (client.available()) {

Character c = client.read();

Serial.write(c);

If (c! ='\ n'&& c! ='\ r') {

Line + = c;}

If (c =='\n') {

If(line.length() == 0) {

client.println("HTTP / 1.1 200 OK");

client.println("Content-Type: text/html");

client.println("Connection:close");

client.println();

162
client.println("<!DOCTYPE HTML>");

client.println("<html>");

client.println("test");

client.println(" </ html>");

rest;

} Other {

line = "";

client.stop();

163
Finally, since the line has been terminated, we can
check the line and take corresponding measures
according to our needs.

Conclusion
when you completely adhere to all the instructions
provided, there is no doubt, success awaits you in
your journey to Arduino Programming.

164

You might also like