0% found this document useful (0 votes)
11 views34 pages

Arduino Basics: Boards & Programming Guide

The document provides an overview of Arduino, detailing various models like Arduino Uno, Nano, and Mega, along with the structure of an Arduino sketch which includes setup and loop functions. It covers key concepts such as digital and analog I/O, timing functions, and examples of interfacing components like LEDs, seven-segment displays, and sensors (IR, PIR, LDR) with Arduino. Additionally, it includes code examples for reading inputs and controlling outputs, demonstrating practical applications in embedded systems projects.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views34 pages

Arduino Basics: Boards & Programming Guide

The document provides an overview of Arduino, detailing various models like Arduino Uno, Nano, and Mega, along with the structure of an Arduino sketch which includes setup and loop functions. It covers key concepts such as digital and analog I/O, timing functions, and examples of interfacing components like LEDs, seven-segment displays, and sensors (IR, PIR, LDR) with Arduino. Additionally, it includes code examples for reading inputs and controlling outputs, demonstrating practical applications in embedded systems projects.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

What is Arduino?

1. Arduino Uno

Presented by: [Link] Kumar Embedded System Trainer at Sofcon India


2. Arduino Nano

Presented by: [Link] Kumar Embedded System Trainer at Sofcon India


3. Arduino Mega

Presented by: [Link] Kumar Embedded System Trainer at Sofcon India


Presented by: [Link] Kumar Embedded System Trainer at Sofcon India
Presented by: [Link] Kumar Embedded System Trainer at Sofcon India
1. Structure of an Arduino Sketch
An Arduino program, called a "sketch," has two main functions:

 setup(): Runs once when the board starts. Used to set up


configurations like pin modes or initialize libraries.
 loop(): Runs continuously after setup(). Used to execute the
main code repeatedly.

2. Key Concepts

Digital I/O
 pinMode(pin, mode): Sets a pin as INPUT or
OUTPUT.
 digitalWrite(pin, value): Sets a digital
pin to HIGH (5V) or LOW (0V).
 digitalRead(pin): Reads the value of a digital
pin (HIGH or LOW).
Analog I/O
 analogRead(pin): Reads the voltage (0–1023)
on an analog pin.
Presented by: [Link] Kumar Embedded System Trainer at Sofcon India
 analogWrite(pin, value): Outputs PWM
signals (0–255) on PWM pins.

Timing
 delay(ms): Pauses the program for ms
milliseconds.

LED (Light Emitting Diode)

Presented by: [Link] Kumar Embedded System Trainer at Sofcon India


 [Link](baudRate): Initializes serial
communication.
 [Link](): Sends data to the serial monitor.
 [Link](): Reads incoming serial data.

Presented by: [Link] Kumar Embedded System Trainer at Sofcon India


A seven-segment display is a common output device
used to display numbers.
1. Types of Seven-Segment Displays

Presented by: [Link] Kumar Embedded System Trainer at Sofcon India


2. Pin Configuration

Common cathode
Code Example:
 // Define segment pins
 int segmentPins[] = {2, 3, 4, 5, 6, 7, 8}; // a, b,
c, d, e, f, g

 // Digit patterns for 0-9 (common cathode
configuration)
 int digits[10][7] = {
 {1, 1, 1, 1, 1, 1, 0}, // 0
 {0, 1, 1, 0, 0, 0, 0}, // 1
 {1, 1, 0, 1, 1, 0, 1}, // 2
Presented by: [Link] Kumar Embedded System Trainer at Sofcon India
 {1, 1, 1, 1, 0, 0, 1}, // 3
 {0, 1, 1, 0, 0, 1, 1}, // 4
 {1, 0, 1, 1, 0, 1, 1}, // 5
 {1, 0, 1, 1, 1, 1, 1}, // 6
 {1, 1, 1, 0, 0, 0, 0}, // 7
 {1, 1, 1, 1, 1, 1, 1}, // 8
 {1, 1, 1, 1, 0, 1, 1} // 9
 };

 void setup() {
 // Set segment pins as OUTPUT
 for (int i = 0; i < 7; i++) {
 pinMode(segmentPins[i], OUTPUT);
 }
 }

 void loop() {
 for (int num = 0; num < 10; num++) { // Display
numbers 0-9
 displayDigit(num);
 delay(1000); // Wait 1 second
 }
 }

 void displayDigit(int num) {
 for (int i = 0; i < 7; i++) {
 digitalWrite(segmentPins[i], digits[num][i]);
 }
 }

Presented by: [Link] Kumar Embedded System Trainer at Sofcon India


Syntax :
const int variableName = value;

Example: Using const int


const int ledPin = 13; // Define pin 13 as a
constant

void setup() {
pinMode(ledPin, OUTPUT); // Use the constant
instead of the raw pin number
}

