100% found this document useful (1 vote)
59 views46 pages

Lecture22 - Arduino - CSE 1

The document discusses Arduino boards and programming. It introduces Arduino boards, the IDE, and basic programming concepts like inputs, outputs, and blinking an LED. It also covers connecting sensors and actuators to Arduinos and programming them using the Arduino IDE.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
100% found this document useful (1 vote)
59 views46 pages

Lecture22 - Arduino - CSE 1

The document discusses Arduino boards and programming. It introduces Arduino boards, the IDE, and basic programming concepts like inputs, outputs, and blinking an LED. It also covers connecting sensors and actuators to Arduinos and programming them using the Arduino IDE.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 46

Introduction and Programming

Arduino
Agenda
• Introduction to Arduino Boards
• Getting started with Arduino IDE
• Arduino Programming and Proteus designs
• Interfacing Output (LED) & Input (Key) devices
• Working with IR Sensor, LDR and its Interfacing
• Serial Communication feature of Arduino
• Working with DHT11/22 and its interfacing
• Ultrasonic Sensor Interfacing
• Microprocessor vs Microcontroller: Key Difference
• Microprocessor consists of only a Central Processing Unit, whereas Micro
Controller contains a CPU, Memory, I/O all integrated into one chip.
• Microprocessor is used in Personal Computers whereas Micro Controller is
used in an embedded system.
• Microprocessor uses an external bus to interface to RAM, ROM, and other
peripherals, on the other hand, Microcontroller uses an internal
controlling bus.
• Microprocessors are based on Von Neumann model Micro controllers are
based on Harvard architecture
• Microprocessor is complicated and expensive, with a large number of
instructions to process but Microcontroller is inexpensive and
straightforward with fewer instructions to process.
Microcontroller vs. Arduino
•The difference between an Arduino development board and a
microcontroller chip is that the development board contains almost
every essential part needed to start a project on the board itself.
•It has a microcontroller chip and ports for connection or extension.
•On the other hand, for a single microcontroller chip (such as
Atmega328 microcontroller), you need a lot of things. First, you need to
buy a separate programmer board to program the chip. For building a
project or experimentation with the chip, you need a breadboard for
circuit wiring/connections with other components, a DC power supply,
jumper wires, etc. You also need to search for program examples from
various sources, books, and the Internet.
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.

• It is an open source hardware, any one can get the details


of its design & modify it or make his own one himself.
What can it do?
• Sensors ( to sense stuff )
– Push buttons, touch pads, tilt switches.
– Variable resistors (eg. volume knob / sliders)
– Photoresistors (sensing light levels)
– Thermistors (temperature)
– Ultrasound (proximity range finder)
• Actuators ( to do stuff )
– Lights, LED’s
– Motors
– Speakers
– Displays (LCD)
Why Arduino?
• It is Open Source, both in terms of Hardware and Software.
• It is cheap(1300र), the hardware can be built from components or a
prefab board can be purchased for approx. 900र.
• It can communicate with a computer via serial connection over USB.
• It can be powered from USB or standalone DC power.
• It can run standalone from a computer (chip is programmable) and it
has memory (a small amount).
• It can work with both Digital and Analog electronic signals. Sensors
and Actuators.
• You can make cool stuff! Some people are even making simple
robots.
Different types of Arduino boards:

UNO Mega LilyPad

Arduino BT Arduino Nano Arduino Mini


Different types of Arduino boards:
Today Arduino is used for the control of
traffic lights, it can also be used for the real
UNO
time control system with programmable
timings, pedestrian lighting etc
Arduino Mega board are Used in IOT
Applications, 3D Printers, temperature
sensing, monitoring of real-time applications,
Mega

•Robotics. ...
•Wearable Technology. ...
•Educational Teaching. ...
•IoT. ...
•Data Logging. ...
•3D Mapping & Printing. ...
•Automated Gardening
Different types of Arduino boards:

To send the signals It can be used for


