Sunteți pe pagina 1din 124

Contents

Chapter 1....................................................................................................................................................... 6
What is Embedded System? ..................................................................................................................... 6
Characteristics of an embedded system ................................................................................................... 6
Elements of an embedded system............................................................................................................ 7
1. Microcontroller ................................................................................................................................. 7
Application areas of embedded systems .................................................................................................. 8
Chapter 2....................................................................................................................................................... 9
Switches .................................................................................................................................................... 9
Connections .............................................................................................................................................. 9
Basic switch configurations..................................................................................................................... 10
Reversing the polarity using DPDT switches ........................................................................................... 11
Hard Wiring Connections for Robotic car using Two DPDT Switches ..................................................... 11
Chapter 3..................................................................................................................................................... 12
What are resistors ? ................................................................................................................................ 12
Different Types of Resistors .................................................................................................................... 12
Resistor Colour Code............................................................................................................................... 15
What is a Variable Resistor? ................................................................................................................... 16
............................................................................................................................................................ 16
............................................................................................................................................................ 16
Voltage Divider Circuits ........................................................................................................................... 17
Chapter 4..................................................................................................................................................... 18
Transistors ............................................................................................................................................... 18
Remembering the direction of arrow ..................................................................................................... 18
BC-547 & BC-557 ..................................................................................................................................... 19
TIP 122 & TIP 127 (Darlington Pair transistors) ...................................................................................... 19
Transistor as a switch (NPN) ................................................................................................................... 19
Transistor as a switch (PNP) .................................................................................................................... 20
Transistor as a motor driver.................................................................................................................... 20

1
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Chapter 5..................................................................................................................................................... 21
5V Power Supply using 7805 Voltage Regulator ..................................................................................... 21
7805 Voltage Regulator Circuit ............................................................................................................... 21
Steps of conversion ................................................................................................................................. 21
Transformer ............................................................................................................................................ 22
Rectifying circuit...................................................................................................................................... 22
7805 IC .................................................................................................................................................... 22
Chapter 6..................................................................................................................................................... 24
H Bridge................................................................................................................................................... 24
Working of a H-Bridge............................................................................................................................. 25
L293 Motor Driver- H Bridge IC ............................................................................................................... 27
Chapter 7..................................................................................................................................................... 28
What is a relay?....................................................................................................................................... 28
Why is a relay used? ............................................................................................................................... 28
Relay Design ............................................................................................................................................ 29
How relay works?.................................................................................................................................... 29
Chapter 8..................................................................................................................................................... 30
Working of Optocoupler ......................................................................................................................... 30
Chapter 9..................................................................................................................................................... 31
What are capacitors ................................................................................................................................ 31
AC Capacitor Circuit ................................................................................................................................ 31
Phasor Diagram of a purely capacitive circuit......................................................................................... 32
Types of Capacitor .................................................................................................................................. 33
Chapter 10................................................................................................................................................... 36
What is a solenoid valve ?....................................................................................................................... 36
Solenoid Switching .................................................................................................................................. 37
Switching Solenoids using a Transistor ............................................................................................... 37
Chapter- 11 (Basics of Microcontroller) ...................................................................................................... 38
What is a microcontroller? ..................................................................................................................... 38
General architecture of a microcontroller .............................................................................................. 38
Read Only Memory (ROM)...................................................................................................................... 38
Program Counter..................................................................................................................................... 39

2
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Processor................................................................................................................................................. 39
Input/output ports (I/O Ports) ................................................................................................................ 39
Oscillator ................................................................................................................................................. 40
Timers/Counters ..................................................................................................................................... 40
Watchdog timer ...................................................................................................................................... 40
Power Supply Circuit ............................................................................................................................... 41
Brown out ............................................................................................................................................... 41
Serial communication ............................................................................................................................. 42
Chapter- 12 (Arduino) ................................................................................................................................. 43
What is an Arduino?................................................................................................................................ 43
Pin configuration of ATmega 328 ........................................................................................................... 43
Chapter- 13 (LED) ........................................................................................................................................ 44
Simple LED circuit.................................................................................................................................... 44
Type of LEDs ............................................................................................................................................ 44
Chapter- 14 (Basic Output Programs) ......................................................................................................... 47
// WAP to blink 8 LEDs together ............................................................................................................. 47
// WAP to make a simple home automation system that turns on a tube light, a fan and a TV all at one
go. Three appliances are switched off one after the other .................................................................... 48
// WAP to make a moving lights LED pattern with one LED moving right.............................................. 49
// WAP to make a moving lights LED pattern with one LED moving right and then left ........................ 50
//WAP to make a LED pattern in which LEDs are switched ON in increasing order and are switched OFF
in reverse order....................................................................................................................................... 51
//WAP to make a LED pattern in which two extreme LEDS glow and move inwards and outwards ..... 53
// WAP to make a robotic car dance in three different patterns ........................................................... 55
//WAP to make Diwali lights with 5 different patterns. Each pattern is repeated 5 times .................... 59
//WAP to code yet another LED pattern................................................................................................. 62
Chapter- 15(Seven Segment) ...................................................................................................................... 63
What is a seven segment display ? ......................................................................................................... 63
Interfacing with microcontroller ............................................................................................................. 63
Working with more than one display ..................................................................................................... 64
Chapter- 16 (Programs based on Seven Segment Displays) ....................................................................... 65
//WAP to make a 0 to 9 counter using a single seven segment display ................................................. 65

3
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
//Program to run a counter from 0 to 99 on a 7 segment display ......................................................... 67
// WAP to control a 0-9 counter with increment and decrement switches ........................................... 70
// WAP to make a 0-999 counter on seven segments ............................................................................ 73
Chapter- 17 (Basic input output programs) ................................................................................................ 76
// WAP to make a basic robotic car ........................................................................................................ 76
// This is an improved code for a robotic car. The motors run with full thrust ...................................... 80
//WAP to solve the debounce problem .................................................................................................. 84
//WAP to make Diwali lights with 3 different patterns. Each pattern is controlled with a switch ........ 85
//WAP to make a password protected system with password:5216, if the password is correct the LED
connected to pin 0 should turn off ......................................................................................................... 89
Chapter- 18 (PWM) ..................................................................................................................................... 91
What is Pulse Width Modulation ............................................................................................................ 91
Chapter- 19 (Programs based on PWM) ..................................................................................................... 92
//WAP to increase and decrease the brightness of an LED connected to pin 3 ..................................... 92
//DC motor speed control using inbuilt PWM pin .................................................................................. 93
Chapter- 20 (RF Module 433 Mhz).............................................................................................................. 95
HT12E Encoder ........................................................................................................................................ 95
HT12D Decoder ....................................................................................................................................... 96
Block Diagram of a RF controlled Car ..................................................................................................... 97
// WAP for the receiver end of a PC controlled robotic car ................................................................... 98
//WAP for the transmitter section of a PC controlled car .................................................................... 101
Chapter- 21 (DTMF Decoder 8870) ........................................................................................................... 104
// WAP to make a GSM controlled Car/Home automation system using DTMF Decoder 8870 .......... 105
Chapter-22 (16x2 LCD) .............................................................................................................................. 109
//WAP to demonstrate the use of a LCD .............................................................................................. 110
//WAP to control a LCD using a variable resistor. The value should be printed on PC when an enter
switch is pressed ................................................................................................................................... 111
//WAP to develop a Washing machine which displays its options on PC as well as LCD ..................... 114
Chapter- 23(Servo Motors) ....................................................................................................................... 119
Controlling a servo motor ..................................................................................................................... 119
// WAP to control a servo motor using a variable resistor ................................................................... 120
Chapter- 24 (Stepper motor) .................................................................................................................... 121

4
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
What is a stepper motor? ..................................................................................................................... 121
How a stepper motor works? ............................................................................................................... 121
Stepper motor sequence ...................................................................................................................... 122
//WAP to rotate a stepper motor ......................................................................................................... 123

5
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Chapter- 1 (Basics of Embedded Systems)
What is Embedded System?
An embedded system is a system that has software embedded into hardware, which makes a
system dedicated for an application (s) or specific part of an application or product or part of a
larger system. It processes a fixed set of pre-programmed instructions to control
electromechanical equipment which may be part of an even larger system (not a computer with
keyboard, display, etc).

Furthermore any machinery which has mechanical parts, electronic circuits and software can be
termed as an embedded system.

Block diagram of a typical embedded system is shown in fig.

Characteristics of an embedded system


a) Embedded systems are application specific & single functioned; application is known
apriori, the programs are executed repeatedly.

b) Efficiency is of paramount importance for embedded systems. They are optimized for
energy, code size, execution time, weight & dimensions, and cost.

c) Embedded systems are typically designed to meet real time constraints; a real time system
reacts to stimuli from the controlled object/ operator within the time interval dictated by the
environment. For real time systems, right answers arriving too late (or even too early) are
wrong.

d) Embedded systems often interact (sense, manipulate & communicate) with external world
through sensors and actuators and hence are typically reactive systems; a reactive system is in

6
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
continual interaction with the environment and executes at a pace determined by that
environment.

Elements of an embedded system

1. Microcontroller
Microcontrollers are single chip semi conductor device which is a computer on chip. In other
words it is a smaller version of a computer but not a complete computer. Its CPU contains an
Arithmetic & Logic Unit(ALU), a Program Counter(PC), a Stack Pointer(SP), registers, a clock and
interrupts circuit on a single chip. To make complete micro computer, one must add memory
usually ROM and RAM, memory decoder, an oscillator, a number of serial and parallel ports

A Microcontrolleris designed to cater for large amount of applications and hence is produced in
bulk. Using it in an embedded system offers various benefits. Design time is low as only
software is to be developed, no digital design is involved. Typical characteristics of a
Microcontroller are relatively high cost, high speeds, higher Power consumption, large
architecture, large memory size, onboard flash and cache, an external bus interface for greater
memory usage.

2. Software

If hardware forms the body of the embedded systems, software is a soul of embedded systems.
The software lends the functionality to an embedded system.Variety of languages has emerged
as the tasks to be performed by embedded systems are of quite diverse nature.

3. Interface to physical world

7
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Embedded systems interface with the physical world through Sensors and Actuators. Sensors act
as “senses” for an embedded system, whereas actuators act as “limbs”. Varieties of sensors
working on variety of sensing principles are used in embedded systems. Light, temperature,
acceleration, speed, mass, distance, etc. are common physical parameters which are sensed.
Motors are one of the actuators used in many systems.

Application areas of embedded systems


 Medical Systems
– pace maker, patient monitoring systems, injection systems, intensive care units, …

 Office Equipment
– printer, copier, fax, …

 Tools
– Multimeter, oscilloscope, line tester, GPS, …

 Banking
– ATMs, statement printers, …

 Transportation
– (Planes/Trains/[Automobiles] and Boats)
– radar, traffic lights, signaling systems, …

8
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Chapter- 2 (Switches)
Switches
A switch is any device used to interrupt the flow of electrons in a circuit. Switches are
essentially binary devices: they are either completely on ("closed") or completely off ("open").
There are many
different types of
switches.

The simplest type of


switch is one where
two electrical
conductors are
brought in contact
with each other by
the motion of an actuating mechanism. Other switches are more complex, containing electronic
circuits able to turn on or off depending on some physical stimulus (such as light or magnetic
field) sensed. In any case, the final output of any switch will be (at least) a pair of wire-
connection terminals that will either be connected together by the switch's internal contact
mechanism ("closed"), or not connected together ("open").

Apart from the simplest switches wherein actuator make or breaks the contact, there are some
complex switches which use some other physical stimulus. There are switches which changes
their state by sensing light/ magnetic field/temperature/pressure, etc.

Connections

 Normally OPEN (NO) switches are those which remain in OFF state. On pressing the
switch, they get closed(or becomes ON).

9
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
 Exactly opposite Normally CLOSED (NC) switches are those which remain in ON state. On
pressing the switch, they get opened(or becomes OFF).

Basic switch configurations

SPST

Single Pole Single Throw (SPST) is the most commonly used


