Ultimate Arduino Handbook
Ultimate Arduino Handbook
0
Arduino UNO image courtesy of Reichelt.com
Ohm's Law
Kirchhoff's Law
pH Scale Chart
Electron Basics
Resistor Essentials
Semiconductor Essentials
Diode Basics
Transistor Basics
Battery Ah Capacities
Capacitance Keynotes
Inductor Essentials
Binary Arithmetic
Practical Gates
Multiple-Input Gates
Digital Pins
Memory
Sketch Basics
Blink
Fade
Button
Debounce
You give prominent credit and link love where credits due, and prominently pass
these same CC BY SA rights onto others within your remix;
You use a completely different title and ecover design, and do not in any way insinuate
that Mark Maffei endorses your remix in any manner whatsoever.
The Publisher of this report has striven to be as accurate and complete as possible in its
creation. However, this does not warrant or represent at any time that the contents within are
suitable for all purposes and intents.
As a reader of this report, you understand and agree to the following; if not, simply
discontinue using this report:
The Publisher and individual authors of this report will not be responsible for any
losses or damages of any kind incurred by the reader; whether directly or indirectly
arising from the use of the information found in this report. Anything you do with
the following information is at your own personal risk.
The Publisher of this report reserves the right to make changes without notice at his
sole discretion and assumes no responsibility or liability whatsoever on the behalf of
the reader of this report.
Readers are advised to exercise due diligence and seek professional help when prudent
to do so.
Simply Stated: This information is for educational, entertainment, and informational research
purposes only.
Lady Ada
R.O. Jones
Bill Wilson
Nate Seidle
Don Lancaster
Tony R. Kuphaldt
Robert Grossblatt
A hearty Thank you! especially goes out to AllAboutCircuits.com, Adafruit, Arduino.cc, and
SparkFun; all of which are hugely monumental in the ongoing research and development of
the Ultimate Arduino Handbook.
Last, but certainly not least, this tasty tome of Maker mayhem is warmly dedicated to
Makers of all persuasions both great and small: Your Arduino creativity never ceases to
amaze me!
Attribution
The Ultimate Arduino Handbook was compiled by Mark Maffei in 2017 via the Design
Science License and equivalent Creative Commons deed: CC BY SA
Fast forward to 2011 and my initial discovery of the Arduino UNO it was love at first sight!
However, finding high quality information was the real issue. I cannot even begin to count
the countless hours Ive spent chasing down information on the web, rather than actually
being creative.
Sure, theres undoubtedly lots of great information, but its spread out all over the place;
and not at all conveniently accessible when I actually needed it (especially considering my
Arduino-related browser bookmarks have gotten completely out of control, over time).
Hence the Ultimate Arduino Handbook was born out of necessity; since nothing stifles
creativity quite like having to stop what youre doing just to go online to find a certain piece
of information, or a particular table or chart that you know exists... but cant remember for
the life of you where you found it or bookmarked it.
As such, the Ultimate Arduino Handbook is an ever-evolving labor of love still in its
infancy; as compared to the sheer amount of pure Arduino-related goodness I will be
continually adding to in the days ahead (thus the v1.x.x designation).
All new editions will be published under the same great open source Creative Commons
license as this one. To know when a new edition is posted, simply subscribe to my blog; as I
do not maintain any type of autoresponder list.
Best regards,
Mark Maffei
Trivia: Contrary to popular belief, It was James Prescott Joule, not Georg Simon Ohm, who
first discovered the mathematical relationship between power dissipation and current
through a resistance.
This discovery, published in 1841, followed the form of the last equation (P = I 2 R), and is
properly known as Joule's Law. However, these power equations are so commonly
associated with the Ohm's Law equations relating voltage, current, and resistance (E=IR ;
I=E/R ; and R=E/I) that they are frequently credited to Ohm.
Attribution: http://www.allaboutcircuits.com/vol_1/chpt_2/4.html
Kirchhoffs Voltage Law (KVL) Summarized: "The algebraic sum of all voltages in a
loop must equal zero"
Kirchhoff's Current Law (KCL) Summarized: "The algebraic sum of all currents
entering and exiting a node must equal zero"
Attribution: http://www.allaboutcircuits.com/vol_1/chpt_4/3.html
To enter numbers in scientific notation into a hand calculator, there is usually a button
marked "E" or "EE" used to enter the correct power of ten. For example, to enter the mass
of a proton in grams (1.67 x 10 -24 grams) into a calculator, enter the following (minus the
brackets):
The [+/-] keystroke changes the sign of the power (24) into a -24. Some calculators allow the
use of the subtraction key [-] to do this, but
I prefer the "change sign" [+/-] key because its more consistent with the use of that key in
other contexts.
If I wanted to enter a negative number in scientific notation into a hand calculator, I would
have to be careful how I used the [+/-] key, lest I change the sign of the power and not the
significant digit value.
The first [+/-] keystroke changes the entry from 3.221 to -3.221; the second [+/-] keystroke
changes the power from 15 to -15.
Attribution: http://www.allaboutcircuits.com/vol_1/chpt_4/5.html
Attribution: http://www.allaboutcircuits.com/vol_1/chpt_9/6.html
The maximum number of electrons that any shell may hold is described by the equation 2n
2 , where n is the principle quantum number. Thusly, the first shell (n=1) can hold 2
electrons; the second shell (n=2) 8 electrons, and the third shell (n=3) 18 electrons:
Principal quantum number n and maximum number of electrons per shell both
predicted by 2(n 2 ), and observed. Orbitals not to scale.
Electron shells in an atom were formerly designated by letter rather than by number. The
first shell (n=1) was labeled K, the second shell (n=2) L, the third shell (n=3) M, the fourth
shell (n=4) N, the fifth shell (n=5) O, the sixth shell (n=6) P, and the seventh shell (n=7) Q.
Attribution: http://www.allaboutcircuits.com/vol_3/chpt_2/2.html
Attribution: http://www.allaboutcircuits.com/vol_1/chpt_3/4.html
Because the Bel was too large of a unit to be used directly, it became customary to apply
the metric prefix deci (meaning 1/10) to it, making it decibels, or dB. Now, the expression
dB is so common that many people do not realize it is a combination of deci- and -Bel,
or that there even is such a unit as the Bel.
To put this into perspective, here is a table contrasting power gain/loss ratios against
decibels:
Attribution: http://www.allaboutcircuits.com/vol_3/chpt_1/5.html
Trivia: An adaptation of the dBm scale for audio signal strength is used in studio recording
and broadcast engineering for standardizing volume levels, and is called the VU scale.
This signal indicator scale is calibrated in according to the dBm scale; albeit it does not
directly indicate dBm for any signal other than steady sine-wave tones. The proper unit of
measurement for a VU meter is volume units.
Resistors are used to control/limit the amount of current in a circuit (or to provide a specific
voltage drop). Heres a typical 330 resistor:
Attribution: http://www.electronicspectrum.com/2012/03/resistor.html
The tolerance band helps you to determine whether the resistor is within acceptable limits,
or if it should be tossed out.
To help you determine which band is the first, heres some handy guidelines:
Some resistors have the color bands grouped/close to one end. Hold the resistor with
the closely grouped bands to your left and read the resistor from left to the right.
With 5% and 10% resistors (most common), the procedure is simple: Hold the resistor
with the silver or gold band to the right and read the resistor from the left to the
right. Since the first band can't be silver or gold, you'll know instantly where to start.
Tip: When determining suitable resistor wattage, a safe rule of thumb is to use resistors
rated at least twice the wattage that your circuit requires for continuous use. Also, the
following attribution link also contains a handy online color code calculator.
Attribution: http://www.hobby-hour.com/electronics/resistorcalculator.php
At the heart of the semiconductor family is the diode; a unidirectional device in which the
cathode (bar) of the diode symbol corresponds to N-type semiconductor and the anode
(arrow) corresponds to the P-type.
A typical silicon diode serves to pass voltage one direction, while blocking voltage in the
opposite direction.
Attribution: http://www.allaboutcircuits.com/vol_3/chpt_2/6.html
Attribution: http://en.wikipedia.org/wiki/File:Diode_pinout_en_fr.svg
Attribution: http://en.wikipedia.org/wiki/Diode
Important Note: Each in-line silicon diode within a circuit has a typical voltage drop of
between 0.6-0.9 volts. So be sure to factor that into your circuit designs. Also, its best to
use diodes rated at least twice the minimum amperage and voltage for your circuit.
Attribution: http://xkcd.com/814/
Attribution: http://www.allaboutcircuits.com/vol_3/chpt_2/7.html
In the figure below, fig(a) is an NPN junction bipolar transistor. Fig(b) represents applying
reverse bias to collector base junction. Note that this increases the width of the depletion
region. The reverse bias voltage could be a few volts to tens of volts for most transistors.
However, the base is manufactured thin. A few majority carriers in the emitter, injected as
minority carriers into the base, actually recombine. See Fig(b) below.
Most of the emitter current of electrons diffuses through the thin base into the collector.
Moreover, modulating the small base current produces a larger change in collector current.
The difference between a PNP and NPN transistor is the polarity of the base emitter diode
junctions, as signified by the direction of the schematic symbol emitter arrow. It points in
the same direction as the anode arrow for a junction diode (i.e. against electron current
flow).
However, the base-collector junction is the same polarity as the base-emitter junction, as
compared to a diode. Compare NPN transistor at (a) with the PNP transistor at (b) below,
noting direction of emitter arrow and supply polarity:
In both cases the base-collector junction is reverse biased. The PNP collector power supply
is negative compared with positive for an NPN transistor.
Junction field effect transistor: (a) Discrete device cross-section, (b) schematic
symbol, (c) integrated circuit device cross-section.
Attribution: http://www.allaboutcircuits.com/vol_3/chpt_2/9.html
The V-MOS device above takes its name from the V-shaped gate region, which increases the
cross-sectional area of the source-drain path. This minimizes losses and allows switching of
higher levels of power. UMOS, a variation using a U-shaped grove, is more reproducible in
manufacture.
Attribution: http://www.allaboutcircuits.com/vol_3/chpt_2/10.html
Attribution: http://en.wikipedia.org/wiki/Transistor
Transistor Equations
Silicon controlled rectifier (SCR): (a) doping profile, (b) BJT equivalent circuit.
Attribution: http://www.allaboutcircuits.com/vol_3/chpt_2/11.html
Attribution: http://www.allaboutcircuits.com/vol_1/chpt_5/1.html
The "table method is a good way to keep the context of Ohm's Law correct for both series
and parallel circuit configurations.
In a table like the one shown below, you are only allowed to apply an Ohm's Law equation
for the values of a single vertical column at a time:
Deriving values horizontally across columns is allowable as per the principles of series and
parallel circuits:
If not, then you must have made a mistake somewhere! While this technique of "cross-
checking" your work is nothing new, using the table to arrange all the data for the cross-
check(s) results in a minimum of confusion.
Attribution: http://www.allaboutcircuits.com/vol_1/chpt_5/6.html
Attribution: http://www.allaboutcircuits.com/vol_1/chpt_6/1.html
Additional resistance can be added to each of the parallel thermocouple circuit branches
below to make their respective resistances more equal.
Without having to custom-size resistors for each branch, so as to make resistances precisely
equal between all the thermocouples...
It is acceptable to simply install resistors with equal values that are significantly higher
than the thermocouple wires' resistances; whereby the wire resistances will have a much
smaller impact on the total branch resistance.
These resistors are called swamping resistors, because their relatively high values over-
shadow or "swamp" the resistances of the thermocouple wires themselves:
If these stresses are kept within the elastic limit of the metal strip (so that the strip does
not permanently deform), the strip can be used as a measuring element for physical force;
the amount of applied force inferred from measuring its resistance.
Such a device is called a strain gauge. Strain gauges are frequently used in mechanical
engineering research and development to measure the stresses generated by machinery.
Note To Self: Could Muscle Wire springs be used as the metal in a strain gauge circuit?!
An amp-hour battery rating is only an approximation of the battery's charge capacity, and
should be trusted only at the current level or time specified by the manufacturer. Such a
rating cannot be extrapolated for very high currents or very long times with any accuracy.
Discharged batteries lose voltage and increase in resistance. The best check for a battery is
a voltage test under load.
Attribution: http://www.allaboutcircuits.com/vol_1/chpt_11/3.html
All batteries in a series bank must have the same amp-hour rating.
All batteries in a parallel bank must have the same voltage rating.
Attribution: http://www.allaboutcircuits.com/vol_1/chpt_11/5.html
Attribution: www.ebatteriestogo.com/SpecSheets/UB645.pdf
Notice the substantial ampacity differences between same-size wires with different types
of insulation. This is due, again, to the thermal limits (60 o , 75 o , 90 o ) of each type of
insulation material.
These ampacity ratings are given for copper conductors in "free air" (maximum typical air
circulation), as opposed to wires placed in conduit or wire trays.
The table fails to specify ampacities for small wire sizes, because the NEC concerns itself
primarily with power wiring (large currents, big wires) vs. wires common to low-current
electronic work.
The letter sequences used to identify conductor types usually refer to properties of the
conductor's insulating layer(s). Some of these letters symbolize individual properties of the
wire while others are simply abbreviations.
For example, the letter "T" by itself means "thermoplastic" as an insulation material, as in
"TW" or "THHN." However, the three-letter combination "MTW" is an abbreviation for
Machine Tool Wire, a type of wire whose insulation is made to be flexible for use in
machines experiencing significant motion or vibration.
Attribution: http://www.allaboutcircuits.com/vol_1/chpt_12/3.html
Notice that the figures for specific resistance in the above table are given in the very
strange unit of "ohms-cmil/ft" (-cmil/ft), This unit indicates what units we are expected to
use in the resistance formula (R=l/A).
The metric unit for specific resistance is the ohm-meter (-m), or ohm-centimeter (-cm),
with 1.66243 x 10 -9 -meters per -cmil/ft (1.66243 x 10 -7 -cm per -cmil/ft).
In the -cm column of the table, the figures are actually scaled as -cm due to their very
small magnitudes. For example, iron is listed as 9.61 -cm, which could be represented as
9.61 x 10 -6 -cm.
When using the unit of -meter for specific resistance in the R=l/A formula, the
length needs to be in meters and the area in square meters.
When using the unit of -centimeter (-cm) in the same formula, the length needs to
be in centimeters and the area in square centimeters.
All these units for specific resistance are valid for any material (-cmil/ft, -m, or -cm).
One might prefer to use -cmil/ft. However, when dealing with round wire where the cross-
sectional area is already known in circular mils.
Conversely, when dealing with odd-shaped busbar or custom busbar cut out of metal stock,
where only the linear dimensions of length, width, and height are known, the specific
resistance units of -meter or -cm may be more appropriate.
Attribution: http://www.allaboutcircuits.com/vol_1/chpt_12/5.html
Resistance values for conductors at any temperature other than the standard temperature
(usually specified at 20 Celsius) on the specific resistance table must be determined via:
For pure metals, this coefficient is a positive number; meaning that resistance
increases with increasing temperature.
For the elements carbon, silicon, and germanium, this coefficient is a negative
number; meaning that resistance decreases with increasing temperature.
For some metal alloys, the temperature coefficient of resistance is very close to zero;
meaning that the resistance hardly changes at all with variations in temperature (a
good property if you want to build a precision resistor out of metal wire!).
Attribution: http://www.allaboutcircuits.com/vol_1/chpt_12/6.html
However, in practice it has been found that the relationship between breakdown voltage
and thickness is not exactly linear. An insulator three times as thick has a dielectric strength
slightly less than 3 times as much. However, for rough estimation use, volt-per-thickness
ratings are fine.
Attribution: http://www.allaboutcircuits.com/vol_1/chpt_12/8.html
Dielectric Material
All other factors being equal, greater permittivity of the dielectric gives greater
capacitance; less permittivity of the dielectric gives less capacitance.
Explanation: Although its complicated to explain, some materials offer less opposition to
field flux for a given amount of field force. Materials with a greater permittivity allow for
Glass, for instance, with a relative permittivity of 7, has seven times the permittivity of a
pure vacuum, and consequently will allow for the establishment of an electric field flux
seven times stronger than that of a vacuum, all other factors being equal.
The following is a table listing the relative permittivities (also known as the "dielectric
constant") of various common substances:
Attribution: http://www.allaboutcircuits.com/vol_1/chpt_13/5.html
Attribution: http://www.allaboutcircuits.com/vol_1/chpt_13/3.html
When capacitors are connected in series, the total capacitance is less than any one of the
series capacitors' individual capacitances. If two or more capacitors are connected in series,
the overall effect is that of a single (equivalent) capacitor having the sum total of the plate
spacings of the individual capacitors.
As we've just seen, an increase in plate spacing, with all other factors unchanged, results in
decreased capacitance. Thus, the total capacitance is less than any one of the individual
capacitors' capacitances.
The formula for calculating the series total capacitance is the same formula as for
calculating parallel resistances:
If two or more capacitors are connected in parallel, the overall effect is that of a single
equivalent capacitor having the sum total of the plate areas of the individual capacitors. As
we've just seen, an increase in plate area, with all other factors unchanged, results
in increased capacitance.
Attribution: http://www.allaboutcircuits.com/vol_1/chpt_13/4.html
Simply stated, the left-hand rule says that the magnetic flux lines produced by a current-
carrying wire will be oriented the same direction as the curled fingers of a person's left
hand (in the "hitchhiking" position), with the thumb pointing in the direction of electron
flow:
Another example is the relay, an electrically-controlled switch contact mechanism that can
be opened and closed (actuated) by a magnetic field when an electromagnet coil is placed
nearby to produce the requisite field (via a current through the coil).
Attribution: http://www.allaboutcircuits.com/vol_1/chpt_14/2.html
What we're trying to do here is show a mathematical relationship between field force and
flux for any chunk of a particular substance, in the same spirit as describing a material's
specific resistance in ohm-cmil/ft instead of its actual resistance in ohms.
This is called the normal magnetization curve, or B-H curve, for any particular material.
Notice how the flux density for any of the above materials (cast iron, cast steel, and sheet
steel) levels off with increasing amounts of field intensity.
Attribution: http://www.allaboutcircuits.com/vol_1/chpt_14/4.html
Faraday was able to mathematically relate the rate of change of the magnetic field flux with
induced voltage.
Note the use of a lower-case letter "e" for voltage; the instantaneous voltage, or voltage at a
specific point in time, rather than a steady, stable voltage:
The "d" terms are standard calculus notation, representing rate-of-change of flux over time.
"N" stands for the number of turns, or wraps, in the wire coil (assuming that the wire is
formed in the shape of a coil for maximum electromagnetic efficiency).
Attribution: http://www.allaboutcircuits.com/vol_1/chpt_14/5.html
Attribution: http://www.allaboutcircuits.com/vol_1/chpt_15/2.html
A fully discharged inductor (no magnetic field), having zero current through it, will initially
act as an open-circuit when attached to a source of voltage (as it tries to maintain zero
current), dropping maximum voltage across its leads.
Over time, the inductor's current rises to the maximum value allowed by the circuit, and the
terminal voltage decreases correspondingly.
Once the inductor's terminal voltage has decreased to a minimum (zero for a "perfect"
inductor), the current will stay at a maximum level, and it will behave essentially as a short-
circuit.
When the switch is first closed, the current is zero, then it increases over time until it is
equal to the battery voltage divided by the series resistance of 1. This behavior is precisely
opposite of a series resistor-capacitor circuit; where current started at a maximum and
capacitor voltage at zero:
Attribution: http://www.allaboutcircuits.com/vol_1/chpt_16/3.html
There are four basic factors of inductor construction determining the amount of inductance
created. These factors all dictate inductance by affecting how much magnetic field flux will
develop for a given amount of magnetic field force (i.e. current through the inductor's wire
coil) as follows:
1) Number Of Turns: All other factors being equal, a greater number of turns of wire in
the coil results in greater inductance; fewer turns of wire in the coil results in less
inductance.
Explanation: More turns of wire means that the coil will generate a greater amount of
magnetic field force (measured in amp-turns) for a given amount of coil current:
2) Coil Area: All other factors being equal, greater coil area (as measured looking
lengthwise through the coil, at the cross-section of the core) results in greater inductance;
less coil area results in less inductance.
Explanation: Greater coil area presents less opposition to the formation of magnetic field
flux, for a given amount of field force (i.e. amp-turns).
3) Coil Length: All other factors being equal, the longer the coil's length, the less
inductance; the shorter the coil's length, the greater the inductance.
Explanation: A longer path for the magnetic field flux to take results in more opposition to
the formation of that flux for any given amount of field force (amp-turns).
An approximation of inductance for any coil of wire can be found with this formula:
Obviously, if permeability () in the equation is unstable, then the inductance (L) will also be
unstable to some degree as the current through the coil changes in magnitude. If the
hysteresis of the core material is significant, this will also have strange effects on the
inductance of the coil.
Inductor designers try to minimize these effects by designing the core in such a way that its
flux density never approaches saturation levels, and so the inductor operates in a more
linear portion of the B/H curve.
Attribution: http://www.allaboutcircuits.com/vol_1/chpt_15/3.html
If inductors are connected together in series (thus sharing the same current, and seeing the
same rate of change in current), then the total voltage dropped as the result of a change in
current will be additive with each inductor.
This results in a greater total voltage than either of the individual inductors alone; greater
voltage for the same rate of change in current means greater inductance.
Thus, the total inductance for series inductors is greater than any one of the individual
inductors' inductances.
The formula for calculating the series total inductance is the same form as for calculating
series resistances:
Since the current through each parallel inductor will be a fraction of the total current, and
the voltage across each parallel inductor will be equal, a change in total current will result in
less voltage dropped across the parallel array than for any one of the inductors considered
separately.
In other words, there will be less voltage dropped across parallel inductors for a given rate
of change in current than for any of those inductors considered separately, because total
current divides among parallel branches. Less voltage for the same rate of change in
current means less inductance.
Attribution: http://www.allaboutcircuits.com/vol_1/chpt_15/4.html
The time constant of the circuit (the amount of time it takes for voltage or current values to
change approximately 63 percent (from their starting values to their final values) in a
transient situation.
Series R/C Circuit: The time constant is equal to the total resistance in ohms
multiplied by the total capacitance in farads.
Series L/R Circuit: The total inductance in henrys divided by the total resistance in
ohms. In either case, the time constant is expressed in units of seconds and
symbolized by the Greek letter "tau" ().
A more universal formula for the determination of voltage and current values in transient
circuits:
Attribution: http://www.allaboutcircuits.com/vol_1/chpt_16/5.html
Wire Symbols
Pushbutton Switch
Momentary switch - normally open
(N.O)
Pushbutton Switch
Momentary switch - normally closed
(N.C)
SPST Relay
Relay open / close connection by an
electromagnet
SPDT Relay
Digital / Common
Ground
Resistor Symbols
Resistor (IEEE)
Resistor reduces the current flow.
Resistor (IEC)
Potentiometer (IEEE)
Adjustable resistor - has 3 terminals.
Potentiometer (IEC)
Variable Resistor /
Rheostat (IEEE)
Adjustable resistor - has 2 terminals.
Variable Resistor /
Rheostat (IEC)
Capacitor Symbols
Capacitor
Capacitor is used to store electric charge. It
acts as short circuit with AC and open circuit
with DC.
Capacitor
Variable Inductor
Varactor / Varicap
Variable capacitance diode
Diode
Tunnel Diode
Transistor Symbols
Misc. Symbols
Bus
Bus
Analog-to-digital
Converts analog signal to digital numbers
converter (ADC)
Digital-to-Analog
Converts digital numbers to analog signal
converter (DAC)
Antenna / aerial
Transmits & receives radio waves
Antenna / aerial
Multiplexer / Mux 2
to 1
Connects the output to selected input line.
Multiplexer / Mux 4
to 1
Demultiplexer /
Connects selected output to the input line.
Demux 1 to 4
Attribution: http://www.rapidtables.com/electric/electrical_symbols.htm
In the binary numeration system, each weight constant is twice as much as the one before
it. The two allowable cipher symbols for the binary system of numeration are 1 and 0,
and are arranged right-to-left in doubling values of weight.
The rightmost place is the ones place, just as with decimal notation. Proceeding to the left,
we have the twos place, the fours place, the eights place, the sixteens place, and so on. For
example, the following binary number can be expressed, just like the decimal number 26, as
a sum of each cipher value times its respective weight constant:
11010 = 2 + 8 + 16 = 26
11010 = (1 x 16) + (1 x 8) + (0 x 4) + (1 x 2) + (0 x 1)
In the above example, were mixing two different kinds of numerical notation. To avoid
unnecessary confusion, we have to denote which form of numeration were using. Typically,
this is done in subscript form, with a 2 for binary and a 10 for decimal; so binary 11010 2
is equal to decimal 2610.
The subscripts are not mathematical operation symbols like superscripts (exponents) are.
They simply indicate what numeration system were using when writing these symbols for
others to read.
Hence, if you see 310, its the number 3 (using decimal numeration).
Conversely, if you see 310, this means three to the tenth power (59,049). If no
subscript is shown, a decimal number is assumed.
Binary is referred to as base two numeration, and decimal as base ten. Additionally, we
refer to each cipher position in binary as a bit rather than the familiar word digit used in
the decimal system.
Attribution: https://www.allaboutcircuits.com/textbook/digital/chpt-1/systems-of-numeration/
To convert a number in binary numeration to its equivalent in decimal form, all you have to
do is calculate the sum of all the products of bits with their respective place-weight
constants. To illustrate:
Remember, a bit value of 1 means that the respective place weight gets added to the
total value, and a bit value of 0 means that the respective place weight does not get
added to the total value. With the above example, we have:
If we encounter a binary number with a dot (.), called a binary point instead of a decimal
point, we follow the same procedure; each place weight to the right of the point is one-half
the value of the one to the left of it (just as each place weight to the right of a decimal point
is one-tenth the weight of the one to the left of it). For example:
Attribution: https://www.allaboutcircuits.com/textbook/digital/chpt-1/decimal-versus-binary-numeration/
To put all of this into perspective, the following chart shows zero to twenty using decimal,
binary, octal, and hexadecimal to contrast these systems of numeration:
Octal and hexadecimal numeration systems would be pointless if not for their ability to be
easily converted to and from binary notation. In essence, they serve as a more human-friendly
shorthand way of denoting a binary number.
Because the bases of octal (eight) and hexadecimal (sixteen) are even multiples of binarys
base (two), binary bits can be grouped together and directly converted to/from their
respective octal or hexadecimal counterparts. With octal, the binary bits are grouped in
We had to group the bits in threes, from the binary point left, and from the binary point
right, adding (implied) zeros as necessary to make complete 3-bit groups. Each octal digit
was translated from the 3-bit binary groups. Binary-to-Hexadecimal conversion is much the
same:
Here we had to group the bits in fours, from the binary point left, and from the binary
point right, adding (implied) zeros as necessary to make complete 4-bit groups:
Likewise, the conversion from either octal or hexadecimal to binary is done by taking each
octal or hexadecimal digit and converting it to its equivalent binary (3 or 4 bit) group, then
putting all the binary bit groups together.
Incidentally, hexadecimal notation is more popular, because binary bit groupings in digital
equipment are commonly multiples of eight (8, 16, 32, 64, and 128 bit), which are also
multiples of 4.
Octal, being based on binary bit groups of 3, doesnt work out evenly with those common
bit group sizings.
Attribution: https://www.allaboutcircuits.com/textbook/digital/chpt-1/octal-and-hexadecimal-numeration/
Of course, we could simply convert the hexadecimal or octal to binary, then convert from
binary to decimal; since we know how to do both, but we can also convert directly.
Because octal is a base-eight numeration system, each place-weight value differs from
either adjacent place by a factor of eight. For example, the octal number 245.37 can be
broken down into place values as such:
The decimal value of each octal place-weight times its respective cipher multiplier can be
determined as follows:
The technique for converting hexadecimal notation to decimal is the same, except that each
successive place-weight changes by a factor of sixteen.
Simply denote each digits weight, multiply each hexadecimal digit value by its respective
weight (in decimal form), then add up all the decimal values to get a total. For example, the
hexadecimal number 30F.A916 can be converted like this:
Attribution: https://www.allaboutcircuits.com/textbook/digital/chpt-1/octal-and-hexadecimal-to-decimal-
conversion/
Because octal and hexadecimal has bases that are multiples of binary (base 2), conversion
back and forth between either hexadecimal or octal and binary is very easy.
Also, because we are so familiar with the decimal system, converting binary, octal, or
hexadecimal to decimal form is relatively easy (simply add up the products of cipher values
and place-weights).
The method which will probably make the most sense is the trial-and-fit method, where
you try to fit the binary, octal, or hexadecimal notation to the desired value as
represented in decimal form.
For example, lets say that I wanted to represent the decimal value of 87 in binary form.
Lets start by drawing a binary number field, complete with place-weight values:
Well, we know that we wont have a 1 bit in the 128s place, because that would
immediately give us a value greater than 87. However, since the next weight to the right
(64) is less than 87, we know that we must have a 1 there.
If we were to make the next place to the right a 1 as well, our total value would be 64 10 +
3210, or 9610. This is greater than 8710, so we know that this bit must be a 0. If we make the
next (16s) place bit equal to 1, this brings our total value to 64 10 + 1610, or 8010, which is
closer to our desired value (8710) without exceeding it:
This trial-and-fit strategy will work with octal and hexadecimal conversions, too. Lets take
the same 8710, and convert it to octal:
If we put a cipher of 1 in the 64s place, we would have a total value of 64 10 (less than
8710). If we put a cipher of 2 in the 64s place, we would have a total value of 128 10 (greater
than 8710). This tells us that our octal numeration must start with a 1 in the 64s place:
Now, we need to experiment with cipher values in the 8s place to try and get a total
(decimal) value as close to 87 as possible without exceeding it. Trying the first few cipher
options, we get:
A cipher value of 3 in the 8s place would put us over the desired total of 87 10, so 2 it is!
Of course, if you were paying attention during the last section on octal/binary conversions,
you will realize that we can take the binary representation of 87 10, which we previously
determined to be 10101112, and easily convert from that to octal to check our work:
Can we do decimal-to-hexadecimal conversion the same way? Sure, but who would want to?
For illustrative purposes. this method is simple to understand, but laborious to carry out.
There is better way to do these conversions; which is essentially the same (mathematically),
but easier to accomplish.
This other method uses repeated cycles of division (using decimal notation) to break the
decimal numeration down into multiples of binary, octal, or hexadecimal place-weight
values.
In the first cycle of division, we take the original decimal number and divide it by the base of
the numeration system that were converting to (binary=2, octal=8, hex=16).
Then, we take the whole-number portion of division result (quotient) and divide it by the
base value again, and so on; until we end up with a quotient of less than 1.
The binary bits are assembled from the remainders of the successive division steps,
beginning with the LSB and proceeding to the MSB. In this case, we arrive at a binary
notation of 10101112. When we divide by 2, we will always end up with either .0 or .5
(i.e. a remainder of either 0 or 1).
As was said before, this repeat-division technique for conversion will work for numeration
systems other than binary. If we were to perform successive divisions using a different
number, such as 8 for conversion to octal, we will necessarily get remainders between 0 and
7. Lets try this with the same decimal number, 87 10:
As with the repeat-division process for integers, each step gives us the next digit (or bit)
further away from the point. With integer (division), we worked from the LSB to the MSB
(right-to-left).
But with repeated multiplication, we worked from the left to the right. To convert a decimal
number > 1, with a < 1 component, we must use both techniques, one at a time. Take the
decimal example of 54.4062510, converting to binary:
Binary Arithmetic
Binary Addition
Adding binary numbers is a very simple task, and very similar to the longhand addition of
decimal numbers. As with decimal numbers, you start by adding the bits (digits) one
column, or place weight, at a time, from right to left. Unlike decimal addition, there is little
to memorize in the way of rules for the addition of binary bits:
0+0=0
1+0=1
0+1=1
1 + 1 = 10
1 + 1 + 1 = 11
Just as with decimal addition, when the sum in one column is a two-bit (two-digit) number,
the least significant figure is written as part of the total sum and the most significant figure
is carried to the next left column. Consider the following examples:
Attribution: https://www.allaboutcircuits.com/textbook/digital/chpt-2/binary-addition/
With addition being easily accomplished, we can perform the operation of subtraction with
the same technique simply by making one of the numbers negative.
For example, the subtraction problem of 7 - 5 is essentially the same as the addition
problem 7 + (-5).
Since we already know how to represent positive numbers in binary, all we need to know
now is how to represent their negative counterparts and well be able to subtract.
Usually we represent a negative decimal number by placing a minus sign directly to the left
of the most significant digit, just as in the example above, with -5.
However, the whole purpose of using binary notation is for constructing on/off circuits that
can represent bit values in terms of voltage (2 alternative values: either high or low).
In this context, we dont have the luxury of a third symbol such as a minus sign, since
these circuits can only be on or off (two possible states). One solution is to reserve a bit
(circuit) that does nothing but represent the mathematical sign:
As you can see, we have to be careful when we start using bits for any purpose other than
standard place-weighted values. Otherwise, 1101 2 could be misinterpreted as the number
thirteen when in fact we mean to represent negative five.
To keep things straight here, we must first decide how many bits are going to be needed to
represent the largest numbers well be dealing with, and then be sure not to exceed that bit
field length in our arithmetic operations.
By using the leftmost bit as a sign indicator and not a place-weighted value, I am sacrificing
the pure form of binary notation for something that gives me a practical advantage: the
representation of negative numbers. The leftmost bit is read as the sign, either positive or
negative, and the remaining bits are interpreted according to the standard binary notation
(left to right, in multiples of two).
As simple as the sign-magnitude approach is, it is not very practical for arithmetic purposes.
For instance, how do I add a negative five (1101 2) to any other number, using the standard
technique for binary addition?
Id have to invent a new way of doing addition in order for it to work, and if I do that, I might
as well just do the job with longhand subtraction; theres no arithmetical advantage to
using negative numbers to perform subtraction through addition if we have to do it with
sign-magnitude numeration, and that was our goal!
Theres another method for representing negative numbers which works with our familiar
technique of longhand addition; and also happens to make more sense from a place-
weighted numeration point of view, called complementation.
With this strategy, we assign the leftmost bit to serve a special purpose, just as we did with
the sign-magnitude approach; defining our number limits just as before.
However, this time, the leftmost bit is more than just a sign bit; rather, it possesses a
negative place-weight value.
With the right three bits being able to represent a magnitude from zero through seven, and
the leftmost bit representing either zero or negative eight, we can successfully represent
any integer number from negative seven (1001 2 = -810 + 110 = -710) to positive seven (01112 =
010 + 710 = 710).
Note that the negative binary numbers in the right column, being the sum of the right three
bits total plus the negative eight of the leftmost bit, dont count in the same progression
as the positive binary numbers in the left column.
Rather, the right three bits have to be set at the proper value to equal the desired
(negative) total when summed with the negative eight place value of the leftmost bit.
Those right three bits are referred to as the twos complement of the corresponding
positive number. Consider the following comparison:
In this case, with the negative weight bit being the fourth bit (place value of negative
eight), the twos complement for any positive number will be whatever value is needed to
add to negative eight to make that positive values negative equivalent.
Thankfully, theres an easy way to figure out the twos complement for any binary number:
simply invert all the bits of that number, changing all 1s to 0s and vice versa (to arrive at
what is called the ones complement) and then add one!
For example, to obtain the twos complement of five (101 2), we would first invert all the bits
to obtain 0102 (the ones complement), then add one to obtain 0112, or -510 in three-
bit/twos complement form.
Lets try this with the former example, converting a positive five to a negative five, but
performing the complementation process on all four bits.
We must be sure to include the 0 (positive) sign bit on the original number, five (0101 2).
First, inverting all bits to obtain the ones complement: 1010 2. Then, adding one, we obtain
the final answer: 10112, or -510 expressed in four-bit, twos complement form.
It is critically important to remember that the place of the negative-weight bit must be
already determined before any twos complement conversions can be done.
If our binary numeration field were such that the eighth bit was designated as the negative-
weight bit (100000002), wed have to determine the twos complement based on all seven of
the other bits. Here, the twos complement of five (0000101 2) would be 11110112. A positive
five in this system would be represented as 00000101 2, and a negative five as 111110112.
Attribution: https://www.allaboutcircuits.com/textbook/digital/chpt-2/negative-binary-numbers/
Binary Subtraction
We can subtract one binary number from another by using the standard techniques
adapted for decimal numbers (subtraction of each bit pair, right to left, borrowing as
needed from bits to the left).
However, if we can leverage the already familiar (and easier) technique of binary addition to
subtract, that would be better. As we just learned, we can represent negative binary
numbers by using the twos complement method (and a negative place-weight bit).
Here, well use those negative binary numbers to subtract through addition. Heres a
sample problem: Subtraction: 710 - 510 Addition equivalent: 710 + (-510)
If all we need to do is represent seven and negative five in binary (twos complemented)
form, all we need is three bits plus the negative-weight bit: positive seven = 0111 2; negative
five = 10112
Another way to understand why we discard that extra bit is to remember that the leftmost
bit of the lower number possesses a negative weight, in this case equal to negative eight.
When we add these two binary numbers together, what were actually doing with the MSBs
is subtracting the lower numbers MSB from the upper numbers MSB. In subtraction, one
never carries a digit or bit on to the next left place-weight. Lets try another example, this
time with larger numbers.
If we want to add -2510 to 1810, we must first decide how large our binary bit field must be.
To represent the largest (absolute value) number in our problem, which is twenty-five, we
need at least five bits, plus a sixth bit for the negative-weight bit.
Lets start by representing positive twenty-five, then finding the twos complement and
putting it all together into one numeration:
Essentially, were representing negative twenty-five by using the negative-weight (sixth) bit
with a value of negative thirty-two, plus positive seven (binary 111 2). Now, lets represent
positive eighteen in binary form, showing all six bits:
Since there were no extra bits on the left, there are no bits to discard. The leftmost bit on
the answer is a 1, which means that the answer is negative, in twos complement form, as it
should be. Converting the answer to decimal form by summing all the bits times their
respective weight values, we get: (1 x -3210) + (1 x 1610) + (1 x 810) + (1 x 110) = -710
Attribution: https://www.allaboutcircuits.com/textbook/digital/chpt-2/binary-subtraction/
One caveat with signed binary numbers is that of overflow, where the answer to an
addition or subtraction problem exceeds the magnitude which can be represented with the
alloted number of bits.
Remember that the place of the sign bit is fixed from the beginning of the problem. With
the last example problem, we used five binary bits to represent the magnitude of the
number, and the left-most (sixth) bit as the negative-weight, or sign, bit.
With five bits to represent magnitude, we have a representation range of 25, or thirty-two
integer steps from 0 to maximum. This means that we can represent a number as high as
+3110 (0111112), or as low as -3210 (1000002).
If we set up an addition problem with two binary numbers, the sixth bit used for sign, and
the result either exceeds +3110 or is less than -3210, our answer will be incorrect.
Lets try adding 1710 and 1910 to see how this overflow condition works for excessive
positive numbers:
The answer (1001002), interpreted with the sixth bit as the -3210 place, is actually equal to
-2810, not +3610 as we should get with +1710 and +1910 added together.
Obviously, this is not correct. What went wrong? The answer lies in the restrictions of the
six-bit number field within which were working. Since the magnitude of the true and proper
sum (3610) exceeds the allowable limit for our designated bit field, we have an overflow
error.
Simply put, six places doesnt give enough bits to represent the correct sum, so whatever
figure we obtain using the strategy of discarding the left-most carry bit will be incorrect.
A similar error will occur if we add two negative numbers together to produce a sum that is
too low for our six-bit binary field.
Lets try adding -1710 and -1910 together to see how this works (or doesnt work, as the case
may be)...
Lets try these two problems again, except this time using the seventh bit for a sign bit, and
allowing the use of 6 bits for representing the magnitude:
By using bit fields sufficiently large to handle the magnitude of the sums, we arrive at the
correct answers. In these sample problems weve been able to detect overflow errors by
performing the addition problems in decimal form and comparing the results with the
binary answers.
For example, when adding +1710 and +1910 together, we knew that the answer was
supposed to be +3610, so when the binary sum checked out to be -2810, we knew that
something had to be wrong.
Although this is a valid way of detecting overflow, it is not very efficient. After all, the whole
idea of complementation is to be able to reliably add binary numbers together and not have
to double-check the result by adding the same numbers together in decimal form!
This is especially true for the purpose of building electronic circuits to add binary quantities
together; the circuit has to be able to check itself for overflow without the supervision of a
Perhaps the most elegant solution is to check for the sign of the sum and compare it
against the signs of the numbers added. Obviously, two positive numbers added together
should give a positive result, and two negative numbers added together should give a
negative result.
Notice that whenever we had a condition of overflow in the example problems, the sign of
the sum was always opposite of the two added numbers: +17 10 plus +1910 giving -2810, or
-1710 plus -1910 giving +2810.
By checking the signs alone we are able to tell that something is wrong. But what about
cases where a positive number is added to a negative number? What sign should the sum be
in order to be correct?
Or, more precisely, what sign of sum would necessarily indicate an overflow error?
The answer to this is equally elegant: there will never be an overflow error when two
numbers of opposite signs are added together! The reason for this is apparent when the
nature of overflow is considered.
Overflow occurs when the magnitude of a number exceeds the range allowed by the size of
the bit field.
The sum of two identically-signed numbers may very well exceed the range of the bit field
of those two numbers; and so in this case, overflow is a possibility. However, if a positive
number is added to a negative number, the sum will always be closer to zero than either of
the two added numbers.
Hence its magnitude must be less than the magnitude of either original number, and so
overflow is impossible. Fortunately, this technique of overflow detection is easily
implemented in electronic circuitry, and it is a standard feature in digital adder circuits
Attribution: https://www.allaboutcircuits.com/textbook/digital/chpt-2/binary-overflow/
What makes binary numeration so important to the application of digital electronics is the
ease in which bits may be represented in physical terms. Because a binary bit can only have
one of two different values (0 or 1), any physical medium capable of switching between two
saturated states may be used to represent a bit.
Consequently, any physical system capable of representing binary bits is able to represent
numerical quantities and potentially has the ability to manipulate those numbers. This is the
basic concept underlying digital computing.
If a transistor circuit is designed to maximize the probability of falling into either one of
these states (and not operating in the linear, or active, mode), it can serve as a physical
representation of a binary bit.
A voltage signal measured at the output of such a circuit may also serve as a representation
of a single bit, a low voltage representing a binary 0 and a (relatively) high voltage
representing a binary 1. Note the following transistor circuit:
In this circuit, the transistor is in a state of saturation by virtue of the applied input voltage
(5 volts) through the two-position switch. Because its saturated, the transistor drops very
little voltage between collector and emitter, resulting in an output voltage of (practically) 0
volts.
If we were using this circuit to represent binary bits, we would say that the input signal is a
binary 1 and that the output signal is a binary 0. Any voltage close to full supply voltage
(measured in reference to ground, of course) is considered a 1 and a lack of voltage is
considered a 0.
Alternative terms for these voltage levels are high (same as a binary 1) and low (same as a
binary 0). A general term for the representation of a binary bit by a circuit voltage is logic
level.
Moving the switch to the other position, we apply a binary 0 to the input and receive a
binary 1 at the output:
The gate shown here with the single transistor is known as an inverter (NOT gate) because
it outputs the exact opposite digital signal as what is input. For convenience, gate circuits
are generally represented by their own symbols rather than by their constituent transistors
and resistors. The following is the symbol for an inverter:
Notice the triangular shape of the gate symbol, much like that of an operational amplifier
(op amp).
The small circle or bubble shown on either the input or output terminal is standard for
representing the inversion function. Remove the bubble from the gate symbol (leaving only
a triangle), and the resulting symbol would no longer indicate inversion, but rather direct
amplification (aka buffer).
Like operational amplifiers, the power supply connections for digital gates are often
omitted from the symbol for simplicitys sake. If we were to show all the necessary
connections needed for operating this gate, the schematic would look something like this:
Power supply conductors are rarely shown in gate circuit schematics, even if the power
supply connections at each gate are. Minimizing lines in our schematic, we get this:
Vcc stands for the constant voltage supplied to the collector of a bipolar junction transistor
circuit, in reference to ground. Those points in a gate circuit marked by the label V cc are all
connected to the same point, and that point is the positive terminal of a DC voltage source,
usually 3.3 or 5 volts.
There are actually quite a few different types of logic gates, most of which have multiple
input terminals for accepting more than one signal. The output of any gate is dependent on
the state of its input(s) and its logical function.
For the previous inverter (NOT gate) circuit illustrated, the truth table looks like this:
Truth tables for more complex gates are, of course, larger than the one shown for the NOT
gate. A gates truth table must have as many rows as there are possibilities for unique input
combinations. A single-input gate like the NOT gate, has only two possibilities, 0 and 1.
However, for a two input gate, there are four possibilities (00, 01, 10, and 11), and thus four
rows to the corresponding truth table. For a three-input gate, there are eight possibilities
(000, 001, 010, 011, 100, 101, 110, and 111), and thus an eight-row truth table.
Bonus points if you realized that the number of truth table rows needed for a gate is equal
to 2 raised to the power of the number of input terminals.
Brief Recap:
In digital circuits, binary bit values of 0 and 1 are represented by voltage signals
measured in reference to a common circuit point called ground; an absence of
voltage represents a binary 0 and the presence of full DC supply voltage represents
a binary 1.
A logic gate is a special form of amplifier circuit designed to input and output logic
level voltages (voltages intended to represent binary bits).
Gate circuits are most commonly represented in a schematic by their own unique
symbols rather than by their constituent transistors and resistors.
Just as with operational amplifiers, the power supply connections to gates are often
omitted in schematic diagrams for the sake of simplicity.
Attribution: https://www.allaboutcircuits.com/textbook/digital/chpt-3/digital-signals-gates/
Practical Gates
The single-transistor inverter circuit illustrated earlier is far too crude to be of practical use.
Real inverter circuits contain more than one transistor to maximize voltage gain (ensuring
final output is either in full cutoff or full saturation), plus circuitry for mitigating accidental
damage. Heres a schematic for a efficient and reliable real-world inverter circuit:
Lets analyze this circuit for the condition where the input is high, or in a binary 1 state.
We can simulate this by showing the input terminal connected to V cc through a switch:
Note that transistor Q1 is not being used in a typical transistor configuration. In reality, Q1
is being used in this circuit as nothing more than a back-to-back pair of diodes.
With the input switch in the up position (connected to V cc), it should be obvious that there
will be no current through the left steering diode of Q1; because there isnt any voltage in
the switch-diode-R1-switch loop to motivate electrons to flow.
However, there will be current through the right steering diode of Q1, as well as through
Q2s base-emitter diode junction and Q4s base-emitter diode junction:
This tells us that in the real gate circuit, transistors Q2 and Q4 will have base current, which
will turn them on to conduct collector current. The total voltage dropped between the base
Now, lets move the input switch to the down position and see what happens:
If we were to measure current in this circuit, it appears as though there is a complete path
for current through diodes Q4 and Q2, the right diode of the pair, and R1... so why no
current through that path?!
Remember that PN junction diodes are very nonlinear devices: they do not even begin to
conduct current until the forward voltage applied across them reaches a certain threshold
(~0.7 volts for silicon and ~0.3 volts for germanium).
And then when they begin to conduct current, they will not drop substantially more than
0.7 volts. When the switch in this circuit is in the down position, the left diode of the
steering diode pair is fully conducting, and so it drops about 0.7 volts across it and no more.
The 0.7 volts provided by the left diodes forward voltage drop is simply insufficient to
allow any electron flow through the series string of the right diode, Q2s diode, and the
R3//Q4 diode parallel sub-circuit; hence no electrons flow through that path.
With no current through the bases of either transistor Q2 or Q4, neither one will be able to
conduct collector current: transistors Q2 and Q4 will both be in a state of cutoff.
Consequently, this circuit configuration allows 100 percent switching of Q2 base current
(and therefore control over the rest of the gate circuit, including voltage at the output) by
diversion of current through the left steering diode.
In the case of our example gate circuit, the input is held high by the switch (connected to
Vcc), making the left steering diode (zero voltage dropped across it). However, the right
steering diode is conducting current through the base of Q2, through resistor R1:
With base current provided, transistor Q2 will be turned on. More specifically, it will be
saturated by virtue of the more-than-adequate current allowed by R1 through the base.
With Q2 saturated, resistor R3 will be dropping enough voltage to forward-bias the base-
emitter junction of transistor Q4, thus saturating it as well:
Due to the presence of diode D2, there will not be enough voltage between the base of Q3
and its emitter to turn it on, so it remains in cutoff. So now lets see what happens if we
reverse the inputs logic level to a binary 0 by actuating the input switch:
Q3, on the other hand, now has sufficient voltage dropped between its base and ground to
forward-bias its base-emitter junction and saturate it, thus raising the output terminal
voltage to a high state. In actuality, the output voltage will be somewhere around 4 volts
depending on the degree of saturation (and any load current)... but still high enough to be
considered a high (1) logic level.
With this, we have a realistic simulation of the inverter circuit: a 1 in gives a 0 out, and
vice versa.
The astute observer will note that this inverter circuits input will assume a high state of
left floating (not connected to either Vcc or ground). With the input terminal left
unconnected, there will be no current through the left steering diode of Q1, leaving all of
R1s current to go through Q2s base; thusly saturating Q2 and driving the circuit output to
a low state:
The tendency for such a circuit to assume a high input state (if left floating) is one shared by
all gate circuits based on this type of design, known as Transistor-to-Transistor Logic (TTL).
This characteristic may be taken advantage of in simplifying the design of a gates output
circuitry; knowing that the outputs of gates typically drive the inputs of other gates. If the
Thus this type of gate circuit has the ability to handle output current in two directions: in and
out.
This is known as sourcing and sinking current, respectively. When the gate output is high,
there is continuity from the output terminal to V cc through the top output transistor (Q3),
allowing electrons to flow from ground, through a load, into the gates output terminal,
through the emitter of Q3, and eventually up to the V cc power terminal (positive side of the
DC power supply):
To simplify this concept, we may show the output of a gate circuit as being a double-throw
switch, capable of connecting the output terminal either to V cc or ground, depending on its
state.
For a gate outputting a high logic level, the combination of Q3 saturated and Q4 cutoff is
analogous to a double-throw switch in the V cc position, providing a path for current
through a grounded load:
Conversely, when a gate circuit is outputting a low logic level to a load, it is analogous to
the double-throw switch being set in the ground position.
Current will then be going the other way if the load resistance connects to V cc: from ground,
through the emitter of Q4, out the output terminal, through the load resistance, and back
to Vcc. In this condition, the gate is said to be sinking current:
However, a standard TTL gate input never needs current to be sourced, only sunk.
That is, since a TTL gate input naturally assumes a high state if left floating, any gate output
driving a TTL input need only sink current to provide a 0 or low input, and need not
source current to provide a 1 or a high logic level at the input of the receiving gate:
To designate open-collector output circuitry within a standard gate symbol, a special marker
is used. Shown here is the symbol for an inverter gate with open-collector output:
Please keep in mind that the high default condition of a floating gate input is only true for
TTL circuitry, and not necessarily for other types, especially for logic gates constructed of
field-effect transistors.
An inverter (aka NOT gate) is one that outputs the opposite state as what is input.
That is, a low input (0) gives a high output (1), and vice versa.
A gate is said to be sourcing current when it provides a path for current between
the output terminal and the positive side of the DC power supply (Vcc). In other words, it
is connecting the output terminal to the power source (+V).
A gate is said to be sinking current when it provides a path for current between the
output terminal and ground. In other words, it is grounding (sinking) the output
terminal.
Gate circuits with totem pole output stages are able to both source and sink current.
Gate circuits with open-collector output stages are only able to sink current, and not
source current.
Open-collector gates are practical when used to drive TTL gate inputs because TTL
inputs dont require current sourcing.
Attribution: https://www.allaboutcircuits.com/textbook/digital/chpt-3/not-gate/
If we were to connect two inverter gates together so that the output of one fed into the
input of another, the two inversion functions would cancel each other out so that there
would be no inversion from input to final output:
A weak signal source that is not capable of sourcing or sinking very much current to a load
(such as with an Arduino) may be boosted by means of two inverters like the pair shown
above. While the logic level is unchanged the full current-sourcing/sinking capabilities of
the final inverter are available to drive a load resistance if needed.
For this purpose, a special logic gate called a buffer is manufactured to perform the same
function as two inverters. Its symbol is simply a triangle, with no inverting bubble on the
output terminal:
The internal schematic diagram for a typical open-collector buffer is not much different
from that of a simple inverter: only one more common-emitter transistor stage is added to
re-invert the output signal:
As before with the inverter circuit, the high input causes no conduction through the left
steering diode of Q1 (emitter-to-base PN junction). All of R1s current goes through the
base of transistor Q2, saturating it:
With a low input signal (input terminal grounded), the analysis looks something like this:
All of R1s current is now diverted through the input switch, thus eliminating base current
through Q2.
This forces transistor Q2 into cutoff so that no base current goes through Q3 either.
With Q3 cutoff as well, Q4 is will be saturated by the current through resistor R4, thus
connecting the output terminal to ground, making it a low logic level.
The schematic diagram for a buffer circuit with totem pole output transistors is a bit more
complex, but the basic principles, and certainly the truth table, are the same as for the
open-collector circuit:
Two inverters (i.e. NOT gates) connected in series so as to invert, then re-invert, a
binary bit perform the function of a buffer.
Buffer gates merely serve the purpose of signal amplification: taking a weak signal
source that isnt capable of sourcing or sinking much current; whereby boosting the
current capacity of the signal so as to be able to drive a load.
Buffers, like inverters, may be made in open-collector output or totem pole output
forms.
Adding more input terminals to a logic gate increases the number of input state
possibilities. With a single-input gate such as the inverter or buffer, there can only be two
possible input states: either the input is high (1) or it is low (0).
Whereas a two input gate has four possibilities (00, 01, 10, and 11), and a three-input gate
has eight possibilities (000, 001, 010, 011, 100, 101, 110, and 111) for input states, and so
forth. The number of possible input states is equal to two to the power of the number of
inputs:
This increase in the number of possible input states obviously allows for more complex gate
behavior. Now, instead of merely inverting or amplifying (buffering) a single high or low
logic level, the output of the gate will be determined by whatever combination of 1s and
0s is present at the inputs.
Each of the main multi-input gate types will be presented in this section; showing its
standard symbol, truth table, and practical operation.
One of the easiest multiple-input gates to understand is the AND gate, so-called because
the output of this gate will be high (1) if and only if all inputs (first input and the second
input and . . .) are high (1). If any input(s) is low (0), the output is guaranteed to be in a
low state as well.
In case you might have been wondering, AND gates are made with more than three inputs,
but this is less common than the simple two-input variety. A two-input AND gates truth
table looks like this:
It is only with all inputs raised to high logic levels that the AND gates output goes high,
thus energizing the LED for only one out of the four input combination states.
A variation on the idea of the AND gate is called the NAND gate. The word NAND is a
verbal contraction of the words NOT and AND.
Essentially, a NAND gate behaves the same as an AND gate with a NOT (inverter) gate
connected to the output terminal.
To symbolize this output signal inversion, the NAND gate symbol has a bubble on the
output line. The truth table for a NAND gate is as one might expect, exactly opposite as
that of an AND gate:
As with AND gates, NAND gates are made with more than two inputs. In such cases, the
same general principle applies: the output will be low (0) if and only if all inputs are high
(1).
The OR Gate
Our next gate to investigate is the OR gate, so-called because the output of this gate will be
high (1) if any of the inputs (first input or the second input or . . .) are high (1). The
output of an OR gate goes low (0) if and only if all inputs are low (0).
As you might have suspected, the NOR gate is an OR gate with its output inverted, just like
a NAND gate is an AND gate with an inverted output:
NOR gates, like all the other multiple-input gates seen thus far, can be manufactured with
more than two inputs. Still, the same logical principle applies: the output goes low (0) if
any of the inputs are made high (1). The output is (1) only when all inputs are low (0).
The last six gate types are all fairly direct variations on three
basic functions: AND, OR, and NOT. The Exclusive-OR gate,
however, is something quite different.
Conversely, they output a low (0) logic level if the inputs are at
the same logic levels.
There are equivalent circuits for an Exclusive-OR gate made up of AND, OR, and NOT gates,
just as there were for NAND, NOR, and the negative-input gates.
Another equivalent circuit for the Exclusive-OR gate uses a strategy of two AND gates with
inverters, set up to generate high (1) outputs for input conditions 01 and 10. A final OR
gate then allows either of the AND gates high outputs to create a final high output:
Exclusive-OR gates are very useful for circuits where two or more binary numbers are to be
compared bit-for-bit, and also for error detection (parity check) and code conversion
(binary to Grey and vice versa).
Brief Recap:
Rule for an AND gate: output is high only if first input and second input are both
high.
Rule for a NAND gate: output is not high if both the first input and the second
input are high.
Rule for a NOR gate: output is not high if either the first input or the second input
are high.
Rule for an Exclusive-OR gate: output is high if the input logic levels are different.
Rule for an Exclusive-NOR gate: output is high if the input logic levels are the same.
Attribution: https://www.allaboutcircuits.com/textbook/digital/chpt-3/digital-signals-gates/
This image was developed using Fritzing. For more circuit examples, see the Fritzing project page
1. Digital pins: Use these pins with digitalRead(), digitalWrite(), and analogWrite().
analogWrite() works only on the pins with the PWM symbol.
2. Pin 13 LED: The only actuator built-in to your board. Besides being a handy target for
your first blink sketch, this LED is very useful for debugging.
3. Power LED: Indicates that your Genuino is receiving power. Useful for debugging.
6. GND and 5V pins: Use these pins to provide +5V power and ground to your circuits.
7. Power connector: This is how you power your Genuino when its not plugged into a USB
port for power. Can accept voltages between 7-12V.
8. TX and RX LEDs: These LEDs indicate communication between your Genuino and your
computer. Expect them to flicker rapidly during sketch upload as well as during serial
communication. Useful for debugging.
9. USB port: Used for powering your Genuino Uno, uploading your sketches to your
Genuino, and for communicating with your Genuino sketch (via Serial. println() etc.).
Digital Pins
The pins on the Arduino can be configured as either inputs or outputs. This document
explains the functioning of the pins in those modes. While the title refers to digital pins, it is
important to note that vast majority of Arduino (Atmega) analog pins, may be configured,
and used, in exactly the same manner as digital pins.
Arduino (Atmega) pins default to inputs, so they don't need to be explicitly declared as
inputs with pinMode() when you're using them as inputs. Pins configured this way are said
to be in a high-impedance state.
Input pins make extremely small demands on the circuit that they are sampling, equivalent
to a series resistor of 100M in front of the pin.
This means that it takes very little current to move the input pin from one state to another,
and can make the pins useful for such tasks as implementing a capacitive touch sensor,
reading an LED as a photodiode, or reading an analog sensor with a scheme such as RCTime.
Often it is useful to steer an input pin to a known state if no input is present. This can be
done by adding a pullup resistor (to +5V), or a pulldown resistor (resistor to ground) on the
input.
There are 20K pullup resistors built into the Atmega chip that can be accessed from
software. These built-in pullup resistors are accessed by setting the pinMode() as
INPUT_PULLUP.
This effectively inverts the behavior of theINPUT mode, where HIGH means the sensor is
off, and LOW means the sensor is on.
The value of this pullup depends on the microcontroller used. On most AVR-based boards,
the value is guaranteed to be between 20k and 50k. On the Arduino Due, it is between
50k and 150k. For the exact value, consult the datasheet of the microcontroller on your
board.
When connecting a sensor to a pin configured with INPUT_PULLUP, the other end should
be connected to ground. In the case of a simple switch, this causes the pin to read HIGH
when the switch is open, and LOW when the switch is pressed.
The pullup resistors provide enough current to dimly light an LED connected to a pin that
has been configured as an input. If LEDs in a project seem to be working, but very dimly, this
is likely what is going on.
The pullup resistors are controlled by the same registers (internal chip memory locations)
that control whether a pin is HIGH or LOW.
Consequently, a pin that is configured to have pullup resistors turned on when the pin is an
INPUT will have the pin configured as HIGH if the pin is then switched to an OUTPUT with
pinMode().
Conversely, an output pin that is left in a HIGH state will have the pullup resistors set if
switched to an input with pinMode().
If you enable its internal 20k pull-up resistor, it will hang at around 1.7V instead of the
expected 5V because the onboard LED and series resistor pull the voltage level down.
In other words, it always returns LOW. If you must use pin 13 as a digital input, set its
pinMode() to INPUT and use an external pull down resistor.
Pins configured as OUTPUT with pinMode() are said to be in a low-impedance state. This
means that they can provide a substantial amount of current to other circuits.
Atmega pins can source (provide positive current) or sink (provide negative current) up to
20mA (relatively safely) and 40 mA (MAX) of current to other devices/circuits.
For example, this is enough current to brightly light up an LED (with a series resistor), or run
several sensors; albeit not enough current to run most relays, solenoids, or motors.
Short circuits on Arduino pins (or attempting to run high current devices from them) can
damage or destroy the output transistors in the pin, or possibly the entire Arduino.
Short circuits often result in a "dead" pin in the microcontroller; yet the remaining chip will
still function adequately. For this reason it is a good idea to connect OUTPUT pins to other
devices with 470 or 1k resistors, unless maximum current draw from the pins is
absolute required for a particular application. If so proceed with caution.
A/D Converter
The Atmega controllers used for the Arduino contain an onboard 6 channel analog-to-
digital (A/D) converter with 10 bit resolution, returning integers from 0 to 1023.
While the main function of the analog pins for most Arduino users is to read analog sensors,
the analog pins also have all the functionality of general purpose input/output (GPIO) pins
(the same as digital pins 0 - 12).
Pin Mapping
The analog pins can be used identically to the digital pins, using the aliases A0 (for analog
input 0), A1, etc.
For example, the code would look like this to set analog pin 0 to an output, and to set it
HIGH:
pinMode(A0,OUTPUT);
digitalWrite(A0,HIGH);
Pullup Resistors
The analog pins also have pullup resistors, which work identically to pullup resistors on the
digital pins. They are enabled by issuing a command such as:
digitalWrite(A0,INPUT_PULLUP);//setpulluponanalogpin0
The analogRead command will not work correctly if a pin has been previously set to
an output; so if this is the case, set it back to an input before using analogRead.
Similarly if the pin has been set to HIGH as an output, the pullup resistor will be set,
when switched back to an input.
The Atmega datasheet also cautions against switching analog pins in close temporal
proximity to making A/D readings (analogRead) on other analog pins. This can cause
electrical noise and introduce jitter in the analog system.
It may be desirable, after manipulating analog pins (in digital mode), to add a short
delay before using analogRead() to read other analog pins.
Memory
The notes on this page are for all boards except the Due, which has a different architecture.
There are three pools of memory in the microcontroller used on avr-based Arduino boards:
SRAM (static random access memory) is where the sketch creates and manipulates
variables when it runs.
Flash memory and EEPROM memory are non-volatile (i.e. the information persists after the
power is turned off). However, SRAM is volatile and will be lost when the power is cycled.
The ATmega328 chip found on the UNO has the following amounts of memory: Flash
- 32kb (of which 0.5bk is used for the bootloader), SRAM - 2kb, and EEPROM 1kb.
The ATmega2560 in the Mega2560 has larger memory space: Flash - 256k bytes (of
which 8k is used for the bootloader), SRAM - 8kb, and EEPROM 4kb.
Notice that there's not much SRAM available in the UNO. It's easy to use it all up by having
lots of strings in your program. For example, a declaration like: charmessage[]="I
supporttheCapeWindproject.";puts 33 bytes into SRAM (each character takes a
byte, plus the '\0' terminator).
This might not seem like a lot, but it doesn't take long to get to 2048, especially if you have
a large amount of text to send to a display, or a large lookup table, for example. If you run
out of SRAM, your program may fail in unexpected ways; it will appear to upload
successfully, but not run, or run strangely.
To check if this is happening, you can try commenting out or shortening the strings or other
data structures in your sketch (without changing the code). If it then runs successfully,
you're probably running out of SRAM.
If your sketch talks to a program running on an external computer, you can try
shifting data or calculations to the computer, reducing the load on the Arduino.
If you have lookup tables or other large arrays, use the smallest data type necessary
to store the values you need; for example, an int takes up two bytes, while a byte
uses only one (but can store a smaller range of values).
If you don't need to modify the strings or data while your sketch is running, you can
store them in flash (program) memory instead of SRAM; to do this, use the
PROGMEM keyword.
Attribution: Arduino.cc
The Arduino Integrated Developer Environment (IDE) software allows you to write
programs and upload them to your board.
1. If you have a reliable Internet connection, you should use the online IDE (Arduino Web
Editor).
It will allow you to save your sketches in the cloud, having them available from any device
and backed up. You will always have the most up-to-date version of the IDE without the
need to install updates or community generated libraries.
To use the online IDE simply follow these instructions. Remember that boards work out-of-
the-box on the Web Editor, no need to install anything.
2. If you would rather work offline, you should use the latest version of the desktop IDE.
To get step-by-step instructions select one of the following link accordingly to your
operating system:
Windows
Mac OS X
The USB cable shown below is necessary to program the board and not just to power it up:
Attribution: Arduino.cc
The UNO automatically draw power from either the USB or an external power supply.
Connect the board to your computer using the USB cable. The green power LED (labelled
PWR) should go on.
If you used the Windows Installer (XP up to 10), it will install drivers automatically as soon
as you connect your board.
If you downloaded and expanded the Zip package or, for some reason, the board wasn't
properly recognized, please follow the procedure below:
While in the Control Panel, navigate to System and Security. Next, click on System.
Once the System window is up, open the Device Manager.
Right click on the "Arduino UNO (COmxx)" port and choose the "Update Driver
Software" option.
Finally, navigate to and select the driver file named "arduino.inf", located in the
"Drivers" folder of the Arduino Software download (not the "FTDI USB Drivers" sub-
directory). If you are using an old version of the IDE (1.0.3 or older), choose the UNO
driver file named "Arduino UNO.inf"
Windows will finish up the driver installation from there. See also: step-by-step screenshots
for installing the Uno under Windows XP.
Open the LED blink example sketch: File > Examples >01.Basics > Blink.
Next, select the entry in the Tools > Board menu that matches your Arduino/Genuino
board.
To find out, you can disconnect your board and re-open the menu; the entry that disappears
should be the Arduino or Genuino board. Reconnect the board and select that serial port.
Now, simply click the "Upload" button in the environment. Wait a few seconds - you should
see the RX and TX leds on the board flashing.
A few seconds after the upload finishes, you should see the pin 13 (L) LED on the board
start to blink (in orange). If it does, congratulations! You've gotten Arduino or Genuino up
and running.
You can learn more about the Desktop IDE via this generic guide on the Arduino IDE with a
few more infos on the Preferences, the Board Manager, and the Library Manager.
Built-in Examples are sketches included in the Arduino Software (IDE), to open them click on
the toolbar menu: File > Examples. These simple programs demonstrate all basic Arduino
commands. They span from a Sketch Bare Minimum to Digital and Analog IO, to the use of
Sensors and Displays.
Writing Sketches
Programs written using Arduino IDE are called sketches. These sketches are written in the
text editor and are saved with the file extension .ino. The editor has features for cutting/
pasting and for searching/replacing text.
The message area gives feedback while saving and exporting and also displays errors. The
console displays text output by the Arduino IDE, including complete error messages and
other information.
The bottom right-hand corner of the window displays the configured board and serial port.
The toolbar buttons allow you to verify and upload programs, create, open, and save
sketches, and open the serial monitor.
NB: Versions of the Arduino IDE prior to 1.0 saved sketches with the extension .pde. It is still
possible to open v1.0 files, albeit you will be prompted to save the sketch with the .ino
extension on save.
Verify
Checks your code for errors compiling it.
Upload
Compiles your code and uploads it to the configured board. See uploading below for
details. Note: If you are using an external programmer with your board, you can hold
down the "shift" key on your computer when using this icon. The text will change to
"Upload using Programmer"
New
Creates a new sketch.
Open
Presents a menu of all the sketches in your sketchbook. Clicking one will open it within
the current window overwriting its content. Note: due to a bug in Java, this menu
doesn't scroll; if you need to open a sketch late in the list, use the File | Sketchbook
menu instead.
Serial Monitor
Opens the serial monitor.
Additional commands are found within the five menus: File, Edit, Sketch, Tools, and Help.
The menus are context sensitive, which means only those items relevant to the work
currently being carried out are available.
File
New: Creates a new instance of the editor, with the bare minimum structure of a
sketch already in place.
Open: Allows to load a sketch file browsing through the computer drives and folders.
Open Recent: Provides a short list of the most recent sketches, ready to be opened.
Sketchbook: Shows the current sketches within the sketchbook folder structure;
clicking on any name opens the corresponding sketch in a new editor instance.
Examples: Any example provided by the Arduino Software (IDE) or library shows up
in this menu item. All the examples are structured in a tree that allows easy access by
topic or library.
Close: Closes the instance of the Arduino Software from which it is clicked.
Save: Saves the sketch with the current name. If the file hasn't been named before, a
name will be provided in a "Save as.." window.
Save as... Allows to save the current sketch with a different name.
Print: Sends the current sketch to the printer according to the settings defined in
Page Setup.
Preferences: Opens the Preferences window where some settings of the IDE may be
customized, as the language of the IDE interface.
Quit: Closes all IDE windows. The same sketches open when Quit was chosen will be
automatically reopened the next time you start the IDE.
Undo/Redo: Goes back of one or more steps you did while editing; when you go
back, you may go forward with Redo.
Cut: Removes the selected text from the editor and places it into the clipboard.
Copy: Duplicates the selected text in the editor and places it into the clipboard.
Copy for Forum: Copies the code of your sketch to the clipboard in a form suitable
for posting to the forum, complete with syntax coloring.
Copy as HTML: Copies the code of your sketch to the clipboard as HTML, suitable for
embedding in web pages.
Paste: Puts the contents of the clipboard at the cursor position, in the editor.
Select All: Selects and highlights the whole content of the editor.
Find: Opens the Find and Replace window where you can specify text to search
inside the current sketch according to several options.
Find Next: Highlights the next occurrence - if any - of the string specified as the
search item in the Find window, relative to the cursor position.
Find Previous: Highlights the previous occurrence - if any - of the string specified as
the search item in the Find window relative to the cursor position.
Sketch
Verify/Compile: Checks your sketch for errors compiling it; it will report memory
usage for code and variables in the console area.
Upload: Compiles and loads the binary file onto the configured board through the
configured Port.
Export Compiled Binary: Saves a .hex file that may be kept as archive or sent to the
board using other tools.
Include Library: Adds a library to your sketch by inserting #include statements at the
start of your code. For more details, see Libraries below. Additionally, from this menu
item you can access the Library Manager and import new libraries from .zip files.
Add File Adds a source file to the sketch (it will be copied from its current
location). The new file appears in a new tab in the sketch window. Files can be
removed from the sketch using the tab menu, accessible by clicking on the small
triangle icon below the serial monitor one on the right side o the toolbar.
Tools
Auto Format: This formats your code nicely (i.e. indents it so that opening and
closing).
Archive Sketch: Archives a copy of the current sketch in .zip format. The archive is
placed in the same directory as the sketch.
Fix Encoding & Reload: Fixes possible discrepancies between the editor char map
encoding and other operating systems char maps.
Serial Monitor: Opens the serial monitor window and initiates the exchange of data
with any connected board on the currently selected Port. This usually resets the
board, if the board supports Reset over serial port opening.
Board: Select the board that you're using. See below for descriptions of the various
boards.
Port: This menu contains all the serial devices (real or virtual) on your machine. It
should automatically refresh every time you open the top-level tools menu.
Note: Ensure that you've selected the correct board from the Boards menu before
burning the bootloader on the target board. This command also set the right fuses.
Help
Here you find easy access to a number of documents that come with the Arduino IDE. You
have access to Getting Started, Reference, this guide to the IDE and other documents
locally, without an Internet connection. The documents are a local copy of the online ones
and may link back to our online website.
Find in Reference: This is the only interactive function of the Help menu; it directly selects
the relevant page in the local copy of the Reference for the function or command under the
cursor.
Sketchbook
The Arduino IDE uses the concept of a sketchbook: a standard place to store your programs
(aka sketches). The sketches in your sketchbook can be opened from the File >
Sketchbook menu or from the Open button on the toolbar.
The first time you run the Arduino software, it will automatically create a directory for your
sketchbook. You can view or change the location of the sketchbook location from with the
Preferences dialog.
Beginning with version 1.0, files are no longer saved as a .pde file, but can still be opened;
automatically renamed with an .ino when saved.
Allows you to manage sketches with more than one file (each of which appears in its own
tab). These can be normal Arduino code files (no visible extension), C files (.c extension), C+
+ files (.cpp), or header files (.h).
Uploading
Before uploading your sketch, you need to select the correct items from the Tools > Board
and Tools > Port menus. The boards are described below.
On the Mac, the serial port is probably something like /dev/tty.usbmodem241 (for
an UNO or Mega2560 or Leonardo) or /dev/tty.usbserial-1B1 (for a Duemilanove or
On Windows, it's probably COM1 or COM2 (for a serial board) or COM4, COM5,
COM7, or higher (for a USB board). To find out, you look for USB serial device in the
ports section of the Windows Device Manager.
Once you've selected the correct serial port and board, press the upload button in the
toolbar or select Upload from the Sketch menu. Current Arduino boards will reset
automatically and begin the upload.
With older pre-Diecimila boards that lack auto-reset, you'll need to press the reset button
on the board just before starting the upload.
On most boards, you'll see the RX and TX LEDs blink as the sketch is uploaded. The Arduino
IDE will display a message when the upload is complete, or show an error.
When you upload a sketch, you're using the Arduino bootloader; a small program that has
been loaded on to the microcontroller on your board. It allows you to upload code without
using any additional hardware.
The bootloader is active for a few seconds when the board resets; then it starts whichever
sketch was most recently uploaded to the microcontroller. The bootloader will blink the on-
board (pin 13) LED when it starts (i.e. when the board resets).
Libraries
Libraries provide extra functionality for use in sketches, e.g. working with hardware or
manipulating data.
To use a library in a sketch, select it from the Sketch > Import Library menu. This will insert
one or more #include statements at the top of the sketch and compile the library with
your sketch.
Because libraries are uploaded to the board with your sketch, they increase the amount of
space it takes up. If a sketch no longer needs a library, simply delete its #include
statements from the top of your code.
Some libraries are included with the Arduino software. Others can be downloaded from a
variety of sources or through the Library Manager. Starting with version 1.0.5 of the IDE,
Third-Party Hardware
Support for third-party hardware can be added to the hardware directory of your
sketchbook directory. Platforms installed there may include board definitions (which
appear in the board menu), core libraries, bootloaders, and programmer definitions.
To install, create the hardware directory, then unzip the third-party platform into its
own sub-directory. Note: Do not use "arduino" as the sub-directory name or you'll
override the built-in Arduino platform.)
To uninstall, simply delete its directory. For details on creating packages for third-
party hardware, see the Arduino IDE 1.5 3rd party Hardware specification.
Serial Monitor
Displays serial data being sent from the Arduino or Genuino board (USB or serial board). To
send data to the board, enter text and click on the "send" button or press enter.
Choose the baud rate from the drop-down that matches the rate passed to Serial.begin in
your sketch. Note: On Windows, Mac or Linux, the Arduino or Genuino board will reset
(rerun your sketch execution to the beginning) when you connect with the serial monitor.
You can also talk to the board from Processing, Flash, MaxMSP, etc (see the interfacing page
for details).
Language Support
Note: on Windows and possibly Linux, this is determined by the locale setting which
controls currency and date formats, not by the language the OS is displayed in.
If your OS language is not supported, the Arduino IDE will default to English. You can return
the software to its default setting of selecting its language based on your operating system
by selecting System Default from the Editor Language drop-down.
This setting will take effect when you restart the Arduino IDE. Similarly, after changing your
operating system's settings, you must restart the Arduino IDE to update it to the new
default language.
Preferences
Some preferences can be set in the preferences dialog (found under the Arduino menu on
the Mac, or File on Windows and Linux). The rest can be found in the preferences file,
whose location is shown in the preference dialog.
Boards
The board selection has two effects: it sets the parameters (e.g. CPU speed and baud rate)
used when compiling and uploading sketches; and sets and the file and fuse settings used
by the burn bootloader command.
Some of the board definitions differ only in the latter, so even if you've been uploading
successfully with a particular selection you'll want to check it before burning the
bootloader. You can find a comparison table between the various boards here.
Arduino IDE includes the built in support for the boards in the following list, all based on
the AVR Core. The Boards Manager included in the standard installation allows to add
support for the growing number of new boards based on different cores like Arduino Due,
Arduino Zero, Edison, Galileo and so on.
Comments
Comments are ignored by the sketch. Theyre included for people reading the code: to
explain what the program does, how it works and/or why it's written the way it is.
It's a good practice to comment your sketches, and to keep the comments up-to-date when
you modify the code; albeit not sloppily, as comments do take up memory resources.
/*Yadayadayada...
*
*Blahblahblah...
*/
Everything between the /* and */ is ignored by the Arduino when it runs the sketch. The *
at the start of each line is only there for clarity, not required. There's also another style for
short single-line comments. These start with // and continue to the end of the line. For
example, in the line:
intledPin=13;//LEDconnectedtodigitalpin13
Variables
A variable is a place for storing a piece of data. It has a name, a type, and a value. For
example, the line from the Blink sketch above declares a variable with the name ledPin,
the typeint, and an initial value of 13.
It's being used to indicate which Arduino pin the LED is connected to. Every time the name
ledPin appears in the code, its value will be retrieved. In this case, the person writing the
program could have chosen not to bother creating the ledPin variable and instead have
simply written 13 everywhere they needed to specify a pin number.
However, the advantage of using a variable is that it's easier to move the LED to a different
pin: you only need to edit the one line that assigns the initial value to the variable.
Often though, the value of a variable will change while the sketch runs. For example, you
could store the value read from an input into a variable. There's more information in the
Variables tutorial.
A function (aka procedure or sub-routine) is a named piece of code that can be used
from elsewhere in a sketch. For example, here's the definition of the setup() function:
voidsetup()
{
pinMode(ledPin,OUTPUT);//setsthedigitalpinasLEDoutput
}
The first line provides information about the function, like its name, "setup". The text
before and after the name specify its return type and parameters: these will be explained
later. The code between the { and } is called the body of the function (what it does).
You can call a function that's already been defined (either in your sketch or as part of the
Arduino language). For example, the line pinMode(ledPin,OUTPUT); calls the
pinMode() function, passing it two parameters: ledPin and OUTPUT. These parameters are
used by the pinMode() function to decide which pin and mode to set.
The pinMode() function configures a pin as either an input or an output. To use it, you pass
it the number of the pin to configure and the constant INPUT or OUTPUT. When configured
as an input, it can detect the state of a sensor like a pushbutton; this is discussed here. As
an output, it can drive an actuator like an LED.
The digitalWrite() functions outputs a value on a pin. For example, the following line sets
the ledPin (pin 13) to HIGH, or 5 volts. Conversely, writing a LOW to ledPin connects it to
ground, or 0 volts.
digitalWrite(ledPin,HIGH);
The delay() causes the Arduino to wait for the specified number of milliseconds before
continuing on to the next line. There are 1000 milliseconds in a second, so the following line
creates a delay of one second: delay(1000);
There are two special functions that are a part of every Arduino sketch: setup() and loop().
The setup() is called once, when the sketch starts. It's a good place to do setup tasks like
setting pin modes or initializing libraries. The loop() function is called over and over and is
heart of most sketches. You need to include both functions in your sketch, even if you don't
need them for anything.
Hardware Required: Only your Arduino or Genuino Board is needed for this example.
Code
The setup() function is called when a sketch starts. Use it to initialize variables, pin modes,
start using libraries, etc. The setup function will only run once, after each powerup or reset
of the board.
After creating a setup() function, the loop() function does precisely what its name suggests,
and loops consecutively, allowing your program to change and respond as it runs. Code in
the loop() section of your sketch is used to actively control the board.
The code below won't actually do anything, but it's structure is useful for copying and
pasting to get you started on any sketch of your own. It also shows you how to make
comments in your code.
Any line that starts with two slashes (//) will not be read by the compiler, so you can write
anything you want after it. The two slashes may be put after functional code to keep
comments on the same line. Commenting your code like this can be particularly helpful in
explaining, both to yourself and others, how your program functions step by step.
LED
220 resistor
Circuit
This example uses the built-in LED that most Arduino and Genuino boards have. This LED is
connected to a digital pin and its number may vary from board type to board type. To make
your life easier, we have a constant that is specified in every board descriptor file.
This constant is LED_BUILTIN and allows you to control the built-in LED easily.
Here are the correspondences between the constant and the digital pin:
D1: Gemma
D6: MKR1000
D13: 101, Due, Intel Edison, Intel Galileo Gen2, Leonardo and Micro, LilyPad and
LilyPad USB, MEGA2560, Mini, Nano, Pro and Pro Mini, UNO, Yn, Zero
If you want to light up an external LED with this sketch, you need to build this circuit;
where you connect one end of the resistor to the digital pin correspondent to the
LED_BUILTIN constant.
Connect the anode of the LED (the long, positive leg) to the other end of the resistor.
Connect the cathode of the LED (the short, negative leg) to the GND. In the diagram below
we show an UNO board that has D13 as the LED_BUILTIN value.
The value of the resistor in series with the LED may be of a different value than 220; the
LED will lit up also with values up to 1K.
Schematic:
After you build the circuit plug your Arduino or Genuino board into your computer, start the
Arduino IDE and enter the code below.
The first thing you do is to initialize LED_BUILTIN pin as an output pin with the line
pinMode(LED_BUILTIN,OUTPUT);
In the main loop, you turn the LED on with the line:
digitalWrite(LED_BUILTIN,HIGH);
This supplies 5 volts to the LED anode. That creates a voltage difference across the pins of
the LED, and lights it up. Then you turn it off with the line:
digitalWrite(LED_BUILTIN,LOW);
That takes the LED_BUILTIN pin back to 0 volts, and turns the LED off. In between the on
and the off, you want enough time for a person to see the change, so the delay() commands
tell the board to do nothing for 1000 milliseconds, or one second.
When you use the delay() command, nothing else happens for that amount of time. Once
you've understood the basic examples, check out the BlinkWithoutDelay example to learn
how to create a delay while doing other things.
Once you've understood this example, check out the DigitalReadSerial example to learn
how read a switch connected to the board.
By passing voltage through a pot and into an analog input on your board, it is possible to
measure the amount of resistance produced by a pot as an analog value.
In this example you will monitor the state of your pot after establishing serial
communication between your Arduino/Genuino and your computer via the Arduino IDE.
Hardware Required:
10k pot
Circuit
Connect the three wires from the pot to your board. The first goes from one of the outer
pins of the pot to ground . The second goes from the other outer pin of the pot to 5 volts.
The third goes from the middle pin of the pot to the analog pin A0.
This image was developed using Fritzing. For more circuit examples, see the Fritzing project page
When the resistance between the center and the side connected to 5 volts is close to zero
(and the resistance on the other side is close to 10k), the voltage at the center pin nears 5
volts.
When the resistances are reversed, the voltage at the center pin nears 0 volts, or ground.
This voltage is the analog voltage that you're reading as an input.
The Arduino and Genuino boards have a circuit inside called an analog-to-digital converter
(ADC) that reads this changing voltage and converts it to a number between 0 and 1023.
When the shaft is turned all the way in one direction, there are 0 volts going to the pin, and
the input value is 0. When the shaft is turned all the way in the opposite direction, there are
5 volts going to the pin and the input value is 1023. In between, analogRead() returns a
number between 0 and 1023; proportional to the voltage being applied to the pin.
Schematic:
In the sketch below, the only thing that you do in the setup function is to begin serial
communications, at 9600 bits of data per second, between your board and your computer
with the command:
Serial.begin(9600);
Next, in the main loop of your code, you need to establish a variable to store the resistance
value (which will be between 0 and 1023, perfect for an int datatype) coming in from your
pot:
intsensorValue=analogRead(A0);
Finally, you need to print this information to your serial monitor window. You can do this
with the command Serial.println() in your last line of code:
Serial.println(sensorValue)
Now, open your Serial Monitor in the Arduino IDE by clicking the icon that looks like a lens,
on the right, in the green top bar (or using the keyboard shortcut Ctrl+Shift+M). You should
see a steady stream of numbers ranging from 0-1023, correlating to the position of the pot.
As you turn your pot, these numbers will respond almost instantly.
Hardware Required:
Arduino/Genuino Board
10k resistor
Hook-up wires
Breadboard
Circuit
Image developed using Fritzing. For more circuit examples, see the Fritzing project page
Connect three wires to the board. The first two, red and black, connect to the two long
vertical rows on the side of the breadboard to provide access to the 5 volt supply and
ground.
The third wire goes from digital pin 2 to one leg of the pushbutton. That same leg of the
button connects through a pull-down resistor (10k) to ground. The other leg of the button
connects to the 5 volt supply.
Pushbuttons or switches connect two points in a circuit when you press them.
When the button is closed (pressed), it makes a connection between its two legs,
connecting the pin to 5 volts, so that the pin reads as HIGH, or 1.
If you disconnect the digital i/o pin from everything, the LED may blink erratically. This is
because the input is "floating"; that is, it doesn't have a solid connection to voltage or
ground, and it will randomly return either HIGH or LOW.
Schematic:
The very first thing that you do will in the setup function is to begin serial communications,
at 9600 bits of data per second, between your board and your computer with the line:
Serial.begin(9600);
Next, initialize digital pin 2, the pin that will read the output from your button, as an input:
pinMode(2,INPUT);
Now that your setup has been completed, move into the main loop of your code. When your
button is pressed, 5 volts will freely flow through your circuit, and when it is not pressed,
the input pin will be connected to ground through the 10k resistor.
This is a digital input, meaning that the switch can only be in either an on state (seen by
your Arduino as a "1", or HIGH) or an off state (seen by your Arduino as a "0", or LOW), with
nothing in between.
The first thing you need to do in the main loop of your program is to establish a variable to
hold the information coming in from your switch.
Since the information coming in from the switch will be either a "1" or a "0", you can use an
int datatype. The variable sensorValue is set to what is being read on D2. You can
accomplish all this with just one line of code:
intsensorValue=digitalRead(2);
Once the bord has read the input, make it print this information back to the computer as a
decimal value. You can do this with the command Serial.println() in our last line of code:
Serial.println(sensorValue);
Now, when you open your Serial Monitor in the Arduino Software (IDE), you will see a
stream of "0"s if your switch is open, or "1"s if your switch is closed.
Hardware Required:
LED
220 resistor
Hook-up wires
Breadboard
Circuit
Connect the anode (the longer, positive leg) of your LED to digital output pin 9 on your
board through a 220 resistor. Connect the cathode (the shorter, negative leg) directly to
ground.
Image developed using Fritzing. For more circuit examples, see the Fritzing project page
Code
After declaring pin 9 to be your ledPin, there is nothing to do in the setup() function of your
code.
The analogWrite() function that you will be using in the main loop of your code requires two
arguments: One telling the function which pin to write to, and one indicating what PWM
value to write.
In order to fade your LED off and on, gradually increase your PWM value from 0 (all the way
off) to 255 (all the way on), and then back to 0 once again to complete the cycle.
The next time through the loop, this change causes brightness to change direction as
well.
AnalogWrite() can change the PWM value very fast, so the delay at the end of the sketch
controls the speed of the fade. Try changing the value of the delay and see how it changes
the fading effect.
Hardware Required:
10k pot
Circuit
Image developed using Fritzing. For more circuit examples, see the Fritzing project page
Connect the three wires from the pot to your board. The first goes to ground from one of
the outer pins of the pot. The second goes to 5 volts from the other outer pin of the pot.
The third goes from the middle pin of the potentiometer to analog input 2.
When the resistance between the center and the side connected to 5 volts is close to zero
(and the resistance on the other side is close to 10k), the voltage at the center pin nears 5
volts. Conversely, when the resistances are reversed, the voltage at the center pin nears 0
volts, or ground. This voltage is the analog voltage that you're reading as an input.
As you may recall, he microcontroller of the board has a circuit inside called an analog-to-
digital converter (ADC) that reads this changing voltage and converts it to a number
between 0 and 1023:
When the shaft is turned all the way in one direction, there are 0 volts going to the
pin, and the input value is 0. Conversely, when the shaft is turned all the way in the
opposite direction, there are 5 volts going to the pin and the input value is 1023.
Schematic:
In the program below, the very first thing that you do will in the setup function is to begin
serial communications, at 9600 bits of data per second, between your board and your
computer with the line:
Serial.begin(9600);
Next, in the main loop of your code, you need to establish a variable to store the resistance
value (which will be between 0 and 1023, perfect for an int datatype) coming in from your
potentiometer:
intsensorValue=analogRead(A0);
To change the values from 0-1023 to a range that corresponds to the voltage the pin is
reading, you'll need to create another variable, a float, and do a little math. To scale the
numbers between 0.0 and 5.0, divide 5.0 by 1023.0 and multiply that by sensorValue:
Finally, you need to print this information to your serial window as. You can do this with the
command Serial.println() in your last line of code:
Serial.println(voltage)
Now, when you open your Serial Monitor in the Arduino IDE, you should see a steady stream
of numbers ranging from 0.0 - 5.0. As you turn the pot, the values change, corresponding to
the voltage coming into pin A0.
Hence this sketch turns on the LED on and then makes note of the time. Then, each time
through loop(), it checks to see if the desired blink time has passed. If it has, it toggles the
LED on or off and makes note of the new time. In this way the LED blinks continuously while
the sketch execution never lags on a single instruction.
Hardware Required:
LED
220 resistor
Circuit
Image developed using Fritzing. For more circuit examples, see the Fritzing project page
Most Arduino and Genuino boards already have an LED attached to pin 13 on the board
itself. If you run this example with no hardware attached, you should see that LED blink.
Schematic:
After you build the circuit plug your board into your computer, start the Arduino IDE, and
Import the code below. The code below uses the millis() function, a command that returns
the number of milliseconds since the board started running its current sketch, to blink an
LED.
Required Hardware:
10K resistor
Hook-up wires
Breadboard
Circuit
Image developed using Fritzing. For more circuit examples, see the Fritzing project page
Connect three wires to the board. The first two, red and black, connect to the two long
vertical rows on the side of the breadboard to provide the 5v and ground access. The third
wire goes from digital pin 2 to one leg of the pushbutton. That same leg of the button
connects through a 10K pull-down resistor to ground. The other leg of the button
connects to the 5 volt supply.
When the pushbutton is open (unpressed) there is no connection between the two legs of
the pushbutton, so the pin is connected to ground (through the pull-down resistor) and we
read a LOW. When the button is closed (pressed), it makes a connection between its two
If you disconnect the digital I/O pin from everything, the LED may blink randomly between
HIGH and LOW; hence the pull-up or pull-down resistor in the circuit.
Schematic:
Without debouncing, pressing the button once may cause unpredictable results. This sketch
uses the millis() function to keep track of the time passed since the button was pressed.
Hardware Required:
10k resistor
Hook-up wires
Breadboard
Circuit
Image developed using Fritzing. For more circuit examples, see the Fritzing project page
Code
The sketch below is based on Limor Fried's version of debounce, but the logic is inverted
from her example. In her example, the switch returns LOW when closed, and HIGH when
open. Here, the switch returns HIGH when pressed and LOW when not pressed.
Hardware Required:
Arduino Board
Breadboard
Hook-up wire
Circuit
Image developed using Fritzing. For more circuit examples, see the Fritzing project page
Pushbuttons or switches connect two points in a circuit when you press them. When the
pushbutton is open (unpressed) there is no connection between the two legs of the
pushbutton.
Because the internal pull-up on pin 2 is active and connected to 5V, we read HIGH when the
button is open. When the button is closed, the Arduino reads LOW because a connection to
ground is completed.
Schematic:
In this sketch, the very first thing that you do will in the setup function is to begin serial
communications, at 9600 bits of data per second, between your Arduino and your computer
with the line:
Serial.begin(9600);
Next, initialize digital pin 2 as an input with the internal pull-up resistor enabled:
pinMode(2,INPUT_PULLUP);
The following line make pin 13, with the onboard LED, an output:
pinMode(13,OUTPUT);
Now that your setup has been completed, move into the main loop of your code. When your
button is not pressed, the internal pull-up resistor connects to 5 volts. This causes the
Arduino to report "1" or HIGH. When the button is pressed, the Arduino pin is pulled to
ground, causing the Arduino report a "0", or LOW.
The first thing you need to do in the main loop of your program is to establish a variable to
hold the information coming in from your switch. Since the information coming in from the
switch will be either a "1" or a "0", you can use an int datatype.
Call this variable sensorValue, and set it to equal whatever is being read on digital pin 2. You
can accomplish all this with just one line of code:
intsensorValue=digitalRead(2);
Once the Arduino has read the input, make it print this information back to the computer as
a decimal (DEC) value. You can do this with the command Serial.println() in our last line of
code:
Serial.println(sensorValue,DEC);
Now, when you open your Serial Monitor in the Arduino environment, you will see a stream
of "0"s if your switch is closed, or "1"s if your switch is open. The LED on pin 13 will
illuminate when the switch is HIGH, and turn off when LOW.
Further Learning: setup(), loop(), pinMode(), digitalRead(), delay(), int, serial, DigitalPins
In this tutorial learn how to check the state change, send a message to the Serial Monitor
with the relevant information, and count four state changes to turn on and off an LED.
Hardware Required:
10k resistor
Hook-up wires
Breadboard
Circuit
Image developed using Fritzing. For more circuit examples, see the Fritzing project page
Connect three wires to the board. The 1 st goes from one leg of the pushbutton through a
pull-down resistor (10k) to ground. The 2nd goes from the corresponding leg of the push-
button to the 5 volt supply. The 3 rd connects to a digital I/O pin (pin 2) which reads the
button's state. When the pushbutton is open (unpressed) there is no connection between
the two legs of the pushbutton. The pin is connected to ground via the pull-down resistor
and we read a LOW.
Schematic:
Code
This sketch continually reads the button's state and compares the button's state to its state
the last time through the main loop. If the current button state is different from the last
button state and HIGH, then the button changed from off to on; incrementing a button
push counter. It also checks the button push counter's value. If it's an even multiple of four,
it turns the LED on pin 13 ON. Otherwise, it turns it off.
Hardware Required:
8 speaker
3 10k resistors
100 resistor
Hook-up wires
Breadboard
Schematic:
Connect one terminal of your speaker to digital pin 8 through a 100 ohm resistor, and its
other terminal to ground. Power your three FSRs (or any other analog sensor) with 5V in
parallel. Connect each sensor to analog pins 0-2, using a 10K resistor as a reference to
ground on each input line.
Image developed using Fritzing. For more circuit examples, see the Fritzing project page
The sketch below reads three analog sensors. Each corresponds to a note value in an array
of notes. If any of the sensors is above a given threshold, the corresponding note is played.
Here's the main sketch:
You may find it useful for whenever you want to make musical notes. To make the pitches.h
file, either click on the button just below the serial monitor icon and choose "New Tab" (or
Ctrl+Shift+N).
Hardware Required:
Hook-up wires
Circuit
Image developed using Fritzing. For more circuit examples, see the Fritzing project page
Code
To make the pitches.h file, either click on the button just below the serial monitor icon and
choose "New Tab" (or Ctrl+Shift+N).