void loop() {
digitalWrite(ledPin, HIGH); // Turn LED on
delay(1000); // Wait for 1 second
digitalWrite(ledPin, LOW); // Turn LED off
delay(1000); // Wait for 1 second
}

Presented by: [Link] Kumar Embedded System Trainer at Sofcon India


Types of Inputs

Presented by: [Link] Kumar Embedded System Trainer at Sofcon India


Inputs on Arduino

 Digital Pins: Read HIGH or LOW using digitalRead(pin).


 Analog Pins: Measure voltage levels (0–1023) using
analogRead(pin).

Example: Reading a Push Button


void setup() {
pinMode(2, INPUT); // Set pin 2 as input
[Link](9600); // Start serial
communication
}

void loop() {
int buttonState = digitalRead(2); // Read
the button state
[Link](buttonState); // Print state
to the serial monitor
delay(100); // Small delay
}

PUSH BUTTON Code Example


const int buttonPin = 2; // Push button connected to pin
2

Presented by: [Link] Kumar Embedded System Trainer at Sofcon India


const int ledPin = 13; // LED connected to pin 13

void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Set button pin as
input with pull-up resistor
pinMode(ledPin, OUTPUT); // Set LED pin as
output
}

void loop() {
int buttonState = digitalRead(buttonPin); // Read the
state of the button

if (buttonState == LOW) { // Button is pressed (LOW


due to INPUT_PULLUP)
digitalWrite(ledPin, HIGH); // Turn LED ON
} else {
digitalWrite(ledPin, LOW); // Turn LED OFF
}
}

To control the brightness of an LED using a potentiometer, we can


use Pulse Width Modulation (PWM) with an analog input. Here's
the Arduino code for brightness control:

How It Works
1. Potentiometer Input:
Presented by: [Link] Kumar Embedded System Trainer at Sofcon India
o Reads the analog value from the potentiometer (0-1023).
2. Mapping to PWM Range:
o The map() function scales the 0-1023 potentiometer

range to the 0-255 PWM range required for controlling


brightness.
3. PWM Output:
o analogWrite() generates a PWM signal on the LED pin.
The brightness of the LED varies according to the duty
cycle (0 = OFF, 255 = FULL BRIGHTNESS).

Connections
1. Potentiometer:
o One outer pin → 5V.
o Other outer pin → GND.
o Middle pin → A0.
2. LED:
o Long leg (anode) → Pin 9 (through a 220Ω
resistor).
o Short leg (cathode) → GND.

Presented by: [Link] Kumar Embedded System Trainer at Sofcon India


Structure of a 4x4 Keypad

Physical Layout
C1 C2 C3 C4
R1 1 2 3 A
R2 4 5 6 B
R3 7 8 9 C
R4 * 0 # D

 Rows: R1, R2, R3, R4


 Columns: C1, C2, C3, C4

Each button press connects a specific row to a specific column:

 Pressing 1 connects R1 and C1.


 Pressing 5 connects R2 and C2.
 Pressing D connects R4 and C4.

Presented by: [Link] Kumar Embedded System Trainer at Sofcon India


Distance=Speed of Sound×Time

2
Presented by: [Link] Kumar Embedded System Trainer at Sofcon India
Common Uses:
 Obstacle Detection: In robots or autonomous vehicles.
 Distance Measurement: In parking sensors or industrial automation.
 Liquid Level Measurement: In tanks or reservoirs.
 Object Detection: In production lines for quality control.

Popular Models:

 HC-SR04: A widely used, affordable ultrasonic sensor with a


range of 2 cm to 400 cm.

Presented by: [Link] Kumar Embedded System Trainer at Sofcon India


Light Dependent Resistor.
Here is an example code for interfacing an LDR sensor with an
Arduino Uno to measure light intensity and display the result in the
serial monitor.

Requirements:
 Arduino Uno
 LDR sensor
 10kΩ resistor
 Jumper wires
 Breadboard

Circuit Diagram:

1. Connect one terminal of the LDR to 5V.


2. Connect the other terminal of the LDR to an analog pin (e.g.,
A0) and one end of a 10kΩ resistor.
3. Connect the other end of the resistor to GND.
Analog Reading:
 The analogRead() function reads the voltage from the LDR
circuit as a value between 0 and 1023.
 0 represents 0V, and 1023 represents 5V.

Presented by: [Link] Kumar Embedded System Trainer at Sofcon India


Presented by: [Link] Kumar Embedded System Trainer at Sofcon India
In the context of an Arduino Uno, an IR sensor is a component used to
detect the presence of objects, measure distances, or sense motion using
infrared radiation. When connected to an Arduino Uno, the sensor interacts
with the microcontroller to perform tasks like obstacle detection, line
following, or proximity sensing.

