NOTES Arduino
NOTES Arduino
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.
Power USB
Arduino board can be powered by using the USB cable from your computer. All you
need to do is connect the USB cable to the USB connection (1).
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).
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….
*/
Variable Scope:
Variables in C programming language, which Arduino uses, have a property called scope. A scope
is a region of the program and there are three places where variables can be declared. They are −
• Inside a function or a block, which is called local variables.
• In the definition of function parameters, which is called formal parameters.
• Outside of all functions, which is called global variables.
Local Variables
Variables that are declared inside a function or block are local variables. They can be used only by
the statements that are inside that function or block of code. Local variables are not known to
function outside their own.
Global Variables
Global variables are defined outside of all the functions, usually at the top of the program. The
global variables will hold their value throughout the life-time of your program.
A global variable can be accessed by any function. That is, a global variable is available for use
throughout your entire program after its declaration.
The following example uses global and local variables −
Int T , S ;
float c = 0 ; Global variable declaration
Void setup () {
Void loop () {
int x , y ;
int z ; Local variable declaration
x = 0;
y = 0; actual initialization
z = 10;
}
Arduino functions:
1. PinMode
A pin on arduino can be set as input or output by using pinMode function.
Syntax:
pinMode(pin_no, input/output);
eg:
pinMode(13, INPUT); // sets pin 13 as input pin
• Inputs is a signal / information going into the board. Examples: Buttons Switches, Light
Sensors
• Output is any signal exiting the board. Examples: LEDs, DC motor, servo motor, a piezo
buzzer, relay, an RGB LED
2. digitalWrite:
digitalWrite function is used to write the value (HIGH or LOW) onto the output pin.
Syntax:
digitalWrite( pin_no, HIGH/LOW);
eg:
• digitalWrite(13, LOW); // Makes the output voltage on pin 13 , 0V
• digitalWrite(13, HIGH); // Makes the output voltage on pin 13 , 5V
3. digitalRead:
digitalRead function is used to Read the digital value (HIGH or LOW) from the input pin. The
function will return the value.
Syntax:
digitalRead( pin_no);
eg:
int buttonState = digitalRead(2); // reads the value of pin 2 in buttonState
ADC in Arduino
• The Arduino Uno board contains 6 Analog pins, A0, A1, A2, A3, A4, A5.
• The Analog value can be read from pins but we cannot write analog value
• Arduino Uno has 10-bit analog to digital converter
• This means that it will map input voltages between 0 and 5 volts into integer values
between 0 and 1023
4. analogRead
Used to read the analog value from the pin.
Syntax:
• analogRead(analog_pin);
eg:
analogRead(A0); // used to read the analog value from the pin A0
5. analogWrite
A few pins on the Arduino allow for us to modify the output to mimic an analog signal using
analogWrite function. This is done by a technique called: Pulse Width Modulation (PWM)
Syntax:
analogWrite(pin_no, val);
where,
• pin_no – refers to the OUTPUT pin (limited to PWM pins 3, 5, 6, 9, 10, 11.) – denoted by a
~ symbol
• val – 8 bit value (0 – 255).
0 => 0V | 255 => 5V
Arduino UART
UART (Universal Asynchronous Receiver Transmitter) module is asynchronous.
The term baud rate is used to denote the number of bits transferred per second [bps].
The following code will make Arduino send hello world when it starts up.
void setup() {
Serial.begin(9600); //set up serial library baud rate to 9600
Serial.println("hello world"); //print hello world
}
void loop() {
}
Some Example programs:
1. Turns an LED on for one second, then off for one second, repeatedly.
void setup()
{
pinMode(12, OUTPUT); // initialize digital pin 12 as an output.
}
// the loop function runs over and over again forever
void loop()
{
digitalWrite(12, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(12, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
2. Turns on and off a light emitting diode(LED) connected to digital pin 13, when
pressing a pushbutton attached to pin 2.
void setup()
{
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH); // turn LED on
}
else
{
digitalWrite(ledPin, LOW); // turn LED off:
}
}
3. Reads a digital input on pin 2, prints the result to the Serial Monitor
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}