0% found this document useful (0 votes)
15 views10 pages

NOTES Arduino

Uploaded by

Mahesh H
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
15 views10 pages

NOTES Arduino

Uploaded by

Mahesh H
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 10

Arduino

Arduino is a prototype platform (open-source) based on an easy-to-use hardware and software. It


consists of a circuit board, which can be programmed and a ready-made software called Arduino
IDE (Integrated Development Environment), which is used to write and upload the computer code
to the physical board.
The key features are −
• Arduino boards are able to read analog or digital input signals from different sensors
and turn it into an output such as activating a motor, turning LED on/off, connect to
the cloud and many other actions.
• board functions can be controlled by sending a set of instructions to the
microcontroller on the board via Arduino IDE (referred to as uploading software).
• Arduino does not need a programmer in order to load a new code onto the board.
• Additionally, the Arduino IDE uses a simplified version of C++, making it easier to
learn to program.

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).

Power (Barrel Jack)


Arduino boards can be powered directly from the AC mains power supply by
connecting it to the Barrel Jack (2).

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).

Pins (3.3, 5, GND, Vin)


• 3.3V (6) − Supply 3.3 output volt
• 5V (7) − Supply 5 output volt
• Most of the components used with Arduino board works fine with 3.3
volt and 5 volt.
• GND (8)(Ground) − There are several GND pins on the Arduino, any of
which can be used to ground your circuit.
• Vin (9) − This pin also can be used to power the Arduino board from an
external power source, like AC mains power supply.
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.

Power LED indicator


This LED should light up when you plug your Arduino into a power source to indicate
that your board is powered up correctly. If this light does not turn on, then there is
something wrong with the connection.

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

An INPUT pin on arduino can be pulled up to +V or pulled down to ground as below.


• pinMode(13, INPUT_PULLUP); // sets pin 13 as input pin and Keeps the input “High”, used
with an open switch connected between I/O and ground.
• pinMode(13, INPUT_PULLDOWN); // sets pin 13 as input pin and Keeps the input “Low”,
used with an open switch connected between I/O and +supply.

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

The character-handling library functions:


The character-handling library includes several functions that perform useful tests and
manipulations of character data. Each function receives a character, represented as an int, or EOF
as an argument. Characters are often manipulated as integers.

S.No. Prototype & Description


int isdigit( int c )
1
Returns 1 if c is a digit and 0 otherwise.

int isalpha( int c )


2
Returns 1 if c is a letter and 0 otherwise.
int isalnum( int c )
3
Returns 1 if c is a digit or a letter and 0 otherwise.
int isxdigit( int c )
4
Returns 1 if c is a hexadecimal digit character and 0 otherwise.
int islower( int c )
5
Returns 1 if c is a lowercase letter and 0 otherwise.
int isupper( int c )
6
Returns 1 if c is an uppercase letter; 0 otherwise.
int isspace( int c )
7 Returns 1 if c is a white-space character—newline ('\n'), space (' '), form feed ('\f'),
carriage return ('\r'), horizontal tab ('\t'), or vertical tab ('\v')—and 0 otherwise.
int iscntrl( int c )
Returns 1 if c is a control character, such as newline ('\n'), form feed ('\f'), carriage
8
return ('\r'), horizontal tab ('\t'), vertical tab ('\v'), alert ('\a'), or backspace
('\b')—and 0 otherwise.
int ispunct( int c )
9 Returns 1 if c is a printing character other than a space, a digit, or a letter and 0
otherwise.
int isprint( int c )
10
Returns 1 if c is a printing character including space (' ') and 0 otherwise.
int isgraph( int c )
11
Returns 1 if c is a printing character other than space (' ') and 0 otherwise.

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.

const int buttonPin = 2; // the number of the pushbutton pin


const int ledPin = 13; // the number of the LED pin

int buttonState = 0; // variable for reading the pushbutton status

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

// digital pin 2 has a pushbutton attached to it. Give it a name:


int pushButton = 2;

// the setup routine runs once when you press reset:


void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
}

// the loop routine runs over and over again forever:


void loop() {
// read the input pin:
int buttonState = digitalRead(pushButton);
// print out the state of the button:
Serial.println(buttonState);
delay(1); // delay in between reads for stability
}
4. Reads an analog input on pin 0, converts it to voltage, and prints the result to the
Serial Monitor.

void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

// the loop routine runs over and over again forever:


void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage);
}

You might also like