How an IR Sensor Works with Arduino Uno

An IR sensor typically has three pins:


1. VCC: Connects to the 5V pin of the Arduino to supply power.
2. GND: Connects to the ground pin of the Arduino.
3. OUT: Outputs a signal (digital or analog) to an Arduino pin,
indicating the detection status or proximity.

Types of IR Sensors for Arduino

1. Digital IR Sensors:
Presented by: [Link] Kumar Embedded System Trainer at Sofcon India
o Provide a high (1) or low (0) output based on object
detection.
o Example: Basic IR proximity sensors.
2. Analog IR Sensors:
o Output a range of values representing the distance to an
object.
o Example: Sharp IR distance sensors.

Example IR Sensor for Arduino Uno

A common IR sensor module for Arduino includes:


 An IR LED for emitting infrared light.
 A photodiode or phototransistor for detecting reflected IR
light.
 A comparator circuit that processes the detection.

Applications of IR Sensors in Arduino Projects

 Obstacle Avoidance Robots: Detect obstacles and change the


robot's path.
 Line Following Robots: Detect lines (white/black) on the
surface.
 Motion Detection: Trigger alarms or lights.
 Proximity Detection: Automate tasks like opening doors.

Example Arduino Code for an IR Sensor

This example demonstrates a basic setup with a digital IR


sensor:

Presented by: [Link] Kumar Embedded System Trainer at Sofcon India


#define IR_SENSOR_PIN 2 // Connect the OUT pin of
the IR sensor to digital pin 2
#define LED_PIN 13 // Built-in LED

