Chapter 2

Digital Inputs, Outputs, and Pulse-Width Modulation

Chapter Summary

This chapter covered the following concepts:

  • How a breadboard works
  • How to pick a resistor to current-limit an LED
  • How to wire an external LED to your Arduino
  • How to use PWM to write “analog” values to LEDs
  • How to read a pushbutton
  • How to debounce a pushbutton
  • How to use for loops
  • How to utilize pullup and pulldown resistors

Parts List

Useful Links


Code errata listed below are corrected in the code downloads available from this page, and from github.

  • In numbered list on pages 26 and 27, there should be another step between steps 1 and 2: “100 is less than or equal to 1000, so the loop contents execute.” This makes the for loop description more clear.
  • In code listing 2-5, there is a missing closing bracket ( } ) following the “current = digitalRead(BUTTON);” line in early prints of the book. This has been fixed in later printings.
  • In code listing 2-6, the “if” statement for Purple should be an “if else” statement.

Color Wiring Diagrams


This video will teach you about digital inputs/outputs, debouncing, and PWM:

This video will teach you the basics of electrical and computer engineering:


Chapter 2 Code Download

All code is licensed via the GNU GPL v3. Code is maintained and updated on GitHub. The download zip linked above always contains the most recent version of the code examples that have been pushed to the GitHub Code Repository.