0% found this document useful (0 votes)
0 views

Programming With Arduino Uno

The document provides an overview of programming with Arduino Uno, detailing its features, setup process, and basic programming structure using the Arduino IDE. It covers essential functions, example programs, and interfacing with components like LEDs and sensors, as well as communication protocols such as Serial, I2C, and SPI. Key notes for exam preparation and debugging techniques are also included to aid understanding and practical application.

Uploaded by

nishiijardar6
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)
0 views

Programming With Arduino Uno

The document provides an overview of programming with Arduino Uno, detailing its features, setup process, and basic programming structure using the Arduino IDE. It covers essential functions, example programs, and interfacing with components like LEDs and sensors, as well as communication protocols such as Serial, I2C, and SPI. Key notes for exam preparation and debugging techniques are also included to aid understanding and practical application.

Uploaded by

nishiijardar6
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
You are on page 1/ 19

Notes Of Fundamental of IOT

Unit - 4 Programming with Arduino Uno

Programming with Arduino Uno

Arduino Uno is an open-source microcontroller board based on the ATmega328P


microcontroller. It is widely used in electronics projects, automation, and embedded
systems due to its ease of use and versatility. Programming the Arduino Uno involves
writing code in the Arduino IDE (Integrated Development Environment) using C/C++
language with Arduino-specific libraries.
1

1. Features of Arduino Uno


●​ Microcontroller: ATmega328P
●​ Operating Voltage: 5V
●​ Input Voltage (recommended): 7-12V
●​ Digital I/O Pins: 14 (6 PWM outputs)
●​ Analog Input Pins: 6
●​ Flash Memory: 32 KB
●​ Clock Speed: 16 MHz
●​ Communication: UART, I2C, SPI
●​ USB Interface: Used for programming and serial communication

2. Setting Up Arduino Uno


Requirements:

●​ Arduino Uno Board


●​ USB Cable (Type A to Type B)
●​ Arduino IDE (Download from www.arduino.cc)
●​ Computer (Windows, macOS, or Linux)
Installation Steps:

1.​ Download and install the Arduino IDE.


2.​ Connect the Arduino Uno to the computer using a USB cable.
3.​ Open Arduino IDE and select the correct board:
○​ Go to Tools > Board > Arduino Uno
4.​ Select the correct port:
○​ Go to Tools > Port > (Select the COM Port)
5.​ Write or load a program (called a sketch).
6.​ Click Upload (Arrow button) to transfer the code to the Arduino board.
2

3. Structure of an Arduino Program (Sketch)


An Arduino program consists of two main functions:
Basic Structure

cpp

CopyEdit

