Arduino _ Architecture, Programming and Application
Arduino _ Architecture, Programming and Application
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
○ mode: INPUT, OUTPUT, or INPUT_PULLUP. (see the digital pins page for a more complete
description of the functionality.)
● 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)
● Syntax: Serial.begin(speed)
● Parameters:
● Syntax: Serial.println(val)
● Parameters
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
● Returns
void setup() {
Serial.println("Sending Data...");
void loop() {
}
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
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
● 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
○ 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
● 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
○ 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
● 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());
}
}
}