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

Arduino-LED Programming

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 65

Arduino- LED Programming

Arduino Coding Basics


 We have already discussed the popular Arduino Boards, Arduino
IDEs, and Installation process of the Arduino software. We learned
that Arduino IDE (Integrated Development Environment) allows us to
draw the sketch and upload it to the Arduino board using code. The
code is written in a simple programming language similar
to C and C++.

 The initial step to start with Arduino is the IDE downloading and
installation.
Basics of Arduino programming
Brackets
There are two types of brackets used in the Arduino coding, which are listed
below:
 Parentheses ( )
 Curly Brackets { }

Parentheses ( )
 The parentheses brackets are the group of the arguments, such as method,
function, or a code statement. These are also used to group the math
equations.
 Curly Brackets { }
The statements in the code are enclosed in the curly brackets. We always
require closed curly brackets to match the open curly bracket in the code or
sketch.
 Open curly bracket- ' { '
 Closed curly bracket - ' } '
Line Comment
There are two types of line comments, which are listed below:
 Single line comment
 Multi-line comment
// Single line comment
 The text that is written after the two forward slashes are considered
as a single line comment. The compiler ignores the code written after
the two forward slashes. The comment will not be displayed in the
output. Such text is specified for a better understanding of the code
or for the explanation of any code statement.
/ * Multi - line comment */
 The Multi-line comment is written to group the information for clear
understanding. It starts with the single forward slash and an asterisk
symbol (/ *). It also ends with the / *. It is commonly used to write the
larger text. It is a comment, which is also ignored by the compiler.
Coding Screen
 The coding screen is divided into two blocks. The setup is
considered as the preparation block, while the loop is considered as
the execution block. It is shown below:
 The set of statements in the setup and loop blocks are enclosed with
the curly brackets. We can write multiple statements depending on
the coding requirements for a particular project.
For example:
void setup ( )
{
Coding statement 1;
Coding statement 2;
.
.
.
Coding statement n;
}
void loop ( )
{
Coding statement 1;
Coding statement 2;
.
.
.
Coding statement n;
}
What is Setup? What type of code
is written in the setup block?
It contains an initial part of the code to be executed.
The pin modes, libraries, variables, etc., are
initialized in the setup section. It is executed only
once during the uploading of the program and after
reset or power up of the Arduino board.

What is Loop? What type of code is written in


the Loop block?
The loop contains statements that are executed
repeatedly. The section of code inside the curly
brackets is repeated depending on the value of
variables.
Arduino - Data Types
 Data types in C refers to an extensive system used for
declaring variables or functions of different types.

 The type of a variable determines how much space it


occupies in the storage and how the bit pattern stored is
interpreted.

 The following table provides all the data types that you
will use during Arduino programming.
void

The void keyword is used only in function declarations.


It indicates that the function is expected to return no
information to the function from which it was called.

Example

