0% found this document useful (0 votes)
12 views

Basic_Arduino_Programming_Tips_How_To_Learn_Arduino_Programming

Basicamente Arduíno

Uploaded by

eletrozzeti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Basic_Arduino_Programming_Tips_How_To_Learn_Arduino_Programming

Basicamente Arduíno

Uploaded by

eletrozzeti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Basic

Arduino Programming
Tips
In this book the basic information of Arduino programming has
been given. What are the things that we should keep in mind
before starting Arduino programming? And some basic
programs are also given.

Author
Vijay Verma
Any Problem then Send me message on Instagram-
vmktechnicalpower

Dedicate
This book is for all those students who want
to learn how to make Arduino based projects.
And if you want to make projects based on
your ideas, then all of them can take help of
this book. With the help of this book, from 8
class students to 12 class students, it can get
the basic knowledge of Arduino projects.
This book is also for all those students. Those
who either doing engineering or want to do
engineering (Electronics Engineering,
Electronics Communication Engineering,
Computer Science Engineering, Information
Technology Engineering). This book will help
them to complete their basic for all of them.
After reading this book and using all its
formulas, you will found the increment in
your knowledge, And I can say this with
conviction.

Arduino Programming Tips


1. Before starting programming in Arduino, we should
understand all the basic concepts of any one programming
language, so that we will not have any problem in doing
Arduino programming.

2. We have to understand all these concepts well before


starting Arduino programming.

Identifiers
Constant
Variable
Keywords
Data Type
Function
Operator
Control Instruction
Loop
break
Switch Control Instruction
Functions
Array
Strings

Tip A. Make your variable names as


descriptive as possible
It's easier said than done. When coding, we usually don't think
too deeply about each variable because we want to end up with
the least number of lines of code written.
So let's say you have declared a variable for a digital pin that is
supposed to turn an LED on or off. Instead of just using vague
terms like 'button_led', try writing Button_led_on_off_Pin
instead. With this simple step, the code became legible and
memorable for memorization.
// read input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to
voltage (0 - 5V):
floatVoltage = sensorValue * (5.0 / 1023.0);
There are obvious exceptions to this rule as well. The for()
loop, initialization variables and similar things are all pretty
self explanatory.
Tip B. Improve the visual cue by
Indentation
We all have fallen victim to this error at least a couple
of times. The code that comes with nested functions is
a little harder than usual to track. As programmers, we
use indentation to clearly identify each block of
functionality from the rest.
Try to find the difference from the below two snippets
of code which provides basically the same output.
void loop() {if(hedgehog==cat){if(cat==likehedgehog){
//hedgehog and cat are friends}else if(hedgehog!=cat){
//hedgehog and cat are friends}else if(cat != hedgehog){
//hedgehog and cat arn't friends}}else{
//hedgehog and cat are not in the same room}}
Now try to compare it with this one:
void loop() {
if (hedgehog == cat) {
if (cat == likehedgehog) {
//hedgehog and cat are friends
}
else if (hedgehog != cat) {
//hedgehog and cat are friends
}
else if (cat != hedgehog)
{
//hedgehog and cat arn't friends
}
}
else {
//hedgehog and cat are not in the same room
}
}
You'll immediately recognize how readable the second piece of
code is, even though the first example had all the comments in
the world to know what each step did.
Another benefit of indenting is that errors are quickly
discovered without gliding through obscure code again. You'll
easily find missing parentheses in a properly indented piece of
writing.
So how do we do it? Basically it is hidden in plain sight.
It's in the Tools menu.
Fire up your ArduinoIDE and it's right there under Tools.

Tip C. Simple comments can ease up


future coding
Commenting in coding is one of the most underrated tasks of
all time. In the heat of work, we usually try to reach output as
quickly as possible.
But as we have seen in the above sections, the code may not be
easily readable for future use if we are not careful about it. This
is where the simple comment proves its worth.
Although they are part of the documentation, they are often not
considered valuable work in most cases. They come in handy
when we, or any client we write, tries to trace the code in a
later instance.
For example consider this piece of code:
// the setup function runs once when you press reset or power
up the board
useless arrangement()
{
// Initialize the digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// loop function runs over and over again forever
void loop()
{
digitalWrite(LED_BUILTIN, HIGH); // turn on the LED (high
is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn off the LED by
reducing the voltage
delay(1000); // wait for a second
}
See how well those comments are described? You will come to
thank them later. You can also add further information such as
the name of the person who wrote it, the date of completion,
what is the purpose of the code etc. to help the reader get up to
speed on the code.
/*
Blink
One LED turns on for one second, then turns off repeatedly for
one second.
https://www.arduino.cc/en/Main/Products
Revised 8 May 2014
by Scott Fitzgerald
Revised 2 September 2016
by Arturo Guadalupic
Revised 8 September 2016
by Colby Newman
*/
It does another thing very well - a version in writing helps to
make history. You can make as many edits as you want and
mention them as comments to let the reader know how your
code was developed.

Tip D. Organize and reuse your code


with functions()
Coding is meant to be universally readable: something that all
coders try to do when they start their job. When writing large
blocks of code that are used for specific purposes, it is always
good to keep them in separate functions.
By putting them in their own separate functions, these become
both readable and reusable at the same time. Here is a small
example to demonstrate how this can be achieved:
void arrange() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, less);
delay(1000);
}
You may have noticed that this is a simple LED blinking code.
Now add in a function and see what happens:
void arrange() {
pinMode(13, OUTPUT);
}
void loop() {
blinkLead();
}
void blinkled() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, less);
delay(1000);
}
Same code, same purpose. Now take a look at the below code
const int LED1 = 13;
const int LED2 = 5;
useless arrangement()
{
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
}
void loop() {
blinkLED(100, LED1);
blinkLED(100, LED1);
blinkLED(100, LED1);
blinkLED(300, LED2);
blinkLED(300, LED2);
}
void blinkLED(int delaytime, int led) {
digitalWrite(led, HIGH);
delay(delay time);
digitalWrite(led, low);
delay(delay time);
}
This code is reusable. Not only is it reader-friendly, it also uses
less memory on your controller board.

