Basic_Arduino_Programming_Tips_How_To_Learn_Arduino_Programming
Basic_Arduino_Programming_Tips_How_To_Learn_Arduino_Programming
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.
Identifiers
Constant
Variable
Keywords
Data Type
Function
Operator
Control Instruction
Loop
break
Switch Control Instruction
Functions
Array
Strings
}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
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
Program-
int LED=13;
void setup() {
pinMode(13, OUTPUT);
}
void loop(){
digitalWrite(13, HIGH);
delay(4000);
digitalWrite(13, LOW);
delay(1000);
}
Circuit Diagram-
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);
}
Circuit Diagram-
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");
}
}
Circuit Diagram-
If the sensor value is not high then this statement will execute.
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-
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);
}
}
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.