Hardware Reference
In-Depth Information
different equally sized portions. A 10-bit device can distinguish 210 different
values—or a total of 1,024 different values. If used on a range between 0 and 5
volts; an input of 0 volts would result in a decimal 0; an input of 5 volts would
give the maximum of 1,023. Something in between, such as 2.5 V would yield
a value of 512. A 10-bit ADC can sense differences of 5 volts divided by the
resolution, or 1,024. This device can therefore have an accuracy of 5 / 1,024, or
roughly 0.005 volts.
analogRead()
To read a value from an analog pin, you call analogRead() .
int analogRead(pin)
analogRead() reads the voltage value on a pin and returns the value as an
int . The pin argument denotes the analog pin you want to read from. When
referring to an analog pin, call them as A0, A1, A2,…A6.
This function takes approximately 100 microseconds to perform. In theory,
you could sample a pin up to 10,000 times a second. However, it's best to let
the ADC “settle” for a few milliseconds between reads for more accurate data
acquisition.
analogWrite()
analogWrite() is used to write an analog output on a digital pin. Wait, analog?
On a digital pin? Well, yes, sort of. It's not a true analog value that's being written.
Arduinos use something called Pulse-width modulation , PWM for short. PWM
is digital but can be used for some analog devices. It uses a simple technique to
“emulate” an analog output. It relies on two things: a pulse width and a duty
cycle. It is a way of simulating any value within a range by rapidly switching
between 0 volts and 5 volts.
The pulse width (also called a period ) is a short duration of time in which the
duty cycle will operate. The duty cycle describes the amount of time that the
output will be at a logical one in the given period. Depending on the Arduino
you're using, the period can range from 490 Hz to 980 Hz. A duty cycle of 50
percent means that during 50 percent of the pulse width, the output will be at
a logical one, and the remaining 50 percent of the pulse width, the duty cycle
will be at a logical 0. A duty cycle of 0 percent means that the output will always
be p, and a duty cycle of 100 percent means that the output will always be 1.
PWM is an excellent method for controlling motors and dimming LEDs; it
worked well in the previous chapter. However, some components do not like
receiving pulses and want a stable output. For example, another Arduino read-
ing an analog input would read in alternating values of 5 V and 0 V instead of
a true analog signal. In this case, adding a capacitor to the circuit will “i lter”
the output.
 
Search WWH ::




Custom Search