Arduino 1
Arduino 1
Board Types
• Various kinds of Arduino boards are available depending on different microcontrollers used.
However, all Arduino boards have one thing in common: they are programmed through the
Arduino IDE.
• The differences are based on the number of inputs and outputs (the number of sensors, LEDs,
and buttons you can use on a single board), speed, operating voltage, form factor etc. Some
boards are designed to be embedded and have no programming interface (hardware), which you
would need to buy separately. Some can run directly from a 3.7V battery, others need at least 5V.
Arduino UNO:
Arduino UNO board is the most popular board in the Arduino board family. In addition, it is the
best board to get started with electronics and coding.
Looking at the board from the top down, this is an outline of what you will see (parts of the
board you might interact with in the course of normal use are highlighted):
Voltage Regulator
The function of the voltage regulator is to control the voltage given to the Arduino
board and stabilize the DC voltages used by the processor and other elements.
Crystal Oscillator
Arduino uses 16 MHz crystal oscillator
Arduino Reset
Used to reset the Arduino board, i.e., start the program from the beginning.
Two ways to reset the UNO board: First, by using the reset button (17) on the board.
Second, connecting an external reset button to the Arduino pin labelled RESET (5).
Analog pins
The Arduino UNO board has six analog input pins A0 through A5. These pins can read
the signal from an analog sensor like the humidity sensor or temperature sensor and
convert it into a digital value that can be read by the microprocessor.
Main microcontroller
Each Arduino board has its own microcontroller (11) which is the brain of board.
ICSP pin
Mostly, ICSP (12) is an AVR, a tiny programming header for the Arduino consisting
of MOSI, MISO, SCK, RESET, VCC, and GND. It is often referred to as an SPI (Serial
Peripheral Interface), which could be considered as an "expansion" of the output.
Actually, you are slaving the output device to the master of the SPI bus.
TX and RX LEDs
On your board, you will find two labels: TX (transmit) and RX (receive). They appear
in two places on the Arduino UNO board. First, at the digital pins 0 and 1, to indicate
the pins responsible for serial communication. Second, the TX and RX led (13). The
TX led flashes with different speed while sending the serial data. The speed of
flashing depends on the baud rate used by the board. RX flashes during the receiving
process.
Digital I/O
The Arduino UNO board has 14 digital I/O pins (15) (of which 6 provide PWM (Pulse
Width Modulation) output. These pins can be configured to work as input digital pins
to read logic values (0 or 1) or as digital output pins to drive different modules like
LEDs, relays, etc. The pins labelled “~” can be used to generate PWM.
AREF
AREF stands for Analog Reference. It is sometimes, used to set an external reference
voltage (between 0 and 5 Volts) as the upper limit for the analog input pins.
Arduino Programming:
The Arduino software is open-source.
The Arduino program is called “sketch”.
Structure of program:
Software structure consist of two main functions −
• Setup( ) function
• Loop( ) function
These are the Basic, Important and required functions in Arduino programming. When Arduino IDE is
opened, these two functions appear on the screen.
setup :
It is called only when the Arduino is powered on or reset. It is used to initialize variables and pin modes.
The setup() function is called when a sketch starts. Use it to initialize the variables, pin modes,
start using libraries, etc. The setup function will only run once, after each power up or reset of the
Arduino board.
loop :
The loop functions runs continuously till the
device is powered off. The main logic of the code
goes here. Similar to while (1) for micro-
controller programming.
After creating a setup() function, which
initializes and sets the initial values,
the loop() function does precisely what its name
suggests, and loops consecutively, allowing your
program to change and respond. Use it to actively
control the Arduino board.
Comments:
Comments are used to give information about the program. They are not compiled by the
compiler. The compiler ignores them. They increase the readability of the program.
There are two types of comments:
1. single line comment
// this is for single line comments
2. multi-line comment
/* this is for multi-line comments
Like this…
And this….
*/
Language Reference
Arduino programming language can be divided in three main parts: values (variables and
constants), functions, and structure.
Constants:
The constants are those variables or values which cannot be modified once they are defined in the
program.
• They have fixed values till the program’s life.
• We can only assign value to the constant in the declaration.
The const keyword stands for constant. It is a variable qualifier that modifies the behavior of the
variable, making a variable "read-only". This means that the variable can be used just as any other
variable of its type, but its value cannot be changed. You will get a compiler error if you try to assign a
value to a const variable.
Constants defined with the const keyword obey the rules of variable scoping that govern other
variables. This, and the pitfalls of using #define, makes the const keyword a superior method for
defining constants and is preferred over using #define.
Example Code
const float pi = 3.14;
float x;
// ....
x = pi * 2; // it's fine to use consts in math
pi = 7; // illegal - you can't write to (modify) a constant
Note:
#define or const
You can use either const or #define for creating numeric or string constants. For arrays, you will
need to use const. In general const is preferred over #define for defining constants.
Types:
1. Integer Constants
Integer constants are numbers that are used directly in a sketch. By default, these numbers are
treated as int but you can change this with the U and L modifiers.
Keyword: int
Normally, integer constants are treated as base 10 (decimal) integers, but special notation
(formatters) may be used to enter numbers in other bases.
BASE EXAMPLE FORMATTER COMMENT
10 (decimal) 123 none
2 (binary) 0b1111011 leading "0b" characters 0 & 1 valid
8 (octal) 0173 leading "0" characters 0-7 valid
16 (hexadecimal) 0x7B leading "0x" characters 0-9, A-F, a-f valid
Keyword: float
Floating point constants can also be expressed in a variety of scientific notation. 'E' and 'e' are
both accepted as valid exponent indicators. Eg: 10.0, 2.34E5, 67e-12
HIGH | LOW
When reading or writing to a digital pin there are only two possible values a pin can take/be-set-to:
HIGH and LOW. These are the same as true and false, as well as 0 and 1.
HIGH
The meaning of HIGH (in reference to a pin) is somewhat different depending on whether a pin is set
to an INPUT or OUTPUT.
When a pin is configured as an INPUT, the Arduino (ATmega) will report HIGH if:
• a voltage greater than 3.0V is present at the pin (5V boards)
• a voltage greater than 2.0V is present at the pin (3.3V boards)
When a pin is configured to OUTPUT, and set to HIGH with digitalWrite(), the pin is at:
• 5 volts (5V boards)
• 3.3 volts (3.3V boards)
LOW
The meaning of LOW also has a different meaning depending on whether a pin is set to INPUT or
OUTPUT.
When a pin is configured as an INPUT with pinMode(), the Arduino (ATmega) will report LOW if:
• a voltage less than 1.5V is present at the pin (5V boards)
• a voltage less than 1.0V (Approx) is present at the pin (3.3V boards)
When a pin is configured to OUTPUT, and set to LOW with digitalWrite(), the pin is at 0 volts.
INPUT | OUTPUT
Digital pins can be used as INPUT, or OUTPUT. Changing a pin with pinMode() changes the electrical
behavior of the pin.
INPUT
Arduino pins configured as INPUT with pinMode() are said to be in a high-impedance state.
Pins configured as INPUT make extremely small demands on the circuit that they are sampling,
equivalent to a series resistor of 100 Megohms in front of the pin.
If a pin is configured as an INPUT, and is reading a switch, when the switch is in the open state
the input pin will be "floating", resulting in unpredictable results. In order to assure a proper reading
when the switch is open, a pull-up or pull-down resistor must be used. The purpose of this resistor is
to pull the pin to a known state when the switch is open.
• If a pull-down resistor is used, the input pin will be LOW when the switch is open and HIGH
when the switch is closed.
• If a pull-up resistor is used, the input pin will be HIGH when the switch is open and LOW when
the switch is closed.
OUTPUT
Pins configured as OUTPUT with pinMode() are said to be in a low-impedance state. This means that
they can provide a substantial amount of current to other circuits.
ATmega pins can source (provide current) or sink (absorb current) up to 40 mA (milliamps) of
current to other devices/circuits. This makes them useful for powering LEDs because LEDs typically
use less than 40 mA. Loads greater than 40 mA (e.g. motors) will require a transistor or other interface
circuitry.
LED_BUILTIN
Most Arduino boards have a pin connected to an on-board LED in series with a resistor. The constant
LED_BUILTIN is the number of the pin to which the on-board LED is connected. Most boards have this
LED connected to digital pin 13.
true | false
There are two constants used to represent truth and falsity in the Arduino language: true, and false.
true: Any integer which is non-zero is true. So -1, 2 and -200 are all defined as true.
false: false is defined as 0 (zero).
*Note that, true and false constants are typed in lowercase unlike HIGH, LOW, INPUT, and OUTPUT.