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

DPS - Arduino Manual - 2019-20

The document provides a complete guide to robotics with programming and circuits for a robotics club. It includes an introduction to key concepts of electronic circuits like current, voltage, resistance and components. The guide has both theoretical and practical hands-on sections, with 20% theory and 80% practical lessons. It covers topics like circuits, Ohm's law, LEDs, resistors, Arduino programming and hands-on projects to build circuits and control outputs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

DPS - Arduino Manual - 2019-20

The document provides a complete guide to robotics with programming and circuits for a robotics club. It includes an introduction to key concepts of electronic circuits like current, voltage, resistance and components. The guide has both theoretical and practical hands-on sections, with 20% theory and 80% practical lessons. It covers topics like circuits, Ohm's law, LEDs, resistors, Arduino programming and hands-on projects to build circuits and control outputs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

ROBOTICS CLUB 2019-20 : DPS GBN

COMPLETE GUIDE TO ROBOTICS WITH


PROGRAMMING & CIRCUITS

HANDS ON LEARNING REFERENCE MANUAL


Technomentis is working towards augmenting the education of young
students with a STEM based curriculum to inspire the next generation of
innovators. Our unique programs expose students to hands-on learnings,
gearing the young minds towards science, technology and reasoning.
Scientific inquiry provides the opportunity for children to develop a range
of skills, either explicitly or implicitly.
• Explore objects, materials, and events.
• Raise questions.
• Make careful observations.
• Engage in simple investigations.
• Describe (including shape, size, number), compare, sort,
classify, and order.
• Record observations using words, pictures, charts, and graphs.
• Identify patterns and relationships.
• Develop tentative explanations and ideas.
• Work collaboratively with others.
• Share and discuss ideas and listen to new perspectives

We are thankful to you for being a part of this program and hope you
have enjoyed and benefitted from the learning process.
We are proud to share that we have impacted over 40000+ students and
educators in 13 states of the country with our unique experiential
learning programs.
20% Theory
80% practical

3
www.technomentis.com
Table of Contents
Contents
Table of Contents ........................................................................................................ 4
Electronic Circuits ........................................................................................................ 6
Learning Objectives .................................................................................................. 6
Electric Current......................................................................................................... 6
Electrical potential .................................................................................................... 6
Circuit Symbols ........................................................................................................ 7
Class Activity: Will these circuits work? .................................................................... 8
Ohm’s Law ............................................................................................................... 9
Resistance ............................................................................................................... 9
Introduction to Basic components ............................................................................. 9
Light Emitting Diode (LED).................................................................................... 9
Light Dependent Resistor or LDR ....................................................................... 10
Digital Multimeter ................................................................................................ 11
Class Activity: Let’s make and learn simple circuits ............................................... 12
Building on Basics: Hands-On Projects .................................................................. 14
1. Project 1: Anti-Theft Alarm System ................................................................. 14
2. Project 2: Smart Lighting System .................................................................... 16
Robotics ..................................................................................................................... 18
Learning Objectives ................................................................................................ 18
What are Microprocessors? .................................................................................... 18
What is a Microcontroller? ...................................................................................... 18
What is Arduino? .................................................................................................... 18
Different Flavours of Arduino Board: ................................................................... 19
What is a development board? ........................................................................... 19
What is a Signal?? ................................................................................................. 19
Arduino UNO Board ............................................................................................... 20
Arduino Pins ....................................................................................................... 20
Power Pins: ........................................................................................................ 20
Powering the Arduino ............................................................................................. 20
Installing the Arduino IDE ....................................................................................... 21
Windows Install ................................................................................................... 21

4
www.technomentis.com
Driver Installation ................................................................................................ 21
Arduino IDE ........................................................................................................ 22
Opening an Example Sketch: The Blink Example ................................................... 22
Select your board type and port .......................................................................... 22
Upload the program ............................................................................................ 23
Arduino sketch structure ..................................................................................... 23
Variable Types ....................................................................................................... 24
Arduino Programming: BIG 6 CONCEPTS: digitalWrite() ....................................... 24
Experiment 1: Turn ON and turn OFF an LED ........................................................ 25
Experiment 2: LED Blink ......................................................................................... 27
Experiment 3: Building a Skill Tester: using multiple LEDs ..................................... 27
Arduino Programming: BIG 6 CONCEPTS: analogWrite() ...................................... 31
What are PWM Pins?.......................................................................................... 31
Arduino Programming: BIG 6 CONCEPTS: if (conditional) and Comparison Operators
............................................................................................................................... 32
Experiment 4: Fading an LED................................................................................. 32
Arduino Programming: BIG 6 CONCEPTS: Serial Communication ........................ 34
Experiment 5: Serial Monitor: Printing String Value to Serial Monitor...................... 34
Experiment 6: Serial Monitor: Printing Variable Value to Serial Monitor .................. 35
Arduino Programming: BIG 6 CONCEPTS: analogRead()...................................... 35
Experiment 7: Reading Value from LDR and Display on Serial Monitor .................. 36
Experiment 8: Building a night light: using a photocell and LED ............................. 37
Links for Reference ................................................................................................ 39

5
www.technomentis.com
Electronic Circuits Activity 01
YOUNG INNOVATORS PROGRAM

Learning Objectives
• To understand several key concepts of electronic circuits: physics behind circuits, key
components in a circuit and their pervasiveness in our homes and everyday lives!
• To learn concepts like current, voltage, resistance, open and closed circuits, series and parallel
circuits etc.
• To build and investigate some fundamental circuits – understand how they can solve some real
– life problems!

Do you know why a cell phone must have a battery or why a computer must be plugged in to work?
(Answer: Those devices need electricity to work.) Did you know that a battery or the power coming from
the outlet in the wall is part of an electrical circuit?
When a battery is placed in the cell phone or when a computer is plugged in, the circuit in the device is
completed or "closed," allowing electric current to flow.
Circuits can be found all around us — in our homes, schools and businesses. Today we are going to
learn about some of the physics behind circuits and also about some of the key components that are used
to create circuits.

To begin to understand circuits and electricity, let's


start with electric current.

Electric Current is a flow of charge carriers,


usually electrons or electron-deficient atoms. It is
the measure of number of electrons that pass
across a set boundary (the cross section of a wire,
for instance).

The unit of electric current is the unit of charge


Open circuit Closed Circuit
(coulomb) per second, or an Ampere.

Electrical potential is the potential energy per unit charge (joules/coulomb) associated with an electric
field.

