Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

Arduino _ Architecture, Programming and Application

Arduino is an open-source electronics platform that includes microcontroller boards and a simplified programming environment, making it accessible for creating interactive electronic projects. It supports various components and is widely used in robotics, IoT, and DIY electronics. The document also covers programming basics, including functions for digital and analog input/output, and interfacing with sensors and displays.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Arduino _ Architecture, Programming and Application

Arduino is an open-source electronics platform that includes microcontroller boards and a simplified programming environment, making it accessible for creating interactive electronic projects. It supports various components and is widely used in robotics, IoT, and DIY electronics. The document also covers programming basics, including functions for digital and analog input/output, and interfacing with sensors and displays.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

Arduino : Architecture,

Programming and
Application
Dr. Jay M. Joshi
What is Arduino?
Arduino is an open-source electronics platform based on easy-to-use hardware and
software. It consists of:

1. Microcontroller boards
● Programmable circuit boards
● Used for creating interactive electronic projects
● Available in various models like Uno, Nano, Mega
What is Arduino?
2. Key Features

● Simplified programming environment


● Uses C/C++ programming language
● Supports numerous sensors and components
● Widely used in robotics, IoT, and DIY electronics
● Affordable and beginner-friendly
What is Arduino?
3. Main Components

● Microcontroller (typically ATmega)


● Digital/analog input/output pins
● USB connection for programming
● Power supply interface

Arduino allows hobbyists, students, and professionals to create electronic projects by


providing an accessible platform for programming and controlling hardware.
What is Special?
● The Arduino website is Arduino.cc
● The cc stands for Creative Commons.
● Anyone can use the plans and software
to create or program an
● Arduino for free (unless you sell a new
Arduino board in which case you pay a
royalty)
Arduino Microcontroller
A microcontroller is a programmable chip that can
be programmed to do specific logical operations
such as reading and writing to sensors and other
parts.
It can also be programmed to do
logical operations such as IF
statements, WHILE statements, and
functions.
Arudino Board
Arudino Uno
Arduino
Programming
Arudino Software
pinMode()
● Configures the specified digital pin to behave either as an input or an output.
● Syntax: pinMode(pin, mode)
● Parameters
○ pin: the number of the pin whose mode you wish to set

○ mode: INPUT, OUTPUT, or INPUT_PULLUP. (see the digital pins page for a more complete
description of the functionality.)

● pinMode(13, OUTPUT); // initialize digital pin 13 as an output


digitalWrite()
● Write a HIGH or a LOW value to a digital pin.
● Syntax: digitalWrite(pin, value)
● Parameters:
○ pin: the pin number
○ value: HIGH or LOW
● digitalWrite(13, HIGH); // HIGH is the voltage level
● digitalWrite(13, LOW); // voltage LOW
delay()
● Pauses the program for the amount of time (in miliseconds) specified as
parameter. (There are 1000 milliseconds in a second.)

● Syntax: delay(ms)
● Parameters
● ms: the number of milliseconds to pause (unsigned long)
● delay(1000);\\ 1sec delay
Write an Arduino program to turn on an inbuilt LED on for one second, then off for one second,
repeatedly.
int led = 13;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
analogWrite()
● Writes an analog value (PWM wave) to a digital pin.
● After a call to analogWrite(), the pin will generate a steady square wave of the specified duty
cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite() on the same
pin).
● The frequency of the PWM signal on most pins is approximately 490 Hz. On the Uno and
similar boards, pins 5 and 6 have a frequency of approximately 980 Hz.
● Syntax: analogWrite(pin, value)
● Parameters:
○ pin: the pin to write to.