switch configuration. It has only a single pole and only a single
throw is possible to open or close the contacts. There is only one
moving metal plate which can make contact with a metal plate.

SPDT

Single Pole Double Throw (SPDT) switch has a single pole as in


the case of an SPST. This single pole can make contact with two
metal plates, but only one at a time. This is made possible by
moving the pole metallic plate to two different directions to
make contact with the metallic plates, with one in each direction.
When the pole makes a contact with one of the metallic plate, it
breaks the contact with the other one simultaneously

DPST

Double Pole Single throw (DPST) has two separate poles but only
single throw direction. There are two movable metallic plates
which are operated together. They can be moved together in a
single direction to make contact with two corresponding fixed
metal plates.

10
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
DPDT

Double Pole Double throw (DPDT) has two separate poles


and two throw directions as well. There are two movable
metallic plates which are operated together. They can be
moved together in two separate directions to make contact
with two set of corresponding fixed metal plates.

Reversing the polarity using DPDT switches

Hard Wiring Connections for Robotic car using Two DPDT Switches

11
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Chapter- 3 (Resistors)
What are resistors ?
Resistors, ( R ) are the most fundamental and commonly used of all the electronic components,
to the point where they are almost taken for granted. There are many different Types of
Resistors available for the electronics constructor to choose from, from very small surface
mount chip resistors up to large wire wound power resistors.

Different Types of Resistors


The principal job of a resistor within an electrical or
electronic circuit is to “resist” (hence the nameResistor),
regulate or to set the flow of electrons (current) through
them by using the type of conductive material from which
they are composed. Resistors can also be connected
together in various series and parallel combinations to
form resistor networks which can act as voltage droppers, voltage dividers or current limiters
within a circuit.

Resistors are “Passive Devices“, that is they contain no source of power or amplification but
only attenuate or reduce the voltage or current signal passing through them. This attenuation
results in electrical energy being lost in the form of heat as the resistor resists the flow of
electrons through it.

There are many thousands of different Types of Resistor and are produced in a variety of forms
because their particular characteristics and accuracy suit certain areas of application, such as
High Stability, High Voltage, High Current etc, or are used as general purpose resistors where
their characteristics are less of a problem.

Some of the common characteristics associated with the resistor are; Temperature Coefficient,
Voltage Coefficient, Noise, Frequency Response, Power as well as a resistorsTemperature
Rating, Physical Size and Reliability.

Composition Types of Resistor

Carbon Resistors are the most common type of Composition Resistors. Carbon resistors are a
cheap general purpose resistor used in electrical and electronic circuits. Their resistive element
is manufactured from a mixture of finely ground carbon dust or graphite (similar to pencil lead)
and a non-conducting ceramic (clay) powder to bind it all together.

12
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Carbon Resistor

The Carbon Composite Resistor is a low to medium type power resistor which has a low
inductance making them ideal for high frequency applications but they can also suffer from
noise and stability when hot. Carbon composite resistors are generally prefixed with
a “CR” notation (eg, CR10kΩ ) and are available in E6 ( ± 20% tolerance (accuracy) ), E12 ( ±
10% tolerance) and E24 ( ± 5% tolerance) packages with power ratings from 0.125 or 1/4 of a
Watt up to 5 Watts.

Carbon composite resistor types are very cheap to make and are therefore commonly used in
electrical circuits. However, due to their manufacturing process carbon type resistors have very
large tolerances so for more precision and high value resistances, film type resistors are used
instead.

Film Type Resistors


The generic term “Film Resistor” consist of Metal Film, Carbon
Film and Metal Oxide Film resistor types, which are generally
made by depositing pure metals, such as nickel, or an oxide
film, such as tin-oxide, onto an insulating ceramic rod or
substrate.

The resistive value of the resistor is controlled by increasing the desired thickness of the
deposited film giving them the names of either “thick-film resistors” or “thin-film resistors”.

Once deposited, a laser is used to cut a high precision spiral helix groove type pattern into this
film. The cutting of the film has the effect of increasing the conductive or resistive path, a bit
like taking a long length of straight wire and forming it into a coil.

13
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Film Resistor
Metal Film Resistors have much better
temperature stability than their carbon
equivalents, lower noise and are
generally better for high frequency or
radio frequency applications. Metal
Oxide Resistors have better high surge
current capability with a much higher
temperature rating than the equivalent metal film resistors.

Metal Film Resistors are prefixed with a “MFR” notation (eg, MFR100kΩ) and a CF for Carbon
Film types. Metal film resistors are available in E24 (±5% & ±2% tolerances), E96 (±1%
tolerance) and E192(±0.5%, ±0.25% & ±0.1% tolerances) packages with power ratings of 0.05
(1/20th) of a Watt up to 1/2 Watt. Generally speaking Film resistors are precision low power
components.

Wirewound Types of Resistor


Another type of resistor, called a Wirewound Resistor, is made by
winding a thin metal alloy wire (Nichrome) or similar wire onto an
insulating ceramic former in the form of a spiral helix similar to the film
resistor above.

These types of resistor are generally only available in


very low ohmic high precision values
(from 0.01 to 100kΩ) due to the gauge of the wire and
number of turns possible on the former making
them ideal for use in measuring circuits and
Whetstone bridge type applications.

14
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Resistor Colour Code

Colour Digit Multiplier Tolerance

Black 0 1

Brown 1 10 ± 1%

Red 2 100 ± 2%

Orange 3 1,000

Yellow 4 10,000

Green 5 100,000 ± 0.5%

Blue 6 1,000,000 ± 0.25%

Violet 7 10,000,000 ± 0.1%

Grey 8

White 9

Gold 0.1 ± 5%

Silver 0.01 ± 10%

15
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Example 1:What is the resistance of this resistor?

Notice that the colors on this resistor are (in order) Red, Green,
Orange, and Silver.

1. The first line is the first digit → Red = 2

2. The second line is the second digit → Green = 5

3. The third line is the multiplier → Orange = 103

4. The last line (if any) is the tolerance → Silver = ± 10%

So the final answer would be 25 x 103Ω ± 10%

Yes, I know it's not proper scientific notation this way. You can also
write it as 25000 Ω

(Notice there are three zeros), or even 2.5 x 102Ω.

What is a Variable Resistor?

A variable resistor is a device that is used to change the resistance according to our needs in
an electronic circuit. It can be used as a three terminal as well as a two terminal device. Mostly
they are used as a three terminal device. Variable resistors are mostly used for device
calibration.

Working of Variable Resistor


As shown in the diagram below, a variable
resistor consists of a track which provides the
resistance path. Two terminals of the device are
connected to both the ends of the track. The
third terminal is connected to a wiper that

16
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
decides the motion of the track. The motion of the wiper through the track helps in increasing and
decreasing the resistance.

The resistance between point A and B is always fixed whereas the resistance between AW and
BW changes when the wiper is turned.

Voltage Divider Circuits


These circuits are often called “potential divider” circuits (because “potentialdifference” means
voltage).Set up the circuit shown below. Use a voltmeter to verify these two formulae:

Variable Potential Divider Circuit

We can make a variable potential divider using a variable resistor. In this case we have a circuit
similar to the previous one butnow the two resistors R1 and R2 are two parts of the same
component. This isuseful for volume controls etc.

The diagram on the right shows the approximate appearance of the variable resistor on its
support. Connect a voltmeter as shown and move s (that is, turn the rheostat). Note that the
voltmeter reading varies from zero to the full voltage of the battery.

17
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Chapter- 4 (Transistors)
Transistors

The transistor is the most important example of an active element. It is


a device that can amplify and produce an output signal with more
power than the input signal. The additional power comes from an
external source i.e. the power supply.

The transistor is the essential ingredient of every electronic circuit:


amplifiers, oscillators and computers. Integrated circuits (ICs), which have replaced circuits
constructed from individual, discrete transistors, are themselves arrays of transistors and other
components built as a single chip of semiconductor material.

A transistor is a 3-terminal device available in 2 kinds: npn and pnp transistors.

The terminals are called: collector (C), base (B) and emitter
(E). Voltage at a transistor terminal (relative to ground) is
indicated by a single subscript, VC is the collector voltage,
for instance. Voltage between 2 terminals is indicated by a
double subscript: VBE is base-to-emitter drop. If the same
letter is doubled, it means power supply voltage: VCC
(positive) is power supply voltage associated with the
collector and VEE (negative) is power supply voltage
associated with the emitter.

Remembering the direction of arrow


We know that conventional current
flows from positive to negative. In
NPN transistor p is at the center and
the current will flow outside
towards N type semi conductor. The
case is opposite for the PNP
transistor.

18
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
BC-547 & BC-557
BC- 547 is NPN and BC-557 is PNP

Ic = 100 mA

TIP 122 & TIP 127 (Darlington Pair transistors)


TIP 122 is NPN & TIP 127 is PNP

Ic = 8A

Transistor as a switch(NPN)
Here the transistor is working as a switch.The
base is ackting as the controlling terminal.

When Vc > Vb > Ve

The transistor will be ON. At this point the


collector and emitter will be short.

We know Ib << Ic, so the current required to


switch the transistor is quiet low as compared to
the current carrying capacity of the C-E circuit.

19
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Transistor as a switch (PNP)

The similar condition for the PNP transistor is

Ve>Vb>Vc. When this condition is true the transistor will turn ON.

Transistor as a motor driver

20
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Chapter- 5 (Power Supply)

5V Power Supply using 7805 Voltage Regulator

7805 Voltage Regulator Circuit


A regulated power supply is very much essential for several electronic devices due to the
semiconductor material employed in them have a fixed rate of current as well as voltage. The
device may get damaged if there is any deviation from the fixed rate. The AC power supply gets
converted into constant DC by this circuit. By the help of a voltage regulator DC, unregulated
output will be fixed to a constant voltage. The circuit is made up of linear voltage regulator
7805 along with capacitors and resistors with bridge rectifier made up from diodes.

Steps of conversion
a) Stepping down the 220V AC supply
b) Conversion of AC to DC using rectifier
c) Smoothening of the rectified DC wave
d) Conversion of 12 DC to 5V regulated DC supply
e) Further smoothening if required

21
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Transformer
Selecting a suitable transformer is of great importance. The current rating and the secondary
voltage of the transformer is a crucial factor.

 The current rating of the transformer depends upon the current required for the load to
be driven.
 As a thumb rule the input voltage to the 78xx IC should be at least 1.5 times therefore
7805 requires an input voltage at least close to 7.5V.
 So I chose a 12-0-12center tapped transformer with current rating 1A.

NOTE: Any transformer which supplies secondary peak voltage up to 35V can be used but as the
voltage increases size of the transformer and power dissipation across regulator increases.

Rectifying circuit

 Its advantage is DC saturation is less


as in both cycle diodes conduct.
 Higher Transformer Utilization
Factor (TUF).
 1N4007 diodes are used as it’s is
capable of withstanding a higher
reverse voltage of
1000v whereas 1N4001 is 50V

7805 IC
Voltage regulator is used to produce a constant linear
output voltage. It’s generally used with AC to
DC power supply. And also it can be used as well as
a DC to DC voltage converter. To regulating low
voltage, most used device is one single IC. 7805, 7812,
7905 etc. 78xx series are design for positive and 79xx
series are for Negative voltage regulator.

7805 is a three terminal +5v voltage regulator IC from


78XX chips family. LM78XX series are from National
Semiconductor. They are linear positive voltage
regulator IC; used to produce a fixed linear stable output voltage

Characteristics of 7805 Voltage regulator:

22
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
 Input voltage range 7V- 35V
 Current rating Ic = 1A
 Output voltage range VMax=5.2V ,VMin=4.8V
 Automatic temperature cut off.

23
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Chapter- 6( H- Bridge/ Motor Driver)
H Bridge
DC motors are really cheap and can be found very
easily. In this post we will discuss the
basic theory behind the DC motor drive system
and its speed control. Having some basic
understanding of how your motor driver works
will play a key role in building your own circuits
debugging them.

The main advantage is that, they are really easy


