U3 Removed
U3 Removed
UNIT – 3
I. INTRODUCTION TO ARDUINO
• This board includes digital I/O pins-14, a power jack, analog i/ps-6, ceramic resonator-A16 MHz, a USB
connection, an RST button, and an ICSP header.
All these can support the microcontroller for further operation by connecting this board to the computer.
The power supply of this board can be done with the help of an AC to DC adapter, a USB cable,
otherwise a battery.
• The ATmega328 is one kind of single-chip microcontroller formed with Atmel within the mega AVR
family. The architecture of this Arduino Uno is a customized Harvard architecture with 8 bit RISC
processor core.
Microcontroller: At the heart of every Arduino board is a microcontroller chip that serves as the brain of
the board. This microcontroller can be programmed to perform various tasks, such as reading sensor data,
controlling motors, and communicating with other devices.
Open Source: Arduino is open-source, which means the hardware and software designs are freely
available to the public. This open nature has led to a vibrant community of developers, makers, and
educators who contribute to and expand upon the platform.
Variety of Boards: There are several types of Arduino boards available, each with its own set of features
and capabilities. Some common Arduino boards include the Arduino Uno, Arduino Nano, and Arduino
Mega. Each board is designed for specific use cases, ranging from simple projects to more complex ones.
Extensible: Arduino boards can be expanded and customized using shields or modules. Shields are add-
on boards that provide additional functionalities like Wi-Fi, Bluetooth, GPS, and motor control. This
extensibility allows you to tailor your Arduino project to your specific needs.
11
III. Arduino Hardware PIN Diagram
Arduino boards feature a variety of pins and headers that are crucial for interfacing
with external components, sensors, and circuits.
1. 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.
Arduino boards can be powered directly from the AC mains power supply by c connecting it to the Barrel
Jack (2).
3. 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.
4. Crystal Oscillator
5. Arduino Reset
Reset your Arduino board, i.e., start your program from the beginning. reset the
UNO board in two ways. First, by using the reset button (17) on the board. Second, you can connect an
external reset button to the Arduino pin labelled RESET (5).
11
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 volts.
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.
7. Analog pins
The Arduino UNO board has five 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.
8. Main microcontroller
Each Arduino board has its own microcontroller (11). it is the brain of your board.
The main IC (integrated circuit) on the Arduino is slightly different from board to board.
The microcontrollers are usually of the ATMEL Company.
9. ICSP pin
ICSP (12) is an AVR, a tiny programming header for the Arduino consisting of MOSI, MISO, SCK,
RESET, VCC, and GND.
This LED should light up when you plug your Arduino into a power source to iindicate 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
• 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.
11
AREF
11
INTERNET OF THINGS
UNIT – 3
I. ARDUINO PROGRAMMING
BASIC CODE
An Arduino sketch (program) typically consists of two main functions: setup () and loop ().
setup (): It is executed once when the board starts or is reset. It's used for initializing variables, configuring
pins, and setting up any hardware.
loop (): It is executed repeatedly after setup () completes.
It contains the main logic of your program and runs indefinitely until the board is turned off or reset.
pinMode()
Description
Configures the specified pin to behave either as an input or an output.
Syntax: pinMode (pin, mode)
pin: Arduino Digital i/o pin number.
mode: INPUT, OUTPUT, or INPUT_PULLUP
Returns: Nothing
11
Example:
void setup() {
pinMode(13, OUTPUT); // sets the digital pin 13 as output
}
digitalWrite()
If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the
corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW.
If the pin is configured as an INPUT, digitalWrite() will enable (HIGH) or disable (LOW) the internal
pullup on the input pin.
Syntax: digitalWrite(pin,value)
11
IDE TOOL SPECIFICATIONS
Example Code://The code makes the digital pin 13 OUTPUT and Toggles it HIGH
and LOW
void setup() {
pinMode(13, OUTPUT); // sets the digital pin 13 as output
}
void loop() {
digitalWrite(13, HIGH); // sets the digital pin 13 on
delay(1000); // waits for a second
digitalWrite(13, LOW); // sets the digital pin 13 off
delay(1000); // waits for a second
}
11
digitalRead()
Description
Reads the value from a specified digital pin, either HIGH or LOW.
Syntax
digitalRead(pin)
Parameters
pin: the Arduino pin number you want to read
Returns
HIGH or LOW
const int buttonPin = 2; // Define the pin number for the button
void setup() {
pinMode(buttonPin, INPUT); // Set pin 2 as an input
Serial.begin(9600); // Initialize serial communication
}
void loop() {
int buttonState = digitalRead(buttonPin); // Read the state of the button
Serial.println(buttonState);// Print the button state (HIGH or LOW) to the Serial
Monitor
delay(100); // Delay for stability
}
11
VARIABLES
2. Assigning Values: Once declared, you can assign a value to the variable.
myNumber = 10; // Assigns the value 10 to the variable myNumber
You can also declare and initialize a variable in a single step:
int myNumber = 10; // Declares and initializes the variable myNumber with the value 10
3. Variable Types: Arduino supports various data types for variables. Some common ones include:
int: Used for integers (whole numbers).
float: Used for floating-point numbers (numbers with decimal points).
char: Used for individual characters.
bool: Used for boolean values (true/false).
4. Using Variables: Once you've declared and assigned values to variables, you can use them in your code.
int a = 5;
int b = 3;
int sum = a + b; // Adds the values of variables a and b and stores the result in sum
EXAMPLE SKETCH
Write an Arduino sketch that demonstrates the use of variables to blink an LED connected to pin
13 at different intervals:
int ledPin = 13; // Define the pin number for the LED
int interval1 = 500; // Interval for the first blink (in milliseconds)
int interval2 = 1000; // Interval for the second blink (in milliseconds)
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as an output
}
void loop() {
digitalWrite(ledPin, HIGH); // Turn on the LED
delay(interval1); // Wait for interval1 milliseconds
digitalWrite(ledPin, LOW); // Turn off the LED
delay(interval2); // Wait for interval2 milliseconds
}
11
INTERNET OF THINGS
UNIT – 3
1. Analog pins are used to read analog signals, such as sensor readings or voltage levels.
2. They provide a range of values between 0 and 1023 based on the input voltage.
3. Labelled as "A" followed by a number (e.g., A0, A5).
1. Analog output pins on Arduino are typically the PWM (Pulse Width Modulation) pins.
2. These pins can generate analog-like output voltages by rapidly switching between
HIGH and LOW states at a fixed frequency and varying the duty cycle of the
signal.
3. Labelled with ~ symbol on Digital Pins
analogRead() : The analogRead() function reads the analog voltage from the sensor,
converting it to a digital value between 0 and 1023.
analogRead()
Description:
Syntax:
analogRead(pin)
Parameters
pin: the name of the analog input pin to read from(A0 to A5 on Arduino UNO, A0 to A6 on
MKR boards, A0 to A7 on the Mini and Nano, A0 to A15 on the Mega).
11
/The code reads the voltage on analogPin and displays it.
void setup() {
Serial.begin(9600); // setup serial
}
void loop() {
val = analogRead(analogPin); // read the input pin
Serial.println(val); // debug value
}
analogWrite()
Description: Writes an analog value (PWM Wave) to a pin. Can be used to light a LED at
varying brightnesses or drive a motor at various speeds.
After a call to analogWrite(), the pin will generate a steady rectangular wave of the specified
duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite() on
the same pin.
11
Syntax: analogWrite(pin, value)
Parameters: - pin: the Arduino pin to write to. Allowed data types: int.
value: the duty cycle: between 0 (always off) and 255 (always on). Allowed data types:
int.
Example
//Sets the output to the LED proportional to the value read from the potentiometer.
intledPin = 9; // LED connected to digital pin 9
intanalogPin = 3; // potentiometer connected to analog pin 3
intval = 0; // variable to store the read value
void setup() {
pinMode(ledPin, OUTPUT); // sets the pin as output
}
void loop() {
val = analogRead(analogPin); // read the input pin
analogWrite(ledPin, val / 4); // analogRead values go from 0 to 1023,
analogWrite values from 0 to 255
}
OUTPUT
LED OFF
LED ON
11
Serial Communication
Serial communication in Arduino is a method used for sending data between the Arduino
board and other devices, such as computers, other Arduinos, or microcontrollers.
It allows you to transmit and receive data through the USB port or hardware serial Ports.
UART (Universal Asynchronous Receiver/Transmitter):
UART is responsible for transmitting and receiving serial data using two pins: TX
(transmit) and RX (receive).
Baud Rate:
Baud rate is the speed at which data is transmitted over the serial connection.
It is measured in bits per second (bps).
Common baud rates include 9600, 19200, and 115200.
4. Serial Monitor:
The Serial Monitor which allows you to view and send data between the Arduino and
the computer. It's commonly used for debugging and monitoring serial communication.
void setup() {
Serial.begin(9600); // Initialize serial communication at 9600 baud rate
}
void loop() {
// Check if there is incoming data available to read
if (Serial.available() > 0)
{
// Read the incoming byte
char receivedChar = Serial.read();
Serial.print("Received character: ");
Serial.println(receivedChar);
}
// Send data to the Serial Monitor
Serial.print("Hello, world! ");
// Send binary data (ASCII value of 'A') over the serial port
Serial.write('A');
delay(1000); // Wait for 1 second }
11
CONDITIONAL EXECUTION
1. if Statement:
Example: -
2. if-else Statement:
The if-else statement allows you to execute one block of code if the condition is true,
and another block of code if the condition is false.
Example: -
3. else if Statement:
The else if statement allows you to specify multiple conditions to be checked
sequentially.
11
Example: -
The switch statement provides an efficient way to select among multiple options based
on the value of an expression.
Example: -
int option = 2;
switch (option) {
case 1:
// Execute code for option 1
break;
case 2:
// Execute code for option 2
break;
case 3:
// Execute code for option 3
break;
default:
// Execute code if none of the above cases match
break;
}
11
INTERNET OF THINGS
UNIT – 3
I. Loops
Loops are control structures that allow you to execute a block of code repeatedly.
There are different types of loops available in Arduino, including for loop, while loop, and do-while loop.
1. for loop
The for loop is used when you know exactly how many times you want to repeat a block of code.
void setup() {
// Initialization code here
}
void loop() {
// Loop code here
for (int i = 0; i < 10; i++) {
// Execute this block of code 10 times
// i will be 0, 1, 2, ..., 9 in each iteration
}
}
2. While loop
The while loop is used when you want to repeat a block of code as long as a condition is true.
void setup() {
// Initialization code here
}
void loop() {
// Loop code here
int i = 0;
while (i < 10) {
// Execute this block of code as long as i is less than 10
i++;
}
}
3. do-while Loop:
The do-while loop is similar to the while loop, but it always executes the block of code at
least once before checking the condition.
void setup()
{
11
// Initialization code here
}
void loop() {
// Loop code here
int i = 0;
do {
// Execute this block of code at least once
i++;
} while (i < 10);
}
Arrays
Arrays in Arduino are a collection of variables of the same type that are accessed
by an index. They allow you to store multiple values of the same data type under
one variable name.
Declaration and Initialization:
You can declare and initialize arrays in Arduino like this:
// Declare an array of integers with a fixed size
int myArray[5];
// Initialize an array with values
int myArray[3] = {10, 20, 30};
Accessing Elements:
You can access individual elements of an array using square brackets [] and the index
of the element (indices start from 0):
Arrays sketch
Functions
Functions are blocks of code that perform a specific task. They allow you to break
down your program into smaller, more manageable parts, making it easier to
understand, debug, and maintain.
Creating a Function:
You can create a function in Arduino by specifying its return type, name, and parameters (if any),
followed by a block of code enclosed in curly braces {}.
// Function declaration
void myFunction() {
// Function body
// Code to perform a specific task
}
Calling a Function:
To use a function, you simply call its name followed by parentheses ().
void setup() {
// Other setup code
11
Function Parameters and Return Values:
You can pass parameters to a function to provide input values, and you can specify a return type
to indicate the type of value the function will return.
Strings
In Arduino, strings can be manipulated using either the built-in String class or
C-style character arrays (also known as C-strings).
The String class in Arduino provides a convenient way to manipulate strings. It allows you to
perform operations such as concatenation, comparison, and conversion easily.
Example:
C-style character arrays (or C-strings) are arrays of characters terminated by a null character
('\0'). They are commonly used in C and C++ for handling strings.
Example:
char firstName[] = "John";
char lastName[] = "Doe";
char fullName[20]; // Allocate space for the full name
strcpy(fullName, firstName); // Copy first name to full name
strcat(fullName, " "); // Concatenate a space
11
strcat(fullName, lastName); // Concatenate last name
Serial.println(fullName); // Output: John Doe
Strings Sketch
11
INTERNET OF THINGS
UNIT – 3
Controlling LED
LED: It's an electronic component used to emit light when an electric current passes through it.
These are the most common LEDs available in various colours, including red, green, blue,
yellow, and white. They are widely used for indicator lights, displays, and general lighting.
Interfacing: Connect the longer lead (anode) to a current-limiting resistor and voltage source
(e.g., 5V for most standard LEDs) and the shorter lead (cathode) to ground.
Use a current-limiting resistor to protect the LED from excess current. For example, a 220-330ohm
resistor is commonly used.
Interfacing: Connect one terminal of the push button to a digital pin on the Arduino (e.g., Pin 2).
Connect the other terminal of the push button to the ground (GND) of the Arduino.
Connect a resistor (typically 10k ohms) from the same digital pin (e.g., Pin 2) to
the 5V supply (to create an external pull-up resistor).
UNIT – 3
A photo resistor changes its resistance based on the intensity of light falling on it.
In bright light, its resistance decreases, while in darkness, its resistance increases.
Interfacing:
LDR Connection: VCC to 5V: Connect one leg of the LDR to the 5V pin on the Arduino
board.
GND to GND: Connect the other leg of the LDR to any ground (GND) pin on the
Arduino board.
Analog to A0: Connect the leg of the LDR (the sensing leg) to analog pin A0 on the
Arduino
void loop() {
// Read the analog value from the LDR
int ldrValue = analogRead(ldrPin);
if (ldrValue > THRESHOLD)
{
digitalWrite(ledPin, HIGH); // Turn on the LED
}
else {
digitalWrite(ledPin, LOW); // Turn off the LED
}
Serial.print("LDR Value: ");
Serial.println(ldrValue);
delay(500);
}
INTERFACING LCD
11
Connections
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
void setup() {
lcd.begin(16, 2);
11
// you can now interact with the LCD, e.g.:
lcd.print("Hello World!");
}
void loop()
{
// Set the cursor to the start of the second line
lcd.setCursor(0, 1);
lcd.print("Learn Arduino");
}
11
INTERNET OF THINGS
UNIT – 3
Experiment 3 : Monitoring the voltage level of the battery and indicating the same using multiple LED's &
OLED with Arduino/ESP32/Raspberry Pi.
AIM: To interface & write a program to monitor battery voltage level and
indicating on OLED display and multiple LED’s.
Equipment Required:
· Arduino UNO
· OLED Display
· Resistors 1kΩ, 10KΩ
· LED card
· 9V Hi-watt Battery
· Jumper wires
Pin wiring
Because the OLED display uses I2C communication protocol, wiring is very simple. You just need to connect to
the Arduino Uno I2C pins as shown in the table below.
Vin 5V
GND GND
SCL A5
SDA A4
To control the OLED display you need the adafruit_SSD1306.h and the adafruit_GFX.h libraries. Follow the
next instructions to install those libraries.
11
1. Open your Arduino IDE and go to Sketch > Include Library >
Manage Libraries. The Library Manager should open.
2. Type “SSD1306” in the search box and install the SSD1306 library from
Adafruit.
Program:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4 Adafruit_SSD1306 display(OLED_RESET);
int greenLed = 2;
int yellowLed = 3;
int redLed = 4;
int analogValue = 0;
float voltage = 0;
int ledDelay = 1000;
void setup()
{
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); display.display();
display.clearDisplay();
Serial.begin(9600);
pinMode(greenLed, OUTPUT);
pinMode(yellow Led, OUTPUT); pinMode(redLed,OUTPUT);
}
void loop()
{
analogValue = analogRead(A0);
11
INTERNET OF THINGS
UNIT – 3
Experiment 4: Generatation of a random value similar to dice game and display the same using a 16X2 LCD
with Arduino/ESP32/Raspberry Pi
AIM:To interface & write a program to generate a random value similar to dice game simulation and display on
LCD using Arduino.
Components Required:
· Arduino UNO
· 16X2 LCD
· Push button switch
· Resistor 470Ω, 1kΩ, 10KΩ
· Jumper wires
LCD(Liquid Crystal Display)
LCDs (Liquid Crystal Displays) are used in embedded system applications for displaying various parameters
and status of the system.
LCD 16x2 is a 16-pin device that has 2 rows that can accommodate 16 characters each.LCD 16x2 can be used
in 4-bit mode or 8-bit mode.
It is also possible to create custom characters.
It has 8 data lines and 3 control lines that can be used for control purposes.
11
Pin No Function Name
1 Ground (0V) Ground
2 Supply voltage; 5V (4.7V – 5.3V) Vcc
3 Contrast adjustment; through a variable resistor VEE
4 Selects command register when low; and data register when high Register Select
5 Low to write to the register; High to read from the register Read/write
6 Sends data to data pins when a high to low pulse is given Enable
7 DB0
8 DB1
9 DB2
10 DB3
8-bit data pins
11 DB4
12 DB5
13 DB6
14 DB7
15 Backlight VCC (5V) Led+
16 Backlight Ground (0V) Led-
Program:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int randomnumber;
void setup()
{ // put your setup code here, to run once:
lcd.begin(16, 2);
randomSeed(7);
pinMode(8, INPUT);
}
void loop()
{
lcd.setCursor(2, 0);
lcd.print("Dice value is:");
int DICEROLL = digitalRead(8);
if(DICEROLL==1)
11
randomnumber = random(1,7);
lcd.setCursor(6, 1);
lcd.print(randomnumber);
}
OUTPUT:
11