void setup() {

// Code here runs once when the Arduino is


powered on or reset

pinMode(13, OUTPUT); // Example: Set pin 13


as output

void loop() {

// Code here runs repeatedly

digitalWrite(13, HIGH); // Turn LED ON

delay(1000); // Wait for 1 second

digitalWrite(13, LOW); // Turn LED OFF

delay(1000); // Wait for 1 second


3

Explanation:

●​ setup(): Initializes settings (runs once).


●​ loop(): Contains the main logic (runs continuously).
●​ pinMode(): Sets pin as INPUT or OUTPUT.
●​ digitalWrite(): Turns a pin HIGH or LOW.
●​ delay(): Pauses execution for a specified time in milliseconds.

4. Important Arduino Functions


Digital and Analog I/O

●​ pinMode(pin, mode): Configures a pin as INPUT or OUTPUT.


●​ digitalWrite(pin, value): Sets a digital pin HIGH or LOW.
●​ digitalRead(pin): Reads HIGH or LOW from a digital pin.
●​ analogRead(pin): Reads an analog value (0-1023) from an analog
pin.
●​ analogWrite(pin, value): Outputs a PWM signal (0-255) on
PWM pins (3, 5, 6, 9, 10, 11).
Communication Functions

●​ Serial.begin(baud_rate): Starts serial communication (e.g.,


Serial.begin(9600)).
●​ Serial.print(data): Prints data to the Serial Monitor.
●​ Serial.println(data): Prints data with a newline.
●​ Serial.read(): Reads incoming serial data.
Timing Functions
4

●​ millis(): Returns the number of milliseconds since the program started.


●​ delay(ms): Pauses the program for a specified time.

5. Example Programs
Blinking LED

cpp

CopyEdit

void setup() {

pinMode(13, OUTPUT);

void loop() {

digitalWrite(13, HIGH);

delay(500);

digitalWrite(13, LOW);

delay(500);

Reading a Button Press


5

cpp

CopyEdit

int buttonPin = 2;

int ledPin = 13;

void setup() {

pinMode(buttonPin, INPUT);

pinMode(ledPin, OUTPUT);

void loop() {

int buttonState = digitalRead(buttonPin);

if (buttonState == HIGH) {

digitalWrite(ledPin, HIGH);

} else {

digitalWrite(ledPin, LOW);

}
6

Reading Analog Sensor (e.g., LDR, Potentiometer)

cpp

CopyEdit

int sensorPin = A0;

int sensorValue;

void setup() {

Serial.begin(9600);

void loop() {

sensorValue = analogRead(sensorPin);

Serial.println(sensorValue);

delay(500);

}
7

6. Communication with Other Devices


Interfacing with LCD Display

cpp

CopyEdit

#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup() {

lcd.begin(16, 2);

lcd.print("Hello, Arduino!");

void loop() {

Interfacing with Servo Motor

cpp
8

CopyEdit

#include <Servo.h>

Servo myServo;

void setup() {

myServo.attach(9);

void loop() {

myServo.write(90); // Move to 90 degrees

delay(1000);

myServo.write(0); // Move to 0 degrees

delay(1000);

7. Key Notes for Exam Preparation


9

●​ Understand the Arduino Uno board components (Microcontroller, pins, power


supply, etc.).
●​ Learn the Arduino IDE and its tools (Serial Monitor, COM port selection).
●​ Understand the basic program structure (setup() and loop()).
●​ Know the difference between digital and analog inputs/outputs.
●​ Familiarize yourself with important functions (digitalWrite(),
analogRead(), Serial.print(), etc.).
●​ Study communication protocols (UART, I2C, SPI).
●​ Be able to write basic programs for LED, button, and sensors.
●​ Practice interfacing with external components (LCD, Servo, Motors, Sensors).
●​ Understand debugging using Serial Monitor.
●​ Know how to use libraries (#include <Servo.h>, #include
<LiquidCrystal.h>).

8. Additional Resources

●​ Arduino Official Website: https://www.arduino.cc


●​ Arduino Reference: https://www.arduino.cc/reference/en/
●​ Arduino Forums & Community: https://forum.arduino.cc/
●​ Online Tutorials (YouTube, Instructables, GitHub Projects)
10

Second Notes Of
Programming with Arduino
Uno
Programming with Arduino Uno

Arduino Uno is one of the most popular microcontroller boards used for embedded
systems and IoT projects. It is based on the ATmega328P microcontroller and is
programmed using the Arduino IDE with a simplified C/C++ language.

1. Introduction to Arduino Uno


●​ Microcontroller: ATmega328P
●​ Operating Voltage: 5V
●​ Input Voltage (recommended): 7-12V
●​ Digital I/O Pins: 14 (6 can be used as PWM outputs)
●​ Analog Input Pins: 6
●​ Clock Speed: 16 MHz
●​ Flash Memory: 32 KB
●​ SRAM: 2 KB
●​ EEPROM: 1 KB

2. Setting Up Arduino Uno


11

2.1 Required Components

●​ Arduino Uno board


●​ USB Cable (Type A to B)
●​ Computer with Arduino IDE
●​ Jumper wires, LEDs, sensors, etc. (optional for projects)
2.2 Installing Arduino IDE

1.​ Download the Arduino IDE from the official website.


2.​ Install the software and drivers.
3.​ Connect the Arduino Uno to your computer via USB.
4.​ Select the correct board:
○​ Go to Tools → Board → Arduino Uno
○​ Select the correct Port
5.​ Open a new sketch and start coding.

3. Programming in Arduino IDE


3.1 Structure of an Arduino Program

An Arduino program (called a sketch) has two main functions:

cpp

Copy

void setup() {

// Runs once when the board is powered on

}
12

void loop() {

// Repeats continuously after setup()

3.2 Basic Commands

●​ pinMode(pin, mode) → Sets a pin as INPUT or OUTPUT


●​ digitalWrite(pin, HIGH/LOW) → Turns a pin ON or OFF
●​ digitalRead(pin) → Reads the state of a digital pin
●​ analogWrite(pin, value) → Outputs a PWM signal (0-255)
●​ analogRead(pin) → Reads an analog input (0-1023)
●​ delay(ms) → Pauses the program for a specified time
3.3 Example: LED Blinking Program

cpp

Copy

void setup() {

pinMode(13, OUTPUT); // Set pin 13 as


output

void loop() {

digitalWrite(13, HIGH); // Turn LED ON


13

delay(1000); // Wait 1 second

digitalWrite(13, LOW); // Turn LED OFF

delay(1000); // Wait 1 second

4. Interfacing Components with Arduino


4.1 Controlling an LED

●​ Connect the anode (longer leg) of the LED to pin 13.


●​ Connect the cathode (shorter leg) to GND via a 220Ω resistor.
●​ Upload the blinking LED code.
4.2 Reading Input from a Push Button

cpp

Copy

const int buttonPin = 2;

const int ledPin = 13;

int buttonState = 0;

void setup() {
14

pinMode(buttonPin, INPUT);

pinMode(ledPin, OUTPUT);

void loop() {

buttonState = digitalRead(buttonPin);

if (buttonState == HIGH) {

digitalWrite(ledPin, HIGH);

} else {

digitalWrite(ledPin, LOW);

4.3 Reading Analog Sensor Values

Example: Reading a temperature sensor (LM35)

cpp

Copy

const int sensorPin = A0;


15

int sensorValue = 0;

void setup() {

Serial.begin(9600);

void loop() {

sensorValue = analogRead(sensorPin);

Serial.println(sensorValue);

delay(500);

5. Communication Protocols
5.1 Serial Communication

Used to send data between the Arduino and the computer.

cpp

Copy
16

void setup() {

Serial.begin(9600);

void loop() {

Serial.println("Hello, Arduino!");

delay(1000);

5.2 I2C Communication

●​ Used for sensors like OLED displays, MPU6050 accelerometers.


●​ Uses SDA (A4) and SCL (A5) pins.
5.3 SPI Communication

●​ Used for modules like SD cards and RFID readers.


●​ Uses MOSI, MISO, SCK, and SS pins.

6. Powering the Arduino


●​ USB Power (5V from a computer)
●​ DC Adapter (7-12V)
●​ Battery (9V or 12V with a voltage regulator)
17

7. Debugging Techniques
●​ Use Serial Monitor (Serial.print()) to display values.
●​ Check connections for loose wires.
●​ Ensure correct board and port are selected in the IDE.
●​ Use LED indicators for debugging.

8. Important Notes for Exam Preparation


Key Points

1.​ Arduino Uno is based on ATmega328P microcontroller.


2.​ Arduino programs have setup() (runs once) and loop() (runs
repeatedly).
3.​ Basic commands: pinMode(), digitalWrite(),
digitalRead(), analogRead(), analogWrite(), and
delay().
4.​ Interfacing:
○​ LEDs (digital output)
○​ Push buttons (digital input)
○​ Sensors (analog input)
5.​ Communication Protocols:
○​ Serial (USB communication)
○​ I2C (SDA, SCL)
○​ SPI (MOSI, MISO, SCK, SS)
6.​ Arduino is powered via USB, adapter, or battery.
7.​ Debugging is done using Serial Monitor.
8.​ Use resistors for LEDs and sensors to prevent damage.
18

9. FAQs for Quick Revision


1.​ What is the clock speed of Arduino Uno? → 16 MHz
2.​ How many digital I/O pins are on the Uno? → 14
3.​ What is the function of pinMode()? → Sets a pin as input or output
4.​ Which function is used to read an analog pin? → analogRead(pin)
5.​ What is the range of analogWrite()? → 0 to 255 (PWM)
6.​ What is the default baud rate of Serial Communication? → 9600

You might also like