to interface. Hook the wires on to the battery
terminals and they will work just fine. The direction of rotation is based on the direction of
current flow in the armature (polarity of the battery).

If you flip the wires connected to the


battery terminals, the direction of rotation
will change! But that is not going to help
much if you want the car to be remote
controlled. So there has to be a way to flip
the wires without having to touch it…
That’s when an H-Bridge network comes
into action.

24
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Working of a H-Bridge

 S1, S4 – ON and S2, S3 – OFF

Now if we were to turn on the switch S1 and S4 we will


observe that the current flows from Battery positive
to S1 and then to the motor and reached the negative
terminal of the battery through S4.

 S2, S3 – ON and S1, S4 - OFF

Similarly, if we were to turn on the switch S2 and S3 we


will observe that the current flows from Battery
positive to S2 and then to the motor and reached the
negative terminal of the battery through S3.

From the above cases you can see that the direction of current flow is reversed. Hence the
direction of rotation of the motor is also reversed. This way we are able to drive the motor in
both the directions without having to manually flipping the wires.

25
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Switches S1 through S4 are replaced with transistors Q1 through Q4. Here is what it will look
like once we are
done replacing
the mechanical
stuff with
electronics,

The 1k
ohm resistors
connected to
the base of all
the transistors
are meant for
current limiting.
The diodes connected parallel to the transistors are called free-wheeling diodes and are used to
prevent the transistors from getting damaged due the back currents (inductive) during
switching.

In the above circuit, all the transistors are NPN so they turn on if logic 1 is given to them and are
OFF when no logic or logic 0 is given. The drawbacks of this network are that there are 4 IO pins
required to drive one motor. A slight variant of this connection is shown below.

Here two transistor


are paired and then
used with a single IO
pin. Here one of two
transistors is PNP so,
it needs logic 0 to
turn ON. So if we
were to give Logic
one to one pair, then
only the lower
transistors (NPN) are
turned. Similarly a logic 0 will turn on only the upper transistors (PNP).This way we are able to
reduce the number of microcontroller pins required to the control the motor. This is the sort of
circuit our L293d drivers use.

26
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
L293 Motor Driver- H Bridge IC

The L293 is the most commonly used motor driver in robotics.

The IC is divided into two channels, one channel contains one motor and its control signals. So
one side of the IC (pin 1 to 8) is channel 1 and the other side (pin 9 to 16) is channel 2. You can
see that each side has an enable pin (pins 1 and 9) they are used to turn ON/OFF the control
for that channel.

The pins labeled as Motor +ve and Motor -ve (pins 3,6,11, and 14) is where the motor has to be
connected and the Logic A and Logic B (pins 2,7,10, and 15) are where the control signal to that
particular motor has to be given.

Vcc1 (pin16) is the supply for the IC and should not exceed 5V. Vcc2 (pin 8) is the supply for the
motors (high voltage high current supply). The two Vcc are internally isolated. The ground (-ve
terminal) of both the supplies (Eg., 5v for IC and 12v for motors) should be connected to the
ground pins (pins 4,5,12, and 13).

27
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Chapter-7 (Relay)
Relay
What is a relay?
We know that most of the high end industrial application devices
have relays for their effective working. Relays are simple switches
which are operated both electrically and mechanically. Relays consist of
an electromagnet and also a set of contacts. The switching mechanism
is carried out with the help of the electromagnet. There are also other
operating principles for its working. But they differ according to their applications. Most of the
devices have the application of relays.

Why is a relay used?


The main operation of a relay comes in places where only a low-power signal can be used to
control a circuit. It is also used in places where only one signal can be used to control a lot of
circuits. The high end applications of relays require high power to be driven by electric motors
and so on.

We may utilize a relay when we wish to control a high voltage (AC or DC) circuit with a low DC
voltage controlling signal.

28
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Relay Design
There are only four main parts in a relay. They are

 Electromagnet
 Movable Armature
 Switch point contacts
 Spring

It is an electro-magnetic relay with a wire coil,


surrounded by an iron core. A path of very low
reluctance for the magnetic flux is provided for the movable armature and also the switch point
contacts. The movable armature is connected to the yoke which is mechanically connected to
the switch point contacts. These parts are safely held with the help of a spring. The spring is
used so as to produce an air gap in the circuit when the relay becomes de-energized.

How relay works?

The diagram shows an inner section diagram of a relay. An iron core is surrounded by a control
coil. As shown, the power source is given to the electromagnet through a control switch and
through contacts to the load. When current starts flowing through the control coil, the
electromagnet starts energizing and thus intensifies the magnetic field. Thus the upper contact
arm starts to be attracted to the lower fixed arm and thus closes the contacts causing a short
circuit for the power to the load. On the other hand, if the relay was already de-energized when
the contacts were closed, then the contact move oppositely and make an open circuit.

As soon as the coil current is off, the movable armature will be returned by a force back to its
initial position. This force will be almost equal to half the strength of the magnetic force. This
force is mainly provided by two factors.

29
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Chapter- 8 (Optocoupler)
Working of Optocoupler
An optocoupler (or an optoelectronic coupler) is basically an interface between two circuits
which operate at (usually) different voltage levels. The key advantage of an optocoupler is the
electrical isolation between the input and output circuits. With an optocoupler, the only
contact between the input and the output is a beam of light. Because of this it is possible to
have an insulation resistance between the two circuits in the thousands of megohms. Isolation

like this is useful in high voltage applications where the potentials of two circuits may differ by
several thousand volts.

The most common industrial use of the optocouplers (or optically-coupled isolators) is as a
signal converter between high-voltage and low- voltage solid-state logic circuits. Optical
isolators can be employed in any situation where a signal must be passed between two circuits
which are isolated from each other.

30
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Chapter- 9 (Capacitors)
What are capacitors
Capacitance in AC Circuits

When capacitors are connected across a direct current DC supply voltage they become charged
to the value of the applied voltage, acting like temporary storage devices and maintain or hold
this charge indefinitely as long as the supply voltage is present. During this charging process, a
charging current, ( i ) will flow into the capacitor opposing any changes to the voltage at a rate
that is equal to the rate of change of the electrical charge on the plates.

This charging current can be defined as: i = CdV/dt. Once the capacitor is “fully-charged” the
capacitor blocks the flow of any more electrons onto its plates as they have become saturated.
Hence it is said that capacitor blocks DC.

However, if we apply an alternating current or AC supply, the capacitor will alternately charge
and discharge at a rate determined by the frequency of the supply. Then the Capacitance in AC
circuits varies with frequency as the capacitor is being constantly charged and discharged.

We know that the flow of electrons through the Capacitor is directly proportional to the rate of
change of the voltage across the plates. Then, we can see that capacitors in AC circuits like to
pass current when the voltage across its plates is constantly changing with respect to time such
as in AC signals, but it does not like to pass current when the applied voltage is of a constant
value such as in DC signals. Consider the circuit below.

AC Capacitor Circuit

In the purely capacitive circuit above, the capacitor is connected directly across the AC supply
voltage. As the supply voltage increases and decreases, the capacitor charges and discharges
with respect to this change. We know that the charging current is directly proportional to the
rate of change of the voltage across the plates with this rate of change at its greatest as the

31
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
supply voltage crosses over from its positive half cycle to its negative half cycle or vice versa at
points, 0o and 180oalong the sine wave.

Phasor Diagram of a purely capacitive circuit

At 0o the rate of change of the supply voltage is increasing in a positive direction resulting in a
maximum charging current at that instant in time. As the applied voltage reaches its maximum
peak value at 90o for a very brief instant in time the supply voltage is neither increasing nor
decreasing so there is no current flowing through the circuit.

As the applied voltage begins to decrease to zero at 180o, the slope of the voltage is negative
so the capacitor discharges in the negative direction. At the 180o point along the line the rate of
change of the voltage is at its maximum again so maximum current flows at that instant and so
on.

Then we can say that for capacitors in AC circuits the instantaneous current is at its minimum or
zero whenever the applied voltage is at its maximum and likewise the instantaneous value of
the current is at its maximum or peak value when the applied voltage is at its minimum or zero.

From the waveform above, we can see that the current is leading the voltage by 1/4 cycle or
90o as shown by the vector diagram. Then we can say that in a purely capacitive circuit the
alternating voltage lags the current by 90o.

We know that the current flowing through the capacitance in AC circuits is in opposition to the
rate of change of the applied voltage but just like resistors, capacitors also offer some form of
resistance against the flow of current through the circuit, but with capacitors in AC circuits this
AC resistance is known as Reactance or more commonly in capacitor circuits, Capacitive
Reactance, so capacitance in AC circuits suffers from Capacitive Reactance.

32
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Types of Capacitor
The comparisons between the different types of capacitor are generally made with regards to
the dielectric used between the plates. Like resistors, there are also variable types of capacitors
which allow us to vary their capacitance value for use in radio or “frequency tuning” type
circuits.

Commercial types of Capacitor are made from metallic foil interlaced with thin sheets of either
paraffin-impregnated paper or Mylar as the dielectric material. Some capacitors look like tubes,
this is because the metal foil plates are rolled up into a cylinder to form a small package with
the insulating dielectric material sandwiched in between them.

 Dielectric Capacitor

Dielectric Capacitors are usually of the variable type were a continuous variation of capacitance
is required for tuning transmitters, receivers and transistor radios. Variable dielectric capacitors
are multi-plate air-spaced types that have a set of fixed plates (the stator vanes) and a set of
movable plates (the rotor vanes) which move in between the fixed plates.

The position of the moving plates with respect to the fixed plates determines the overall
capacitance value. The capacitance is generally at maximum when the two sets of plates are
fully meshed together. High voltage type tuning capacitors have relatively large spacings or air-
gaps between the plates with breakdown voltages reaching many thousands of volts.

Variable Capacitor Symbols

 Film Capacitor

Film Capacitors are the most commonly available of all types of


capacitors, consisting of a relatively large family of capacitors with
the difference being in their dielectric properties. These include
polyester (Mylar), polystyrene, polypropylene, polycarbonate,

33
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
metalised paper, Teflon etc. Film type capacitors are available in capacitance ranges from as
small as 5pF to as large as 100uF is depending upon the actual type of capacitor and its voltage
rating.

 Ceramic Capacitor

They exhibit large non-linear changes in capacitance against temperature


and as a result are used as de-coupling or by-pass capacitors as they are
also non-polarized devices. Ceramic capacitors have values ranging from a
few Pico farads to one or two microfarads, ( μF ) but their voltage ratings
are generally quite low.

Ceramic types of capacitors generally have a 3-digit code printed onto


their body to identify their capacitance value in pico-farads. Generally the
first two digits indicate the capacitors value and the third digit indicates the number of zero’s to
be added. For example, a ceramic disc capacitor with the markings 103 would indicate 10 and 3
zero’s in pico-farads which is equivalent to 10,000 pF or 10nF.

 Electrolytic Capacitors

Electrolytic Capacitors are generally used when very large capacitance values
are required. Here instead of using a very thin metallic film layer for one of
the electrodes, a semi-liquid electrolyte solution in the form of a jelly or
paste is used which serves as the second electrode (usually the cathode).

The dielectric is a very thin layer of oxide which is grown electro-chemically in


production with the thickness of the film being less than ten microns. This
insulating layer is so thin that it is possible to make capacitors with a large value of capacitance
for a small physical size as the distance between the plates, d is very small.

The majority of electrolytic types of capacitors are Polarized, that is the DC voltage applied to
the capacitor terminals must be of the correct polarity, i.e. positive to the positive terminal and
negative to the negative terminal as an incorrect polarization will break down the insulating
oxide layer and permanent damage may result.

Electrolytic Capacitors are generally used in DC power supply circuits due to their large
capacitance’s and small size to help reduce the ripple voltage or for coupling and decoupling
applications. One main disadvantage of electrolytic capacitors is their relatively low voltage
rating and due to the polarization of electrolytic capacitors, it follows then that they must not
be used on AC supplies. Electrolyte’s generally come in two basic forms; Aluminum Electrolytic
Capacitors and Tantalum Electrolytic Capacitors.

