DPS - Arduino Manual - 2019-20
DPS - Arduino Manual - 2019-20
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.
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
Capacitor
Inductor
7
www.technomentis.com
Class Activity: Will these circuits work?
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.
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.
9
www.technomentis.com
The operating voltages of different colored LEDs are as follows –
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
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
12
www.technomentis.com
4. LDR with LEDs
Ω
13
www.technomentis.com
Building on Basics: Hands-On Projects
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.
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:
NPN
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 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.
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 Digital
19
www.technomentis.com
Arduino UNO Board
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.
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.
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.
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
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.
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”
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
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.
digitalWrite(13, LOW); //This command sets the voltage at Pin 13 to be 0v and hence the LED turns OFF
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.
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 */
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)
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.
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.
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.
}
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.
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
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
}
digitalWrite()
analogWrite()
If() Statements
analogRead()
Serial Communication
digitalRead()
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.
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)
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
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()
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.
}
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
}
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
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);
}
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