Voltage is a measure of the difference in electrical potential between any two points. The unit for voltage
is joules per coulomb (energy per charge), or a Volt (V).

Voltage is similar to the difference in gravitational potential energy for an object due to its height. It is only
useful when we analyse the difference between two points. Any time a voltage measurement is taken, it
is measured between two points.

Voltage also has what is called polarity, or a positive and negative end (similar to a magnet with a north
and south pole). Polarity is important in determining whether an element in a circuit is absorbing energy
or supplying energy.

6
www.technomentis.com
Circuit Symbols
Here are a few most commonly used symbols of electronic circuits for your reference.

Name Symbols

Ground

Cell

Battery

Bulb

Switch On

Switch Off

Resistor

Potentiometer

Buzzer

Transistor (NPN)

Diode

Light Emitting Diode (LED)

Light Dependent Resistor (LDR)

Capacitor

Inductor

7
www.technomentis.com
Class Activity: Will these circuits work?

a. Look at the circuit diagram below.

b. Do you think the bulb(s) will glow? If you do, put a tick in box A under the diagram. If not, put a
cross along with the reason below

8
www.technomentis.com
Ohm’s Law
One main physics relationship used for the analysis of all circuits is known as Ohm's law. Ohm's
Law deals with the relationship between voltage and current in an ideal conductor. This relationship states
that:
The potential difference (voltage) across an ideal conductor is proportional to the current through it
provided the physical conditions i.e. temperature, etc. remain constant.
The constant of proportionality is called the "Resistance", R.
V = IR
where V is the voltage across the resistor, I is the current flowing through the resistor and R is
the resistance of the resistor.
Resistance
Resistance is the measure of an object's ability to restrict the flow of current through it. A resistor is an
object that produces a voltage drop across its terminals in order to resist the flow of electric current
through it.
The unit of resistance is Ohms (Ω). Ohm's law provides us with the definition of a resistor

The relationships described by Ohm's law give us a way to calculate variables like the current
through a resistor, or the voltage across a resistor, in a circuit. Ohm's law is important in the analysis
of circuits when the current or voltage across a resistor is unknown.

Introduction to Basic components

Light Emitting Diode (LED)

A Light Emitting Diode or an LED is a two-lead semiconductor light source which conducts and emits light
when its anode is made more positive (approx. 1.4V) than its cathode.
With reverse polarity, LED stops conducting and emitting light.

Commonly available in green, yellow, red, blue and white color.


Most 3mm and 5mm LEDs will operate close to their peak brightness at a drive current of 20 mA.
Note: Please make sure to put a suitable resistor in LED circuit to limit the current in LED to safe value.

Typical 5mm LED specification:


Forward voltage: 1.8-2.2V
Maximum safe current: 20mA.

9
www.technomentis.com
The operating voltages of different colored LEDs are as follows –

Wavelength Color Operating Voltage

<400 nm Ultraviolet / Purple 3.2 – 3.4 V

400 – 450 nm Violet 2.8 – 4.0 V

450 – 500 nm Blue 3.2 – 3.4 V

500 – 570 nm Green 3.2 – 3.4 V

570 – 590 nm Yellow 2.2 – 2.8 V

590 – 610 nm Orange / Amber 2.2 – 2.8 V

630 – 660 nm Red 2.1 – 2.3 V

850 – 940 nm Infrared ~ 1.2 V

~ 6500 nm White 3.2 – 3.4 V

Light Dependent Resistor or LDR

An LDR offers resistance in response to the ambient light. The


resistance decreases as the intensity of incident light increases,
and vice versa.

In the absence of light, LDR exhibits a resistance of the order of


mega-ohms which decreases to few ohms in the presence of light.

It can act as a sensor, since a varying voltage drop can be obtained


in accordance with the varying light.

Potentiometer
Potentiometer (informally a POT) is a three-terminal resistor with a sliding or rotating contact that forms
an adjustable voltage divider.
If only two terminals are used, one end and the wiper, it acts as a variable resistor or rheostat.
It is often used for dimmers, fan speed control. Common values of pots are 1k, 2k, 5k, 10k… 1Meg etc.

Breadboard
Breadboards are a simple and useful way to quickly assemble and play around with different circuits. It
is often used for prototyping of electronic circuits. Picture below is a commonly used bread board.

10
www.technomentis.com
Figure 1: Breadboard and its connections

Digital Multimeter

A multimeter is a device used to measure


voltage, resistance and current in electronics
& electrical equipment.

Red meter lead is connected to


Voltage/Resistance or amperage port. It is
considered to be the positive connection

Black meter lead is always connected to the


common port. It is considered to be the
negative connection

For measuring any values, start one level


above the expected value range

The most common four usages of a multimeter are as follows:


1. Voltage: To check the potential difference between two points, connect the probes of the
multimeter in parallel with the required component. For example,To measure voltage drop
across a resistor, turn the knob to the expected range of DC Voltage and place the red probe
on pin of the resistor and the black probe on the other pin.

2. Current: To measure the current flowing through a branch, firstly turn the knob to the
expected range of current and then connect the multimeter in series with the component.
While connecting make sure that the black probe connects towards the ground and not the
positive of the battery/source.

3. Resistance: To check the resistance offered by a component, firstly turn the knob to the
expected range of resistance and then connect the probes in parallel with the component.

4. Connectivity: One of the elementary steps in debugging a circuit is to make sure all the
components are correctly connected and that no two components are incorrectly
connected. If the knob is turned towards the continuity segment and the tips of both the
probes are touched with each other, a small beep sound can be heard. So, if there is a
connection between two terminals of components and if probes are also placed on the
terminals individually, a beep sound can be heard.

11
www.technomentis.com
Class Activity: Let’s make and learn simple circuits

Let us explore the components practically by creating simple circuits and understanding how they work.
Kindly note down your observations with each experiment.

Materials Required:
1. Breadboard x 1
2. Jumper wires x 5
3. 9V battery x 1
4. LDR x 1
5. Potentiometer x 1
6. 470Ω resistor x 1
7. 220Ω resistor x 1
8. DC motor x 1

CIRCUIT ON BREADBOARD CORRESPONDING CIRCUIT DIAGRAM


1. Single LED circuit

2. Series connection with LEDs

3. Parallel connection with LEDs


12
www.technomentis.com
4. LDR with LEDs

5. LED fading using potentiometer Ω

6. Controlling motor speed with


potentiometer

13
www.technomentis.com
Building on Basics: Hands-On Projects

1. Project 1: Anti-Theft Alarm System