Tip E. Documentation is key


Documentation is often looked down upon by most
programmers because they feel it has less connection or value
than code. But this is untrue on many levels.
Deep documentation always saves money and time for backend
tasks because otherwise it would be hard to get a hold of the
work for someone who is reading your work for the first time.
One suggestion I'd like to make is to maintain a separate
documentation for your working software and hardware
section. Your projects are your brainchild and may not be
viewed by others the way you see them.
You can start with a clear description on Sketch, how to use it,
and start explaining from there. The tasks you choose, the
libraries you use, and even the quirky methods you use to get
the job done.
Make sure you use the same techniques we learned here in
your documentation - keep it simple and readable.

Tip F: Use functions() to organize and reuse


your code
Tasks can be considered an intermediate level, but it makes
sense to try to do so even as a beginner. If you have large
blocks of code that serve a specific purpose it makes sense to
put it in a separate function. This makes your code more
readable, and can also reduce the amount of code you have to
write because it is easier to reuse the same code in a different
part of your program.
As an example here is a piece of code from a previous tutorial I
did about reading data from memory. without the use of a
function before;
void loop() {
// Put your main code here, to run repeatedly:
String ssid = "";
String password = "";
/**
* reading ssid out of memory using eeprom library
* ssid starts at memory position 0 and is 30 characters long
*/

for (int n = 0; n < 30; ++n)


{
if(char(EEPROM.read(n))!=';'){//Looking for end of data
character ';'
if(isWhitespace(char(EEPROM.read(n)))){//Remove
Whytespaces e.g. one place
//Do not do anything
}else ssid += String(char(EEPROM.read(n)));

}else n=31;
}
/**
* Reading the password out of memory using the eeprom
library
* ssid memory starts at position 100 and is 30 characters
long
*/
for (int n = 100; n < 130; ++n)
{
if(char(EEPROM.read(n))!=';'){//Looking for end of data
character ';'
if(isWhitespace(char(EEPROM.read(n)))){//Remove
Whytespaces e.g. one place
//Do not do anything
}else ssid += String(char(EEPROM.read(n)));

} else n=131;
}
}
You see we have two blocks of code doing the same thing.
Here's an example of similar code but using a function;
void loop() {
// Put your main code here, to run repeatedly:
String ssid = "";
String password = "";
/**
* Reading a string out of memory using the eeprom library
* read_string(length of data int, position in memory int) is a
function
* You call to read data from memory. you give it the starting
position
* in memory and length and it returns a string containing the
data
*/
ssid=read_string(0,30); // read ssid out of memory
password = read_string(100,30); // read password out of
memory
/**
* reads a string out of memory, l is the length of the data to be
read out of memory,
* p is the position in memory from which to start reading
*/
string read_string(int l, int p) {
string temp;
for (int n = p; n < l+p; ++n)
{
if (char (EEPROM.read(n)) != ';') {
if (isWhitespace(char (EEPROM.read(n)))) {
//Do not do anything
} else temp += String(char(EEPROM.read(n)));

}else n=l+p;

}
return temp;
}
You can see that I reused the code by creating a function. This
makes it easier to read, and uses less memory on your
controller board. If you become proficient in using functions
you will save time and create more user friendly code that you
can reuse in your next project

3. We will understand all the basic concepts of any one


programming language, then after that we will start
programming Arduino.

4. To learn Arduino programming, we will first learn to make


basic programs in it, then gradually we will add some
concepts to Arduino programming. And in this way we
will learn to make advanced Arduino programs which we
need to make Arduino projects.

5. You must download the Arduino IDE software and prepare


it for uploading programs to the Arduino before working
on the Arduino device.
6. So first you have to download Arduino IDE. For this, you
have to go to Google and type arduino.cc and as soon as
you enter, the screen of Arduino platform will open. In this
screen you will see many options. But you have to click
where the software is written, then after that two options
will open there. In one option it will be written ARDUINO
CREATE and in the second option it will be written
DOWNLOADS (DOWNLOAD) then you have to click on
download, So after this many download options will open,
but you have to download according to your window. So as
soon as you click on the option you want to download, then
the Arduino IDE will start downloading. And as soon as
the file is downloaded, then you have to run that file by
double clicking on it. So then in this way your Arduino
IDE is ready for programming, now after this we will learn
programming on it.

7. Let us tell you that the programming of Arduino is divided


into three parts, declaration statement, void setup ( ), void
loop ( ) in these three parts we do Arduino programming.

8. Declaration- In the declaration, we define the pins which


we are going to use in our program. like-

int LED = 13;


int pir_pin = 7;
int value = 0;
const int ledPin = 13;
const int ldrPin = A0;
In this program we have written int LED=13; So in this it is
understood from int that we have created a variable of type int
and named this variable as LED and for this LED variable we
have defined 13 number pin in Arduino.

9. void setup( ) - In void setup( ) we tell whether the pin


which we are using in our program is input pin or output
pin. And you have to use the pinMode() function to tell the
input and output pins in your program, let's see how to
write it.

pinMode(LED, OUTPUT); / pinMode(13, OUTPUT);


pinMode(pir_pin, INPUT);
pinMode(ldrPin,INPUT);
It is understood from this that the number 13 which is declared
for pin LED. That's the output pin. pir_pin This is the input pin.
And pin 7 in Arduino board is declared for pir_pin. In this, we
will write the pin mode INPUT for the pin which we will use in
the input. And for the pin which you will use in the output, the
pinmode will be written OUTPUT.
10. void loop( ) - In void loop ( ) we write in
the program what to do through this program and in what
way. We do all the actions in the program inside the void
loop ( ). Like we have made a program to turn on the LED.
And we have declared 13 pin for LED. So we will use
digitalWrite( ) function to keep 13 number pin ON. And in
its bracket we will write (LED, HIGH). So when we will
connect the positive pin of LED to pin number 13 of
Arduino and negative pin of LED to GND pin of Arduino.
Then the LED will turn ON. Loop means that the
statements written in the loop will be repeated again and
again.
Now we make this program complete.
Program-
int LED=13;
void setup() {
pinMode(LED,OUTPUT);
OR
pinMode(13,OUTPUT);
}
void loop() {
digitalWrite(LED,HIGH);
OR
digitalWrite(13,HIGH);
}

11. Now we will make programs for Arduino


and we will start these programs with simple and gradually
we will continue to implement more rules in them. And we
will also make circuit diagrams of all these Arduino
programs and we will also do them practically. Now we
start to program\

12. Every time we will upload the program in


arduino, before that we will select the USB key port in
arduino IDE and the board in which we are uploading the
program. Will select that board.

13. If we want to make any system through


arduino, first we will upload the program in arduino. Then
after that we will design the circuit.

14. Before working on Arduino, you should


know all the basic information about Arduino. So that there
is no possibility of any kind of damage.

Arduino Board Basic Details


The Arduino UNO is an open-source microcontroller board
based on the Microchip ATmega328P microcontroller and
developed by Arduino.cc. The board is equipped with sets of
digital and analog input/output (I/O) pins that may be
interfaced to various expansion boards (shields) and other
circuits. The board has 14 Digital pins, 6 Analog pins, and
programmable with the Arduino IDE (Integrated Development
Environment) via a type B USB cable. It can be powered by a
USB cable or by an external 9 volt battery, though it accepts
voltages between 7 and 20 volts. It is also similar to the
Arduino Nano and Leonardo. The Uno board is the first in a
series of USB Arduino boards, and the reference model for the
Arduino platform. The ATmega328 on the Arduino Uno comes
preprogrammed with a bootloader that allows uploading new
code to it without the use of an external hardware programmer.
It communicates using the original STK500 protocol. The Uno
also differs from all preceding boards in that it does not use the
FTDI USB-to-serial driver chip. Instead, it uses the
Atmega16U2 (Atmega8U2 up to version R2) programmed as a
USB-to-serial converter.
What is the use of Arduino board?
Arduino is an open-source electronics platform based on easy-
to-use hardware and software. Arduino boards are able to read
inputs - light on a sensor, a finger on a button, or a Twitter
message - and turn it into an output - activating a motor,
turning on an LED, publishing something online.

What are the components of Arduino board?


Some boards look a bit different from the one below, but
most Arduinos have the majority of these components in
common:
Power (USB / Barrel Jack) ...
Pins (5V, 3.3V, GND, Analog, Digital, PWM, AREF) ...
Reset Button. ...
Power LED Indicator. ...
TX RX LEDs. ...
Main IC. ...
Voltage Regulator.
What are the 3 main sections on the Arduino Uno board?
The major components of Arduino UNO board are as follows:

USB connector.
Power port.
Microcontroller.
Analog input pins.
Digital pins.
Reset switch.
Crystal oscillator.
USB interface chip.
What are the features of Arduino?
Features of Arduino Uno Board

The operating voltage is 5V.


The recommended input voltage will range from 7v to
12V.
The input voltage ranges from 6v to 20V.
Digital input/output pins are 14.
Analog i/p pins are 6.
DC Current for each input/output pin is 40 mA.
DC Current for 3.3V Pin is 50 mA.
Flash Memory is 32 KB.

How to Create First Arduino Program


We make the first program of LED Blinking. Through this
program, we will turn on the LED for a few seconds and OFF
for a few seconds. And this ON and OFF loop of the LED will
continue to run. In this program we first write the declaration
statement, that is, we select the pin for the LED. On which pin
of Arduino we have to connect the LED. Like we have to
connect the LED on the number 13 pin in the Arduino. So we
will write in the program - int LED = 13; Now understand why
this is written. It has an integer value of 13. So that's why we
created a variable of type int and named this variable LED. We
can name the variable anything. And after the variables are
created, we need semicolon; It is necessary to apply. In this
way, the part of our declaration gets complete, now after this
we go to void setup (). And in void setup () we write the entire
program inside Curly bracket. Like- {...} and we will write
inside it that the pin we selected for LED, we will use that pin
as output or input. Use as So we know. That we have selected
the LED pin in the Arduino program. We will use it as output.
Now, understand when to use output and input. So the output
(OUTPUT) we write in Arduino for the pins in which we have
to connect those loads or components. That works according to
our program. Such as - LED, Motor, etc. And input (INPUT)
we write in Arduino for the pins in which we have to connect
those sensors or components. That works according to our
program. According to which a message is given to Arduino.
And according to the same message, then Arduino generates
the output and then in the output pins of the Arduino that the
load or component is connected. They will all work according
to this message like- All Sensors, Bluetooth Module, etc. Write
the output statement for the program we are currently creating.
- pinMode (13, OUTPUT); Now understand why this is
written. pinMode () is a function and this function is used to
treat a specific pin as an input or output. To do this we first
write the pinMode function, then in its parenthesis we write the
number of the pin which we will use in the input or output but
now we will use the 13 number pin in the output. After writing
the pin number we put commas and then we have to use the pin
in the input or in the output they write. Just now we have to use
13 number pins in the output, then a complete statement will be
created - pinMode (13, OUTPUT); Now after this we go to
void loop (). And also in void loop () we write the entire
program inside Curly bracket. Like- {...} and in void loop () we
write in the program what to do through this program and how
to do it. We do all actions in the program within void loop ().
Right now we are making a program for LED Blinking. So we
have to turn on the LED for a few seconds and turn it off for a
few seconds and if the program is made in void loop () then it
will keep running, then we first write a statement to turn on the
LED in the void loop. - digitalWrite (13, HIGH); Now
understand why this is written. digitalWrite () is a function.
This function is used to write a higher or lower value to a
digital pin. If the pin is configured as an OUTPUT with
pinMode (), then its voltage will be set to this value: We are
currently programming the LED to ON, in which we first write
the digitalWrite () function. Then in his parenthesis we write
that PIN number. To which the output pin is created and then
after comma is written HIGH so HIGH is written because now
we are making a program to turn the LED ON, then this will
create a statement- digitalWrite (). But if we have to make a
program for LED blinking, then for this we have to set a time
for the LED to stay ON for how long the LED will remain ON,
for this we will use the delay () function. And inside this
function we set the time and the value of the time to be written
inside its parenthesis is in milliseconds. Milliseconds means 1
second of 1000 i.e. when we write 1000 in parenthesis it will
mean 1 second, so now we write for our program how long we
have to keep the LED ON like we have to keep ON for 4
seconds, then we will write- delay (4000); With this, the LED
will remain ON for 4 seconds, then it will turn off and it will
not run again. Because we have not told in the program how
long to keep the LED off, then we have to write a statement to
turn off the LED, so we write digitalWrite (13, LOW); Under
its parenthesis, we write 13, LOW because we are turning off
the LED i.e. the 13 number pin is turned off and after that we
will also have to tell how long we have to turn off the LED. If
we do not write, the LED will remain off, then again we write
the time inside the parenthesis of delay (), then we wrote -
delay (1000). Will go off and then this loop will continue like
this and our LED will start blinking, now we make the whole
program.

Program-
int LED=13;
void setup() {
pinMode(13, OUTPUT);
}
void loop(){
digitalWrite(13, HIGH);
delay(4000);
digitalWrite(13, LOW);
delay(1000);
}

In this way our LED Blinking program is ready, now we can


check it by uploading it to Arduino.
Now we see the circuit diagram of this project. How will we
connect Arduino and LED.

Circuit Diagram-

We have written the program in the Arduino IDE, after that we


take an Arduino UNO board. And connect USB to your
Arduino board and connect it to your computer or laptop, then
we will open our program in Arduino IDE and we see many
further options in this IDE. So there is an option at the top,
Tools (Tools) will click on it. So many further options will be
seen and the board will be written in one place in these options
and will click on it. Then we will see more options, in which
we have to click according to our board. Just like we are using
the Arduino UNO board right now, we will click on the
Arduino UNO itself. If we used another board, we would click
on it. And as soon as we select the Arduino UNO board, then
our board will be selected. After this, we click on Tools again.
So many options will appear again and port will be written in
one place in this option and click on it. Then we will see the
port number, it will start with COM (COM) like - COM1,
COM2, COM3, COM4, etc. Clicking on it, the port will also be
selected, after which we see the Scatch option on the screen. If
you click on this, we will see Verify option at the top, and if
you click on it, we will know that there is no error in our
program. When we know that our program is correct. So again
we will click on Scatch and this time the option coming in it
will click on the option of another number i.e. Upload. Then in
some time the program will be uploaded to Arduino. And for
which we had made the program, that work will start
happening i.e. LED Blink will start, in this way our project and
program of LED Blinking becomes complete.

How to Create Second Arduino Program


Through this program we will operate the LED and Buzzer.
Through this program, we will turn on the LED and Buzzer for
a few seconds. And OFF for a few seconds. And this loop will
continue in the program forever. In this program also we will
first complete the declaration statement. So we will first select
the pin for the LED in the Arduino, then we will write a
statement for this. - int LED = 13; So in this way the 13
number pin in the Arduino board is fixed for the LED. Now
after this we will select pin for Buzzer also in Arduino. So for
this we will write the statement. - int Buzzer = 12; Now
understand why it is written like this. In this 13 is an integer
value. So that's why we have created a variable of type int and
named this variable LED. We can name the variable anything.
And after creating variables we have to use semicolon ; It is
necessary to put And the same condition will apply for the
buzzer as well. In this way our declaration part is completed.

Now after this we go to void setup( ). And in void setup( ) we


write the whole program inside the Carly bracket. Like- {...}
and we will write inside it that the pins we have selected for
LED and Buzzer, we will use those pins as output Or use it as
input. So we know. That we have selected the LED and Buzzer
pins in the Arduino program, we will use them as output. Now
let us understand when output and input are used. So the output
(OUTPUT) we write in Arduino for those pins to which we
have to connect those loads or components. That which works
according to our program. Like- LED, Motor, etc. And Input
(INPUT) we write in Arduino for those pins in which we have
to connect those Sensors or Components. That which works
according to our program. According to which a message is
given to the Arduino. And according to the same message then
the Arduino generates the output. And then the load or
component is connected to the output pins of the Arduino. All
of them will work according to this message like - All Sensors,
Bluetooth Module, etc. We write the output statement for the
program we are making now.- pinMode(13, OUTPUT); Now
understand why it is written like this. pinMode( ) is a function
and this function is used to make a specific pin behave as input
or output. To do this we first write the pinMode function, then
in its parenthesis we write the number of the pin which we will
use in the input or output but now we will use the 13 number
pin in the output. After writing the pin number, we put a
comma and then we have to use the pin in the input or write it
in the output. Like now we have to use the 13 number pin in
the output, then the complete statement will be made -
pinMode(13, OUTPUT); And then in the same way the
statement will be written for the Buzzer as well. But for the
buzzer the pin will write 12. eg- pinMode(12, OUTPUT); In
this way the void setup() part of the program also completes.

Now after this we go to void loop ( ). And in void loop( ) also


we write the whole program inside Carly bracket. Like- {...}
and in void loop( ) we write in the program what to do through
this program and in what way. We do all the actions in the
program inside the void loop ( ). Right now we are making a
program to operate LED and Buzzer. So we have to turn on the
LED and Buzzer for a few seconds and turn it off for a few
seconds and this program is made in void loop ( ) so it will
continue to run, then we write the statement to turn on the LED
first in the void loop. - digitalWrite(13,HIGH); Now we write
the statement to turn on the Buzzer. - digitalWrite(12,HIGH);
Now understand why it is written like this. digitalWrite( ) is a
function This function is used to write a high or low value to
the digital pin. If the pin is configured as an OUTPUT with
pinMode ( ), its voltage will be set to the corresponding value.
Right now we are making a program to turn on the LED and
Buzzer, in this we first write the digitalWrite ( ) function, then
in its parenthesis we write the pin number which has been
made the output pin and then write HIGH after putting comma
HIGH so We write that because now we are making a program
to turn on the LED and Buzzer, then this statement will be
made-

Right now we have made a program to turn on only the LED


and Buzzer. But we have to make a program to turn on and off
the LED and Buzzer, then for this we have to set a time for the
LED and Buzzer to be ON, for how long the LED and Buzzer
will be ON. Now for this we will use delay() function. And
inside this function we set the time and the value of time which
will be written inside its parenthesis is in milliseconds.
Millisecond means 1 second of 1000 i.e. when we write 1000
in parenthesis, it will mean 1 second, so now we write for our
program how long we have to keep the LED Buzzer ON like
we have to keep ON for 5 seconds then we Will write
delay(5000);

This will keep the LED and Buzzer ON for 5 seconds. Then it
will be closed. And won't run again. Because we have not told
in the program how long to keep the LED and Buzzer off, then
we have to write the statement to turn off the LED and Buzzer
also, then we write digitalWrite(13,LOW); Inside its
parenthesis we write 13,LOW because we are turning off the
LED i.e. turning off the number 13 pin and then write the same
way for the Buzzer. That is, we will also write for 12 pin. And
after this we also have to tell that for how long we have to turn
off the LED and Buzzer, if we do not write then the LED Sur
Buzzer will remain off. So again writing the time inside the
parenthesis of delay(), so we wrote delay(1000) This will turn
off the LED and Buzzer for one second. Then it will turn ON.
And then the LED and Buzzer will be ON for 5 seconds. Then
it will be off.

And then this loop will continue like this and in this way the
LED and Buzzer will continue to operate. Now we make the
complete program

Program-
int LED=13;
void setup() {
pinMode(13, OUTPUT);
}
void loop(){
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
delay(5000);
digitalWrite(13, LOW);
digitalWrite(12,LOW);
delay(1000);
}

In this way our program to operate LED and Buzzer is ready,


now we can check it by uploading it to Arduino.
Now let us see the circuit diagram of this system. How to
connect Arduino and LED and Buzzer.

Circuit Diagram-

We have written the program in the Arduino IDE, after that we


take an Arduino UNO board. And connect USB to your
Arduino board and connect it to your computer or laptop, then
we will open our program in Arduino IDE and we see many
options in this IDE. So there is an option at the top, Tools
(Tools) will click on it. So many further options will be seen
and the board will be written in one place in these options and
will click on it. Then we will see more options, in which we
have to click according to our board. Just like we are using the
Arduino UNO board right now, we will click on the Arduino
UNO itself. If we used another board, we would click on it.
And as soon as we select the Arduino UNO board, then our
board will be selected. After this, we click on Tools again. So
further many options will appear again and port will be written
in one place in this option and click on it. Then we will see the
port number, it will start with COM (COM) like - COM1,
COM2, COM3, COM4, etc. Clicking on it, the port will also be
selected, after which we see the Scatch option on the screen. If
you click on it, we will see Verify option at the top, and if you
click on it, we will know that there is no error in our program.
When we come to know that our program is correct, then we
will click on Scatch again and this time the option coming in it
will click on the option of another number i.e. Upload. Then in
some time the program will be uploaded to Arduino. And for
which we had created the program, it will start working i.e.
That is, LED and Buzzer will start turning on and off. In this
way the system to operate the LED and Buzzer is ready.
How to Create Third Arduino Program
Through this program, we will turn the LED ON and OFF with
the LDR (Light Depand Resistor) sensor. So for this, we have
to declare the LDR sensor and the pins for the LED in the
declaration statement. So first we will declare the pin for LED
like we did in the previous program - int LED = 13; This is
fixed to the 13 number pin LED, after this we will declare the
pin for the LDR sensor, then we will write for it- int LDR =
A0; In this way, pin A0 for the sensor is fixed, we have
declared A0 for LDR i.e. Analog Pin. We know that LED is
used only in the output mode and we had understood that all
sensors are used in the input mode, so we will write the mode
input of the LDR sensor as the LDR is a light demand register,
so the value of this resistor depends on the light of the light If it
does, then as the light on this sensor slows down, then this
sensor will turn on the LED after a time. So we have to set a
value for this sensor which will depend on its darkness that
when it is dark on the LDR, then when the value of this resistor
becomes light on (ON) we will change the value of this sensor
to the part with void loop. Now we will complete the void
setup part of this program, so in this, we have to first see how
many components we are using in our program. And how
many components have been declared variable like we have
just used two components in this program. LED and LDR
sensor, then we will write the pin mode statement. So we know
that for LED we keep the pin output (OUTPUT). So we will
write - pinMode (LED, OUTPUT); And we'll keep the pinnode
input (INPUT) for the LDR sensor, then we'll write. PinMode
(LDR, INPUT); After this, how much speed we have to
communicate with our Arduino and system via USB. For this,
we have to set the baud rate in the program. Use the serial
begin (Serial.begin) command to set the baud rate. What we
want to keep the baud rate in our program, we will know from
our Arduino software itself and according to the baud rate
given in the software, we keep the baud rate in our program.
Now we will write the baud rate in our program-
Serial.begin(9600); And all the statements that we have written
in voice setup, all of them must be written inside the curly
bracket {.....}. Now we will write the program in void loop, in
this we also write the program inside curly bracket {.....}, in
this we will first read the value of the sensor, for this we write
the statement. Right now the sensor will connect to Arduino's
analog pin A0 so we will write in it-int LDRStatus
=analogRead (LDR); In this we have used digitalRead ()
command to read the value of LDR sensor. We know that
whatever statement is written in void loop will be repeated
again and again. Now we have write a statement to read the
value of the sensor. If the value of the sensor is 1 i.e. High
(HIGH) then the LED will also be in High state (HIGH) i.e.
LED will also be ON and if the value of the sensor is not 1 i.e.
Low then the LED will remain in the LOW state. That is, the
LED will be off. We have to use ifelse loop to apply this
condition in the program, then let us see how we will use it. If
the sensor value is high then this statement will execute. We
will use the ifelse loop in this program and we will write a
value of the sensor in the if statement itself. Now we write in
the if block of the loop if-if (LDRStatus <= 300); If the value
of the LDR sensor or the status of the LDR sensor is equal to
or less than 300, then the statement of the BALLE block will
be executed. And when the program of the block with if is
executed then the statement to turn the LED on is written in it.
digitalWrite(LED, HIGH); If that statement is executed then
LED will be turned on and when LED is on, then we want to
print a message on the serial monitor of Arduino also, for this
we will use the Serial.println () function. And inside its
parenthesis, we will write the message that we have to print,
then we will write- Serial.println("LDR is DARK, LED is
ON"); And if the value of the LDR sensor or the status of the
LDR sensor is more than 300, then the else block will execute.
In this, the statement of LED being off is written - digitalWrite
(LED, LOW); So this statement will be executed. Then the
LED will be OFF and when the LED is OFF then we want to
print another message on the serial monitor of Arduino again,
then we will write that message inside the parenthesis of the
function- Serial.println("LDR is Light, LED is OFF"); So in
this way we have made a program of LED with LDR, when we
use this project, when the LDR is dark on the sensor, the LED
will automatically turn ON. And when the light is on the LDR,
the LED will be off and thus the ifelse loop will continue in
this way. We can create many projects through this program,
such as - security system, Home Automation System,
Automatic Night Lamp, etc. Now we make the complete
program.

Program-
int LED = 13;
int LDR = A0;
void setup() {
pinMode(LED, OUTPUT);
pinMode(LDR, INPUT);
Serial.begin(9600);
}
void loop() {
int LDRStatus = analogRead(LDR);
if (LDRStatus <=300) {
digitalWrite(LED, HIGH);
Serial.println("LDR is DARK, LED is ON");
}
else {
digitalWrite(LED, LOW);
Serial.println("LDR is Light, LED is OFF");
}
}

Now we see the circuit diagram of this project. How will


Arduino and LDR connect to Sensor and LED. In this circuit
we will connect a resistance of 1k ohm and we will connect it
only by looking at the circuit.

Circuit Diagram-

We have written the program in the Arduino IDE, after that we


take an Arduino UNO board. And connect USB to your
Arduino board and connect it to your computer or laptop, then
we will open our program in Arduino IDE and we see many
options in this IDE. So there is an option at the top, Tools
(Tools) will click on it. So many further options will be seen
and the board will be written in one place in these options and
will click on it. Then we will see more options, in which we
have to click according to our board. Just like we are using the
Arduino UNO board right now, we will click on the Arduino
UNO itself. If we used another board, we would click on it.
And as soon as we select the Arduino UNO board, then our
board will be selected. After this, we click on Tools again. So
further many options will appear again and port will be written
in one place in this option and click on it. Then we will see the
port number, it will start with COM (COM) like - COM1,
COM2, COM3, COM4, etc. Clicking on it, the port will also be
selected, after which we see the Scatch option on the screen. If
you click on it, we will see Verify option at the top, and if you
click on it, we will know that there is no error in our program.
When we come to know that our program is correct, then we
will click on Scatch again and this time the option coming in it
will click on the option of another number i.e. Upload. Then in
some time the program will be uploaded to Arduino. And for
which we had created the program, it will start working i.e.
when the LDR is dark on the sensor then the LED will
automatically turn ON and the LED will be off as long as the
light is on the LDR. Now we can use this project according to
its working in any way.

How to Create Fourth Arduino Program


Through this program, we will turn the LED ON and OFF with
the PIR sensor. So for this, we have to declare the PIR sensor
in the declaration statement and the pins for the LED. And if
PIR Sensor is a sensor, then we will declare a variable for the
value of its sensor and if the value of the sensor is first placed
on the LOW, then we will also declare a variable for this, so
we can also keep the value of the sensor at HIGH. But for now
we will keep the sensor value at LOW and for this we will
declare the variable. So first we will declare the pin for LED
like we did in the previous program - int LED = 13; This is
fixed to the 13 number pin LED, after this, we will declare the
pin for the PIR sensor, for this we will write - int pir_pin = 7;
In this way, pin 7 is fixed for the sensor. We know that LED is
used only in the output mode and we had understood that all
sensors are used in the input mode, so we will write the mode
input of the PIR sensor. And all the sensors we use have
values in 0 and 1 state and 0 means low state and 1 means high
state. Now we will declare a variable to keep the value of the
sensor zero - int value = 0 ; This will cause the sensor value to
be 0 in the starting and then we will loop in the program and
change the value of the sensor to 0 and 1 again and again as if
the value of the sensor is now 0, its state will be LOW, then we
will also declare a variable for its state.- int pirState = LOW;
Our declaration statement for this program has been completed.
Now we will complete the void setup part of this program, so
in this, we have to first see how many components we are
using in our program. And how many components have been
declared variable like we have done two components in this
program right now. LED and PIR sensor then we will write the
pin mode statement. So we know that for LED we keep the pin
output (OUTPUT). Then we will write-
pinMode(LED,OUTPUT); And we will keep the pinnode input
(INPUT) for the PIR sensor then we will write. - pinMode
(pir_pin, INPUT); After this, we have to set the baud rate in the
program for the speed at which data is to be communicated to
our Arduino and system via USB. Use the serial begin
(Serial.begin) command to set the baud rate. What do we need
to keep the baud rate in our program. We will know this only
from our Arduino software and according to the baud rate
given in the software, we keep the baud rate in our program.
Now we will write the baud rate in our program - Serial.begin
(9600); And all the statements that we have written in the voice
setup will be written inside the curly bracket {.....}, now we
will write the program in void loop, in this we will also
program curly bracket {.....} } Inside. In this, we will first read
the value of the sensor, for this we write a statement. Right
now the sensor will connect to the digital pin 7 of Arduino so
we will write in it- value = digitaRead (pir_pin); In this, we
have used digitalRead () command to read the value of the PIR
sensor. We know that whatever statement is written in the void
loop will be repeated. Now we have written a statement to read
the value of the sensor. If the value of the sensor is 1 i.e. High
(HIGH) then the LED will also be in High state (HIGH) i.e.
LED will also be ON and if the value of the sensor is not 1 ie
Low then the LED will remain in the LOW state. That is, the
LED will be off. We have to use ifelse loop to apply this
condition in the program, then let us see how we will use it. If
the sensor value is high then this statement will execute.

If the sensor value is not high then this statement will execute.

Now understand why this is written. digitalWrite () is a


function / command This function is used to write a higher or
lower value to a digital pin. If the pin is configured as an
OUTPUT with pinmod (), then its voltage will be set to this
value: after this our program is complete, now we will upload
this program to Arduino. And then according to the program,
we connect all the components to the Arduino. Now as soon as
we move an object in front of the PIR sensor or if an object
comes in front of it, the LED will be turned on. This is
happening becausearrival of an object in front of the sensor, the
value of the sensor is getting high (HIGH), then the condition
with if will run at this time and when an object is not in front of
it, then its value will remain low (LOW) and it will remain in
the LED OFF state, then this time the condition with else will
run. And thus the loop of ifelse will continue in this way. Now
we make the complete program.

Program-
int LED = 13;
int pir_pin = 7;
int value = 0;
int pirState = LOW;
void setup() {
pinMode(LED, OUTPUT);
pinMode(pir_pin, INPUT);
Serial.begin(9600);
}
void loop() {
value = digitalRead(pir_pin);
if (value == HIGH) {
digitalWrite(LED, HIGH);
}
else{
digitalWrite(LED, LOW);
}
}
Now we see the circuit diagram of this project. How will
Arduino and PIR connect to Sensor and LED.

Circuit Diagram-

We have written the program in the Arduino IDE, after that we


take an Arduino UNO board. And connect USB to your
Arduino board and connect it to your computer or laptop, then
we will open our program in Arduino IDE and we see many
further options in this IDE. So there is an option at the top,
Tools (Tools) will click on it. So many options will be seen
and the board will be written in one place in these options and
will click on it. Then we will see more options, in which we
have to click according to our board. Just like we are using the
Arduino UNO board right now, we will click on the Arduino
UNO itself. If we used another board, we would click on it.
And as soon as we select the Arduino UNO board, then our
board will be selected. After this, we click on Tools again. So
further many options will appear again and port will be written
in one place in this option and click on it. Then we will see the
port number, it will start with COM (COM) like - COM1,
COM2, COM3, COM4, etc. Clicking on it, the port will also be
selected, after which we see the Scatch option on the screen. If
you click on it, we will see Verify option at the top, and if you
click on it, we will know that there is no error in our program.
When we come to know that our program is correct, then we
will click on Scatch again and this time the option coming in it
will click on the option of another number i.e. Upload. Then in
after some time the program will be uploaded to Arduino. And
for which we had created the program, it will start working,
that is, when we move an object in front of the PIR sensor or
an object comes in front of it, the LED will be on. And when
no object will remain in front of the PIR sensor. The LED will
be OFF. Now we can use this project according to its working
in any way.

How to Create Fifth Arduino Program


Through this program, we will operate Relay and Buzzer from
IR sensor. So for this, we have to declare the IR sensor in the
declaration statement and the pins for Relay and Buzzer. So
first we will declare PIN for Relay, then we know how to
declare, then we will write - int Relay = 13; We can keep more
than the variable name relay (Relay), now the number 13 pin
has been fixed for relay (Relay). Now we will declare the
variable for Buzzer and for that we will define the pin. - int
Buzzer = 7; Right now the number 7 pin has been fixed for the
buzzer. After this, we will declare the pin for the IR sensor, for
this we will write - int IR = A0; In this way, pin A0 for the
sensor is fixed. We have declared A0 for Analog pin ie IR. All
the sensors that we use have values in 0 and 1 state and 0
means low state and 1 means high state. Now we will declare a
variable to keep the value of the sensor zero. int
sensorValue=0; With this, the sensor value will remain 0 in
the starting and then we will loop the program and change the
value of the sensor to 0 and 1 again and again as the value of
the sensor is 0 then its state will be LOW. Now we will
complete the void setup part of this program, so in this, we
have to first see how many components we are using in our
program. And how many components have been declared
variable like we have just used three components in this
program. Relay and Buzzer and IR sensor then we will write
PIN mode statement. So for Relay and Buzzer we keep
PinMode output (OUTPUT). So we will write for Relay-
pinMode (Relay, OUTPUT); And for Buzzer we will write-
pinMode (Relay, OUTPUT); And we will keep the pinnode
input (INPUT) for the IR sensor, then we will write.-
pinMode(IR, INPUT); After this, we have to set the baud rate
in the program for the speed at which data is to be
communicated to our Arduino and system via USB. Use the
serial begin (Serial.begin) command to set the baud rate. What
we want to keep the baud rate in our program, we will know
from our Arduino software itself and according to the baud rate
given in the software, we keep the baud rate in our program.
Now we will write the baud rate in our program. -
Serial.begin(9600); And all the statements that we have
written in void setup will all be written inside curly bracket
{.....} Now we will write the program in void loop, in this we
will also program curly bracket {..... }, We will first read the
value of the sensor in it, for this we write the statement. Right
now the sensor is connected to Analog pin A0 of Arduino.So
we will write in-int sensorValue = analogRead (IR); In this, we
have used the analogRead () command to read the value of the
IR sensor. We know that whatever statement is written in the
voice loop will be repeated. Now we have written a statement
to read the value of the sensor. If the value of the sensor is 1 ie
High (HIGH) then Relay and Buzzer will also be in high state
(HIGH) i.e. Relay and Buzzer will also become ON and if the
sensor does not have a value of 1 ie Low then Relay and
Buzzer will remain in LOW state i.e. Relay and Buzzer will
remain off. We have to use ifelse loop to apply this condition
in the program, then let us see how we will use it. If the sensor
value is high then this statement will execute. We will use the
ifelse loop in this program and we will write a value of the
sensor itself in the statement of if. 200); In starting, the value
of IR sensor will be 0 and in this statement the value of sensor
is set to 200. If the value of the sensor is greater than 0 and is
less than 200 or equal to 200, then the statement of the if block
will be executed. And when the program of the block with if is
executed, then it has written a statement to turn Relay and
Buzzer ON, then it will write a statement to turn relay (ON) to
Relay, digitalWrite (Relay, HIGH); And the statement to turn
the buzzer ON will write - digitalWrite (Buzzer, HIGH); And
both these statements will be written inside the same Curly
bracket. And if the block of this Curly bracket is executed if
the condition is true, then the relay will be turned ON and the
buzzer will also be ON. And if the condition written in
parentheses of if is false then the statement of the block
containing else will be executed and in the block of else the
statement of keeping relay (Relay) and Buzzer off is written.
So the statement to turn off relay (Relay) will say - digitalWrite
(Relay, LOW); And the statement to turn off the buzzer will
say - digitalWrite (Buzzer, LOW); So when the program of the
else block executes, the relay will be OFF and the buzzer will
also be OFF. Accordingly to this system, when an object goes
in front of the IR sensor, the relay will turn ON and the buzzer
will also be ON. And if there is no object in front of the sensor,
then the lead will remain off and in this way the loop of ifelse
will continue in this way. We can create many projects through
this program, such as - security system, Home Automation
System, Automatic, etc. Now we make the complete program