Void Loop ( ) { // rest of the code }


int
 Integers are the primary data-type for number storage.

 int stores a 16-bit (2-byte) value. This yields a range of -


32,768 to 32,767 (minimum value of -2^15 and a
maximum value of (2^15) - 1).
 The int size varies from board to board. On the Arduino
Due, for example, an int stores a 32-bit (4-byte) value.
This yields a range of -2,147,483,648 to 2,147,483,647
(minimum value of -2^31 and a maximum value of (2^31)
- 1).
Arduino - Variables & Constants
What is Variable Scope?
Variables in C programming language, which Arduino
uses, have a property called scope. A scope is a
region of the program and there are three places
where variables can be declared. They are −
Inside a function or a block, which is called local
variables.
In the definition of function parameters, which is
called formal parameters.
Outside of all functions, which is called global
variables.
Local Variables
Variables that are declared inside a function or block are
local variables. They can be used only by the statements
that are inside that function or block of code. Local
variables are not known to function outside their own.
Following is the example using local variables −
void setup () { }
void loop ()
{ int x , y ;
int z ; Local variable declaration
x = 0;
y = 0; actual initialization
z = 10; }
Global Variables

Global variables are defined outside of all the


functions, usually at the top of the program. The
global variables will hold their value throughout the
life-time of your program.

A global variable can be accessed by any function.


That is, a global variable is available for use
throughout your entire program after its declaration.
The following example uses global and local variables −

Int T , S ;

float c = 0 ; Global variable declaration

void setup () { }

void loop () {

int x , y ;

int z ; Local variable declaration

x = 0;

y = 0; actual initialization

z = 10; }
Arduino - Operators
An operator is a symbol that tells the compiler to
perform specific mathematical or logical functions. C
language is rich in built-in operators and provides the
following types of operators −

Arithmetic Operators

Comparison Operators

Boolean Operators etc.


Arithmetic Operators
Assume variable A holds 10 and variable B holds 20
then −
Comparison Operators
 Assume variable A holds 10 and variable B holds 20
then −
Boolean Operators
 Assume variable A holds 10 and variable B holds 20
then −
Arduino - Control Statements
Decision making structures require that the
programmer specify one or more conditions to be
evaluated or tested by the program.

It should be along with a statement or statements to


be executed if the condition is determined to be true,
and optionally, other statements to be executed if the
condition is determined to be false.
Following is the general form of a typical decision
making structure found in most of the programming
languages −
Control Statements are elements in Source Code
that control the flow of program execution. They are
Arduino - Loops
 A loop statement allows us to execute a statement or
group of statements multiple times and following is the
general form of a loop statement in most of the
programming languages −

C programming language provides the following
types of loops to handle looping requirements.
Arduino - Arrays
 An array is a consecutive group of memory locations that
are of the same type.

 To refer to a particular location or element in the array,


we specify the name of the array and the position number
of the particular element in the array.
The figure given on next slide shows an integer array
called C that contains 11 elements. You refer to any
one of these elements by giving the array name
followed by the particular element‟s position number
in square brackets ([]).
The position number is more formally called a
subscript or index (this number specifies the number
of elements from the beginning of the array).
The first element has subscript 0 (zero) and is
sometimes called the zeros element.
Thus, the elements of array C are C[0] (pronounced
“C sub zero”), C[1], C[2] and so on. The highest
subscript in array C is 10, which is 1 less than the
number of elements in the array (11).
Array names follow the same conventions as other
variable names.
Time in Arduino

 The time in Arduino programming is measured in a


millisecond.

 Where, 1 sec = 1000 milliseconds

 We can adjust the timing according to the milliseconds.

 For example, for a 5-second delay, the time displayed will


be 5000 milliseconds
The void setup () would include pinMode as the main function.
 pinMode ( )

The specific pin number is set as the INPUT or OUTPUT in the


pinMode () function.
 The Syntax is: pinMode (pin, mode);

Where,
pin: It is the pin number. We can select the pin number
according to the requirements.
Mode: We can set the mode as INPUT or OUTPUT according to
the corresponding pin number.
Let' understand the pinMode with an example.

Example:

We want to set the 12 pin number as the output pin.

Code:

pinMode (12, OUTPUT);


digitalWrite( )
The digitalWrite ( ) function is used to set the value of a pin as HIGH or
LOW.

Where,

HIGH: It sets the value of the voltage. For the 5V board, it will set the
value of 5V, while for 3.3V, it will set the value of 3.3V.

LOW: It sets the value = 0 (GND).

If we do not set the pinMode as OUTPUT, the LED may light dim.

The syntax is: digitalWrite( pin, value HIGH/LOW)

pin: We can specify the pin number or the declared variable.


Let's understand with an example.

Example:

digitalWrite (13, HIGH);

digitalWrite (13, LOW);

The HIGH will ON the LED and LOW will OFF the LED
connected to pin number 13.
What is the difference between
digitalRead () and digitalWrite ()?

The digitalRead () function will read the HIGH/LOW value


from the digital pin, and the digitalWrite () function is used
to set the HIGH/LOW value of the digital pin.
delay ( )
The delay () function is a blocking function to pause a program
from doing a task during the specified duration in milliseconds.
For example, - delay (2000)
Where, 1 sec = 1000millisecond
Hence, it will provide a delay of 2 seconds.
Code:
digitalWrite (13, HIGH);
delay (2000);
digitalWrite (13, LOW);
delay (1000);
The LED connected to pin number 13 will be ON for
2 seconds and OFF for 1 second. The task will
repeatedly execute as it is in the void loop ().

We can set the duration according to our choice or


project requirements.
What is LED?
 In its simplest terms, a light emitting diode (LED) is a semiconductor
device that emits light when an electric current is passed through it.

 Light is produced when the particles that carry the current (known as
electrons and holes) combine together within the semiconductor
material.
What is Resistor?
 The resistor is a passive electrical component to create resistance in
the flow of electric current. In almost all electrical networks and
electronic circuits they can be found.
 The resistance is measured in ohms. An ohm is the resistance that
occurs when a current of one ampere passes through a resistor with
a one volt drop across its terminals.
 The current is proportional to the voltage across the terminal ends.
 This ratio is represented by Ohm‟s law: R=V/I
 Fixed Resistor Symbol
 Resistor has no polarity. Its value is fixed.
How to measure Resistance
value through color code table
Arduino - Blinking LED
LEDs are small, powerful lights that are used in many
different applications. It is as simple as turning a light
on and off.
Components Required
You will need the following components −
1 × Breadboard
1 × Arduino Uno R3
1 × LED
1 × 220Ω Resistor
2 × Jumper
Circuit
Note − To find out the polarity of an LED, look at it closely. The shorter of
the two legs, towards the flat edge of the bulb indicates the negative
terminal.

Components like resistors need to have their terminals bent into 90°
angles in order to fit the breadboard sockets properly. You can also cut
the terminals shorter.
Sketch
 Open the Arduino IDE software on your computer. Coding in the
Arduino language will control your circuit. Open the new sketch File
by clicking New.
CODING
void setup()
{
pinMode(6, OUTPUT);
}
void loop() {
digitalWrite(6, HIGH);
delay(1000);
digitalWrite(6, LOW);
delay(1000);
}
Code to Note
pinMode(6, OUTPUT) − Before you can use one of
Arduino‟s pins, you need to tell Arduino Uno R3
whether it is an INPUT or OUTPUT. We use a built-in
“function” called pinMode() to do this.
digitalWrite(6, HIGH) − When you are using a pin as
an OUTPUT, you can command it to be HIGH
(output 5 volts), or LOW (output 0 volts).
Result
You should see your LED turn on and off. If the
required output is not seen, make sure you have
assembled the circuit correctly, and verified and
uploaded the code to your board.
Controlling two LED’s using
digitalWrite()

Components Required
You will need the following components −
1 × Breadboard
1 × Arduino Uno R3
2 × LED
2 × 220Ω Resistor
4 × Jumper
Circuit
CODING
void setup()
{
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
}
void loop() {
digitalWrite(10, HIGH);
Serial.println(“LED1 ON”);
delay(1000);
digitalWrite(10, LOW);
Serial.println(“LED1 OFF”);
delay(1000);
digitalWrite(11, HIGH);
Serial.println(“LED2 ON”);
delay(1000);
digitalWrite(11, LOW);
Serial.println(“LED2 OFF”);
delay(1000);
}
Controlling LED using User input
 In this experiment, we are using a character to ON and
OFF the LED. „H‟ or „h‟ represents the active high state
and the „L‟ or „l‟ represents the active low state.
Components Required
You will need the following components −
1 × Breadboard
1 × Arduino Uno R3
1 × LED
1 × 220Ω Resistor
2 × Jumper
Circuit
CODING
int led=6;
void setup()
{
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop()
{
if (Serial.available() > 0)
{
char state = Serial.read();
Coding(Contd.)
if (state == „H' || state == „h')
{
digitalWrite(led, HIGH);
Serial.println("LED ON");
}
if (state == „L' || state == „l')
{
digitalWrite(led, LOW);
Serial.println("LED OFF");
}
}
delay(50);
}
Blinking 2 LED’s by user input

Components Required
You will need the following components −
1 × Breadboard
1 × Arduino Uno R3
2 × LED
2 × 220Ω Resistor
4 × Jumper
Circuit
CODING
int led1 = 10;
int led2 = 11;
char color;
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
Serial.begin(9600);
}
void loop() {
Serial.println("Which led do you want to blink up, 1 or 2?");
while (Serial.available() == 0)
{}
color = Serial.read();
if (color == „1‟) {
digitalWrite(led1, HIGH);
delay(1000);
digitalWrite(led1, LOW);
delay(1000);
}
if (color == „2‟)
{
digitalWrite(led2, HIGH);
delay(1000);
digitalWrite(led2, LOW);
delay(1000);
}
}
Driving Multiple LEDs

Parts Needed
You will need the following parts:
1x Breadboard

1x Arduino Uno

8x LEDs

8x 330Ω Resistors

9x Jumper Wires


Circuit
Polarized Components

 Pay special attention to the component‟s markings indicating how to


place it on the breadboard. Polarized components can only be
connected to a circuit in one direction.
CODING
int led1 = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;
int led5 = 6;
int led6 = 7;
int led7 = 8;
int led8 = 9;
void setup() {
// initialize the digital pin as an output.
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(led7, OUTPUT);
pinMode(led8, OUTPUT);
}
void loop() {
digitalWrite(led1, HIGH);
delay(100);
digitalWrite(led1, LOW);
delay(100);

digitalWrite(led2, HIGH);
delay(100);
digitalWrite(led2, LOW);
delay(100);
digitalWrite(led3, HIGH);
delay(100);
digitalWrite(led3, LOW);
delay(100);
digitalWrite(led4, HIGH);
delay(100);
digitalWrite(led4, LOW);
delay(100);
digitalWrite(led5, HIGH);
delay(100);
digitalWrite(led5, LOW);
delay(100);
digitalWrite(led6, HIGH);
delay(100);
digitalWrite(led6, LOW);
delay(100);
digitalWrite(led7, HIGH);
delay(100);
digitalWrite(led7, LOW);
delay(100);
digitalWrite(led8, HIGH);
delay(100);
digitalWrite(led8, LOW);
delay(100);
}
Controlling Many LEDs Using
Array
CODING
int ledPins[] = {2, 3, 4, 5, 6, 7,8,9},i;
void setup()
{
for (i = 0; i< =7; i++) {
pinMode(ledPins[i], OUTPUT);
}
}
void loop() {
for (i= 0; i <=7; i++)
{
digitalWrite(ledPins[i], HIGH);
delay(500);
}
for (i = 0; i < =7; i++)
{
digitalWrite(ledPins[i], LOW);
delay(500);
}
}

You might also like