Overview:
Anti - Theft Home Security System is an apparatus when fixed at home will restrict the entry of any
unauthorized person. Therefore, as soon as a thief/unauthorized person opens the door, the buzzer will
start buzzing alerting everyone in the house.

In this system, we use LDR to detect security threat or theft attempt. When we leave the house, we
activate the system by switch.

When someone tries to break open the door, buzzer goes on alarming the presence of intruder (i.e. the
light from LED falls on LDR changing its resistivity, triggering the buzzer)

Materials Required:
1. Bread board x 1
2. 5mm LED x 1
3. 9V Battery x 1
4. Buzzer x 1
5. LDR x 1
6. 470 Ω resistor x 1
7. Jumper Leads (M-M, M-F)

Circuit Diagram:

Construction:
1. Take the PCB provided.
2. Then place the switch on the board with all its pins inserting into the board.
3. Take the 9v battery and connect its positive terminal to 1st pin of the switch and solder it.
4. Place the buzzer on the board with both its terminal pins inserting the board.
5. Connect the negative terminal of battery to the negative terminal of buzzer and to negative terminal of
LED. Place the LDR on the PCB and connect one of its terminals to the positive terminal of the buzzer
and solder it.
6. Connect the other terminal of the LDR to the 3 rd pin of the Switch and solder it
7. Now place a resister and connect its one terminal to the 3 rd pin of switch and other terminal to the
positive terminal of LED and solder properly.

14
www.technomentis.com
LED and LDR should be positioned in front of each other, as in picture above.

Circuit Design on Breadboard:

After soldering, your circuit will look like this –

Now design a House for your Alarm System or a Safety Locker

Please note that a piece of door (sunboard) should come


between LDR & LED when the door is close.

15
www.technomentis.com
2. Project 2: Smart Lighting System

Overview:
In this project, we will make a smart lighting system in which we turn ON an LED when the surrounding
is dark and turn OFF the LED when there is enough external light.

Materials Required:
➢ Bread board x 1
➢ 5mm LED x 1
➢ 9V Battery x 1
➢ PN2222A transistor x 1
➢ LDR x 1
➢ 8.2K Ω resistor x 1
➢ 470 Ω resistor x 1
➢ Jumper Leads (M-M, M-F)

Circuit Diagram:

Circuit Diagram on Breadboard –

NPN

What value of resistor is suitable for the circuit?


The voltage drop across LED is 2V and the maximum current for LED is 20mA, which means the potential
drop across resistor should be 7V (if we use 9V battery) and the current is 20mA.
20 𝑉 7×1000
Since: = 𝐼𝑅 ; 𝑉 = 7𝑉; 𝐼 = 20𝑚𝐴 = 1000 𝐴 ; 𝑅 = => 𝑅 = ; ∴ 𝑅 = 350Ω
𝐼 20

16
www.technomentis.com
So, a minimum resistance of 350Ω is necessary. That is why we have used a resistance of 470Ω.

Role of Transistor:
The PN2222A transistor is an NPN transistor. It has three legs – (emitter, base and collector). In this
experiment, it is used as a switch.
When the base pin of the transistor is supplied with a voltage greater than 0.7V, it allows conduction from
collector to emitter. This completes LED circuit.

Wiring:
1. Place the PN2222A transistor on the bread board with the flat side facing you. Now, the pins of the
transistor are in the order of Emitter, Base and Collector (from left to Right).
2. Connect the cathode of the LED to the collector.
3. Connect the anode of the LED to one end of the 470 Ω resistor.
4. Connect the other end of the 470 Ω resistor to the positive terminal of the Battery
5. Connect the emitter to the negative terminal of the battery.
6. Connect one end of the 8.2𝐾 Ω resistor to the positive terminal of the battery.
7. Connect the other of the 8.2𝐾 Ω resistor to one leg of the LDR.
8. Connect the base of the PN2222A transistor to the junction of the 8.2𝐾Ω resistor and the LDR
9. Connect the other end of the LDR to the negative terminal of the battery.

Construction on PCB:
1. Place the PN2222A transistor on the PCB with the flat side facing you and solder all the three terminals
on to the PCB
2. Place the cathode of the LED near the collector and solder both pins.
3. Place the anode of the LED near one end of the 470 Ω resistor and solder both pins.
4. Solder the other end of the 470 Ω resistor with the positive terminal of the Battery.
5. Solder the emitter with the negative terminal of the battery.
6. Solder one end of the 8.2𝐾 Ω resistor to the positive terminal of the battery.
7. Solder the other of the 8.2𝐾 Ω resistor with one leg of the LDR and make sure that this junction falls
near the base of the transistor
8. Solder the junction and the base.
9. Solder the other end of the LDR with the negative terminal of the battery

17
www.technomentis.com
Robotics Activity 02
YOUNG INNOVATORS PROGRAM

Learning Objectives
➢ To introduce Arduino as an easy tool to build simple electronics projects and automate things
➢ Fundamentals of programming: How to make and program a simple bot
➢ Encourage students to tinker, solve some simple problems by building something of their own.

What are Microprocessors?


The microprocessor is the heart of any normal computer, whether it is a desktop machine, a server or
a laptop.
A microprocessor -- also known as a CPU or central processing unit -- is a complete computation engine
that is fabricated on a single chip.
The first microprocessor was the Intel 4004, introduced in 1971. The 4004 was not very powerful -- all it
could do was add and subtract, and it could only do that 4 bits at a time.
The microprocessor can be programmed to perform functions on given data by writing specific instructions
into its memory.

What is a Microcontroller?
A small computer on a single chip containing a processor, memory, and input/output. Typically,
"embedded" inside some device that they control. A microcontroller is often small and low cost.

What is Arduino?
When talking about Arduino as a platform, it is important to understand that this refers to both hardware
and software.
Arduino hardware is a series of development boards that come in a variety of models depending on the
needs of the user. Most commonly used Arduino boards include the Arduino Uno, Arduino Mega and
Arduino Leonardo.

18
www.technomentis.com
There are also “Arduino-Compatible” boards that share the capabilities of “official” Arduino boards, but
often feature expanded functionality. The Arduino UNO is an Arduino-compatible development board,
sharing some features and capabilities of the Arduino Uno R3 development board.
Arduino’s software refers to the Arduino Integrated Development Environment (IDE), a computer program
used to load programs onto an Arduino development board. Users write programs for Arduino hardware
using an amalgamated version of the C and C++ programming languages. This language is also referred
to as Embedded programming.
The Arduino IDE runs on Windows, OSX and Linux machines. The programs can be uploaded using a
cable (such as USB) which connects the Arduino hardware to your computer.

