Arduino For Dummies - Steps To Learn Arduino
Arduino For Dummies - Steps To Learn Arduino
Voltage Regulator
The voltage received by the Arduino is controlled by the
Voltage Regulator, it also stabilizes the DC voltages utilized
by the processor.
Crystal Oscillator
Arduino calculates time through the crystal oscillator. The
frequency of the crystal is 16 MHz for high fast clocking.
Reset
Resetting makes the controller start from its initial state and
will restart the program it has in its memory.
Resetting the Arduino can be done in two ways. You can press
the reset button or use reset pin by connecting an external
reset button to it.
Microcontroller
Terms to know:
I/O- Input/Output
IC - Integrated Circuit
PWM- Pulse Width Modulation
DIP- Dual-Inline-Package
SMD- Surface Mount Device
These microcontrollers are usually manufactured by ATMEL
Company. You can say that it is the brain of the board because
it executes the instructions in your program. The main IC
(Integrated Circuit) varies from board to board.
ATmega328 - used on Arduino UNO R3. Its data-bus
architecture and internal registers are designed to handle 8
parallel data signals which make it an 8-bit device. It has 14
Digital I/O pins, 6 of which provide PWM output. It has 32
KB Flash memory, its SRAM is 2 KB and 1KB EEPROM.
Other microcontrollers are ATMEGA32u4, ATMEGA2560,
and AT91SAM3X8E.
ICSP pin
In-Circuit Serial Programming pin. This pin is mainly used for
boot-loading and to communicate with the SPI (Serial
Peripheral Interface). Meaning that this pin is used when an
external programmer or another Arduino microcontroller will
be used. It is also considered as an expansion of the output.
TX and RX LEDs
TX means transmit and RX means receive. The TX led
flashes when sending serial data while RX flashes when
receiving data.
Digital I/O
There are 14 digital I/O pins on the board of which 6 of these
provide PWM output. If you configure these pins they work as
input digital pins that can read logic values 0 or 1, or as digital
output pins to drive different modules such as relays, LEDs,
and etc. “~” labeled pins can be used to generate PWM
AREF Pin
The reference voltage used for analog input and is configured
by the analogReference. Anything less than 0V or more than
5V can’t be used as external reference voltage on the AREF
pin because it can possibly damage the microcontroller.
Placing comments
The programs we normally write are usually meant for the
computers and not for people to understand once they are
opened up. There is a good provision that allows us, humans,
to read the program easily and the computer will have no clue
about it. This is made possible by the use of comments that are
added to the program. There are two comments that are
possible in this program:
1. The block comment style starts with two characters, /*
which progresses until */ is seen. Multiple lines are then
crossed and here are a few examples.
/* This is the first line*/
/* the program was successful*/
/* we
*are
*going
*far */
Arduino Zero
Communication
1. A programming ports
2. Four hardware UARTs
3. USB host
4. Two I2C
5. Interface JTAG
6. CAN Interface (Automotive communication
procedure)
7. SPI
However, unlike all of the other panels, the Due board is going
to run at 3.3V. The maximum voltage is going to be 3.3V
because that is all the input-output pins are going to be able to
tolerate. If you try and apply voltage any higher than 3.3V to
any of the pins, you are going to risk damaging the panel.
The panel is going to hold everything that is needed to support
the microcontroller. All you have to do is plug the panel into
your machine via a USB cable or turn it on using an adapter
that is going to convert AC to DC, or you can even use a
battery. The Due is going to be compatible with any Arduino
shield that works with three point three volts.
Arduino Zero
The zero is going to be a powerful thirty-two-bit extension of
the machine that was first established by the UNO Arduino
panel. This panel expanded the family of Arduino panels by
increasing the performance and making it to where there is a
wide array of projects that can be completed as well as
allowing it to act as an educational tool for those that wanted
to learn about a 32-bit application and how it is developed.
Some of the most prominent features that you are going to find
in the Arduino zero are:
Basic Principles
Pulse width modulation is going to essentially be a square
wave that will have high times and low times. The essential
beacon for the PWM is going to be shown in this image.
Here are some terms that you are going to need to know when
it comes to working with pulse width modulation.
1. Duty cycle: this will be represented as a
percentage of time beacon that is going to stay on
during the periods that the PWM beacon is being
sent out.
2. On time: this is when the beacon is going to
be high.
3. Off time: this is when the beacon is going to
be low.
4. Period: the period will be produced as a tally of
time on and time off for the PWM beacon.
Period
Just as you can see in the figure, the T on is going to represent
when the beacon is on, and the T off is going to show when it
is off. The period will be the total time that the beacon will be
on or off which can be calculated by adding both of these
digital representations together.
Duty cycle
You are going to calculate the duty cycle by using the on time
and off time as well. This is the equation that you are going to
use to get the duty cycle.
D equals T on / t on plus t off equals t on / t total
Board
1 Reset
4 Digital Pin 2
5 Digital Pin 3
6 Digital Pin 4
7 VCC
8 GND
9 XTAL 1
10 XTAL 2
11 Digital Pin 5
12 Digital Pin 6
13 Digital Pin 7
14 Digital Pin 8
18 Digital Pin 12
20 AVCC
21 AREF
22 GND
23 Analog Input 0
24 Analog Input 1
25 Analog Input 2
26 Analog Input 3
27 Analog Input 4
28 Analog Input 5
Step 3
Once your pins are in place, add the tact switch near pin 1.
This will reset your Arduino breadboard when necessary.
Then, connect a small jumper wire between the bottom leg of
the switch and pin 1. The next step is to connect the 10K
resistor between pin row 1 and the power switch. The final
thing you will need to do in this area is connect a GND wire to
the top leg.
Next, connect power and GND jumpers between VCC (pin 7)
and GND (pin 8). The 16 MHz clock crystal should then be
added to pins 9 and 1-. Next, add the .22pF capacitors from
these pins to GND. You can stop here if you choose, and add a
programming chip. If you are interested in setting the
breadboard up for programming, however, continue reading.
Step 4
The connections you will need for programming include the
pins GND, NC, 5V, TX, RX, and NC. Connect the GND wire
from the power rail to the GND pin. Add a power wire to the
5V pin. Finally, connect a wire between the TX pin and the
RX pin. Your Arduino breadboard is now ready to be
programmed. You can do this by using the USB – Serial
Converter Cable from the list of necessary items.
Chapter 7: How to Build a Swimming
Electronic Snake
Arduino Uno
Seeeduino Mega
10 Servo Motors (Remember that you get
what you pay for. The nylon gears in these
motors may wear out quickly if you choose
lower quality motors.)
Servo Extension Wire
Servo Motor Shield
2 Xbee Series 1
Xbee Explorer
Xbee Breakout (with 2 rows Xbee 2mm
female headers and 2 rows of 10 male header
pins)
3 6V NiMh Battery packs
Mechanical Hardware
Urethane Sealant
Marine Epoxy Sealant
Marine Grease
Nylon String
Green Loctite
2” x 10” of 1/8” Thick Rubber
3 Strips Each of Carbon Fiber – 1/32”, 1” x
12”
2 2.5 to 1.25” Shop Vac Vacuum Reducer
O Rings
5/16” Hose Clamp
Convoluted Hose Clamp
5/16” Tubing
5 Servo Brackets
5 Injection Molded Servo Hinge
5’ x 2.5” Urethane Dust Collection Tube
5 Lynxmotion C-Brackets
5 Lynxmotion Servo Brackets
Tools
Data analytics
The upcoming cloud technologies help to organize
and present user data in very descriptive and graphical ways
like bar graphs, graphs, pie charts etc.
Remote monitoring
Any place that has Internet facility can be monitored
from all over the world through GPRS or Ethernet or wired
connection.
Process optimization
As the availability of data is time independent and
location independent, work can be optimized accordingly.
Simplicity to access
Controls are available through mobile devices. Even
apps can be developed to give users ease of access.
Applications of IOT:
IOT has a list of applications ranging from household things to
heavy industrial machinery. There is no end to the usage of
IOT in technology, business and personal lives.
Home automation
Household devices like fans, lights, air conditioners
can be connected to internet thus giving users access to their
house hold devices from any part of the world.
Industrial automation
Automation in industry is very critical, as there are
harsh and unpleasant conditions for human beings. Instead of
following manual processes, when taking a step further with
automated control – by connecting the systems and controlling
over the Internet, it gives great control for the
employer/manager and monitoring since you can monitor not
only from the company’s premise but from anywhere. This
facilitates productivity and ensures tasks/people can be
monitored simultaneously.
Security system
Burglar alarm, break detectors fitted in house or
shops can be connected to Internet. This will give a high
priority alert to owners and nearby police stations.
Agricultural automation
Agricultural fields can be fitted with various sensors
like soil moisture, humidity temperature and even water
pumps can be connected to Internet. All this can be controlled
and monitored over Internet.
Vehicle security system
Vehicles can be fitted with security system that not only buzz
alarm when unauthorized person tries to unlock vehicle but
also gives instant alert to owner’s mobile.
Interrupts
Interrupts enable microcontroller to respond to events without
checking continuously to verify if anything has changed. You
may also make use of timer-generated interrupts.
Hardware interrupts
Let us see a sample code below:
void loop
{
if ( digitalRead(input pin) == LOW)
{
// do something
}
}
In the above-mentioned code, we check continuously to check
input pin and the instant it turns low, processor need to
perform the events that are mentioned in the loop. Hardware
interrupts help to receive inputs (even if they are shorter pulses
for minute duration). In Arduino UNO, there are only two
pins, which can be used as hardware interrupts.
Resistor retains the interrupt pin (D2) HIGH until the button
on the switch is pushed. Once D2 is grounded, it will turn
LOW.
Fig 10.1
Sketch
int ledPin = 13;
void setup()
{
pinMode (ledPin, OUTPUT);
attachInterrupt ( 0, stuffHappened, FALLING);
}
void loop ()
{
}
void stuffHappened ()
{
digitalWrite(ledPin, HIGH);
}
Code explanation:
pinMode (ledPin, OUTPUT); - sets the LED pin to be an
output
attachInterrupt ( 0, stuffHappened, FALLING); - Here the
argument 0 refers to interrupt number (In Arduino UNO, there
are only 2 interrupt pins, interrupt 0 is pin D2 and interrupt 1
is pin D3. Argument stuffHappened is the function name that
has to be used when interrupt happens. This function is known
as Interrupt Service Routine or ISR, which is analogous to
void function in that it does not take parameters nor do they
return anything. The last parameter that we have given is a
constant, FALLING. This implies that interrupt would result in
interrupt service routine only when D2 falls from HIGH to
LOW. (Resistor retains the interrupt pin (D2) HIGH until the
button on the switch is pushed. Once D2 is grounded, it will
turn LOW. We need to call the interrupt only when it is
grounded). In the sample code, we have left the loop function
as blank. If we need to perform any action in the case of an
interrupt, we need to mention that in the loop. Usually, we
would write code to light up LED in case of interrupt.
Arduino Library
Arduino library is a set of functions, which helps to
accomplish particular tasks which renders life effortless for
you. We have seen functions like digitalWrite() and delay()
which are included in Arduino library. Every library needed to
be incorporated in the sketch when you would like to include a
particular function. An example for Arduino library is
LiquidCrystal library, which enables you to communicate with
LCD displays if it is incorporated in the sketch. Some of the
Arduino libraries work as a stand-alone library, whereas other
Arduino libraries have to be utilized with extra electronic
components which are also known as shield.
Arduino libraries can be broadly classified into three:
1. Core
2. Standard
3. Contributed
Using libraries
Arduino programmer need to explicitly mention the complier
about Arduino library before they are added. # include pre-
processor directive has to be added to mention file name of
Arduino library. Pre-processor directive includes instructions,
which enables the compiler to add relevant function in the
Arduino sketch.
Syntax is as follows:
# include < LibraryName.h>
You need to replace LibraryName with the file name of
Arduino library. For example, if you would like to
communicate with LCD displays, you need to write as
# include < LiquidCrystal.h>
You may also add library by navigating through Sketch menu
and then click on Import library. You need to select library
name from the menu. Once you have added the library, you
may need to either create a library instance or perform library
initialization. For example, library instance can be created as
below:
LiquidCrystal lcd(5,6,7,8,9,10);
Here, the name of library instance is lcd and we have assigned
pin numbers 5,6,7,8,9 and 10. If you would like to add
multiple library instances, you may do so by creating library
instances as lcd1, lcd2 etc.
Library Initialization
Now, let us see how we could initialize Arduino library. We
need to define size of LCD that we are going to implement.
This can be done using begin(cols, rows)
lcd.begin(16,2);
Here, we have created an instance known as lcd and Arduino
function begin. Two parameters are passed in this Arduino
function, which describes that LCD display is 16 characters
wide and 2 characters tall or a 16*2 display.
print()
This function is used to transmit information to liquid crystal
display unit. Syntax for print() can be either of the following:
print(data)
print(data, BASE)
Data could be anything that you would like to see in the LCD
screen as output. It has to be provided in double quotes. An
example would be like
lcd.print(“ Success”);
clear()
This function helps to clear display content and place cursor in
top left side of the LCD display. clear() is analogous to void()
and does not need any argument. This function helps to write
new line of code without the need of overwriting data.
setCursor()
This function helps to change the cursor position to a new
position without clearing the display content. Syntax is
setCursor(col,row);
write()
This function helps to transmit character to LCD display.
Syntax is
write(data)
createChar()
This function helps to create a symbol such as a smiley face.
To accomplish this, one needs to describe a bitmap array,
which matches up with every dot for the character.
createChar() is then used to transmit the bitmap array to the
relevant display memory address.
Syntax for createChar() is createChar(number,data);
scrollDisplayLeft()
This function enables the display to be shifted one character to
the left.
scrollDisplayRight()
This function enables the display to be shifted one character to
the right.
Servo library
This library can regulate up to a maximum of twelve servos in
the Arduino UNO board. An instance of servo library can be
created as below
Servo servo1; ( Servo library name has to be provided
followed by the instance of Arduino sketch here it is servo1).
attach()
This function has to be used to use servos, syntax for attach()
can be either of the following
name.attach(pin) or
name.attach(pin,min,max)
In some cases, Arduino will have minimum and maximum
rotation angles, which have to be provided. (Note: Rotation
angle is mentioned in microseconds. Default minimum is 544
for 0 degree whereas maximum is 2400 for 180 degree).
write()
Write() helps the Servo to move and the syntax for write() is
name.write(angle)
Stepper Library
Stepper motor offers positional accuracy and could rotate for
the complete 360 degrees. As the name implies, stepper motor
rotates in a specific number of degrees or steps. Rotation angle
per step can vary from one stepper motor to another. An
instance of stepper library can be created as follows:
Stepper instancename( steps, pin1, pin2)
setSpeed()
This function helps to set motor rotational speed. Syntax for
setSpeed() is as below
name.setSpeed(rpm)
Here, we provide motor speed in terms of rotation per minute.
(Note: To ensure that stepper motor runs properly, user needs
to select suitable speed for the motor. If the speed is too high,
it can result in missed steps or sporadic operation of the motor.
step()
Motor can be moved by using step(). Syntax for step() is as
follows
name.step(steps)
Parameter that we pass to step() is the count of steps through
which the stepper motor need to move
SD library
SD stands for Secure digital flash memory, which helps to
write to an SD memory card.
File
File instance is created with the help of file and takes the
below syntax
File Name
SD.begin()
SD library is initialized with the help of SD.begin().It will
default to CS or chip select pin and takes the below syntax
SD.begin(csPin)
SD.open()
This function helps to write data to Arduino file and it takes
the below syntax
SD.open(filename,mode)
Here, filename is the name of the file that you would like to
access. Make sure to give short file names as this function
works in a 8.3 format, which means file name could be
character string with maximum of 8 characters and file
extension is 3character extension like .doc. Second parameter,
viz. mode can take two values either FILE_READ or
FILE_WRITE. Default mode of SD.open() is FILE_READ
which helps to open the file quoted in filename. FILE_WRITE
helps to write data into the filename mentioned.
close()
This function helps to close the current file and takes the
below syntax
name.close();
write ()
This function helps to write into the SD card and takes the
below syntax
name.write (data)
Name is the instance of file class and data is anything that you
would like to input into the file. It could assume different data
types like char, string etc.
available()
This function helps to know the number of bytes present in a
specific file and takes the below mentioned syntax
name.available()
read()
This function is used to extract information from a specific file
and takes the below mentioned syntax
name.read()
Arrays
Arrays are lists of variables and include multiple elements
with same data type. Elements in an array can be accessed
with the help of index number.
Array declaration
Array declaration is similar to variable declaration. For
example,
int sampleArray[3];
In the above array declaration, three distinct variables are
referred in the Arduino sketch as
sampleArray[0], sampleArray[1] and sampleArray[2].
When we declare array, Arduino compiler will reserve a block
of memory for our use. For array declaration we need three
things –
array data type – It is int in the example
array name- It is sampleArray in the example
index – [3] in the example . Index refers to the count of
variables in the array.
Assigning values
First element in the array starts at position 0 and the last
element is at an index, which is one less than array size.
int ledPins[3];
ledPin[0] = 8;
ledPin[1] = 9;
ledPin[2] = 10;
We can assign values like below also.
int ledPins[] = { 8,9,10};
Character arrays
In the above discussion, we dealt with integer arrays. Now, let
us learn about character arrays.
Char Sample[] = “ Good morning”;
Multidimensional arrays
Usually, we work with one-dimensional arrays. However, we
may need to work with multidimensional arrays at times. For
example
int SampleArray[2][3];
The above mentioned array will contain total of 2*3 = 6
elements. Elements are as below:
SampleArray[0][0];
SampleArray[0][1];
SampleArray[0][2];
SampleArray[1][0];
SampleArray[1][1];
SampleArray[1][2];
Chapter 11: The Theоry оf Creating a
Rоbоt Based оn Arduinо
Avoid pointers
We didn’t even touch on them in this document, and we are
only going to tell you they exist to tell you not to use them
unless you’re an advanced user. They are the most likely
‘tool’ to cause the crazy, bad kinds of problems from
happening in your coding, and are notoriously tough for a
beginner to use properly. So avoid them until you are sure you
know what you are doing.
Embrace self-improvement
Understand from day 1 that as a fledgling coder that you will
grow and improve over time. Use each challenge you come
across to try writing new sketches as an opportunity to grow
and hone your skills.