34
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Tantalum Electrolytic Capacitors
Tantalum Electrolytic Capacitors and Tantalum Beads, are available in both wet (foil) and dry
(solid) electrolytic types with the dry or solid tantalum being the most common. Solid tantalum
capacitors use manganese dioxide as their second terminal and are physically smaller than the
equivalent aluminum capacitors.

The dielectric properties of tantalum oxide is also much better than those of aluminium oxide
giving a lower leakage currents and better capacitance stability which makes them suitable for
use in blocking, by-passing, decoupling, filtering and timing applications.

Also, Tantalum Capacitors although polarized, can tolerate being connected to a reverse
voltage much more easily than the aluminium types but are rated at much lower working
voltages. Solid tantalum capacitors are usually used in circuits where the AC voltage is small
compared to the DC voltage.

However, some tantalum capacitor types contain two capacitors in-one, connected negative-to-
negative to form a “non-polarised” capacitor for use in low voltage AC circuits as a non-
polarised device. Generally, the positive lead is identified on the capacitor body by a polarity
mark, with the body of a tantalum bead capacitor being an oval geometrical shape. Typical
values of capacitance range from 47nF to 470uF.

35
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Chapter-10 (Solenoid valve)
What is a solenoid valve ?
Solenoid is a type of electromagnetic actuator that converts an electrical signal into a magnetic
field. The linear solenoid works on the same basic principal as the electromechanical relay, they
can also be switched and controlled using transistors or MOSFET’s. A “Linear Solenoid” is an
electromagnetic device that converts electrical energy into a mechanical pushing or pulling
force or motion.

Linear Solenoid

Linear solenoid’s basically consist of an electrical coil wound around a cylindrical tube with a
ferro-magnetic actuator or “plunger” that is free to move or slide “IN” and “OUT” of the coils
body.

When electrical current flows through a conductor it generates a magnetic field, and the
direction of this magnetic field with regards to its North and South Poles is determined by the
direction of the current flow within the wire. This coil of wire becomes an “Electromagnet” with
its own north and south poles exactly the same as that for a permanent type magnet.

The strength of this magnetic field can be increased or decreased by either controlling the
amount of current flowing through the coil or by changing the number of turns or loops that
the coil has.

Magnetic Field produced by a Coil

When an electrical current is passed through


the coils windings, it behaves like an
electromagnet and the plunger, which is
located inside the coil, is attracted towards
the center of the coil by the magnetic flux
setup within the coils body, which in turn
compresses a small spring attached to one

36
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
end of the plunger. The force and speed of the plungers movement is determined by the
strength of the magnetic flux generated within the coil.

When the supply current is turned “OFF” (de-energized) the electromagnetic field generated
previously by the coil collapses and the energy stored in the compressed spring forces the
plunger back out to its original rest position. This back and forth movement of the plunger is
known as the solenoids “Stroke”, in other words the maximum distance the plunger can travel
in either an “IN” or an “OUT” direction, for example, 0 – 30mm.

Solenoid Switching
Generally solenoids either linear or rotary operate with the application of a DC voltage, but
they can also be used with AC sinusoidal voltages by using full wave bridge rectifiers to rectify
the supply which then can be used to switch the DC solenoid. Small DC type solenoids can be
easily controlled using Transistor or MOSFET switches and are ideal for use in robotic
applications.

However, as we saw previously with electromechanical relays, linear solenoids are “inductive”
devices so some form of electrical protection is required across the solenoid coil to prevent
high back emf voltages from damaging the semiconductor switching device.

Switching Solenoids using a Transistor

37
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Chapter- 11 (Basics of Microcontroller)

What is a microcontroller?
A microcontroller (sometimes abbreviated µC, uC or MCU) is a small computer on a
single integrated circuit containing a processor core, memory, and
programmable input/output peripherals. Program memory in the form of NOR flash or OTP ROM is
also often included on chip, as well as a typically small amount of RAM. Microcontrollers are
designed for embedded applications, in contrast to the microprocessors used in personal
computers or other general purpose applications.

General architecture of a microcontroller

Read Only Memory (ROM)


Read Only Memory (ROM) is a type of memory used to permanently save the program being executed.
The size of the program that can be written depends on the size of this memory. ROM can be built in the
microcontroller or added as an external chip, which depends on the type of the microcontroller. Both
options have some disadvantages. If ROM is added as an external chip, the microcontroller is cheaper
and the program can be considerably longer. At the same time, a number of available pins is reduced as
the microcontroller uses its own input/output ports for connection to the chip. The internal ROM is
usually smaller and more expensive, but leaves more pins available for connecting to peripheral
environment. The size of ROM ranges from 512B to 64KB.

38
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Program Counter
Program Counter is an engine running the program and points to the memory address
containing the next instruction to execute. After each instruction execution, the value of the
counter is incremented by 1. For this reason, the program executes only one instruction at a
time just as it is written. However…the value of the program counter can be changed at any
moment, which causes a “jump” to a new memory location. This is how subroutines and branch
instructions are executed. After jumping, the counter resumes even and monotonous
automatic counting +1, +1, +1…

Processor
As its name suggests, this is a unit which monitors and controls all processes within the
microcontroller and the user cannot affect its work. It consists of several smaller subunits, of
which the most important are:

 Instruction decoder is a part of the electronics which recognizes program instructions


and runs other circuits on the basis of that. The abilities of this circuit are expressed in
the "instruction set" which is different for each microcontroller family.
 Arithmetical Logical Unit (ALU) performs all mathematical and logical operations upon
data.
 Accumulator is an SFR closely related to the operation of ALU. It is a kind of working
desk used for storing all data upon which some operations should be executed
(addition, shift etc.). It also stores the results ready for use in further processing. One of
the SFRs, called the Status Register, is closely related to the accumulator, showing at any
given time the "status" of a number stored in the accumulator (the number is greater or
less than zero etc.).

Input/output ports (I/O Ports)


In order to make the microcontroller useful, it is necessary
to connect it to peripheral devices. Each microcontroller
has one or more registers (called a port) connected to the
microcontroller pins.

Why do we call them input/output ports? Because it is


possible to change a pin function according to the user's
needs.

These registers are the only registers in the microcontroller


the state of which can be checked by voltmeter!

39
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Oscillator
Even pulses generated by the oscillator enable harmonic and
synchronous operation of all circuits within the
microcontroller. It is usually configured as to use quartz-
crystal or ceramics resonator for frequency stabilization. It is
important to say that program instructions are not executed
at the rate imposed by the oscillator itself, but several times
slower. It happens because each instruction is executed in
several steps.

Timers/Counters
Most programs use these
miniature electronic "stopwatches"
in their operation. These are
commonly 8- or 16-bit SFRs the
contents of which are
automatically incremented by each
coming pulse. Once the register is
completely loaded, an interrupt is
generated!

If these registers use an internal


quartz oscillator as a clock source,
then it is possible to measure the
time between two events (if the register value is T1 at the moment measurement has started,
and T2 at the moment it has finished, then the elapsed time is equal to the result of subtraction
T2-T1 ). If the registers use pulses coming from external source, then such a timer is turned into
a counter.

Watchdog timer
The Watchdog Timer is a timer connected to a completely separate RC oscillator within the
microcontroller.

If the watchdog timer is enabled, every time it counts up to the program end, the
microcontroller reset occurs and program execution starts from the first instruction. The point
is to prevent this from happening by using a special command. The whole idea is based on the
fact that every program is executed in several longer or shorter loops.

40
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
If instructions resetting the watchdog timer are set at the appropriate program locations,
besides commands being regularly executed, then the operation of the watchdog timer will not
affect the program execution.

If for any reason (usually electrical noise in industry), the program counter "gets stuck" at some
memory location from which there is no return, the watchdog will not be cleared, so the
register’s value being constantly incremented will reach the maximum et voila! Reset occurs!

Power Supply Circuit


There are two things worth attention concerning the microcontroller power supply circuit:

Brown out is a potentially dangerous state which occurs at the moment the microcontroller is
being turned off or when power supply voltage drops to the lowest level due to electric noise.
As the microcontroller consists of several circuits which have different operating voltage levels,
this can causeits out of control performance. In order to prevent it, the microcontroller usually
has a circuit for brown out reset built-in. This circuit immediately resets the whole electronics
when the voltage level drops below the lower limit.

Reset pin is usually referred to as Master Clear Reset (MCLR) and serves for external reset of
the microcontroller by applying logic zero (0) or one (1) depending on the type of the
microcontroller. In case the brown out is not built in the microcontroller, a simple external
circuit for brown out reset can be connected to this pin.

41
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Serial communication
Parallel connections between the
microcontroller and peripherals
established over I/O ports are the
ideal solution for shorter
distances up to several meters.
However, in other cases, when it
is necessary to establish
communication between two devices on longer distances it is obviously not possible to use
parallel connections. Then, serial communication is the best solution.

42
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Chapter- 12 (Arduino)
What is an Arduino?
Arduino is an open-source platform used for building electronics projects. Arduino consists of
both a physical programmable circuit board (often referred to as a microcontroller) and a piece
of software, or IDE (Integrated Development
Environment) that runs on your computer, used to
write and upload computer code to the physical
board.

The Arduino platform has become quite popular with


people just starting out with electronics, and for good
reason. Unlike most previous programmable circuit
boards, the Arduino does not need a separate piece
of hardware (called a programmer) in order to load
new code onto the board – you can simply use a USB cable. Additionally, the Arduino IDE uses a
simplified version of C++, making it easier to learn to program. Arduino can sense the
environment by receiving input from a variety of sensors and can affect its surroundings by
controlling lights, motors, and other actuators. The microcontroller on the board is
programmed using theArduino programming language (based on Wiring) and the Arduino
development environment (based on Processing). Arduino projects can be stand-alone or they
can communicate with software running on a computer (e.g. Flash, Processing,MaxMSP). The
boards can be built by hand or purchased preassembled; the software can be downloaded for
free.

Pin configuration of ATmega 328

43
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Chapter- 13 (LED)

A light-emitting diode (LED) is a two-lead semiconductor light source that resembles a basic pn-
junction diode, except that an LED also emits light. When an LED's anode lead has a voltage that
is more positive than its cathode lead by at least the LED's forward voltage drop, current
flows. Electrons are able to recombine with holes within the device, releasing energy in the form
of photons. This effect is called electroluminescence, and the color of the light (corresponding to
the energy of the photon) is determined by the energy band gap of the semiconductor.

Simple LED circuit

Type of LEDs
1. Miniature

This is perhaps the most common form of LED available today.


Miniature LEDs are considerably small, and usually available in a
single shape/color. They’re used as indicators on devices such as
cell phones, calculators, and remote controls.

44
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
2. Bi-color and Tri-color

A bi-color LED light has two light-emitting dies in a single


casing. It features three leads and is offered with either a
common anode or common cathode. The wiring for the bi-
color LED is considered “inverse parallel”; that is, one is
forward and one is backward. This means that only one of the
dies can be lit at a time. Current flow alternates between dies
in order to produce color variation. If you alternate the current
at a high enough frequency, it will appear that both lights are
on at the same time, and produce a third color.

Similar to a bi-color LED, the tri-color LED also combines two


light emitting dies in one encasing. What’s different, though,
is there are (usually) three leads instead. There’s a center
lead, which is the common cathode for both LEDs, and on
either side are the outer leads, which are the anodes to the
separate LEDs. This design allows for both dies to be lit either
separately or together which, when the colors are combined,
produce a third color. Please note that while this example describes a common cathode-based
design, tri-color LEDs are available in either a common anode or common cathode
configuration.

3. Alphanumeric

The alphanumeric LED light has fallen


in popularity in recent years. While
some point to the cancellation of the
television show 24, the decline is
actually due in large part to the
increased sophistication of LCDs, which
offer greater visual flexibility and much
less power consumption.
As the image goes to show, there are four subtypes of the alphanumeric display. The 7-segment
handles all numbers and only a limited set of letters; 14- and 16-segment displays are referred
to as the “starburst” displays: They can cover the full 26-character Roman alphabet in upper
case as well as numerals 0-9. You’ll note that the only difference between the two is a break on
the top and bottom bars on the 16-segment digit. This is done to improve the look of some