Different Flavours of Arduino Board:


➢ Arduino Uno
➢ Arduino Leonardo
➢ Arduino LilyPad
➢ Arduino Mega
➢ Arduino Nano
➢ Arduino Mini
➢ Arduino Mini Pro
➢ Arduino BT

What is a development board?


A development board is a piece of hardware, featuring a microcontroller built on to a single printed circuit
board. This hardware allows users to create devices that interact with their environment using input
sensors and output actuators.
After being programmed by the user, microcontrollers can perform specific tasks, depending on the
capabilities of the board itself. Some development boards also include microprocessors, so they are
effectively small computers without the peripherals.
Because of their small size and relatively low-cost, development boards such as the Arduino UNO and
Arduino Mega became popular in recent years. They allow users of all levels of experience the chance
to understand electronics in a physical capacity, tinker and experiment with making their own devices.

What is a Signal??
Signal is a function that conveys information about the behaviour or attributes of some phenomenon.
Mostly they are of two types: Analog and Digital

Analog Signals Digital Signal


An Analog or analogue signal is one which is A Digital signal refers to an electrical signal that is
continuously variable. converted into a pattern of bits.
It may have positive or negative as min /max values. It must have finite set of possible values.
May be periodic or non-periodic. Eg. Most commonly digital signals will be one of two
Eg. Voltage varying with time values – like either 0V or 5V. Timing graphs of these
Eg. Sound from human voice signals look like square waves
Eg. HDMI video and audio signals

Analog Digital

19
www.technomentis.com
Arduino UNO Board

It is a microcontroller board based on the ATmega328.


It consists of:
➢ Atmel ATmega 328P microcontroller
➢ 14 digital input/output pins (of which 6 pins can be used as PWM outputs)
➢ 6 analog inputs
➢ Voltage regulators
➢ 16 MHz resonator
➢ USB connection
➢ Power Jack
➢ Reset button
➢ LEDs

