You are on page 1of 13

ELECTRONICS HUB

P R OJ E C T S | T U TO R I A L S | C O U R S E S | K I T S

Convert Any File to a PDF. Get the Free From


1 PDF Converter Extension Doc to Pdf App! free.fromdoctopdf.com

2 3 PCBs at $1 Only Hurry up and Get your PCB Done seeedstudio.io

HOME PROJECTS MINI PROJECTS ARDUINO FREE CIRCUITS TUTORIALS SYMBOLS

DIY COURSES CONTACT US

YOU ARE HERE: HOME / ARDUINO / HOW TO WRITE ARDUINO CODE FOR BEGINNERS?

How to Write Arduino Code for


Beginners?
NOVEMBER 20, 2016 BY ADMINISTRATOR — 2 COMMENTS

In the previous tutorial, we have analyzed the “Blink” sketch by understanding di erent objects
of the sketch. We have seen about comments, setup, loop, pin initialization, driving an output
pin as HIGH or LOW and delay function. We have also seen how to modify some parameters
like LED pin and duration delay.

3 PCBs at $1 Only
Hurry up and Get your PCB
Done

In order to explore further in to Arduino, we will design a circuit and write our own sketch for it.
For the purpose of this tutorial, we need the following components.
Arduino UNO
USB cable Type – A to Type – B
LED
1 KΩ Resistor
10 KΩ Resistor
Push button
Bread board (Prototyping board) and
Connecting wires

In this project, we are going to turn on an LED when a button is pushed. The schematic for the
project is shown below.

In this circuit, the anode of the LED is connected to 13th pin (or any other digital pin for that
matter) of the Arduino UNO through a current limiting resistor of 1 Kilo Ohms. The cathode of
the LED is connected to ground.

The next step is to connect a 10 Kilo Ohm resistor between 10th pin of Arduino UNO and 5V
supply i.e. the 10th pin of the Arduino is pulled high. Hence, 10th pin of the Arduino will
continuously detect logic HIGH.

Now connect one end of the switch or button to the 10th pin of the Arduino and the other end
is connected to ground. The power supply to the breadboard can be given from 5V and GND
pins of the Arduino UNO. This nishes the design of the circuit.
Download PDF (Free)
To View PDF, Download Here

FromDocToPDF.com OPEN

Now, we will see the theory behind the button press. As mentioned earlier, the 10th pin of the
Arduino is pulled high and as a result, it continuously detects logic HIGH. Also, the button is
connected between pin 10 and ground.

Hence, if the button is pressed, the connection between the pin 10 of Arduino and ground is
closed and as a result, Arduino will detect a logic LOW on the 10th pin. With the help of this
transition from Logic HIGH to Logic LOW, we can detect if the button is pressed or not and
subsequently turn the LED on or o .

As the circuit is successfully described with its design and theory, the next step is to write the
program or sketch for this circuit and logic in the Arduino IDE.

Writing the Sketch for the circuit

For writing the code easily, we need to follow the following steps.

Initialize a pin as output for the LED.


Initialize a pin as input for the button or switch.
Detect the status of the button.
Turn the LED on or o .

So, the rst step is to initialize a pin (Pin 13) of the Arduino as output for LED and another pin
(Pin 10) as input for button. As we have seen in the previous tutorial, we need to use the
function pinMode to initialize a pin as input or output. Hence, write the pinMode functions for
both LED and button pin in the setup function.
Once we initialized the pins for LED and switch, the next step is to check for the status of the
switch. In order to do this, we need to use a function called digitalRead ();

digitalRead, as the name speci es, is a function which is used to read values from digital pins
i.e. it reads for logic LOW or HIGH at the digital pins.

The syntax of digitalRead function is digitalRead (pin no);

In the syntax, pin no indicates the number of the digital pin from which you want to read the
digital data. The digitalRead function can return two values: HIGH or LOW. If the digitalRead
detects 5V at the pin, then HIGH is returned. Similarly, if the digitalRead detects 0V at the pin,
then LOW is returned.