45
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
characters. Last is the matrix which is, obviously, the most versatile of the four. It covers the full
alphabet (both in upper case and lower case), all numbers and a full variety of symbols.

Lighting LEDs
Lighting LEDs (also referred to as LED lamps, LED bars, or
illuminators) come in many different shapes and sizes,
including the popular Edison light bulb design. As
mentioned throughout this article, heat is the enemy of
an LED. An example of ways in which manufacturers are
addressing this issue with these more commonly used
LEDs is they incorporate an aluminum/ceramic body with fins to increase the total area for heat
to escape.

46
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Chapter- 14 (Basic Output Programs)

// WAP to blink 8 LEDs together


// Coded at Optimus Research Labs Pvt. Ltd., By Meharban Singh at Pitampura. Contact :
9717663142

void setup()
{
//**************Code to set the mode to output **********
for(int i=0;i<8;i++)
{
pinMode(i,OUTPUT);
digitalWrite(i,LOW);
}
}
void loop()
{
//**************Code to turn ON**********
for (int i=0;i<8;i++)
{
digitalWrite (i,HIGH);
}
delay(1000);
//**************Code to turn OFF**********
for(int i=0;i<8;i++)
{
digitalWrite(i,LOW);
}
delay(1000);
}

47
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
// WAP to make a simple home automation system that turns on a tube light, a fan
and a TV all at one go. Three appliances are switched off one after the other

// Coded at Optimus Research Labs Pvt. Ltd., By Meharban Singh at Pitampura. Contact :
9717663142

void setup()
{
for(int i=2;i<5;i++)
pinMode(i,OUTPUT); // Pins 2,3 & 4 are used to connect relays(Active HIGH)
}
void loop()
{
for(int i=0;i<3;i++)
{
digitalWrite(i,LOW); // Switch ON all the three appliances
}
delay(10000);
for(int i=0;i<3;i++)
{
digitalWrite(i,HIGH); // Switch OFF three appliances one by one
delay(1000);
}
}

48
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
// WAP to make a moving lights LED pattern with one LED moving right

// Coded at Optimus Research Labs Pvt. Ltd., By Meharban Singh at Pitampura. Contact :
9717663142

void setup()
{
For (int i=0;i<8;i++)
{
pinMode(i, OUTPUT); // initialize the digital pins as an output.
// This can be done in one go using a loop.
}
}

void loop()
{
for (int i=0;i<8;i++)
{
digitalWrite(i, HIGH);
delay(200);
digitalWrite(i, LOW);
}
}

49
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
// WAP to make a moving lights LED pattern with one LED moving right and then left

// Coded at Optimus Research Labs Pvt. Ltd., By Meharban Singh at Pitampura. Contact :
9717663142

void setup()
{
for (int i=0;i<8;i++)
{
pinMode(i, OUTPUT); // initialize the digital pins as an output.
} //This can be done in one go using a loop .
}

void loop()
{
for (int i=0;i<8;i++)
{
digitalWrite(i, HIGH);
delay(200);
digitalWrite(i, LOW);
}
for (int i=7;I >=0;i--) // Execute the loop in reverse direction
{
digitalWrite(i, HIGH);
delay(200);
digitalWrite(i, LOW);
}
}

50
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
//WAP to make a LED pattern in which LEDs are switched ON in increasing order and
are switched OFF in reverse order.

// Coded at Optimus Research Labs Pvt. Ltd., By Meharban Singh at Pitampura. Contact :
9717663142

void setup()
{
for (int i=0;i<8;i++)
{
pinMode(i, OUTPUT); // initialize the digital pins as an output.
}
}

void loop()
{
for (int i=0;i<8;i++)
{
digitalWrite(i, HIGH); // LEDs glow one by one
delay(800);
}

delay(1000);

for (int i=7;i>=0;i--)


{
digitalWrite(i, LOW); // LEDs switch off one by one
delay(800);
}

51
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
delay(1000);

for (int i=7;i>=0;i--)


{
digitalWrite(i, HIGH); // LEDs switch off one by one
delay(800);
}

delay(1000);

for (int i=0;i<8;i++)


{
digitalWrite(i, HIGH); // LEDs glow one by one
delay(800);
}
delay(1000);
}

52
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
//WAP to make a LED pattern in which two extreme LEDS glow and move inwards
and outwards

// Coded at Optimus Research Labs Pvt. Ltd., By Meharban Singh at Pitampura. Contact :
9717663142

void setup()
{
for (int i=0;i<8;i++)
{
pinMode(i, OUTPUT); // initialize the digital pins as an output.
}
}

void loop()
{
int j;
j=7; // Dummy variable to enable opposite movement
// Till the time i reaches 3, j will reach 4
for (int i=0;i<=3;i++)
{
digitalWrite(i, HIGH);
digitalWrite(j, HIGH); // Make the pattern
delay(800);
j--;
}

for(int i=0;i<8;i++)
{
digitalWrite(i, LOW); //Erase the pattern
}

53
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
delay(1000);

j=4; // Dummy variable to enable opposite movement


// Till the time i reaches 0, j will reach 7
for (int i=3;i>=0;i--)
{
digitalWrite(i, LOW);
digitalWrite(j, LOW);
delay(800);
j++;
}

delay(1000);
}

54
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
// WAP to make a robotic car dance in three different patterns

// Coded at Optimus Research Labs Pvt. Ltd., By Meharban Singh at Pitampura. Contact :
9717663142

// ********* Code to allot pins to motors


int m1_f=2;
int m1_b=3;
int m2_f=4;
int m2_b=5;

void forward() // Function to move the car forward


{
digitalWrite(m1_f,LOW);
digitalWrite(m2_f,LOW);
}

void backward() // Function to move the car backward


{
digitalWrite(m1_b,LOW);
digitalWrite(m2_b,LOW);
}

void right() // Function to move the car right


{
digitalWrite(m1_f,LOW);
digitalWrite(m2_b,LOW);
}

void left() // Function to move the car left


{
digitalWrite(m1_b,LOW);

55
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
digitalWrite(m2_f,LOW);
}
void stop1() // Function to stop the car
{
for(int i=2;i<6;i++)
{
digitalWrite(i,HIGH);
}
}

//******** Code for different patterns

void pattern1()
{
forward();
delay(2000);
stop1();
delay(2000);
backward();
delay(2000);
stop1();
delay(2000);
right();
delay(2000);
stop1();
delay(2000);
left();
delay(2000);
stop1();
delay(2000);
}

56
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
void pattern2()
{
for(int rep=0;rep<=4;rep++)
{
right();
delay(500);
stop1();
delay(2000);
left();
delay(500);
stop1();
delay(2000);
}
}

void pattern3()
{
for(int rep=0;rep<5;rep++)
{
forward();
delay(1000);
stop1();
delay(2000);
backward();
delay(1000);
stop1();
delay(2000);
}
}

57
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
void setup()
{
for (int i=2;i<6;i++)
{
pinMode(i,OUTPUT);
digitalWrite(i,HIGH); // Set all pins high initially since our H-Bridge
// Circuit is Active LOW
}

delay(5000);
}
void loop()
{
pattern1();
delay(4000) ;

pattern2();
delay(4000) ;

pattern3();
delay(4000);
}

58
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
//WAP to make Diwali lights with 5 different patterns. Each pattern is repeated 5
times

// Coded at Optimus Research Labs Pvt. Ltd., By Meharban Singh at Pitampura. Contact :
9717663142

void setup()
{
for(int i=0;i<8;i++)
{
pinMode(i,OUTPUT);
digitalWrite(i,LOW); // Set all LEDs to LOW since we have connected
// LEDs in CC configuration
}
}

void loop()
{
//********************Code for Pattern 1**************
for(int j=0;j<5;j++) // Pattern is repeated five times
{
for (int i=0;i<8;i++)
{
digitalWrite (i,HIGH);
}

delay(1000);

for(int i=0;i<8;i++)
{
digitalWrite(i,LOW);

59
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
}
delay(1000);
}

//********************Code for Pattern 2**************


for(int j=0;j<5;j++) // Pattern is repeated five times
{
for (int i=0;i<8;i++)
{
digitalWrite (i,HIGH);
delay(500);
digitalWrite(i,LOW);
}
}

//********************Code for Pattern 3**************


for(int j=0;j<5;j++) // Pattern is repeated five times
{
for(int i=7;i>=0;i--)
{
digitalWrite(i,HIGH);
delay(500);
digitalWrite(i,LOW);
}
}

//********************Code for Pattern 4**************


for(int j=0;j<5;j++) // Pattern is repeated five times
{
for (int i=0;i<4;i++)
{

60
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
digitalWrite (i,HIGH);
digitalWrite(7-i,HIGH);

delay(500);
digitalWrite (i,LOW);
digitalWrite(7-i,LOW);
}
}

//********************Code for Pattern 5**************


for(int j=0;j<5;j++) // Pattern is repeated five times
{
for(int i=3;i>=0;i--)
{
digitalWrite (i,HIGH);
digitalWrite(7-i,HIGH);
delay(500);
digitalWrite (i,LOW);
digitalWrite(7-i,LOW);
}
}
}

61
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
//WAP to code yet another LED pattern

// Coded at Optimus Research Labs Pvt. Ltd., By Meharban Singh at Pitampura. Contact :
9717663142

void setup()
{
for(int i=0;i<8;i++)
{
pinMode(i,OUTPUT);
}
}

void loop()
{
int j=4;
for(int i=0;i<4;i++)
{
digitalWrite(i,HIGH);
digitalWrite(j,HIGH);
delay(500);
j++;
}
int y=4;
for(int i=0;i<4;i++)
{
digitalWrite(i,LOW);
digitalWrite(y,LOW);
delay(500);
y++;
}
}

62
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Chapter- 15(Seven Segment)

What is a seven segment display ?

A seven-segment display (SSD), or seven-segment indicator, is a form of


electronic display device for displaying decimal numerals that is an alternative to
the more complex dot matrix displays. Seven-segment displays are widely used
in digital clocks, electronic meters, and other electronic devices for displaying
numerical information.

A seven segment is generally available in ten pin package. While eight pins
correspond to the eight LEDs, the remaining two pins (at middle) are common
and internally shorted. These segments come in two configurations, namely,
Common cathode (CC) and Common anode (CA). In CC configuration, the
negative terminals of all LEDs are connected to the common pins. The common is
connected to ground and a particular LED glows when its corresponding pin is given high. In CA
arrangement, the common pin is given a high logic and the LED pins are given low to display a
number.

Interfacing with microcontroller

63
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Working with more than one display
Let’s say we want to print 1234 on the display. In order to accomplish this task we would
require 4 seven segment displays. If we try to devote dedicated pins for every display, we
would require 32 I/O pins for this purpose. Here is a flow chart that will help you understand
the concept of POV.

The data lines are shared between all seven segment displays. So if you write 1 to the data bus
and enable the first segment, you will have to first step. Likewise, write 2 to data bus and
enable the second segment. Similarly do the third and fourth step. Once this cycle is finished,
repeat the step above steps at a frequency of 50 Hz (or higher if your controller has nothing
else to do).

Common anode data for seven segment

64
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Chapter- 16 (Programs based on Seven Segment Displays)

//WAP to make a 0 to 9 counter using a single seven segment display

// Coded at Optimus Research Labs Pvt. Ltd., By Meharban Singh at Pitampura. Contact :
9717663142

intseven_seg[10][8] = // Array containing the seven segment data for CA configuration


{
{0,0,0,0,0,0,1,1},
{1,0,0,1,1,1,1,1},
{0,0,1,0,0,1,0,1},
{0,0,0,0,1,1,0,1},
{1,0,0,1,1,0,0,1},
{0,1,0,0,1,0,0,1},
{1,1,0,0,0,0,0,1},
{0,0,0,1,1,1,1,1},
{0,0,0,0,0,0,0,1},
{0,0,0,1,1,0,0,1}
};

