Taking input (Part-1)

What good a computer is if it can not take user input and process it ? But taking input can be done in multiple ways. So I will be writing about in several parts. Let’s first talk about what it means by taking user input. It means that the controller can take in the digital response of a switch or similar device. Consider the following circuit –

Screenshot from 2016-03-11 07-21-10

When the switch S1 is not pressed, we get VCC at node A. This is read as digital 1. If the switch S1 is pressed, we get GND at node A. This is read as digital 0.

This seems pretty simple. But question arises as we start considering the procedure of reading the input. The question is – when we determine a variation at the input terminal is to be read ? Answer to this question is we can determine that in 2 ways –

  1. Blocking I/O – As soon as the controller detects that the switch is pressed it enters into a loop where it checks whether the switch is released at every iteration. So as long as the switch is held at pressed position the controller gets stuck into the loop and blocks all other functionalities of it. The moment the switch is released the controller gets out of the loop and counts the input in. So controller counts only 1 input for 1 press and release of the switch.
  2. Non-blocking I/O – Whenever the controller detects a pressed condition, it counts the input and may or may not wait for while to continue whatever it was doing before. In this way, the controller essentially keeps on going even if the switch is held at pressed position for a while. So if the user holds the switch for a while the controller will count multiple inputs and keep counting on as long as the switch is being pressed.

The choice for blocking or non-blocking I/O is completely application specific.

REMINDER : The controller works a lot faster than human. A short time pressing of the switch will take at least some milliseconds for the user. Within this time the controller will execute thousands of instructions ! So it is important to choose blocking or non-blocking I/O system.

Now let’s consider how we want provide the digital 1 / high voltage for the switch. This can be done in 2 ways –

  1. External Pull-up : As you can see in the figure above, VCC provides the digital 1 / high voltage for the switch. This kind of circuitry is called external pull-up, meaning the switch is pulled to high by external resistor and power.
  2. Internal Pull-up : Thanks to the manufacturer, the microcontroller can also provide VCC internally by enabling an internal pull-up resistor. This omits the need for external power bus and the resistor. But it has it’s fallback – it can’t be used for a long distance. The switch has to be put near the controller.

One other important thing is bouncing. All mechanical switch(including the push-buttons) have an effect called bouncing. It means when the switch is pressed the contacts of it do not connect cleanly. It bounces back and falls back several times before making a proper contact. Here’s a trace of switch bouncing –


This bouncing may cause extra input counts which is unwanted.  So debouncing the switch important to ensure the input integrity. To do so you can connect a capacitor in parallel. The capacitor will eliminate the abrupt changes and give you a smoothed  input. But using an RC circuit to debounce will cost some response time too.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s