○ value: the duty cycle: between 0 (always off) and 255 (always on).
for statements
for (initialization; condition; increment) {
//statement(s);
}
for (int value = 0 ; value <= 255; value += 5){
//statement(s);
}
for (int value = 255 ; value >= 0; value -= 5){
//statement(s);
}
Write an Arduino program to fade an LED on pin 9 as shown in schematic
layout
Write an Arduino program to fade an LED on pin 9 as shown in schematic
layout
int led = 9; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is void loop() {
int fadeAmount = 5; // how many points to fade the LED // fade in from min to max in increments of 5 points:
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5)
// the setup routine runs once when you press reset: {
void setup() { // sets the value (range from 0 to 255):
// declare pin 9 to be an output: analogWrite(ledPin, fadeValue);
pinMode(led, OUTPUT); // wait for 30 milliseconds to see the dimming effect
} delay(30);
// the loop routine runs over and over again forever: }
// fade out from max to min in increments of 5 points:
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5)
{
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
Digital read and
serial output
digitalRead()
● Description: Reads the value from a specified digital pin, either HIGH or LOW.

● Syntax: digitalRead(pin)

● Parameters

○ pin: the number of the digital pin you want to read (int)

○ Returns: HIGH or LOW


Serial.begin()
● Description: Sets the data rate in bits per second (baud) for serial data transmission. For
communicating with the computer, use one of these rates: 300, 600, 1200, 2400, 4800, 9600,
14400, 19200, 28800, 38400, 57600, or 115200.

● Syntax: Serial.begin(speed)

● Parameters:

○ speed: in bits per second (baud) – long


Serial.println()
● Description: Prints data to the serial port as human-readable ASCII text followed by a
carriage return character (ASCII 13, or '\r') and a newline character (ASCII 10, or '\n').

● Syntax: Serial.println(val)

● Parameters

○ val: the value to print - any data type


if / else statement
● Description: if/else allows greater control over the flow of code than
the basic if statement, by allowing multiple tests to be grouped
together.
if (pinFiveInput < 500)
{
// action A
}
else
{
// action B
}
Write an Arduino program to reads a IR sensor input on pin 2, prints the result to the serial
monitor
int pushButton1 = 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(pushButton1, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
int buttonState1 = digitalRead(pushButton1);
// print out the state of the button:
if (buttonState1)
{
Serial.println("Object is detected");
}
else
{
Serial.println("Object is not detected");
}
delay(1); // delay in between reads for stability
}
Write an Arduino program to read a digital input on pin 2 and 4, prints the
result to the serial monitor
Write an Arduino program to read a digital input on pin 2 and 4, prints the
result to the serial monitor
int SW1=2;
int SW2=4;

void setup() {
pinMode(SW1,INPUT);
pinMode(SW2,INPUT);
Serial.begin(9600);
}
void loop() {
int val1=digitalRead(SW1);
int val2=digitalRead(SW2);
if(val1)
{
Serial.println("SW1 is pressed");
}
else if(val2)
{
Serial.println("SW2 is pressed");
}
else
{
Serial.println("Switches are not pressed");
}
delay(1);
}
Sensors and LCD
Interface
analogRead()
● Description: Reads the value from the specified analog pin. The Arduino board contains a 6
channel, 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. This yields a resolution between
readings of: 5 volts / 1024 units or, .0049 volts (4.9 mV) per unit.

● It takes about 100 microseconds (0.0001 s) to read an analog input, so the maximum reading
rate is about 10,000 times a second.

● Syntax: analogRead(pin)

● Parameters

○ pin: the number of the analog input pin to read from (0 to 5 on most boards, 0 to 7 on the Mini and Nano, 0 to 15
on the Mega)

● Returns

○ int (0 to 1023)

map():
Syntax: map(value, fromLow, fromHigh, toLow, toHigh)

● Description: Re-maps a number from one range to another. That is, a value of fromLow would get
mapped to toLow, a value of fromHigh to toHigh, values in-between to values in-between, etc.

● Parameters
○ value: the number to map

○ fromLow: the lower bound of the value's current range

○ fromHigh: the upper bound of the value's current range

○ toLow: the lower bound of the value's target range

○ toHigh: the upper bound of the value's target range

● Returns

○ The mapped value.

● map(sensorReading, 0, 1023, 0, 10);


Write an Arduino program to read an analog input on pin 0, converts it to voltage, and prints the result to the serial
monitor. Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
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);
}
Connect LDR at analog input pin 0, and check the voltage value on serial monitor.
Connect LM35 temperature sensor at analog input pin 0, and check temperature on serial monitor.
Write an Arduino program to turn on a series of LEDs based on the value of an analog sensor. This is a simple way to make a bar graph
display. Though this graph uses 8 LEDs, you can use any number by changing the LED count and the pins in the array.
Write an Arduino program to turn on a series of LEDs based on the value of an analog sensor. This is a simple way to make a bar graph display.
Though this graph uses 8 LEDs, you can use any number by changing the LED count and the pins in the array.
const int analogPin = A0; // the pin that the potentiometer is attached to
const int ledCount = 10; // the number of LEDs in the bar graph
int ledPins[] = {
2, 3, 4, 5, 6, 7, 8, 9, 10, 11
}; // an array of pin numbers to which LEDs are attached
void setup() {
// loop over the pin array and set them all to output:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
}
void loop() {
// read the potentiometer:
int sensorReading = analogRead(analogPin);
// map the result to a range from 0 to the number of LEDs:
int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
// loop over the LED array:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// if the array element's index is less than ledLevel,
// turn the pin for this element on:
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
}
// turn off all pins higher than the ledLevel:
else {
digitalWrite(ledPins[thisLed], LOW);
}
}
}
Interfacing the HC-05/HC-06 Bluetooth module with Arduino
#include <SoftwareSerial.h>

SoftwareSerial BTSerial(2, 3); // RX, TX