void setup() {
pinMode(IR_SENSOR_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
[Link](9600);
}

void loop() {
int sensorValue = digitalRead(IR_SENSOR_PIN); //
Read the sensor output

if (sensorValue == LOW) { // Object detected


digitalWrite(LED_PIN, HIGH); // Turn on the
LED
[Link]("Object detected!");
} else { // No object detected
digitalWrite(LED_PIN, LOW); // Turn off the
LED
[Link]("No object detected.");
}

delay(100);
}

Presented by: [Link] Kumar Embedded System Trainer at Sofcon India


Wiring

1. VCC → Arduino 5V
2. GND → Arduino GND
3. OUT → Arduino digital pin (e.g., D2)

In the context of an Arduino Uno, a PIR sensor (Passive Infrared


Sensor) is a device used to detect motion by sensing infrared
radiation emitted by objects, especially living beings, within its
range. PIR sensors are commonly used in motion-activated devices
like alarms, lighting systems, and security cameras.

How a PIR Sensor Works

A PIR sensor detects changes in infrared radiation levels. It does not


emit infrared light; instead, it passively senses heat signatures from
objects in its field of view:
 Stable IR levels: No motion detected.
 Changing IR levels: Motion detected (caused by a moving object or
person).
Presented by: [Link] Kumar Embedded System Trainer at Sofcon India
PIR Sensor with Arduino Uno

A typical PIR sensor module has three pins:


1. VCC: Power supply, connected to the 5V pin on the Arduino.
2. GND: Ground, connected to the Arduino's GND.
3. OUT: Signal output, connected to a digital pin on the Arduino (e.g.,
D2).

Features of PIR Sensors

 Adjustable Sensitivity: Some modules have a potentiometer to adjust


the detection range.
 Adjustable Delay: Another potentiometer may control the duration of
the output signal.
 Detection Range: Typically 3–7 meters.
 Wide Field of View: Usually 110°–180°.

Applications of PIR Sensors with Arduino

 Motion Detection: Trigger alarms or lights.


 Security Systems: Detect intrusions.
 Energy Saving: Automate lighting or appliances.
 Robotics: Sense nearby humans or animals.

Example Arduino Code for a PIR Sensor

This example detects motion and turns on an LED or prints a


message on the Serial Monitor:
#define PIR_PIN 2 // Connect the PIR sensor OUT
pin to digital pin 2

Presented by: [Link] Kumar Embedded System Trainer at Sofcon India


#define LED_PIN 13 // Built-in LED on the Arduino
Uno

void setup() {
pinMode(PIR_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
[Link](9600); // Initialize Serial Monitor
}

void loop() {
int motionDetected = digitalRead(PIR_PIN); //
Read the PIR sensor output

if (motionDetected == HIGH) { // Motion detected


digitalWrite(LED_PIN, HIGH); // Turn on the
LED
[Link]("Motion detected!");
} else { // No motion
digitalWrite(LED_PIN, LOW); // Turn off the
LED
[Link]("No motion.");
}
Presented by: [Link] Kumar Embedded System Trainer at Sofcon India
delay(100); // Small delay for stability
}

Wiring a PIR Sensor to Arduino

1. VCC → Arduino 5V
2. GND → Arduino GND
3. OUT → Arduino digital pin (e.g., D2)

How It Works

1. When motion is detected, the PIR sensor sends a HIGH signal to the
Arduino.
2. The Arduino performs an action, such as turning on an LED or
triggering an alarm.
3. If no motion is detected, the signal remains LOW.

Presented by: [Link] Kumar Embedded System Trainer at Sofcon India


Key Features of a Servo Motor:
1. Control Angle: Most servo motors rotate within a range of 0 to 180
degrees (or 360 degrees for continuous rotation servos)
2. Ease of Use: They only require three connections (power, ground, and
signal).

Wiring a Servo Motor to an Arduino Uno:


 Red Wire: Connects to the 5V pin on the Arduino.
 Black/Brown Wire: Connects to the GND pin on the Arduino.
 Yellow/White Wire (Signal): Connects to a PWM-capable digital pin on
the Arduino

Presented by: [Link] Kumar Embedded System Trainer at Sofcon India


1. Servo myServo;
Creates a servo object to control the motor.
2. [Link](9);
Links the servo to pin 9.
3. [Link](angle);
Moves the servo to the specified angle (0–180 degrees).
4. delay(ms);
Waits for a specified number of milliseconds, allowing
the servo to reach its position.

Code Example Using the Arduino Servo Library:


#include <Servo.h> // Include the Servo
library
Servo myServo; // Create a servo
object
void setup() {
[Link](9); // Attach the servo
to digital pin 9
}
void loop() {
[Link](0); // Set servo to 0
degrees
delay(1000); // Wait 1 second

Presented by: [Link] Kumar Embedded System Trainer at Sofcon India


[Link](90); // Set servo to 90
degrees
delay(1000); // Wait 1 second
[Link](180); // Set servo to
180 degrees
delay(1000); // Wait 1 second
}

UART (Universal Asynchronous Receiver Transmitter) and


USART (Universal Synchronous and Asynchronous Receiver
Transmitter) are both serial communication protocols used for data
transfer between devices. Let’s understand them in detail.

UART Communication

UART is an asynchronous communication protocol, which means it


does not require a clock signal for data transfer.

Key Features:
1. Pin Usage:
o TX (Transmit): To send data.
o RX (Receive): To receive data.

Presented by: [Link] Kumar Embedded System Trainer at Sofcon India


2. Data Frame Format:
o Start Bit: Indicates the beginning of data transfer.
o Data Bits: Actual data (5-9 bits).
o Parity Bit (optional): For error checking.
o Stop Bit: Indicates the end of data transfer.
3. Baud Rate: The speed of transmission (e.g., 9600, 115200).

Example:
Both devices must operate at the same baud rate; otherwise, the data
cannot be sent or received correctly.
UART in Arduino:
 Arduino Uno has hardware UART pins:
o Pin 0 (RX) and Pin 1 (TX).
 It is initialized using [Link]() on the Arduino.

Code Example (UART):


void setup() {
[Link](9600); // Start UART communication at 9600
baud rate.
}

void loop() {
[Link]("Hello, World!"); // Send data via UART.
delay(1000); // Send data every 1 second.
}

USART Communication

USART is an advanced version of UART that supports both


synchronous and asynchronous modes.

Presented by: [Link] Kumar Embedded System Trainer at Sofcon India


Key Features:
1. Synchronous Mode:
o A clock signal is used between the master and slave devices.
o The clock signal synchronizes data, enabling faster and more
accurate transfers.
2. Asynchronous Mode:
o Functions like UART and does not require a clock signal.

USART in Arduino:
 The microcontroller (ATmega328P) of Arduino Uno supports USART.
 However, accessing synchronous mode in Arduino IDE requires low-
level register coding.

Differences Between UART and USART


Parameter UART USART

Sync Mode Asynchronous only Supports both sync and async

Clock Signal Not required Required for synchronous mode

Hardware Complexity Simpler More complex

Speed Slower Faster

Using UART Between Two Arduinos

Connections:
1. TX (Arduino 1) → RX (Arduino 2).
Presented by: [Link] Kumar Embedded System Trainer at Sofcon India
2. RX (Arduino 1) → TX (Arduino 2).
3. GND (Arduino 1) → GND (Arduino 2).

Code for Arduino 1 (Sender):


void setup() {
[Link](9600);
}

void loop() {
[Link]("Hello from Arduino 1");
delay(1000);
}

Code for Arduino 2 (Receiver):


void setup() {
[Link](9600);
}

void loop() {
if ([Link]()) {
String received = [Link]();
[Link]("Received: " + received);
}
}

Conclusion:

 UART is simple and suitable for basic device communication.


 USART is better for high-speed or precise communication, especially
when synchronous mode is used.

Presented by: [Link] Kumar Embedded System Trainer at Sofcon India

You might also like