and to receive almost every task,
signals in between from minor to
Smartphone and massive industrial-
Arduino hardware scale projects. It
can also be used
for prototyping and
developing new
applications
Arduino Uno Board
Digital output
~: PWM.
0,1: Serial port.
Connectors/Cables to work with Uno:
Arduino Uno Board Description
Arduino Uno Board Description (Cont..)
Arduino Uno Board Description (Cont..)
Arduino Uno Board Description (Cont..)
Arduino Uno Board Description (Cont..)
Arduino Uno Board Description (Cont..)
NOTE: Do NOT use a power supply greater than 20 Volts as you will overpower
(and thereby destroy) your Arduino. The recommended voltage for most
Arduino models is between 6 and 12 Volts
INPUT v/s OUTPUT
Referenced from the perspective of the microcontroller (electrical board).

Inputs is a signal / information Output is any signal exiting the


going into the Arduino board. Arduino board.

Examples: Buttons Switches, Examples: LEDs, DC motor,


Light Sensors, Flex Sensors, servo motor, a piezo buzzer,
Humidity Sensors, Temperature relay, an RGB LED
Sensors…

https://getintopc.com/softwares/3d-cad/proteus-professional-2020-free-download/
Getting Started (Installing Arduino IDE and Setting up Arduino Board)

Check out: http://arduino.cc/en/Reference/HomePage ,


http://arduino.cc/en/Guide/HomePage .
1. Download & install the Arduino environment (IDE)
https://www.arduino.cc/en/Main/Software
2. Connect the board to your computer via the USB cable
3. If needed, install the drivers
4. Launch the Arduino IDE
5. Select your board
6. Select your serial port
7. Open the blink example
8. Upload the program
Basic Procedure

•Design the circuit:


– What are electrical requirements of the sensors or actuators?
– Identify inputs (analog inputs)
– Identify digital outputs

•Write the code:


– Build incrementally
• Get the simplest piece to work first
• Add complexity and test at each stage
• Save and Backup frequently
– Use variables, not constants
– Comment liberally
Writing and Uploading Code into Arduino
Running Code
• Running Code While Tethered

• Running Code Stand-Alone


Arduino IDE

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..)

Settings: Tools → Serial Port

Your computer
communicates to the
Arduino microcontroller via a
serial port → through a USB-
Serial adapter.

Check to make sure that the


drivers are properly installed.
Arduino IDE (Cont..)

Settings: Tools → Board


Next, double-check that the proper board is selected under the
Tools→Board menu.
Breadboard Layout:
Arduino Digital I/0

pinMode(pin, mode);
Sets pin to either INPUT or OUTPUT
Eg1. pinMode(13, OUTPUT);

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 Bunary-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 Bunary-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*
}
Uploading and Running the blink sketch
In Arduino, open up:
File → Examples → 01.Basics → Blink

const int ledPin = 13; // LED connected to


digital pin 13
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
digitalWrite(ledPin, HIGH); // set the LED
on
delay(2000); // wait for two seconds
digitalWrite(ledPin, LOW); // set the LED
off
delay(2000); // wait for two seconds
}
Uploading and Running the blink sketch (Cont..)

• Write your sketch/program


• Press compile button
(to check for errors)
• Press upload button to program
Arduino board with your sketch
• Try it out with the “Blink” sketch!

Arduino programs can be divided in three main


