Introduction to Arduino Programming – Part I
Dr. Sudip Misra
Associate Professor
Department of Computer Science and Engineering
IIT KHARAGPUR
Email: smisra@sit.iitkgp.ernet.in
Website: http://cse.iitkgp.ac.in/~smisra/
Introduction to Internet of Things 1
Features of Arduino
▪ Open source based electronic programmable board
(micro controller)and software(IDE)
▪ Accepts analog and digital signals as input and gives
desired output
▪ No extra hardware required to load a program into the
controller board
Introduction to Internet of Things 2
Types of Arduino Board
▪ Arduino boards based on ATMEGA328 microcontroller
▪ Arduino boards based on ATMEGA32u4 microcontroller
▪ Arduino boards based on ATMEGA2560 microcontroller
▪ Arduino boards based on AT91SAM3X8E microcontroller
Introduction to Internet of Things 3
Arduino UNO
Feature Value
Operating Voltage 5V
Clock Speed 16MHz
Digital I/O 14
Analog Input 6
PWM 6
UART 1
Interface USB via ATMega16U2
Introduction to Internet of Things 4
Board Details
▪ Power Supply: USB or
power barrel jack
▪ Voltage Regulator
▪ LED Power Indicator
▪ Tx-Rx LED Indicator
▪ Output power, Ground
▪ Analog Input Pins
▪ Digital I/O Pins
Image source: https://upload.wikimedia.org/wikipedia/commons/9/9d/UnoConnections.jpg
Introduction to Internet of Things 5
Arduino IDE
▪ Arduino IDE is an open source software that is used to
program the Arduino controller board
▪ Based on variations of the C and C++ programming
language
▪ It can be downloaded from Arduino’s official website
and installed into PC
Introduction to Internet of Things 6
Set Up
▪ Power the board by connecting it to a PC via USB cable
▪ Launch the Arduino IDE
▪ Set the board type and the port for the board
▪ TOOLS -> BOARD -> select your board
▪ TOOLS -> PORT -> select your port
Introduction to Internet of Things 7
Set up (contd..)
Introduction to Internet of Things 8
Arduino IDE Overview
Program coded in Arduino IDE
is called a SKETCH
Introduction to Internet of Things 9
Arduino IDE Overview (contd..)
▪ To create a new sketch
▪ File -> New
▪ To open an existing sketch
▪ File -> open ->
▪ There are some basic ready-to-use
sketches available in the EXAMPLES
section
▪ File -> Examples -> select any
program
Introduction to Internet of Things10
Arduino IDE Overview (contd..)
▪ Verify: Checks the code for
compilation errors
▪ Upload: Uploads the final code
to the controller board
▪ New: Creates a new blank
sketch with basic structure
▪ Open: Opens an existing sketch
▪ Save: Saves the current sketch
Introduction to Internet of Things11
Arduino IDE Overview (contd..)
▪ Serial Monitor: Opens the serial
console
▪ All the data printed to the
console are displayed here
Introduction to Internet of Things12
Sketch Structure
▪ A sketch can be divided into two parts:
▪ Setup ()
▪ Loop()
▪ The function setup() is the point where
the code starts, just like the main()
function in C and C++
▪ I/O Variables, pin modes are initialized
in the Setup() function
▪ Loop() function, as the name suggests,
iterates the specified task in the
program
Introduction to Internet of Things13
Supported Datatype
▪ Arduino supports the following data
types-
Void Long
Int Char
Boolean Unsigned char
Byte Unsigned int
Word Unsigned long
Float Double
Array String-char array
String-object Short
Introduction to Internet of Things14
Arduino Function Libraries
▪ Input/Output Functions:
▪ The arduino pins can be configured to act as input or output pins using the
pinMode() function
Void setup ()
{
pinMode (pin , mode);
}
Pin- pin number on the Arduino board
Mode- INPUT/OUTPUT
Introduction to Internet of Things15
Arduino Function Libraries (contd..)
▪ digitalWrite() : Writes a HIGH or LOW value to a digital pin
▪ analogRead() : Reads from the analog input pin i.e., voltage applied
across the pin
▪ Character functions such as isdigit(), isalpha(), isalnum(), isxdigit(),
islower(), isupper(), isspace() return 1(true) or 0(false)
▪ Delay() function is one of the most common time manipulation function
used to provide a delay of specified time. It accepts integer value (time
in miliseconds)
Introduction to Internet of Things16
Example- Blinking LED
▪ Requirement:
▪ Arduino controller board, USB connector,
Bread board, LED, 1.4Kohm resistor,
connecting wires, Arduino IDE
▪ Connect the LED to the Arduino using the
Bread board and the connecting wires
▪ Connect the Arduino board to the PC using
the USB connector
▪ Select the board type and port
▪ Write the sketch in the editor, verify and
upload.
Introduction to Internet of Things17
Example- Blink (contd..)
Connect the positive terminal of
the LED to digital pin 12 and the
negative terminal to the ground
pin (GND) of Arduino Board
Introduction to Internet of Things18
Example- Blink (contd..) image setup
void setup() {
pinMode(12, OUTPUT); // set the pin mode
}
void loop() {
digitalWrite(12, HIGH); // Turn on the LED
delay(1000);
digitalWrite(12, LOW); //Turn of the LED
delay(1000);
}
Introduction to Internet of Things19
Example- Blink (contd..)
Set the pin mode as output which
is connected to the led, pin 12 in
this case.
Use digitalWrite() function to set
the output as HIGH and LOW
Delay() function is used to specify
the delay between HIGH-LOW
transition of the output
Introduction to Internet of Things20
Example- Blink (contd..) image setup
▪ Connect he board to the PC
▪ Set the port and board type
▪ Verify the code and upload, notice the
TX –RX led in the board starts flashing as
the code is uploaded.
Introduction to Internet of Things21
Acknowledgement
✓ "UnoConnections.jpg", 1sfoerster, 11 September 2011,
CC-BY-SA-3.0,https://commons.wikimedia.org/wiki/
File:UnoConnections.jpg
22
Thank You!!
Introduction to Internet of Things23