Simple Digital and Analog Input
Simple Digital and Analog Input
Mechanical Engineering
ME 425
Simple Digital and Analog Input
Solution:
There are many ways to solve this problem; here is one
using the wiring shown in Figure 5-3 from Recipe 5.1:
Faculty Of Engineering 2
Mechanical Engineering
Reliably Detecting the Closing
of a Switch:
Faculty Of Engineering 3
Mechanical Engineering
Reliably Detecting the Closing
of a Switch:
Faculty Of Engineering 4
Mechanical Engineering
Reliably Detecting the Closing
of a Switch:
Faculty Of Engineering 5
Mechanical Engineering
Reliably Detecting the Closing
of a Switch:
Faculty Of Engineering 6
Mechanical Engineering
Determining How Long a
Switch Is Pressed:
Problem:
Your application wants to detect the length of time a switch has been in its
current state. Or you want to increment a value while a switch is pushed and
you want the rate to increase the longer the switch is held (the way many
electronic clocks are set). Or you want to know if a switch has been pressed
long enough for the reading to be stable (see Recipe 5.3).
Solution:
The following sketch demonstrates the setting of a countdown timer. The
wiring is the same as in Figure 5-5 from Recipe 5.2. Pressing a switch sets the
timer by incrementing the timer count; releasing the switch starts the
countdown. The code debounces the switch and accelerates the rate at which
the counter increases when the switch is held for longer periods. The timer
count is incremented by one when the switch is initially pressed (after
debouncing). Holding the switch for more than one second increases the
increment rate by four; holding the switch for four seconds increases the rate
by ten.
Faculty Of Engineering 7
Mechanical Engineering
Determining How Long a
Switch Is Pressed:
Faculty Of Engineering 8
Mechanical Engineering
Determining How Long a
Switch Is Pressed:
Faculty Of Engineering 9
Mechanical Engineering
Determining How Long a
Switch Is Pressed:
Faculty Of Engineering 10
Mechanical Engineering
Determining How Long a
Switch Is Pressed:
Faculty Of Engineering 11
Mechanical Engineering
Reading a Keypad:
Problem:
You have a matrix keypad and want to read the key presses
in your sketch. For example, you have a telephone-style
keypad similar to the SparkFun 12-button keypad (Spark-
Fun COM-08653).
Solution:
Wire the rows and columns from the keypad connector to
the Arduino, as shown in Figure 5-6.
Faculty Of Engineering 12
Mechanical Engineering
Reading a Keypad:
Faculty Of Engineering 13
Mechanical Engineering
Reading a Keypad:
Faculty Of Engineering 14
Mechanical Engineering
Reading a Keypad:
Faculty Of Engineering 15
Mechanical Engineering
Reading a Keypad:
Faculty Of Engineering 16
Mechanical Engineering
Reading a Keypad:
Faculty Of Engineering 17
Mechanical Engineering
Reading a Keypad:
Faculty Of Engineering 18
Mechanical Engineering
Reading More Than Six Analog
Inputs:
Problem:
You have more analog inputs to monitor than you have available
analog pins. A standard Arduino board has six analog inputs (the Mega
has 16) and there may not be enough analog inputs available for your
application. Perhaps you want to adjust eight parameters in your
application by turning knobs on eight potentiometers.
Solution:
Use a multiplexer chip to select and connect multiple voltage sources to
one analog input. By sequentially selecting from multiple sources, you
can read each source in turn. This recipe uses the popular 4051 chip
connected to Arduino as shown in Figure 5-8. Your analog inputs get
connected to the 4051 pins marked Ch 0 to Ch 7. Make sure the voltage
on the channel input pins is never higher than 5 volts:
Faculty Of Engineering 25
Mechanical Engineering
Reading More Than Six Analog
Inputs:
Faculty Of Engineering 26
Mechanical Engineering
Reading More Than Six Analog
Inputs:
Faculty Of Engineering 27
Mechanical Engineering
Reading More Than Six Analog
Inputs:
Faculty Of Engineering 28
Mechanical Engineering
Reading More Than Six Analog
Inputs:
Faculty Of Engineering 29
Mechanical Engineering
Displaying Voltages Up to 5V:
Problem:
You want to monitor and display the value of a voltage
between 0 and 5 volts. For example, suppose you want to
display the voltage of a single 1.5V cell on the Serial
Monitor.
Solution:
Use AnalogRead to measure the voltage on an analog pin.
Convert the reading to a voltage by using the ratio of the
reading to the reference voltage (5 volts), as shown in
Figure 5-9.
Faculty Of Engineering 30
Mechanical Engineering
Displaying Voltages Up to 5V:
Faculty Of Engineering 31
Mechanical Engineering
Displaying Voltages Up to 5V:
Faculty Of Engineering 32
Mechanical Engineering
Displaying Voltages Up to 5V:
Faculty Of Engineering 33
Mechanical Engineering
Responding to Changes in
Voltage:
Problem:
You want to monitor one or more voltages and take some action when
the voltage rises or falls below a threshold. For example, you want to
flash an LED to indicate a low battery levelperhaps to start flashing
when the voltage drops below a warning threshold and increasing in
urgency as the voltage drops further.
Solution:
You can use the connections shown in Figure 5-7 in Recipe 5.9, but
here well compare the value from analogRead to see if it drops below
a threshold. This example starts flashing an LED at 1.2 volts and
increases the on-to-off time as the voltage decreases below the
threshold. If the voltage drops below a second threshold, the LED stays
lit:
Faculty Of Engineering 34
Mechanical Engineering
Responding to Changes in
Voltage:
Faculty Of Engineering 35
Mechanical Engineering
Responding to Changes in
Voltage:
Faculty Of Engineering 36
Mechanical Engineering
Responding to Changes in
Voltage:
Faculty Of Engineering 37
Mechanical Engineering
Measuring Voltages More Than
5V (Voltage Dividers):
Problem:
You want to measure voltages greater than 5 volts. For
example, you want to display the voltage of a 9V battery
and trigger an alarm LED when the voltage falls below a
certain level.
Solution:
Use a solution similar to Recipe 5.9, but connect the
voltage through a voltage divider (see Figure 5-10). For
voltages up to 10 volts, you can use two 4.7K ohm
resistors. For higher voltages, you can determine the
required resistors using Table 5-4.
Faculty Of Engineering 38
Mechanical Engineering
Measuring Voltages More Than
5V (Voltage Dividers):
Faculty Of Engineering 39
Mechanical Engineering
Measuring Voltages More Than
5V (Voltage Dividers):
Faculty Of Engineering 40
Mechanical Engineering
Measuring Voltages More Than
5V (Voltage Dividers):
Faculty Of Engineering 41
Mechanical Engineering
Measuring Voltages More Than
5V (Voltage Dividers):
Faculty Of Engineering 42
Mechanical Engineering
Measuring Voltages More Than
5V (Voltage Dividers):
Faculty Of Engineering 43
Mechanical Engineering
Assignment # 6:
Design a voltage tester that monitors the voltage of a battery
and take some action when the voltage rises or falls below a
threshold. For example, you want to flash an LED to indicate a
low battery levelperhaps to start flashing when the voltage
drops below a warning threshold and increasing in urgency as
the voltage drops further. And you want to turn on a green
LED when the battery is fully charged.