Hardware Reference
In-Depth Information
The analogWrite() command sets the duty cycle of a square wave depending
on the value you pass to it:
Writing a value of 0 with analogWrite() indicates a square wave with a
duty cycle of 0 percent (always low).
Writing a 255 indicates a square wave with a duty cycle of 100 percent
(always high).
Writing a 127 indicates a square wave with a duty cycle of 50 percent
(high half of the time, low half of the time).
The graphs in Figure 2-4 show that for a signal with a duty cycle of 25 percent,
it is high 25 percent of the time, and low 75 percent of the time. The frequency
of this square wave, in the case of the Arduino, is about 490Hz. In other words,
the signal varies between high (5V) and low (0V) about 490 times every second.
So, if you are not actually changing the voltage being delivered to an LED,
why do you see it get dimmer as you lower the duty cycle? It is really a result of
your eyes playing a trick on you! If the LED is switching on and off every 1ms
(which is the case with a duty cycle of 50 percent), it appears to be operating at
approximately half brightness because it is blinking faster than your eyes can
perceive. Therefore, your brain actually averages out the signal and tricks you
into believing that the LED is operating at half brightness.
ReadingDigitalInputs
Now it is time for the other side of the equation. You've managed to successfully
generate both digital and analog(ish) outputs. The next step is to read digital
inputs, such as switches and buttons, so that you can interact with your project
in real time. In this section, you learn to read inputs, implement pullup and
pulldown resistors, and debounce a button in software.
ReadingDigitalInputswithPulldownResistors
You should start by modifying the circuit that you first built from Figure 2-1.
Following Figure 2-5, you'll add a pushbutton and a pulldown resistor con-
nected to a digital input pin.
TIP Besuretoalsoconnectthepowerandgroundbusesofthebreadboardto
theArduino.Nowthatyou'reusingmultipledevicesonthebreadboard,thatwill
comeinhandy.
Before you write the code to read from the pushbutton, it is important to
understand the significance of the pulldown resistor used with this circuit. Nearly
 
Search WWH ::




Custom Search