void setup()
{
for(int i=0;i<8;i++)
{
pinMode(i,OUTPUT);
digitalWrite(i,HIGH);
}
}

void loop()

65
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
{
for (int count=0;count<10;count++)
{
for(int i=0;i<8;i++)
{
digitalWrite(i,seven_seg[count][i]); // Place the content of one
} // complete row on the pins 0-7

delay(1000); // Give some time to display a particular number


}
}

66
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
//Program to run a counter from 0 to 99 on a 7 segment display

// Coded at Optimus Research Labs Pvt. Ltd., By Meharban Singh at Pitampura. Contact :
9717663142

byteones_segment=8;
bytetens_segment=9;
byte counter;

bytesegment[10][8]=
{
{0,0,0,0,0,0,1,1},
{1,0,0,1,1,1,1,1},
{0,0,1,0,0,1,0,1},
{0,0,0,0,1,1,0,1},
{1,0,0,1,1,0,0,1},
{0,1,0,0,1,0,0,1},
{0,1,0,0,0,0,0,1},
{0,0,0,1,1,1,1,1},
{0,0,0,0,0,0,0,1},
{0,0,0,0,1,0,0,1}};

void setup()
{
for (int i=0;i<9;i++)
{
pinMode(i,OUTPUT);
digitalWrite(i,HIGH);
}
}

67
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
void loop()
{
int ones;
int tens; // variables to separate the value of one's digit and ten's digit

for (counter=0;counter<100;counter++)
{
ones=counter%10; // Formula to calculate the value of one's place
tens=counter/10; // Formula to calculate the value of ten's place

for (int rep=0;rep<50;rep++)


// Every value of the counter should be displayed multiple times
// times so as to allow the human eye to see that particular number

//****************** Code to display the one's digit ****************


{
digitalWrite(ones_segment,LOW);
digitalWrite(tens_segment,HIGH); // Switch on the seven segment for one's
// digit and switch of the seven segment for ten's digit
for (intpin_num=0;pin_num<8;pin_num++)
{
digitalWrite(pin_num,segment[ones][pin_num]);
}

68
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
delay(3);

//****************** Code to display the ten's digit ****************


{
digitalWrite(ones_segment,LOW);
digitalWrite(tens_segment,HIGH);// Switch on the seven segment for ten's digit and switch
// of the seven segment for one's digit
for (intpin_num=0;pin_num<8;pin_num++)
{
digitalWrite(pin_num,segment[tens][pin_num]);
}
delay(3);
}
}

69
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
// WAP to control a 0-9 counter with increment and decrement switches

// Coded at Optimus Research Labs Pvt. Ltd., By Meharban Singh at Pitampura. Contact :
9717663142

int sw_1=12;
int sw_2=11;
int count=0;

intseven_seg[10][8] =
{
{0,0,0,0,0,0,1,1},
{1,0,0,1,1,1,1,1},
{0,0,1,0,0,1,0,1},
{0,0,0,0,1,1,0,1},
{1,0,0,1,1,0,0,1},
{0,1,0,0,1,0,0,1},
{1,1,0,0,0,0,0,1},
{0,0,0,1,1,1,1,1},
{0,0,0,0,0,0,0,1},
{0,0,0,1,1,0,0,1}
};

void setup()
{
for(int i=0;i<10;i++)
{
pinMode(i,OUTPUT);
digitalWrite(i,HIGH);
}

70
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
pinMode(12,INPUT);
pinMode(11,INPUT);
}
void loop()
{
if (digitalRead(sw_1)==HIGH) // Increment Switch
{
delay(15);
while(digitalRead(sw_1)==HIGH);
delay(15); // Code to handle debounce problem
count++;
}

if(digitalRead(sw_2)==HIGH) // Decrement Switch


{
delay(15);
while(digitalRead(sw_2)==HIGH);
delay(15); // Code to handle debounce problem
count--;
}

if (count>9)
{
count=9;
}

if (count<0)
{
count=0;
}

71
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
// Code to display the count
for(int i=0;i<8;i++)
{
digitalWrite(i,seven_seg[count][i]);
}

72
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
// WAP to make a 0-999 counter on seven segments

// Coded at Optimus Research Labs Pvt. Ltd., By Meharban Singh at Pitampura. Contact :
9717663142

inti,j;
int segment[10][8]=
{
{0,0,0,0,0,0,1,1},
{1,0,0,1,1,1,1,1},
{0,0,1,0,0,1,0,1},
{0,0,0,0,1,1,0,1},
{1,0,0,1,1,0,0,1},
{0,1,0,0,1,0,0,1},
{0,1,0,0,0,0,0,1},
{0,0,0,1,1,1,1,1},
{0,0,0,0,0,0,0,1},
{0,0,0,1,1,0,0,1},
};

intgen,hun,ten,one;

void setup()
{
for(int k=0;k<12;k++)
{
pinMode(k,OUTPUT);
}
}

void loop()

73
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
{
for (i=0;i<1000;i++)
{
//***************** Calculation of hundred's place, Ten's place and one's place
inthun=i/100;
int gen=i%100;
int ten=gen/10;
int one=gen%10;

for(int rep=0;rep<50;rep++)
// Repeat a single number multiple times so as to allow smooth display
{
digitalWrite(9,LOW);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH); // Switch on the hundred's segment

for(j=0;j<8;j++)
{
digitalWrite(j,segment[hun][j]);
}
delay(5);

digitalWrite(9,HIGH);
digitalWrite(10,LOW);
digitalWrite(11,HIGH); // Switch on the ten's segment
for(j=0;j<8;j++)
{
digitalWrite(j,segment[ten][j]);
}

74
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
delay(5);

digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,LOW); // Switch on the one's segment
for(j=0;j<8;j++)
{
digitalWrite(j,segment[one][j]);
}
delay(5);
}
}

75
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Chapter- 17 (Basic input output programs)

// WAP to make a basic robotic car

//********** Block to specify the pin configuration************

intswitch_forward=3;

intswitch_backward=4;

intswitch_right=5;

intswitch_left=6;

int m1_f=7;

int m1_b=8;

int m2_f=9;

int m2_b=10;

//************************************************************

void setup()

for(int i=3;i<7;i++)

pinMode(i,INPUT);

for(int i=7;i<11;i++)

pinMode(i,OUTPUT);

76
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
//*********** Code to specify the actions that need to be taken for different switches

void forward() // Function based approach to move forward

digitalWrite(m1_f,LOW);

digitalWrite(m1_b,HIGH);

digitalWrite(m2_f,LOW);

digitalWrite(m2_b,HIGH);

void backward() // Function based approach to move backward

digitalWrite(m1_f,HIGH);

digitalWrite(m1_b,LOW);

digitalWrite(m2_f,HIGH);

digitalWrite(m2_b,LOW);

void right() // Function based approach to move right

digitalWrite(m1_f,HIGH);

digitalWrite(m1_b,LOW);

digitalWrite(m2_f,LOW);

digitalWrite(m2_b,HIGH);

77
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
}

void left() // Function based approach to move left

digitalWrite(m1_f,LOW);

digitalWrite(m1_b,HIGH);

digitalWrite(m2_f,HIGH);

digitalWrite(m2_b,LOW);

//***********************************************************************

void loop()

for(int i=7;i<11;i++) // Loop to stop both the motors

digitalWrite(i,HIGH);

//*********** Next block reads the status of switches******************

if(digitalRead(switch_forward)==HIGH)

forward();

if(digitalRead(switch_backward)==HIGH)

backward();

78
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
}

if(digitalRead(switch_right)==HIGH)

right();

if(digitalRead(switch_left)==HIGH)

left();

//*****************************************************************

79
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
// This is an improved code for a robotic car. The motors run with full thrust

// Coded at Optimus Research Labs Pvt. Ltd., By Meharban Singh at Pitampura. Contact :
9717663142

//********** Block to specify the pin configuration************

intswitch_forward=3;

intswitch_backward=4;

intswitch_right=5;

intswitch_left=6;

int m1_f=7;

int m1_b=8;

int m2_f=9;

int m2_b=10;

//************************************************************

void setup()

for(int i=3;i<7;i++)

pinMode(i,INPUT);

for(int i=7;i<11;i++)

pinMode(i,OUTPUT);

80
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
}

//*********** Code to specify the actions that need to be taken for different switches

void forward() // Function based approach to move forward

digitalWrite(m1_f,LOW);

digitalWrite(m1_b,HIGH);

digitalWrite(m2_f,LOW);

digitalWrite(m2_b,HIGH);

void backward() // Function based approach to move backward

digitalWrite(m1_f,HIGH);

digitalWrite(m1_b,LOW);

digitalWrite(m2_f,HIGH);

digitalWrite(m2_b,LOW);

void right() // Function based approach to move right

digitalWrite(m1_f,HIGH);

digitalWrite(m1_b,LOW);

digitalWrite(m2_f,LOW);

digitalWrite(m2_b,HIGH);

81
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
}

void left() // Function based approach to move left

digitalWrite(m1_f,LOW);

digitalWrite(m1_b,HIGH);

digitalWrite(m2_f,HIGH);

digitalWrite(m2_b,LOW);

//***********************************************************************

void loop()

for(int i=7;i<10;i++) // Loop to stop both the motors

digitalWrite(i,HIGH);

//*********** Next block reads the status of switches******************

if(digitalRead(switch_forward)==HIGH)

forward();

while(digitalRead(switch_forward)==HIGH);

// The processor remains stuck in this loop until the user doesn't release the switch

82
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
if(digitalRead(switch_backward)==HIGH)

backward();

while(digitalRead(switch_backward)==HIGH);

if(digitalRead(switch_right)==HIGH)

right();

while(digitalRead(switch_right)==HIGH);

if(digitalRead(switch_left)==HIGH)

left();

while(digitalRead(switch_left)==HIGH);

//*****************************************************************

83
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
//WAP to solve the debounce problem

// Coded at Optimus Research Labs Pvt. Ltd., By Meharban Singh at Pitampura. Contact :
9717663142

void setup()

pinMode(11,OUTPUT);

digitalWrite(11,LOW);

pinMode(12,INPUT);

digitalWrite(12,LOW);

int a=0;

void loop()

if(digitalRead(12)==HIGH)

delay(15); //Delay to allow the switch to settle down

while(digitalRead(12)==HIGH); // This loop avoids multiple readings

delay(15);

a=~a; // Complement the value of a

digitalWrite(11,a);

84
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
//WAP to make Diwali lights with 3 different patterns. Each pattern is controlled
with a switch

// Coded at Optimus Research Labs Pvt. Ltd., By Meharban Singh at Pitampura. Contact :
9717663142

void pattern1()

//********************Code for Pattern 1**************

for(int j=0;j<5;j++) // Pattern is repeated five times

for (int i=0;i<8;i++)

digitalWrite (i,HIGH);

delay(1000);

for(int i=0;i<8;i++)

digitalWrite(i,LOW);

delay(1000);

85
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
void pattern2()

//********************Code for Pattern 2**************

for(int j=0;j<5;j++) // Pattern is repeated five times

for (int i=0;i<8;i++)

digitalWrite (i,HIGH);

delay(500);

digitalWrite(i,LOW);

void pattern3()

//********************Code for Pattern 3**************

for(int j=0;j<5;j++) // Pattern is repeated five times

for(int i=7;i>=0;i--)

digitalWrite(i,HIGH);

delay(500);

digitalWrite(i,LOW);

86
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
}

void setup()

for(int i=0;i<8;i++)

pinMode(i,OUTPUT);

digitalWrite(i,LOW); // Set all LEDs to LOW since we

// have connected LEDs in CC configuration

for(int i=8;i<=12;i++)

pinMode(i,INPUT);

// Set all LEDs to LOW since we have connected LEDs in CC configuration

void loop()

if(digitalRead(8)==HIGH)

// If the switch connected to pin 8 is pressed then call pattern1()

pattern1();

87
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
if(digitalRead(9)==HIGH)

// If the switch connected to pin 8 is pressed then call pattern2()

pattern2();

if(digitalRead(10)==HIGH)