Coming to our circuit, we need to read the status of the button pin i.e. 10th pin. Create a
variable before setup and name it as “buttonstate”, so that the value returned by the
digitalRead function can be captured. Now in the loop function, assign the return value of the
digitalRead of 10th pin to the buttonstate variable.

With this step, we now have captured the status of the button in a variable. The next step is to
control the LED. In order to control the LED i.e. either turn it on or o , we need to use a
conditional statement called if and else.
If – else statement is used to check for a condition and execute a block of code in the
program. If the condition in the “if” statement is true, the block of code corresponding to the “if”
statement is executed. If the condition in the “if” statement is false, then the block of code in
the “else” statement is executed.

So, in the “if” statement, we compare the buttonstate variable with LOW. This is because, the
Arduino UNO will detect a logic LOW when the button is pressed. If this condition is true i.e. if
the button is pressed, then we turn on the LED by writing digitalWrite function to output high
on LED pin in the “if” statement.

If the condition is false i.e. if the button is not pressed, then we turn of the LED by writing
digitalWrite function to output low on the LED pin in the “else” statement.
In the next tutorial, we will start a new topic called Serial Communication and see how Arduino
communicates with the computer by modifying the above sketch.

Resume Template (Free)


Top Resume Builder. Build a Free & Perfect
Resume with ease !
Resume.io OPEN

FILED UNDER: ARDUINO


Comments

hemant says
OCTOBER 25, 2017 AT 8:48 AM

sir my name is hemant and i am student of b.tech(ece) 3rd year .sir i want to make arduinio
based project but how to start my project .my project name is Interfacing RTC Module
(DS1307) With 8051 Micro controller

Reply

Jishnudeb Mondal says


SEPTEMBER 9, 2018 AT 5:37 PM

if(buttonstate==HIGH)

Reply

Leave a Reply
Your email address will not be published. Required elds are marked *

Comment

Name *

Email *
Website

POST COMMENT

Search this website …


Arduino Project

Arduino Circuit

Connector Wire

Arduino Programming

PROJECTS BY CATEGORY

Arduino Projects (200+)


Electronics Projects (250+)
Mini Project Circuits (160+)
Mini Project Ideas (150+)
ECE Projects (150+)
EEE Projects (150+)
8051 Projects (110+)
Raspberry Pi Projects (101+)
Electrical Project Ideas (100+)
Embedded Projects (100+)
Latest Electronics Ideas (100+)
Microcontroller Mini Projects (100+)
Robotics Projects (100+)
VLSI Projects (100+)
Solar Projects (100+)
IOT Projects (100+)

Communication Projects (70+)


LED Projects (70+)
Power Electronics Projects (60+)
RFID Projects (60+)
Home Automation Projects (50+)
Matlab Projects (50+)
EIE Projects (50+)
Wireless Projects (50+)
LabView Projects (45+)
Zigbee Projects (45+)
GSM Projects (40+)
555 Timer Circuits (40+)
Sensor Projects (40+)
ARM Projects (60+)
DTMF Projects (30+)
PIC Projects (30+)
Electrical Mini Projects (25)
ESP8266 Projects (15)

KITS

Best Drone Kits [12]


3D Printer Kits [12]
Best Robot Vacuum Clears [14]
Best Waveform Generators [12]
Led Strip Light
kit

Digital Neon LED Flex,


360° Neon LED Flex, Mini
Neon LED Flex, Support
Customize

LED Strip Light Kit

GENERAL PROJECTS PROJECTS

Tutorials Electrical Mini projects


Symbols Electronics Microcontroller
Courses Embedded Arduino

Calculator Power Solar


Contact Robotics Free circuits
ARM Home Automation
IOT Seminar Topics

Electronics Questions

TUTORIALS TUTORIALS FOLLOW US

Capacitors Ampli ers Instagram


Resistors IO Devices Youtube
Filters Thyristors Facebook

Diodes DC Circuits Google Plus


Transistors Number System Twitter
Copyright © 2018 Electronicshub.org

You might also like