2.1.13. Exercise: Read Switch Input Objective

Measure a binary physical property as a single-bit number.

Some devices in the physical world do approximate a digital reality: switches are usually either on or off. But even these devices have non-ideal properties reflecting the physical reality, e.g. switch contacts can “bounce” as they make contact, creating a momentary pulse train rather than a clean on/off transition.

../../../_images/digital-read-serial.png Steps and observations

  1. Wire up the circuit as shown using a breadboard. You may recognize the voltage divider formed by the switch and resistor.
  2. Load and run the ReadSwitchInput sketch.
  3. Observe the numbers printed in the serial monitor as the switch changes state. As wired, the circuit is active-low, so if the switch is open the value will be 1 (HIGH), else 0 (LOW).
  4. Try to observe switch bounce, e.g. several transitions measured for a single physical transition. You may not succeed at this, the bounces are quite fast and the program is sampling them at a relatively slow rate limited by the serial communication.
  5. Use an oscilloscope to observe the digital input. Can you see the bounce now? You may need to adjust the scope for a fast time scale and then manipulate the switch. Comments

A digital input is essentially a single-bit analog-to-digital converter, albeit with an ambiguously defined threshold between the voltages representing zero and one. The digital input sampling rate can be much higher than the 10-bit ADC, so much higher frequency digital signals could be processed with careful real-time programming.

A related challenge exercise is the Arduino RCtime tutorial in which a photoresistor-capacitor circuit is used to form an ad-hoc ADC using a single digital I/O pin. Multiple bits of conversion resolution are obtained from measuring the single input bit over time. Arduino Code

  1. Documentation: ReadSwitchInput Arduino Sketch
  2. Sketch Folder: ReadSwitchInput Other Files

  1. EAGLE file: digital-read-serial.sch