Arduino-LED Programming
Arduino-LED Programming
Arduino-LED Programming
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.
The following table provides all the data types that you
will use during Arduino programming.
void
Example
Int T , S ;
void setup () { }
void loop () {
int x , y ;
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
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:
Code:
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.
If we do not set the pinMode as OUTPUT, the LED may light dim.
Example:
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 ()?
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
8x LEDs
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);
}
}