Arduino1 (2)
Arduino1 (2)
• Introduction
• Microprocessors and microcontrollers are both
types of integrated circuits that perform
various processing tasks, but they have some
fundamental differences in terms of their
architecture, functionality, and typical
applications. Here are the basic differences
between microprocessors and
microcontrollers:
• Microprocessor: A microprocessor is a central
processing unit (CPU) of a computer system
that executes instructions, performs arithmetic
and logic operations, and manages data
processing. It requires external components
like memory, input/output (I/O) devices,
timers, and peripherals to form a complete
functional system.
• Microcontroller: A microcontroller is a compact
single-chip computer system that contains not
only a CPU but also additional components such
as memory (RAM and ROM/Flash), I/O ports,
timers, and sometimes peripherals like ADC
(Analog-to-Digital Converter), UART (Universal
Asynchronous Receiver-Transmitter), etc. These
components are integrated onto the same chip,
making it a self-contained and stand-alone
system.
• Functionality:Microprocessor: It is designed
primarily for general-purpose computing tasks.
It executes instructions fetched from memory,
and its operation is determined by the software
it runs.
• Microcontroller: It is designed for specific tasks
and embedded systems. It is optimized to
control devices or perform dedicated functions,
and its operation is often determined by the
firmware (software embedded in ROM/Flash
memory).
• Typical Applications:Microprocessor:
Microprocessors are commonly used in personal
computers, servers, laptops, smartphones, and
other computing devices where general-purpose
processing is required.
• Microcontrollers are widely used in embedded
systems, IoT (Internet of Things) devices, robotics,
home automation, automotive control systems,
industrial automation, medical devices, consumer
electronics, etc. These applications require
specialized control and data processing.
• System Complexity:Microprocessor: Due to its
general-purpose nature, the microprocessor-
based systems often require external
components to be connected for memory, I/O,
and other peripherals. This can make the
overall system more complex and may require
additional PCB (Printed Circuit Board) space.
• Microcontroller: The integration of CPU,
memory, and peripherals on a single chip
simplifies the overall system design and
reduces the need for external components.
This makes microcontroller-based systems
more compact and cost-effective.
Microprocessor: Microprocessors can be power-hungry, especially in high-
performance computing applications, which may not be suitable for battery-powered
or low-power devices.
Famous microcontroller
manufacturers are MicroChip,
Atmel, Intel, Analog devices,
and more.
What is Arduino?
• A microcontroller board, contains on-board power supply,
USB port to communicate with PC, and an Atmel
microcontroller chip.
• It simplify the process of creating any control system by
providing the standard board that can be programmed
and connected to the system without the need to any
sophisticated PCB design and implementation.
https://getintopc.com/softwares/3d-cad/proteus-professional-2020-free-download/
Getting Started (Installing Arduino IDE and Setting up Arduino Board)
IDE =
Integrated Development
Environment
http://www.arduino.cc/en/Guide/Environ
ment
Arduino IDE (Cont..)
Two required functions /
methods / routines:
void setup()
{
// runs once
}
void loop()
{
// repeats
}
Arduino IDE (Cont..)
Your computer
communicates to the
Arduino microcontroller via a
serial port → through a USB-
Serial adapter.
digitalRead(pin);
Reads HIGH or LOW from a pin
Eg3. digitalRead(2);
digitalWrite(pin, value);
Writes HIGH or LOW to a pin
Eg2. digitalWrite(13, HIGH);
Our first Arduino Sketch/Program
/*
* Arduinos ketch to toggle the LED connected to pin-13 with a rate/delay of 1sec
*/
void setup()
{
// put your setup code here, to run once: -->I*
pinMode(13, OUTPUT); //pin-13 configures as o/p -->II
}
void loop()
{
// put your main code here, to run repeatedly: -->1*
digitalWrite(13, HIGH); //HIGH Value or Binary-1 send to pin-13 -->2
//delay(x); //x-ms second(s) delay -->3*
//delayMicroseconds(y); //y-us second(s) delay -->4*
delay(1000); //1000-milliseconds=1second delay -->5
digitalWrite(13, LOW); //LOW Value or Binary-1 send to pin-13 -->6
delay(1000); //1000-milliseconds=1second delay -->7
//Toggling rate of led connected to pin-13 is of 1second -->8*
}l
Uploading and Running the blink sketch
In Arduino, open up:
File → Examples → 01.Basics → Blink
Arduino dedicates:
Digital I/O pin # 0 to receiving(Rx) and
Digital I/O pin # 1 to transmit(Tx).
Using Serial Communication (Cont..)
• Serial communications provide an easy and flexible way for your Arduino
board to interact with your computer and other devices. This chapter
explains how to send and receive information using this capability.
• You can also send data from the Serial Monitor to Arduino by entering text
in the text box to the left of the Send button.
• Baud rate is selected using the drop-down box on the bottom right. You can
use the drop down labeled “No line ending” to automatically send a carriage
return or a combination of a carriage return and a line at the end of each
message sent when clicking the Send button.
• Your Arduino sketch can use the serial port to indirectly access (usually via
a proxy program written in a language like Processing) all the resources
(memory, screen, keyboard, mouse, network connectivity, etc.) that your
computer has. Your computer can also use the serial link to interact with
sensors or other devices connected to Arduino.
Serial Monitor & analogRead()
Opens up a
Serial Terminal
Window
Digital Input
• Connect digital input to your Arduino using Pins # 0 – 13
(Although pins # 0 & 1 are also used for programming)
const int ledPin = 13; // choose the pin for the LED
const int inputPin = 2; // choose the input pin (for a pushbutton)
void setup()
{
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare pushbutton as input
}
void loop()
{
int val = digitalRead(inputPin); // read input value
if (val == HIGH) // check if the input is HIGH
{
digitalWrite(ledPin, HIGH); // turn LED on if switch is pressed
}
else
{
digitalWrite(ledPin, LOW); // turn LED off
}
Proteus design (with external resister)
Using a key/push button with external resistor
Decimal Digit G F E D C B A
0 0 1 1 1 1 1 1
1 0 0 0 0 1 1 0
2 1 0 1 1 0 1 1 A
F G B
3 1 0 0 1 1 1 1
4 1 1 0 0 1 1 0 E D C
5 1 1 0 1 1 0 1
6 1 1 1 1 1 0 1
7 0 0 0 0 1 1 1
8 1 1 1 1 1 1 1
9 1 1 0 1 1 1 1
Driving a 7-Segment Display
• Although a 7-segment display can be thought of as a single
display, it is still seven individual LEDs within a single package
and as such these LEDs need protection from over-current.
• LEDs produce light only when it is forward biased with the
amount of light emitted being proportional to the forward
current.
• This means that an LEDs light intensity increases in an
approximately linear manner with an increasing current.
• So this forward current must be controlled and limited to a safe
value by an external resistor to prevent damaging the LED
segments.
Driving a 7-Segment Display (Cont..)
• The forward voltage drop across a red LED segment is very low at
about 2-to-2.2 volts.
• To illuminate correctly, the LED segments should be connected to a
voltage source in excess of this forward voltage value with a series
resistance used to limit the forward current to a desirable value.
• Typically for a standard red colored 7-segment display, each LED
segment can draw about 15mA to illuminated correctly, so on a 5
volt digital logic circuit,
– the value of the current limiting resistor would be about 200Ω
(5v – 2v)/15mA, or
– 220Ω to the nearest higher preferred value.
Driving a 7-Segment Display (Cont..)
• In this example,
– the segments of a common anode display are illuminated using
the switches.
– If switch a is closed, current will flow through the “a” segment
of the LED to the current limiting resistor connected to pin a
and to 0 volts, making the circuit.
– Then only segment a will be illuminated.
– If we want the decimal number “4″ to
illuminate on the display, then switches
b, c, f and g would be closed to light the
corresponding LED segments.
Interfacing 7-Segment Display to Arduino
//int CommonAnode[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,
0x48,0x03,0x46,0x21,0x06,0x0E};
int CommonCathode[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,
0x77,0x7C,0x39,0x5E,0x79,0x71};
void setup()
{
DDRD=B11111111; //pins 7 to 0, set port D as output i.e. pin-0 to A-led
//DDRD=0xFF; //pins 7 to 0 (port D), configured/set as output port
}
void loop()
{
for(int j=0;j<10;j++)
{
//PORTD= CommonAnode[j];
PORTD= CommonCathode[j]; // PORTD= CommonCathode[0]; => 0x3F -->’0’
delay(1000);
}
}
Proteus Design (CommonCathode)
•
LCD Interfacing with Arduino
Agenda
• Introduction to LCD
• LCD Interfacing (8-bit mode)
• LCD Interfacing (4-bit mode)
• LCD Library Commands
• Still message display on LCD
• Moving message display on LCD
• Custom message display on LCD
Introduction
• Liquid crystal displays (LCDs) offer a convenient and
inexpensive way to provide a user interface for a
project.
LCD pin configuration:
LCD Interfacing (4-bit mode)
LCD Interfacing (4-bit mode)
LCD Interfacing (4-bit mode)
// Arduino sketch to display: **Hello Students** in Line-0 and **Study Well** in Line-1 of LCD
#include <LiquidCrystal.h> // include the library code
//constants for the number of rows and columns in the LCD
const int numRows = 2; //2 is a decimal number
const int numCols = 16; //16 is a decimal number
void setup()
{
lcd.begin(numCols, numRows); // lcd.begin(16, 2); //set up the LCD's number of columns and rows
}
void loop()
{
lcd.setCursor(0, 0); // set the cursor to column 0, line 0
lcd.print(" **Hello Students** "); //(note: line 0 is the 1st row, since counting begins with 0)
lcd.setCursor(0, 1); // set the cursor to column 0, line 1
lcd.print(" **Study Well** "); //(note: line 1 is the 2nd row, since counting begins with 0)
}
LCD Library Commands
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)
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
LCD Library Commands (Cont..)
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)
LCD Library Commands (Cont..)
write()
Description
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
LCD Library Commands (Cont..)
print()
Description
Prints text to the LCD.
Syntax
lcd.print(data)
Parameters
lcd: a variable of type LiquidCrystal
LCD Library Commands (Cont..)
cursor() noCursor()
Description: Description
Display the LCD cursor Hides the LCD cursor.
an underscore (line) at the
position to which the next
character will be written.
Syntax
Syntax lcd.noCursor()
lcd.cursor()
LCD Library Commands (Cont..)
noDisplay() display()
Description Description
Turns off the LCD display, without Turns on the LCD display, after it's been
losing the text currently shown on it. turned off with noDisplay(). This will
restore the text (and cursor) that was on
the display.
Syntax
Syntax
lcd.display()
lcd.noDisplay()
LCD Library Commands (Cont..)
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)
LCD Library Commands (Cont..)
scrollDisplayLeft() scrollDisplayRight()
Description Description
Scrolls the contents of Scrolls the contents of
the display (text and the display (text and
cursor) one space to the cursor) one space to the
left. right.
Syntax Syntax
lcd.scrollDisplayLeft() lcd.scrollDisplayRight()
LCD Library Commands (Cont..)
leftToRight() rightToLeft()
Description Description
Set the direction for text written to the
Set the direction for text written to the
LCD to right-to-left (the default is left-
LCD to left-to-right, the default. This to-right). This means that subsequent
means that subsequent characters characters written to the display will
written to the display will go from left go from right to left, but does not
to right, but does not affect affect previously-output text.
previously-output text.
Syntax
Syntax lcd.rightToLeft()
lcd.leftToRight()
LCD Library Commands (Cont..)
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
Eg1. Still Message Display On LCD
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
lcd.begin(16, 2);
lcd.print("hello, world!");
}
void loop()
{
lcd.setCursor(0,1);
delay(250);
}
Eg2. Moving Message Display On LCD
#include <LiquidCrystal.h>
char message[]={"Lovely Professional University"};
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void loop()
void setup() {
{ lcd.setCursor(0, 0);
lcd.begin(16, 2);
} for (int i = 0; i <= 30; i++)
{
lcd.print(message[i]);
delay(200);
if(i>16)
{
lcd.autoscroll();
}
}
}
Eg3. LCD Custom Message Display
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
// create new custom characters
lcd.createChar(0, customChar);
lcd.createChar(1, customChar1);