4.3.1. Exercise: Read Switch Input¶
4.3.1.1. 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.
4.3.1.2. Steps and observations¶
- Wire up the circuit as shown using a breadboard. You may recognize the voltage divider formed by the switch and resistor.
- Load and run the ReadSwitchInput sketch.
- 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).
- 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.
- 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.
4.3.1.3. 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.
4.3.1.4. Arduino Code¶
- Documentation: ReadSwitchInput Arduino Sketch
- Sketch Folder: ReadSwitchInput
4.3.1.5. Other Files¶
- EAGLE file:
digital-read-serial.sch