// If the switch connected to pin 8 is pressed then call pattern3()

pattern3();

for(int i=0;i<8;i++)

digitalWrite(i,LOW);

// Set all LEDs to LOW since we have connected LEDs in CC configuration

88
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
//WAP to make a password protected system with password:5216, if the password is
correct the LED connected to pin 0 should turn off

// Coded at Optimus Research Labs Pvt. Ltd., By Meharban Singh at Pitampura. Contact :
9717663142

int b=0,c=0,d=0;

void setup()

pinMode(0,OUTPUT);

digitalWrite(0,HIGH);

for(int i=1;i<7;i++)

pinMode(i,INPUT);

void loop()

if(digitalRead(5)==HIGH)

delay(15);

while(digitalRead(5)==HIGH);

delay(15);

b=1;

89
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
if(digitalRead(2)==HIGH && b==1)

delay(15);

while(digitalRead(2)==HIGH);

delay(15);

c=1;

if(digitalRead(1)==HIGH && c==1)

delay(15);

while(digitalRead(1)==HIGH);

delay(15);

d=1;

if(digitalRead(6)==HIGH && d==1 )

delay(15);

while(digitalRead(6)==HIGH);

delay(15);

digitalWrite(0,LOW);

while(1);

90
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Chapter- 18 (PWM)

What is Pulse Width Modulation


PWM is a way of digitally encoding analog
signal levels. Through the use of high-
resolution counters, the duty cycle of a
square wave is modulated to encode a
specific analog signal level. The PWM signal
is still digital because, at any given instant of
time, the full DC supply is either fully on or
fully off. The voltage or current source is
supplied to the analog load by means of a
repeating series of on and off pulses. The on-
time is the time during which the DC supply
is applied to the load, and the off-time is the period during which that supply is switched off.
Given a sufficient bandwidth, any analog value can be encoded with PWM.

Figure shows three different PWM signals.


Figure 1a shows a PWM output at a 10% duty
cycle. That is, the signal is on for 10% of the
period and off the other 90%. Figures 1b and
1c show PWM outputs at 50% and 90% duty
cycles, respectively. These three PWM outputs
encode three different analog signal values, at
10%, 50%, and 90% of the full strength. If, for
example, the supply is 9V and the duty cycle is
10%, a 0.9V analog signal results.

Figure shows a simple circuit that


could be driven using PWM. In the
figure, a 9V battery powers an
incandescent light bulb. If we
closed the switch connecting the
battery and lamp for 50ms, the bulb would receive 9V during that interval. If we then opened
the switch for the next 50ms, the bulb would receive 0V. If we repeat this cycle 10 times a
second, the bulb will be lit as though it were connected to a 4.5V battery (50% of 9V). We say
that the duty cycle is 50% and the modulating frequency is 10Hz.

91
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Chapter- 19 (Programs based on PWM)

//WAP to increase and decrease the brightness of an LED connected to pin 3

// Coded at Optimus Research Labs Pvt. Ltd., By Meharban Singh at Pitampura. Contact :
9717663142

void setup()
{
pinMode(3,OUTPUT);
}

void loop()
{
for(int value=0;value<256;value++)
{
analogWrite(3,value);
delay(50);
}

for(int value=255;value>=0;value--)
{
analogWrite(3,value);
delay(50);
}
}

92
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
//DC motor speed control using inbuilt PWM pin

// Coded at Optimus Research Labs Pvt. Ltd., By Meharban Singh at Pitampura. Contact :
9717663142

void setup()
{
for(int i=2;i<6;i++)
{
pinMode(i,INPUT);
}
pinMode(9,OUTPUT);
}

// Functions to vary the speed of the motor according to the switch pressed
// Please note that the H Bridge circuit we are using is active low so 255 corresponds to the
minimum speed and 0 corresponds to maximum speed

void gear1()
{
analogWrite(9,200); // Slowest speed
while(digitalRead(2)==HIGH); // Stay here until the switch is released
}
void gear2()
{
analogWrite(9,150);
while(digitalRead(3)==HIGH); // Stay here until the switch is released
}
void gear3()
{
analogWrite(9,75);

93
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
while(digitalRead(4)==HIGH); // Stay here until the switch is released
}