Arduino Pins
The pins are the places where you connect wires to construct a circuit (probably in conjunction with a
breadboard and some wire.
They have black plastic ‘headers’ that allow you to just plug a wire right into the board.
Several kinds of pins are:
1. Power
2. Analog
3. Digital
4. PWM
5. GND

Power Pins:
The board can be supplied with power either from the DC power jack (7- 12V), the USB connector (5V),
or the VIN pin of the board (7-12V).
1. VIN - The input voltage to the Arduino board when it's using an external power source.
2. 5V - This pin outputs a regulated 5V from the regulator on the board.
3. 3V3 - A 3.3 volts supply generated by the on-board regulator. This is also called TTL voltage or
Transistor-Transistor Logic voltage
4. GND - Ground pins.

Powering the Arduino


The Arduino Uno can be powered via the USB connection or with an external power supply.
External (non-USB) power can come either from an AC-to-DC adapter or battery. Leads from a battery
can be inserted in the Gnd and Vin pin headers of the POWER connector. Power can also be fed by the
banana jack next to the USB port.

20
www.technomentis.com
The board can operate on an external supply of 6 to 20 volts.
If supplied with less than 7V, however, the 5V pin may supply less than five volts and the board may be
unstable. If using more than 12V, the voltage regulator may overheat and damage the board. The
recommended range is 7 to 12 volts.
Connect your Uno board with an A B USB cable; sometimes this cable is called a USB printer cable.
The USB connection with the PC is necessary to program the board and not just to power it up. The Uno
automatically draw power from either the USB or an external power supply. Connect the board to your
computer using the USB cable. The green power LED (labelled PWR) should go on.

Installing the Arduino IDE


Both Mac and Windows versions of the IDE software are delivered in a ZIP format. You’ll need to unzip
that archive in order to use the software but be careful where and how you unzip it!

Windows Install
Windows users should unzip the file to the top directory of your drive (e.g. C:/). You can leave the
unzipped directory as named (arduino-1.8.0), or rename it to something descriptive, just make sure there
are no spaces in the name of the directory.

Examples Windows directory structure. The Arduino 1.8.0 software is installed at the top level (C:/) of the
drive.
Double-click, or run Arduino.exe to open up the Arduino IDE.
Driver Installation
If you used the Installer, Windows - from XP up to 10 - will install drivers automatically as soon as you
connect your board.
If you downloaded and expanded the Zip package or, for some reason, the board wasn't properly
recognized, please follow the procedure below.

1. Click on the Start Menu and open the Control Panel.


2. While in the Control Panel, navigate to System and Security. Next, click on System. Once the
System window is up, open the Device Manager.
3. Look under Ports (COM & LPT). You should see an open port named "Arduino UNO (COMxx)".
If there is no COM & LPT section, look under "Other Devices" for "Unknown Device".
4. Right click on the "Arduino UNO (COMxx)" port and choose the "Update Driver Software" option.
5. Next, choose the "Browse my computer for Driver software" option.

21
www.technomentis.com
6. Finally, navigate to and select the driver file named "arduino.inf", located in the "Drivers" folder of
the Arduino Software download (not the "FTDI USB Drivers" sub-directory). If you are using an
old version of the IDE (1.0.3 or older), choose the Uno driver file named "Arduino UNO.inf"
7. Windows will finish up the driver installation from there.

Arduino IDE

Opening an Example Sketch: The Blink Example

Select your board type and port


You'll need to select the entry in the Tools > Board menu that corresponds to your Arduino board.

Select the serial device of the board from the Tools | Serial Port menu. This is likely to be COM3 or higher
(COM1 and COM2 are usually reserved for hardware serial ports). To find out, you can disconnect your
board and re-open the menu; the entry that disappears should be the Arduino or Genuino board.
Reconnect the board and select that serial port.

22
www.technomentis.com
Upload the program
Now, simply click the "Upload" button in the environment. Wait a few seconds - you should see the RX
and TX LEDs on the board flashing. If the upload is successful, the message "Done uploading." will
appear in the status bar.

A few seconds after the upload finishes, you should see the pin 13 (L) LED on the board start to blink (in
orange). If it does, congratulations! You've gotten Arduino or Genuino up-and-running.

Arduino sketch structure


void setup()
➢ Will be executed only when the program begins (or reset button is pressed)

void loop()
➢ Will be executed repeatedly

23
www.technomentis.com
Variable Types

Operators
➢ = is used to assign a value
➢ == is used to compare values
➢ && is “and”
➢ || is “or”

Arduino Programming: BIG 6 CONCEPTS: digitalWrite()

digitalWrite()

analogWrite()

If() Statements

analogRead()

Serial Communication

digitalRead()

Digital Pins:
➢ A digital signal is a signal that has only two states. ON/HIGH or OFF/LOW
➢ To receive or send Digital signals, it uses Digital pins 0 - 13. You may also setup your Analog
pins to act as Digital pins.
➢ Digital Pins 3, 5, 6, 9, 10 and 11 have PWM capabilities.
➢ Examples of Digital: Sensors/Interfaces: Buttons, Switches, Relays, CDs, etc....
➢ Digital pins can be used for both digital input (like telling if a button is pushed) and digital output
(like powering an LED).

Digital I/O

pinMode(pin, mode)
Sets pin to either INPUT or OUTPUT

digitalRead(pin)
Reads HIGH or LOW from a pin

24
www.technomentis.com
digitalWrite(pin, value)
Writes HIGH or LOW to a pin

Experiment 1: Turn ON and turn OFF an LED


In this activity, we will learn how to turn ON and turn OFF an LED with the Arduino UNO

Materials Required
1. 5mm LED - #1
2. Breadboard-#1
3. Arduino UNO board -#1
4. Arduino UNO software - #1
5. 470 Ω Resistor- #1
6. Jumper Leads (M-M) -#2

Wiring Instructions
LEDs have a negative and a positive lead and current only flows one way. The convention is that the
shorter lead is the negative lead while the longer lead is the positive lead.

Connect the negative (shorter) lead of the LED to a resistor by twisting them together and connect the
other end of the resistor to GND next to Pin 13.

Connect the positive (longer) lead of the LED to Pin 13. Make sure there is no touching between the
resistor and the positive lead of the LED.

Command required for Turning LED Off


To Turn the LED OFF, we need to supply 0V at Pin 13. Therefore, we write:

digitalWrite(13, LOW); //This command sets the voltage at Pin 13 to be 0v and hence the LED turns OFF

Program required for Glowing LED


void setup() { // setup routine runs only once
pinMode(13, OUTPUT); // sets the pin13 as OUTPUT
}
void loop() { // loop routine runs repeatedly
digitalWrite(13, HIGH); // sets the pin 13 with digital HIGH(+ 5V)
}

25
www.technomentis.com
Explanation of the Arduino Code
In Arduino programs, there are always two functions: setup() and loop().

Setup()
The setup() function runs once when you load a new program or press the reset button.

It is represented by the code:

void setup()
{
}

Loop()
The loop() function runs after the setup function and runs over and over again forever. It is represented
by the code:

void loop()
{
}

pinMode(pin,INPUT/OUTPUT)
The digital pins on the Arduino, (0 – 13) located next to the USB slot can be set to be either output or
input pins using the command, pinMode().
The pinMode function expects two inputs inside the parentheses: the first input is the number of the
Arduino pin while the second input is either OUTPUT or INPUT.

digitalWrite(pin,HIGH/LOW)
To set an output on the Arduino we use the command digitalWrite. It also takes two inputs: the first input
is the number of the Arduino pin to write to and the second input is whether to write HIGH or LOW.

A note on comments
Comments are lines of script inside an Arduino sketch that provide notes to yourself or other users. These
can be about the program’s functionality, any issues with the code, or anything else you would like a user
to understand about your program
Comments are ignored by the Arduino IDE and are not uploaded to the Arduino UNO hardware.
Comments are often signified with the symbols “//” or “/* */” and can be written as follows:
// This is a single line comment. Anything after the slashes is a comment
// to the end of the line
/* this is a multiline comment
Use it to comment out whole blocks of code */

Commands required for Turning LED Off


To Turn the LED OFF, we need to supply 0V at Pin 13. Therefore, we write:
digitalWrite(13, LOW); //This command sets the voltage at Pin 13 to be 0v and hence the LED turns OFF

Program required for Turning LED Off


void setup() { // setup routine runs only once
pinMode(13, OUTPUT); // sets the pin13 as OUTPUT
}
void loop() { // loop routine runs repeatedly
digitalWrite(13,LOW); // sets the pin 13 with digital LOW(0V)
}

26
www.technomentis.com
Experiment 2: LED Blink

In this activity, we will learn how to blink an LED using Arduino code. Turns on an LED on for one second,
then off for one second, repeatedly.

Materials Required
➢ 1 x 5mm LED
➢ 1 x Breadboard
➢ 1 x Arduino UNO board
➢ 1 x Arduino UNO software
➢ 1 x 470 Ω Resistor
➢ 2 x Jumper Leads (M-M)

Led Blink Program


void setup()
{ // the setup routine runs once when you press reset:
pinMode(13,OUTPUT); // initialize the digital pin 13 as an output.
}
void loop() // Whatever code you want to run over and over again
{
digitalWrite(13,HIGH); // Giving 5v to pin number 13
delay(1000); // Wait for 1 sec. The LED connected to pin 13 will be on for 1 sec
digitalWrite(13,LOW); //Giving 0v to pin number 13. Turning off LED
delay(1000); // Waiting for 1 second. The LED will be off for this much time
} // As the above code is written under loop(), The LED will have the blinking effect.

Explanation of the Code:


The delay command makes the program wait for a certain number of milliseconds before moving onto
the next command. Thus, delay(1000); instructs the code to wait for 1 second.

Experiment 3: Building a Skill Tester: using multiple LEDs


Overview:
Let us try out following experiments with multiple LEDs

I. Program to BLINK 3 LEDS at the same time


II. Program for left shift
III. Program for right shift
IV. Program to display the binary equivalent number of 0,1 ,2,3,4,5,6 and 7

Materials Required
1. 1 x Arduino UNO board
2. 1 x Arduino Programming Cable
3. 1 x Breadboard
4. 4 x Jumper leads (M-M)
5. 3 x 5mm LEDs (choose colours to suit)
6. 3 x 470 Ω Resistors

Wiring
Wire up the LEDs and 470 Ω pull-down resistors as per the diagram.
➢ To do this, connect the negative (shorter) lead of the LED to one end of the 470 Ω resistor.
➢ Connect the other end of the resistor to the ground rail.
➢ Connect the longer positive leads of the LEDs (3 LEDs in a row) to pins 3, 4 and 5.

27
www.technomentis.com
Programs
Open the Arduino IDE. Check that the COM port assigned to the Arduino board (go to Device Manager
to check), matches the correct COM port according to the IDE:
Check also that the correct board has been selected.

Program to BLINK 3 LEDS at the same time

Please read the comments for the explanation of every single line of the program.

void setup()
{ // put your setup code here, to run once:
pinMode(3, OUTPUT); // Initializing pin 3 which is connected to first LED as output
pinMode(4, OUTPUT); // Initializing pin 4 which is connected to second LED as output
pinMode(5, OUTPUT); // Initializing pin 5 which is connected to third LED as output
}
void loop() { // put your main code here, to run repeatedly:
digitalWrite(3,HIGH); // supplying 5v to the pin 3 i.e. switching the first LED ON.
digitalWrite(4,HIGH); // supplying 5v to the pin 4 i.e. switching the second LED ON.
digitalWrite(5,HIGH); // supplying 5v to the pin 5 i.e. switching the third LED ON.
delay(500); /* Giving small amount of delay. The LED’s will be on until the time
in the delay */
digitalWrite(3,LOW); // supplying 0v to the pin 3 i.e. switching the first LED OFF.
digitalWrite(4,LOW); // supplying 0v to the pin 4 i.e. switching the second LED OFF.
digitalWrite(5,LOW); // supplying 0v to the pin 5 i.e. switching the third LED OFF.
delay(500); /* Giving small amount of delay. The LED’s will be on until the time in
the delay */
}
Program for left shift
Please read the comments for the explanation of every single line of the program.

In this program, we want to shift the glowing of LED from right to left. So, in this, firstly, right most led will
be on. Then the middle led and then left most LED. If we keep the delay low, then it will represent Left
Shift.

void setup() { // put your setup code here, to run once:


pinMode(3, OUTPUT); //Initialize pin 3 which is connected to LED 1 as output
pinMode(4, OUTPUT); // Initializing pin 4 which is connected to LED 2 as output
pinMode(5, OUTPUT); // Initializing pin 5 which is connected to LED 3 as output
}

28
www.technomentis.com
void loop() { // put your main code here, to run repeatedly:
digitalWrite(3, LOW); // supplying 0v to the pin 3 i.e. switching the left most LED OFF.
digitalWrite(4, LOW); // supplying 0v to the pin 4 i.e. switching the middle LED OFF
digitalWrite(5, LOW); // supplying 0v to the pin 5 i.e. switching the right most LED OFF.
delay(200); /*Supplying a delay of 200ms so that the next part of the program is not executed until
this delay time */
// initially all the LED’s are off
digitalWrite(3, LOW); //supplying 0v to the pin 3 i.e. switching the left most LED OFF.
digitalWrite(4, LOW); //supplying 0v to the pin 4 i.e. switching the middle LED OFF
digitalWrite(5, HIGH); // supplying 5v to the pin 5 i.e. switching the right most LED ON.
delay(200); //Now only rightmost LED is on. Rest all are OFF.
digitalWrite(3, LOW); //supplying 0v to the pin 3 i.e. switching the left most LED OFF.
digitalWrite(4, HIGH); //supplying 5v to the pin 4 i.e. switching the middle LED ON
digitalWrite(5, LOW); // supplying 0v to the pin 5 i.e. switching the right most LED OFF.
delay(200); //Now only middle LED is on. Rest all are OFF.
digitalWrite(3,HIGH); //supplying 5v to the pin 3 i.e. switching the left most LED ON.
digitalWrite(4,LOW); //supplying 0v to the pin 4 i.e. switching the middle LED OFF
digitalWrite(5,LOW); // supplying 0v to the pin 5 i.e. switching the right most LED OFF.
delay(200); //Now only leftmost LED is on. Rest all are OFF.
}

Program for Right Shift


Please read the comments for the explanation of every single line of the program.

In this program, we want to shift the glowing of LED from left to right. So, in this, firstly, left most led will
be on. Then the middle led and then right most LED. If we keep the delay low, then it will represent Right
Shift.

void setup() { // put your setup code here, to run once:


pinMode(3, OUTPUT); //Initializing pin 3 which is connected to first LED as output
pinMode(4, OUTPUT); /* Initializing pin 4 which is connected to second LED as output */
pinMode(5, OUTPUT); // Initializing pin 5 which is connected to third LED as output
}
void loop() { // put your main code here, to run repeatedly:
digitalWrite(3, LOW); /*supplying 0v to the pin 3 i.e. switching the left most LED OFF. */
digitalWrite(4, LOW); //supplying 0v to the pin 4 i.e. switching the middle LED OFF
digitalWrite(5, LOW); /* supplying 0v to the pin 5 i.e. switching the right most LED OFF. */
delay(200); //Supplying a delay of 200ms so that the next part of the program is not executed
// Initially all the LED’s are off
digitalWrite(3, HIGH); /*supplying 5v to the pin 3 i.e. switching the left most LED ON. */
digitalWrite(4, LOW); //supplying 0v to the pin 4 i.e. switching the middle LED OFF
digitalWrite(5, LOW); /* supplying 0v to the pin 5 i.e. switching the right most LED OFF. */
delay(200);
//Now only leftmost LED is on. Rest all are OFF.
digitalWrite(3, LOW); /* supplying 0v to the pin 3 i.e. switching the left most LED OFF. */
digitalWrite(4, HIGH); //supplying 5v to the pin 4 i.e. switching the middle LED ON
digitalWrite(5, LOW); /* supplying 0v to the pin 5 i.e. switching the right most LED OFF. */
delay(200); //Now only middle LED is on. Rest all are OFF.
digitalWrite(3,LOW); //supplying 0v to the pin 3 i.e. switching the left most LED OFF.
digitalWrite(4,LOW); //supplying 0v to the pin 4 i.e. switching the middle LED OFF
digitalWrite(5,HIGH); // supplying 5v to the pin 5 i.e. switching the right most LED ON.
delay(200); //Now only rightmost LED is on. Rest all are OFF.
}

29
www.technomentis.com
Program to display the binary equivalent number of 0,1 ,2,3,4,5,6 and 7

The three LED’s attached represents binary numbers. If the LED is on, the binary number is 1 and if the
LED is off, the binary number is 0. So, we need to display the binary equivalent of decimal number 0 to
7. So, according to the table below, if we want to represent 0 in binary, then all the LED’s should be off.
Similarly, for 1, only the rightmost LED Should be ON and rest all should be off and so on.

Number Equivalent

void setup() { // put your setup code here, to run once:


pinMode(3, OUTPUT); // Initializing pin 3 which is connected to first LED as output
pinMode(4, OUTPUT); // Initializing pin 4 which is connected to second LED as output
pinMode(5, OUTPUT); // Initializing pin 5 which is connected to third LED as output
}
void loop() { // put your main code here, to run repeatedly:
digitalWrite(3, LOW); //supplying 0v to the pin 3 i.e. switching the left most LED OFF.
digitalWrite(4, LOW); //supplying 0v to the pin 4 i.e. switching the middle LED OFF
digitalWrite(5, LOW); // supplying 0v to the pin 5 i.e. switching the right most LED OFF.
delay(2000); //Supplying a delay of 2000ms
// In this all the LED’s are off. So, the number represented by each LED is 000 which is 0 in Decimal
digitalWrite(3, LOW); //supplying 0v to the pin 3 i.e. switching the left most LED OFF.
digitalWrite(4, LOW); //supplying 0v to the pin 4 i.e. switching the middle LED OFF
digitalWrite(5, HIGH); /* supplying 5v to the pin 5 i.e. switching the right most LED ON. */
delay(2000);
//Now only rightmost LED is ON. Rest all are OFF. So equivalent Binary number is 001 i.e. 1 in Decimal
digitalWrite(3, LOW); //supplying 0v to the pin 3 i.e. switching the left most LED OFF.
digitalWrite(4, HIGH); //supplying 5v to the pin 4 i.e. switching the middle LED ON
digitalWrite(5, LOW); /* supplying 0v to the pin 5 i.e. switching the right most LED OFF. */
delay(2000);
//Now only middle LED is ON. Rest all are OFF. So equivalent Binary number is 010 i.e. 2 in Decimal
digitalWrite(3, LOW); //supplying 0v to the pin 3 i.e. switching the left most LED OFF.
digitalWrite(4, HIGH); //supplying 5v to the pin 4 i.e. switching the middle LED ON
digitalWrite(5, HIGH); /* supplying 5v to the pin 5 i.e. switching the right most LED ON. */
delay(2000);

30
www.technomentis.com
//Now middle and rightmost LED’s are ON. So equivalent Binary number is 011 i.e. 3 in Decimal
digitalWrite(3, HIGH); //supplying 5v to the pin 3 i.e. switching the left most LED ON.
digitalWrite(4, LOW); //supplying 0v to the pin 4 i.e. switching the middle LED OFF
digitalWrite(5, LOW); // supplying 0v to the pin 5 i.e. switching the right most LED OFF.
delay(2000);
//Now only leftmost LED is ON. Rest all are OFF. So equivalent Binary number is 100 i.e. 4 in Decimal
digitalWrite(3, HIGH); //supplying 5v to the pin 3 i.e. switching the left most LED ON.
digitalWrite(4, LOW); //supplying 0v to the pin 4 i.e. switching the middle LED OFF
digitalWrite(5, HIGH); // supplying 5v to the pin 5 i.e. switching the right most LED ON.
delay(2000);
//Now leftmost and rightmost LED’s are ON. That means equivalent Binary number is 101 i.e. 5 in Decimal
digitalWrite(3, HIGH); //supplying 5v to the pin 3 i.e. switching the left most LED ON.
digitalWrite(4, HIGH); //supplying 5v to the pin 4 i.e. switching the middle LED ON
digitalWrite(5, LOW); // supplying 0v to the pin 5 i.e. switching the right most LED OFF.
delay(2000);
//Now leftmost and middle LED’s are ON. That means equivalent Binary number is 110 i.e. 6 in Decimal
digitalWrite(3, HIGH); //supplying 5v to the pin 3 i.e. switching the left most LED ON.
digitalWrite(4, HIGH); //supplying 5v to the pin 4 i.e. switching the middle LED ON
digitalWrite(5, HIGH); // supplying 5v to the pin 5 i.e. switching the right most LED ON.
delay(2000);
//Now all the LED’s are ON. That means equivalent Binary number is 111 i.e. 7 in Decimal
}

Arduino Programming: BIG 6 CONCEPTS: analogWrite()

digitalWrite()

analogWrite()

If() Statements

analogRead()

Serial Communication

digitalRead()

What are PWM Pins?


Pin numbers 3, 5, 6, 9, 10, and 11 on the Arduino UNO.

These pins act as normal digital pins but can also be used for something called Pulse-Width Modulation
(PWM). It is a technique for getting analog results with digital means.

Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern
can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the
signal spends on versus the time that the signal spends off.

31
www.technomentis.com
The duration of "on time" is called the pulse width. To get varying analog values, you change, or modulate,
that pulse width. You can’t use digital pins to directly supply say 2.5V, but can pulse the output on and off
really fast to produce the same effect. The on-off pulsing happens so quickly, the connected output
device “sees” the result as a reduction in the voltage.

Arduino Programming: BIG 6 CONCEPTS: if (conditional) and Comparison


Operators
if, which is used in conjunction with a comparison operator, tests whether a certain condition has been
reached, such as an input being above a certain number. The format for an if test is:

if (some variable > some value)


{
//then do this;
}

Comparision Operators
➢ x == y (x is equal to y)
➢ x != y (x is not equal to y)
➢ x < y (x is less than y)
➢ x > y (x is greater than y)
➢ x <= y (x is less than or equal to y)
➢ x >= y (x is greater than or equal to y)

Experiment 4: Fading an LED

Overview:
In this activity, we will learn how to fade an LED using the analogWrite() function.

Materials Required
1. 1 x 5mm LED
2. 1 x Breadboard
3. 1 x Arduino UNO board
4. 1 x Arduino UNO software
5. 1 x 470 Ω Resistor
6. 2 x Jumper Leads (M-M)

32
www.technomentis.com
Wiring
• Connect the negative (shorter) lead of the LED to a resistor by twisting them together and connect
the other end of the resistor to GND next to Pin 3 which has a ‘ ~ ’ sign.
• Connect the positive (longer) lead of the LED to Pin 3. Make sure there is no touching between
the resistor and the positive lead of the LED.

Program 1:
int i;
void setup() { // setup runs only once
pinMode(3,OUTPUT); // configures pin 3 to be the OUTPUT
}
void loop() { // loop routine runs over and over again
for(i=255 ; i<=0; i-5) // initialization of for loop
{
analogWrite(3,i); // PWM signals defined by variable ‘i’ is sent as output on pin 3
delay(50); // sets a delay of 50 millisecond
} // end of for loop statement
} // end of loop routine

Explanation of the code:


The code starts by declaring a variable of integer data type and assigning its value as zero. You may
have noticed that the variable ‘i’ is declared outside the ‘setup’ routine or the ‘loop’ routine. This type of
variable is known as ‘Global variable’.

A global variable can be accessed in any routines and can be used anywhere in the program.

In the ‘setup’ routine, pin 3 is configured to be the output. The ‘analogWrite’ function sends out a PWM
(Pulse Width Modulated) signal at pin – 3. The range of 0-255 gives an output with voltage equivalent
from 0V-5V.

For loop:

For loop repeats the process “n” number of times. There are three different parameters used as input by
“for” loop. All three are separated by “;”. First one is an initializer which initializes from where to start the
loop. Second one is the condition where loop should stop and the third one represents the increment or
decrement in the variable (on which loop is applied).

33
www.technomentis.com
Arduino Programming: BIG 6 CONCEPTS: Serial Communication

digitalWrite()

analogWrite()

If() Statements

Serial Communication

analogRead()

digitalRead()

Experiment 5: Serial Monitor: Printing String Value to Serial Monitor

Overview
This activity helps us to understand the use of Serial Monitor and Serial Communication. In this, we will
print the value of a string onto the Serial Monitor.
Materials Required
➢ 1 x Arduino UNO board
➢ 1 x Arduino Programming Cable
Program
Open the Arduino IDE. Check that the COM port assigned to the Arduino UNO, matches the correct COM
port according the IDE.
Check also that the correct Arduino board has been selected.

34
www.technomentis.com
void setup() {
Serial.begin(9600); // Starting Serial Communication.
}
void loop() {
Serial.println("Hello World"); // Printing String Value to Serial Device
delay(2000); // Providing Delay of 2 Sec.
}

Experiment 6: Serial Monitor: Printing Variable Value to Serial Monitor


Overview
This activity helps us to understand the use of Serial Monitor and Serial Communication. In this, we will
print the value of a string onto the Serial Monitor.

Materials Required
➢ 1 x Arduino UNO board
➢ 1 x Arduino Programming Cable

Program
Open the Arduino IDE. Check that the COM port assigned to the Arduino UNO, matches the correct COM
port according the IDE.
Check also that the correct Arduino board has been selected.
void setup() {
Serial.begin(9600); // Starting Serial Communication.
}
void loop() {
int y=1000; // Initializing Variable and storing Value in it
Serial.println(y); // Printing Value of y in Serial Monitor
}

Arduino Programming: BIG 6 CONCEPTS: analogRead()

digitalWrite()

analogWrite()

If() Statements

Serial Communication

analogRead()

digitalRead()

Analog Pins:
➢ Pins under the ‘AnalogIn’ label (A0 through A5 on the UNO) are Analog Input pins.

35
www.technomentis.com
➢ These pins can read the signal from an analog sensor (like a temperature sensor) and convert it
into a digital value that we can read.
➢ Analog Input values range from 0 to 1023 (1024 values because it uses 10 bits, 210)

Digital has two values: on and off. Analog has many (infinite) values. Computers don’t do analog, they
quantize

Experiment 7: Reading Value from LDR and Display on Serial Monitor


Overview
This activity combines the use of a photocell (light-dependent resistor) as an input to the Arduino UNO
board. We will read the value coming from analog Input and Print it on Serial Monitor. Programming code
is minimal and is easy to do.

Materials Required
1. 1 x Arduino UNO board
2. 1 x Arduino Programming Cable
3. 1 x Breadboard
4. 4 x Jumper leads (M-M)
5. 1 x Photocell
6. 1 x 470 Ω Resistor

Wiring
➢ Connect one end of LDR to 5V on the board using male to male connecting wire.
➢ Connect the other end of the photocell to the resistor.
➢ Connect the other end of the resistor to the ground pin using male to male connecting wire
➢ Connect the end attached to resistor of the LDR to A0 on the board using male to male connecting
wire.

36
www.technomentis.com
Program
Open the Arduino IDE. Check that the COM port assigned to the Arduino board (go to ‘Device Manager’
to check), matches the correct COM port according the IDE.
Check also that the correct Arduino board has been selected.

void setup() {
Serial.begin(9600); // Starting Serial Communication. Print the data on Serial //Monitor //that
we get from LDR
pinMode(A0, INPUT); // Initializing analog Pin A0 as INPUT
}
void loop() {
int y=analogRead(A0); //analogRead(); is used to read the analog data from analog pins.
//Storing the data from LDR to a variable whose data type is integer
Serial.println(y); // Printing the value of variable y on Serial Monitor
delay(500);
}

Experiment 8: Building a night light: using a photocell and LED


Overview
This activity combines the use of a photocell (light-dependent resistor) as an input to the Arduino UNO
board with an LED as an output, to operate when in dark and turn off when in light (a night light).
Programming code is minimal and is easy to do.

Materials Required
1. 1 x Arduino UNO board
2. 1 x Arduino Programming Cable
3. 1 x Breadboard
4. 4 x Jumper leads (M-M)
5. 1 x Photocell
6. 1 x 5mm red LED
7. 2 x 470 Ω Resistor

Wiring
➢ Connect positive lead of LED to Pin 3 which is the PWM Pin.
➢ Connect a resistor on the negative lead, connecting the other end of the resistor to GND.
➢ Connect one end of LDR to 5V on the board using male to male connecting wire.
➢ Connect the other end of the photocell to the resistor.
➢ Connect the other end of the resistor to the ground pin using male to male connecting wire
➢ Connect the end attached to resistor of the LDR to A0 on the board using male to male connecting
wire.

37
www.technomentis.com
Program

void setup() {
Serial.begin(9600); // Starting Serial Communication. Print the data we get from LDR on Serial
Monitor
pinMode(3,OUTPUT); // Initializing pin 3 which is connected to second LED as output
pinMode(A0, INPUT); // Initializing analog Pin A0 as input
}
void loop() {
int y=analogRead(A0); //analogRead(); is used to read the analog data from analog pins.
//Storing the data from LDR to a variable whose data type is an integer
if (y >=512) // checking if the value of y is greater than threshold value
{
digitalWrite(3,LOW); // only executes if the condition is true
}
else
{
digitalWrite(3,HIGH); // if condition is false then this statement will be executed
}
}

Code Explanation:
If / else conditional statement -

Syntax -

If(condition)
{
// if condition is true only then these statements will be executed
}
else
{
// if the condition is false only then these statements will be executed
}

38
www.technomentis.com
Links for Reference
i. https://www.arduino.cc/en/Guide/HomePage
ii. https://www.arduino.cc/en/Tutorial/HomePage
iii. https://www.grc.nasa.gov/www/k-12/rocket/rktbot.html
iv. https://www.teachengineering.org/lessons/view/cub_housing_lesson02

39
www.technomentis.com

You might also like