Program-
int Relay =13;
int Buzzer = 7;
int IR = A0;
int sensorValue = 0;
void setup() {
pinMode(IR, INPUT);
pinMode(Relay, OUTPUT);
pinMode(Buzzer, OUTPUT);
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(IR);
if(sensorValue<=200)
{
digitalWrite(Relay, HIGH);
digitalWrite(Buzzer, HIGH);
}
else
{
digitalWrite(Relay, LOW);
digitalWrite(Buzzer, LOW);
}
}

Now we see the circuit diagram of this project. How Arduino


and IR Sensor and Relay will connect Buzzer.

Circuit Diagram-
We have written the program in the Arduino IDE, after that we
take an Arduino UNO board. And connect USB to your
Arduino board and connect it to your computer or laptop, then
we will open our program in Arduino IDE and we see further
many options in this IDE. So there is an option at the top,
Tools (Tools) will click on it. So further many options will be
seen and the board will be written in one place in these options
and will click on it. Then we will see more options, in which
we have to click according to our board. Just like we are using
the Arduino UNO board right now, we will click on the
Arduino UNO itself. If we used another board, we would click
on it. And as soon as we select the Arduino UNO board, then
our board will be selected. After this, we click on Tools again.
So many options will appear again and port will be written in
one place in this option and click on it. Then we will see the
port number, it will start with COM (COM) like - COM1,
COM2, COM3, COM4, etc. Clicking on it, the port will also be
selected, after which we see the Scatch option on the screen. If
you click on it, we will see Verify option at the top, and if you
click on it, we will know that there is no error in our program.
When we come to know that our program is correct, then we
will click on Scatch again and this time the option coming in it
will click on the option of another number i.e. Upload. Then in
some time the program will be uploaded to Arduino. And for
which we had created the program, it will start working, that is,
when we move an object in front of the IR sensor or an object
will come in front of it, then the relay and buzzer will get ON.
And when there is no object in front of the IR sensor, then the
relay and buzzer will remain off. Now we can use this project
according to its working in any way.

Extra-
Coding can easily be one of the most in-demand skills in the
21 st century. A domain that never existed a century ago, there
aren't many fields of work that can come close enough to the
versatility it offers.
One of the most common mistakes that young or even
experienced coders make is writing obsessively without
structure. Although they will still do the task quickly, it makes
future work on the code significantly more difficult.
We all agree that codes should be easily readable and
navigation friendly if they are to be used for future functions as
well. Here are five basic tips that can improve the usability of
your code.

Arduino Coding Tips – Conclusion


I suggest you try these techniques on your old code to see if
you can minify any of these or convince your teammates or
coworkers to use them.
We instinctively know how easily we get caught up in our
work and forget how we got into our coding routines. Being
mindful of your code and a little forethought can really save
you a lot of time in future coding efforts as well.
Reusable, easily readable and understandable code is what the
world needs today and honestly, it's not that hard, is it?

You might also like