void setup() {

Serial.begin(9600); // Start Serial Monitor

BTSerial.begin(9600); // Start Bluetooth module communication

Serial.println("Bluetooth Module Ready");

Serial.println("Sending Data...");

delay(2000); // Wait for Bluetooth to initialize

void loop() {

String message = "Hello World - Code to demonstrate BT Communication";

BTSerial.println(message); // Send message via Bluetooth

Serial.println("Sent: " + message); // Display message on Serial Monitor

delay(2000); // Send message every 2 seconds

}
16×2 LCD Display interface with Arduino

A register select (RS) pin that controls where in the LCD’s memory you’re
writing data to.
You can select either the data register, which holds what goes on the screen,
or an instruction register, which is where the LCD’s controller looks for
instructions on what to do next.
16×2 LCD Display interface with Arduino

A Read/Write (R/W) pin that selects reading mode or writing mode

An Enable pin that enables writing to the registers

8 data pins (D0 -D7). The states of these pins (high or low) are the bits that you’re
writing to a register when you write, or the values you’re reading when you read.
16×2 LCD Display interface with Arduino

There’s also a display constrast pin (Vo), power supply


pins (+5V and Gnd) and LED Backlight (Bklt+ and BKlt-)
pins that you can use to power the LCD, control the display
contrast, and turn on and off the LED backlight, respectively.
ER
DB7–DB0
RS
8/W
Alphanumeric LCD Interfacing
● Pinout
○ 8 data pins D7:D0 Microcontroller
○ RS: Data or Command
Register Select communications bus
○ R/W: Read or Write
○ E: Enable (Latch data)

● RS – Register Select
LCD
○ RS = 0 → Command Register controller

○ RS = 1 → Data Register
LCD Module
● R/W = 0 → Write , R/W = 1 → Read
■ LCD RS pin to digital pin 12
● E – Enable
■ LCD Enable pin to digital pin 11
○ Used to latch the data present on the data pins. ■ LCD D4 pin to digital pin 5
■ LCD D5 pin to digital pin 4
● D0 – D7 ■ LCD D6 pin to digital pin 3
■ LCD D7 pin to digital pin 2
○ Bi-directional data/command pins.
○ Alphanumeric characters are sent in ASCII format.
Connecting LCD to Arduino UNO
LiquidCrystal()
● #include <LiquidCrystal.h>

● Description

○ Creates a variable of type LiquidCrystal. The display can be controlled using 4 or 8 data lines. If the
former, omit the pin numbers for d0 to d3 and leave those lines unconnected. The RW pin can be tied to
ground instead of connected to a pin on the Arduino; if so, omit it from this function's parameters.

● Syntax

○ LiquidCrystal lcd(rs, enable, d4, d5, d6, d7)

● LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


begin()
● Description

○ Initializes the interface to the LCD screen, and specifies the dimensions (width and
height) of the display. begin()needs to be called before any other LCD library commands.

● Syntax
○ lcd.begin(cols, rows)

○ lcd.begin(16, 2);

● Parameters
lcd: a variable of type LiquidCrystal
cols: the number of columns that the display has
rows: the number of rows that the display has
setCursor()
● Description

○ Position the LCD cursor; that is, set the location at which subsequent text written to the LCD
will be displayed.

● Syntax

○ lcd.setCursor(col, row)

● Parameters
○ lcd: a variable of type LiquidCrystal

○ col: the column at which to position the cursor (with 0 being the first column)

○ row: the row at which to position the cursor (with 0 being the first row)
● Description
print()
○ Prints text to the LCD.

● Syntax

○ lcd.print(data)
lcd.print(data, BASE)

● Parameters
○ lcd: a variable of type LiquidCrystal
○ data: the data to print (char, byte, int, long, or string)
○ BASE (optional): the base in which to print numbers: BIN for binary (base 2), DEC for decimal (base 10), OCT for
octal (base 8), HEX for hexadecimal (base 16).

● Returns

○ Byte: print() will return the number of bytes written, though reading that number is optional
Write a Arduino program display the string “Welcome to Microcontroller” in the screen in two lines of LCD
continuously.
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.setCursor(1, 0);
lcd.print("Welcome to");
lcd.setCursor(1,1);
lcd.print("Microcontroller");
}
● Description
write()
○ Write a character to the LCD.

● Syntax

○ lcd.write(data)

● Parameters

○ lcd: a variable of type LiquidCrystal

○ data: the character to write to the display

● Returns

○ Byte: write() will return the number of bytes written, though reading that number is optional
clear()
● Description

○ Clears the LCD screen and positions the cursor in the upper-left corner.

● Syntax

○ lcd.clear()

● Parameters

○ lcd: a variable of type LiquidCrystal


Serial Monitor in Arduino IDE
available()
● Description

○ Get the number of bytes (characters) available for reading from the serial port. This is data that's already arrived
and stored in the serial receive buffer (which holds 64 bytes). available() inherits from the Stream utility class.

● Syntax

○ Serial.available()

● Parameters

○ none

● Returns

○ the number of bytes available to read


● Description
read()
○ Reads incoming serial data. read() inherits from the Stream utility class.

● Syntax

○ Serial.read()

● Parameters

○ None

● Returns

○ the first byte of incoming serial data available (or -1 if no data is available) - int
Write a Arduino program to display a text sent over the serial port (e.g. from the Serial Monitor) on an attached LCD.

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
}
void loop()
{
if (Serial.available()) {
delay(100);
lcd.clear();
while(Serial.available()>0)
{
lcd.write(Serial.read());
}
}
}

You might also like