parts: Values(Variables & Constants), Structure,
and functions.
Proteus design
Arduino Data Types
• Except in situations where maximum performance or memory efficiency
is required, variables declared using int will be suitable for numeric
values if the values do not exceed the range (shown in the first row in
below Table) and if you don’t need to work with fractional values.
• Most of the official Arduino example code declares numeric variables as
int.
• But sometimes you do need to choose a type that specifically suits your
application.
Arduino Data Types (Cont..)
• Sometimes you need negative numbers and sometimes you don’t,
so numeric types come in two varieties: signed and unsigned.
• unsigned values are always positive. Variables without the
keyword unsigned in front are signed so that they can represent
negative and positive values.
• One reason to use unsigned values is when the range of signed
values will not fit the range of the variable (an unsigned variable
has twice the capacity of a signed variable).
• Another reason programmers choose to use unsigned types is to
clearly indicate to people reading the code that the value
expected will never be a negative number.
Arduino Data Types (Cont..)
Arduino Data Types (Cont..)
• Boolean types have two possible values: true or false. They are
commonly used for things like checking the state of a switch (if
it’s pressed or not).
• You can also use HIGH and LOW as equivalents to true and
false where this makes more sense;
– digitalWrite(pin, HIGH) is a more expressive way to turn on
an LED than digitalWrite(pin, true) or digitalWrite(pin,1),
although all of these are treated identically when the sketch
actually runs.
Data Types and Operators
Integer: used with integer variables with value between 2147483647
and -2147483647.
Eg: int x=1200;
Character: used with single character, represent value from - 127 to 128.

Eg. char c=‘r’;


Long: Long variables are extended size variables for number storage,
and store 32 bits (4 bytes), from -2,147,483,648 to 2,147,483,647.
Eg. long u=199203;
Floating-point numbers can be as large as 3.4028235E+38 and as low
as -3.4028235E+38. They are stored as 32 bits (4 bytes) of information.
Eg. float num=1.291; [The same as double type]
Statements and operators:
Statement represents a command, it ends with ; Eg: int
x; x=13; Boolean Operators:
Operators are symbols that used to == (is indicate
equal?)a specific
function: != (is not equal?)
> (greater than)
Math operators: [+,-,*,/,%,^]
>= (greater than or equal)
Logic operators: [==, !=, &&, ||] < (less than)
Comparison/Boolean operators: [==, >, <, !=, <=, >=] <= (less than or equal)
Syntax: Compound Operators:
++ (increment)
; Semicolon, {} curly braces, -- (decrement)
// single line comment, += (compound
/*Multi-line addition)
comments*/
-= (compound subtraction)
*= (compound multiplication)
/= (compound division)
Control statements:
If Conditioning: Switch case:
if(condition) switch (var)
{ {
statements-1; case 1:
… //do something when var equals 1 break;
Statement-N; case 2:
} //do something when var equals 2 break;
else if(condition2) default:
{ // if nothing else matches, do the default
Statements; // default is optional
} }
else
{
statements;
}
Loop statements:
Do… while:
do
{
Statements;
}
while(condition); // the statements are run at least
While: once.
While(condition)
{ statements; }
for
for (int i=0; i <= val; i++)
{
statements;
}
Use break statement to stop the loop whenever needed.
Digital Input
• Connect digital input to your Arduino using Pins # 0 – 13
(Although pins # 0 & 1 are also used for programming)

• Digital Input needs a pinMode command: pinMode


(pinNumber, INPUT); Make sure to use ALL
CAPS for INPUT

• To get a digital reading:


int buttonState = digitalRead (pinNumber);

• Digital Input values are only HIGH (On) or LOW (Off)


Digital Input (Cont..)
// Pushbutton sketch: a switch is connected to pin 2 lights the LED on pin 13
// (or) Control an LED connected to pin-13 w.r.to. a switch connected to pin-2,
with the help of external resister

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

 The digitalRead function monitors the voltage on the input pin (inputPin),
and it returns a value of HIGH if the voltage is high (5 volts) and LOW if
the voltage is low (0 volts).

 Actually, any voltage that is greater than 2.5 volts (half of the voltage
powering the chip) is considered HIGH and less than this is treated as LOW.
 If the pin is left unconnected (known as floating) the value returned from
digitalRead is indeterminate (it may be HIGH or LOW, and it cannot be
reliably used).
 The resistor shown in Figure shown in previous slide ensures that the
voltage on the pin will be low when the switch is not pressed, because the
resistor “pulls down” the voltage to ground.
 When the switch is pushed, a connection is made between the pin and +5
volts, so the value on the pin interpreted by digital Read changes from LOW
to HIGH.

You might also like