void gear4()
{
analogWrite(9,0); // Fastest speed
while(digitalRead(5)==HIGH); // Stay here until the switch is released

void loop()
{
digitalWrite(9,HIGH); // To turn OFF the motor
if(digitalRead(2)==HIGH)
{
gear1();
}
if(digitalRead(3)==HIGH)
{
gear2();
}
if(digitalRead(4)==HIGH)
{
gear3();
}
if(digitalRead(5)==HIGH)
{
gear4();
}
}

94
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Chapter- 20 (RF Module 433 Mhz)

HT12E Encoder
The next IC is HT12E. The HT12E is an encoder. It converts digital signals into suitable form to be
transmitted through EM signals. It is an 8-bit Encoder. The HT12E is usually used for 433 MHz
wireless modules. It is an 18 leg IC.

The pin configurations are as follows:

1. A0: 7 – these are the address pins.


2. GND – this is the ground pin. This pin should also be connected to the –ve of the
battery.
3. Vcc – the VCC pin is where we need to supply the input voltage for the working of the
encoder. It is used to power the IC.

95
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
 Osc1: 2 – these pins are the oscillator input and output pins. For the ordinary circuit,
they are connected to each other with the help of an external resistor.
 TE – this is the transmission enable pin. When this is given true, then data transmission
starts.
 Output – this is an output pin. The data in EM signals is given out from this pin.
 AD0: 3 – these are the data/address pins.

Note - How does a transmitter know to which receiver it has to send the signal to? It depends on
the configuration of the address pins on Both the ICs. For the Tx-Rx pair to work, they should
have the same configuration of the address pins.The address pins can either be grounded, or can
be given Vcc individually. So the corresponding pins on the Tx and Rx have to have the same
configuration.

You can also use a gamepad for the inputs. Remember, gamepads are designed to be active low
in configuration i.e. when you press a key in the gamepad, it sends out ‘0’ and when left
floating, it gives ‘1’.

HT12D Decoder
The next IC is HT12D. The HT12D is a decoder. It decodes signals into suitable form to be
transmitted through EM signals. It is an 8-bit Encoder. The HT12D is usually used for 433 MHz
wireless modules. It is an 18 leg IC.

The pin configurations are as follows:

1. A0: 7 – these are the address pins.


2. Vss – this is the ground pin. This pin should be connected to the –ve of the battery.

96
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
3. VDD – the VCC pin is where we need to supply the input voltage for the working of the
encoder. It is used to power the IC.
4. Osc1: 2 – these pins are the oscillator input and output pins. For the ordinary circuit,
they are connected to each other with the help of an external resistor.
5. DIN – this is the Data input pin. The data input from the receiver module is fed in this
pin.
6. VT – this is an output pin. This pin gives the output as soon as any signal s received by
the receiver.
7. D8: 11 – these are the data pins.

Block Diagram of a RF controlled Car

Transmitter

Receiver

97
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
// WAP for the receiver end of a PC controlled robotic car

// Coded at Optimus Research Labs Pvt. Ltd., By Meharban Singh at Pitampura. Contact :
9717663142

intarsh[4];
int m1_f=6;
int m1_b=7;
int m2_f=8;
int m2_b=9;
void stop1() // Function for moving forward
{
digitalWrite(m1_f,HIGH);
digitalWrite(m2_f,HIGH);
digitalWrite(m1_b,HIGH);
digitalWrite(m2_b,HIGH);
}

void forward() // Function for moving forward


{
digitalWrite(m1_f,LOW);
digitalWrite(m2_f,LOW);
}

void backward() // Function for moving backward


{
digitalWrite(m1_b,LOW);
digitalWrite(m2_b,LOW);

98
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
void right() // Function for moving right
{
digitalWrite(m1_f,LOW);
digitalWrite(m2_b,LOW);
}

void left() // Function for moving left


{
digitalWrite(m1_b,LOW);
digitalWrite(m2_f,LOW);
}

void setup()
{
for(int i=2; i<6;i++)
{
pinMode(i,INPUT);
}

for(int i=6; i<10;i++)


{
pinMode(i,OUTPUT);
digitalWrite(i,HIGH);
}
}

void loop()
{
for(int i=0; i<4;i++)
{
arsh[i]=digitalRead(i+2);

99
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
}
int sum=0;
sum =(arsh[0]*8) + (arsh[1]*4) +(arsh[2]*2) +(arsh[3]*1);
//Simplifying the process ofrecognition of the action to be taken

if (sum==1)
{
forward();
}

if (sum==2)
{
backward();
}

if (sum==3)
{
right();
}

if (sum==4)
{
left();
}

if (sum==15)
{
stop1();
}
}

100
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
//WAP for the transmitter section of a PC controlled car

// Coded at Optimus Research Labs Pvt. Ltd., By Meharban Singh at Pitampura. Contact :
9717663142

//Binary codes that are to be transmitted to the receiver


intarsh[5][4]=
{
{0,0,0,1}, //f
{0,0,1,0}, //b
{0,0,1,1}, //r
{0,1,0,0}, //l
{1,1,1,1}
};

void setup()
{
Serial.begin(9600);
pinMode(13,OUTPUT);
}
intspiderman; // Dummy variable to store the serial input data

void loop()
{
if (Serial.available())
{
spiderman=Serial.read();
}

if(spiderman== 'f')
{

101
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
for(int i=0;i<4;i++)
{
digitalWrite(i+2,arsh[0][i]); // transmit row number zero of the array when
} //'f' is pressed on the keyboard
}

if(spiderman== 'b')
{
for(int i=0;i<4;i++)
{
digitalWrite(i+2,arsh[1][i]); //transmit row number one of the array
} // when 'b' is pressed on the keyboard
}

if(spiderman== 'r')
{
for(int i=0;i<4;i++)
{
digitalWrite(i+2,arsh[2][i]); //transmit row number two of the array when
} //'r' is pressed on the keyboard
}

if(spiderman== 'l')
{
for(int i=0;i<4;i++)
{
digitalWrite(i+2,arsh[3][i]); //transmit row number three of the array
} // when 'l' is pressed on the keyboard

102
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
if(spiderman== 'M')
{
for(int i=0;i<4;i++)
{
digitalWrite(i+2,arsh[4][i]);
}
}
}

103
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Chapter- 21 (DTMF Decoder 8870)

This circuit detects the dial


tone from a telephone line
and decodes the keypad
pressed on the remote
telephone. The dial tone we
heard when we pick up the
phone set is call Dual Tone
Multi-Frequency, DTMF in
short. The name was given
because the tone that we
heard over the phone is
actually make up of two
distinct frequency tone, hence the name dual tone. The DTMF tone is a form of one way
communication between the dialer and the telephone exchange. A complete communication
consists of the tone generator and the tone decoder.

When a key is being pressed on the matrix keypad, it generate a unique tone consisting of two
audible tone frequency. For example, if the key '1' is being press on the phone, the tone you
hear is actually consist of a 697hz & 1209hz sine signal. Pressing key '9' will generate the tone
form by 852hz& 1477hz. The frequency use in the dial tone system is of audible range suitable
for transmission over the telephone cable.

Q4 Q3 Q2 Q1
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
0 1010
* 1011
# 1100
A 1101
B 1110
C 1111
D 0000

104
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
// WAP to make a GSM controlled Car/Home automation system using DTMF
Decoder 8870

// Coded at Optimus Research Labs Pvt. Ltd., By Meharban Singh at Pitampura. Contact :
9717663142

intbinary_data_from_DTMF[4]={0,0,0,0};
int sum=0; // To convert the binary data to decimal equivalent
int m1_f=4;
int m1_b=5;
int m2_f=6;
int m2_b=7;

void forward() // function for moving the car forward


{
digitalWrite(m1_f,LOW);
digitalWrite(m2_f,LOW);
delay(500);
}

void backward() // function for moving the car backward


{
digitalWrite(m1_b,LOW);
digitalWrite(m2_b,LOW);
delay(500);
}
void right() // function for moving the car right
{
digitalWrite(m1_f,LOW);
digitalWrite(m2_b,LOW);
delay(500);
}

105
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
void left() // function for moving the car left
{
digitalWrite(m1_b,LOW);
digitalWrite(m2_f,LOW);
delay(500);
}
//************************* Code for stopping the motors
void stop1()
{
for(int i=4;i<8;i++)
{
digitalWrite(i,HIGH);
}
}
void setup()
{
for(int i=0;i<4;i++)
{
pinMode(i,INPUT); // Connect the binary data lines of 8870 to these pins
}

for(int i=4;i<8;i++)
{
pinMode(i,OUTPUT); // Connect H- Bridges to these pins
digitalWrite(i,HIGH);
}
}

106
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
void loop()
{
//****************** Code to store the data from DTMF in an array
for(int i=0;i<4;i++)
{
binary_data_from_DTMF[i]=digitalRead(i);
}

//***************** Code for conversion of binary data to decimal equivalent


sum = ((binary_data_from_DTMF[0])*1) + ((binary_data_from_DTMF[1])*2) +
((binary_data_from_DTMF[2])*4) + ((binary_data_from_DTMF[3])*8);

//***************** Code for taking the necessary action based on the input

if (sum==1)
{
forward();
}

if (sum==2)
{
backward();
}

if (sum==3)
{
right();
}

if (sum==4)

107
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
{
left();
}

if (sum== 10)
{
stop1();
}
}

108
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Chapter-22 (16x2 LCD)

A 16x2 LCD display is very basic module and is very commonly


used in various devices and circuits. A 16x2 LCD means it can
display 16 characters per line and there are 2 such lines. In this
LCD each character is displayed in 5x7 pixel matrix.

Pin Configuration
This LCD has two registers, namely, Command and Data.

The command register stores the command instructions given to


the LCD. A command is an instruction given to LCD to do a
predefined task like initializing it, clearing its screen, setting the
cursor position, controlling display etc. The data register stores
the data to be displayed on the LCD. The data is the ASCII value
of the character to be displayed on the LCD.

Interfacing with microcontroller

109
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
//WAP to demonstrate the use of a LCD

#include <LiquidCrystal.h>

LiquidCrystaloptimus(12,11,5,4,3,2); // Order of pins is RS, E, D4, D5, D6, D7

void setup()

optimus.begin(16,2);

void loop()

for(intnum=0;num<5;num++)

optimus.setCursor(0,0);

optimus.print("Meharban Singh");

optimus.setCursor(0,1);

optimus.print("Welcomes You All");

delay(500);

optimus.clear();

optimus.home();

optimus.print("To");

optimus.setCursor(0,1);

optimus.print("OPTIMUS Labs");

delay(500);

optimus.clear();

110
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
//WAP to control a LCD using a variable resistor. The value should be printed on PC
when an enter switch is pressed

#include <LiquidCrystal.h>

LiquidCrystaloptimus(12,11,5,4,3,2); // Optimus is an object of class LiquidCrystal

int flag=0; // A dummy flag variable to help us in passing valu from one block of code to another

void setup()

optimus.begin(16,2); // Create a link between arduino and LCD

Serial.begin(9600); // Create a serial link b/w PC and arduino

pinMode(8,INPUT);

void loop()

//************ Code to decide the value to be printed according to the analog input

if((analogRead(A0>=0)) && (analogRead(A0<256)))

optimus.clear();

optimus.setCursor(2,0);

optimus.print("Come and buy");

optimus.setCursor(8,1);

optimus.print("CAKES");

flag=1;

if((analogRead(A0) >=256) && (analogRead(A0)<512))

111
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
optimus.setCursor(2,0);

optimus.print("Come and buy");

optimus.setCursor(6,1);

optimus.print("PASTRIES");

flag=2;

if((analogRead(A0) >=512) && (analogRead(A0) <768))

optimus.setCursor(2,0);

optimus.print("Come and buy");

optimus.setCursor(6,1);

optimus.print("BROWNIES");

flag=3;

if((analogRead(A0) >=768) && (analogRead(A0) <1024))

optimus.setCursor(2,0);

optimus.print("Come and buy");

optimus.setCursor(6,1);

optimus.print("BISCUITS");

flag=4;

112
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
//************** Code to print the selected value over the serial monitor

if(digitalRead(8)==HIGH)

if(flag==1)

Serial.print("CAKES");

if(flag==2)

Serial.print("PASTRIES");

if(flag==3)

Serial.print("BROWNIES");

if(flag==4)

Serial.print("BISCUITS");

delay(200);

optimus.clear(); // Clear display

113
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
//WAP to develop a Washing machine which displays its options on PC as well as LCD

int flag_1=0;

int time_1=1000;

int value=256;

#include <LiquidCrystal.h>

LiquidCrystaloptimus(12,11,5,4,3,2);

void setup()

Serial.begin(9600);

optimus.begin(16,2);

void loop()

if(flag_1==0) // Flag to assure that the menu is displayed only once

optimus.setCursor(0,0);

optimus.print("Enter mode");

Serial.println("Enter mode");

optimus.setCursor(0,1);

optimus.print("Light");

Serial.println("Light");

optimus.setCursor(6,1);

optimus.print("Normal");

Serial.println("Normal");

114
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
optimus.setCursor(13,1);

optimus.print("Hvy");

Serial.println("Heavy");

flag_1=1;

delay(5000); //Wait for the user to enter choice

if(Serial.available())

int data;

data=Serial.read();

data = data-48;

if(data == 1)

Serial.println("Light mode started, Washing will take 10 seconds");

time_1=2000;

optimus.clear();

optimus.setCursor(0,0);

optimus.print("Light Mode ");

washing();

optimus.clear();

digitalWrite(9,HIGH);

115
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
if(data == 2)

Serial.println("Normal mode started, Washing will take 15 seconds");

time_1=3000;

value=200;

optimus.clear();

optimus.setCursor(0,0);

optimus.print("Normal Mode ");

washing();

optimus.clear();

digitalWrite(9,HIGH);

if(data == 3)

Serial.println("Heavy mode started, Washing will take 20 seconds");

time_1=4000;

value=100;

optimus.clear();

optimus.setCursor(0,0);

optimus.print("Heavy Mode ");

washing();

optimus.clear();

digitalWrite(9,HIGH);

116
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
flag_1=0;

else

Serial.println("Waiting for your response");

//**************************** Code for Washing Process***********88888

void washing()

Serial.write("Soaking");

optimus.setCursor(0,1);

optimus.print("Soaking");

digitalWrite(9,HIGH);

delay(time_1);

Serial.write("washing");

optimus.setCursor(0,1);

optimus.print("Washing ");

analogWrite(9,value/2);

delay(time_1);

Serial.write("Rinsing");

optimus.setCursor(0,1);

optimus.print("Rinsing ");

analogWrite(9,value/4);

delay(time_1);

117
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Serial.println("Spining");

optimus.setCursor(0,1);

optimus.print("Spining ");

analogWrite(9,value/8);

delay(time_1);

118
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Chapter- 23(Servo Motors)

The servo motor is actually an assembly of four things:


a normal DC motor, a gear reduction unit, a position-
sensing device (usually a potentiometer—a volume
control knob), and a control circuit.

The function of the servo is to receive a control signal


that represents adesired output position of the servo
shaft, and apply power to its DC motor until its shaft
turns to that position. It uses the position-sensing
device to determine the rotational position of the
shaft, so it knows which way the motor must turn to
move the shaft to the commanded position. The shaft
typically does not rotate freely round and round like a
DC motor, but rather can only turn 200 degrees or so
back and forth.

The servo has a 3 wire connection: power, ground, and


control. The power source must be constantly applied;
the servo has its own drive electronics that draw
current from the power lead to drive the motor.

Controlling a servo motor


The control signal is pulse width modulated (PWM),
but here the duration of the positive-going pulse
determines the position of the servo shaft. For
instance, a 1.520 millisecond pulse is the center
position for a Futaba S148 servo. A longer pulse makes
the servo turn to a clockwise-from-center position,
and a shorter pulse makes the servo turn to a counter-
clockwise-from-center position.

The servo control pulse is repeated every 20 milliseconds. In essence, every 20 milliseconds you
are telling the servo, “go here.”

119
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
// WAP to control a servo motor using a variable resistor

#include <Servo.h>

Servo shashi; // create servo object to control a servo

intflexpin = 0; // analog pin used to connect the potentiometer

intflexSensor; // variable to read the value from the analog pin

void setup()

shashi.attach(9); // attaches the servo on pin 9 to the servo object

void loop()

flexSensor = analogRead(flexpin); // reads the value of the potentiometer

//(value between 0 and 1023)

flexSensor = map(flexSensor, 330, 570, 179, 0); // scale it to use it with the servo

(value between 0 and 180)

shashi.write(flexSensor); // sets the servo position according to

thez scaled value

delay(15); // waits for the servo to get there

120
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Chapter- 24 (Stepper motor)

What is a stepper motor?


A Stepper Motor or a step motor is a brushless,
synchronous motor which divides a full rotation into a
number of steps. Unlike a brushless DC motor which
rotates continuously when a fixed DC voltage is applied
to it, a step motor rotates in discrete step angles.
The Stepper Motors therefore are manufactured with
steps per revolution of 12, 24, 72, 144, 180, and 200,
resulting in stepping angles of 30, 15, 5, 2.5, 2, and 1.8
degrees per step.

How a stepper motor works?


Stepper motors work on the principle of
electromagnetism. There is a soft iron or
magnetic rotor shaft surrounded by the
electromagnetic stators. The rotor and
stator have poles which may be teethed or
not depending upon the type of stepper.
When the stators are energized the rotor
moves to align itself along with the stator
(in case of a permanent magnet type
stepper) or moves to have a minimum gap
with the stator (in case of a variable
reluctance stepper). This way the stators
are energized in a sequence to rotate the
stepper motor.

121
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
Stepper motor sequence

The top electromagnet (1) is turned on, attracting the nearest


teeth of a gear-shaped iron rotor. With the teeth aligned to
electromagnet 1, they will be slightly offset from electromagnet 2

The top electromagnet (1) is turned off, and the right


electromagnet (2) is energized, pulling the nearest teeth slightly to
the right. This results in a rotation of 3.6° in this example.

The bottom electromagnet (3) is energized; another 3.6° rotation


occurs.

The left electromagnet (4) is enabled, rotating again by 3.6°.


When the top electromagnet (1) is again enabled, the teeth in the
sprocket will have rotated by one tooth position; since there are
25 teeth, it will take 100 steps to make a full rotation in this
example.

122
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
//WAP to rotate a stepper motor

// Coded at Optimus Research Labs Pvt. Ltd., By Meharban Singh at Pitampura. Contact :
9717663142

int in_1 =8; // Allot the pins that are to be connected to 4 windings of motor

int in_2 =9;

int in_3 =10;

int in_4 =11;

int t=2; // Time gap between sequence

void setup()

for (int i=8;i<12;i++)

pinMode(i,OUTPUT);

digitalWrite(in_1,HIGH);

digitalWrite(in_2,HIGH);

void loop()

digitalWrite(in_1,HIGH); // Activate winding number 1 and 2

digitalWrite(in_2,HIGH);

123
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142
delay(t);

digitalWrite(in_1,LOW);

digitalWrite(in_2,LOW);

digitalWrite(in_2,HIGH); // Activate winding number 2 and 3

digitalWrite(in_3,HIGH);

delay(t);

digitalWrite(in_2,LOW);

digitalWrite(in_3,LOW);

digitalWrite(in_3,HIGH); // Activate winding number 3 and 4

digitalWrite(in_4,HIGH);

delay(t);

digitalWrite(in_3,LOW);

digitalWrite(in_4,LOW);

digitalWrite(in_4,HIGH); // Activate winding number 4 and 1

digitalWrite(in_1,HIGH);

delay(t);

digitalWrite(in_4,LOW);

digitalWrite(in_1,LOW);

124
Optimus Research Labs Pvt Ltd, Pitampura, M: 9717663142

S-ar putea să vă placă și