3. Pulse-Width Modulation withanalogWrite()
So, you have mastered digital control of your pins. This is great for blinking LEDs,
controlling relays, and spinning motors at a constant speed.
But what if you want to output a voltage other than 0V or 5V?
● Well, you can’t—unless you are using the digital-to-analog converter (DAC) pins
on the Due or are using an external DAC chip.
● However, you can get pretty close to generating analog output values by using
a trick called pulse-width modulation (PWM).
● you can decrease the voltage being dropped across the resistor, the LED should
glow more dimly because less current will flow.
● These pins are marked with a ~ on the board.
● The PWM output is an 8-bit value. In other words, you can write values
from 0 to 255. Try using a similar for loop structure to the one you used previously
to cycle through varying brightness values.
4.
5.
6. pullupor pulldown resistor
all digital inputs use a pullup or pulldown resistor to set the “default state” of the
input pin. Imagine the circuit without the 10kΩ resistor. In this scenario, the pin
would obviously read a high value when the button is pressed. But, what happens
when the button is not being pressed? In that scenario, the input pin you would be
reading is essentially connected to nothing—the input pin is said to be “floating.”
And because the pin is not physically connected to 0V or 5V, reading it could cause
unexpected results as electrical noise on nearby pins causes its value to fluctuate
between high and low.
7. Understanding AnalogandDigitalSignals
● If you want your devices to interface with the world, they will inevitably be
interfacing with analog data.
● Consider the projects you completed in the preceding video. You used a switch
to control an LED. A switch is a digital input—it has only two possible states: on
or off, high or low, 1 or 0.
● The world around you, however, rarely expresses information in only two ways, a
Sunlight is not on or off; its brightness varies over the course of a day.
Similarly, wind does not just have two states; it gusts at different speeds all the
time.