| 
					
				 | 
			
			
				@@ -1,10 +1,10 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 # Keypad Scanning 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-There's a dozen ways to connect a keyboard to a microcontroller, and this paper attempts to enumerate many of them. [test link](#one-per-pin) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-There are similar articles on the internet [[see below](#ext_ref)],  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-but this paper introduces some new techniques: [row-grounded](#3x4-plus-ground-[new-design]),  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-[row-grounded-with-diodes](#3x4diodes),  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-[row-grounded-with-4-diodes](#3x4double_diodes) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+There's a dozen ways to connect a keyboard to a microcontroller, and this paper attempts to enumerate many of them. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+There are similar articles on the internet [[see below](#external-references)],  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+but this paper introduces some new techniques: [row-grounded](#3x4-plus-ground-new-design),  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+[row-grounded-with-diodes](#3x4-plus-ground-and-2-diodes-new-design),  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+[row-grounded-with-4-diodes](#3x4-plus-ground-and-4-diodes-new-design) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 Some articles focus on a full-function, unambiguous keyboard,  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 where multiple key-presses can be detected. Something 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -23,7 +23,7 @@ In this document, *n* refers to the number of keys that are in the keyboard. The 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 that is being pressed during an explanation. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-## <a name="one_per_pin" /> One per Pin  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+## One per Pin  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 The simplest way to connect keys to a microcontroller is one key to one pin.  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 Connect the other side of the key to ground.  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -40,7 +40,7 @@ This configuration has a few advantages: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 - there is no keyboard scan....which helps keep scan-noise out of the power supply; helpful in an audio environment 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 - can easily be set to wake-on-interrupt, which means that you can power down between keystrokes 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-See [KeyBounce](#bounce) below. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+See [KeyBounce](#keyboard-bounce) below. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 Of course, this solution does not apply to a keyboard which has *rows* and *columns*. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -56,7 +56,6 @@ microcontrollers have an A/D converter, and that can be used to determine which 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 A naive approach would be to simply use equal-valued resistors, and then each tap would be *k*/(*n*+1) of the supply voltage. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- <a name="3x4chain" /> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 ### 3x4 Chain 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 Sometimes a keyboard will be pre-wired as a grid, so you don't have access to the individual keys. You can still use  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -109,7 +108,7 @@ See also [this article](https://github.com/sgmne/AnalogKeypad) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 ## Scanning 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-### 3x4 Scanning <a name="3x4scan" /> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+### 3x4 Scanning 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 The simplest connection of a 3x4 keyboard is to simply use 7 GPIO pins, 4 for the rows, and 3 for the columns. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 Enable input & pullups on the columns and rows. At scan time, assert each row pin, one at a time, as a low, and measure 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -136,10 +135,10 @@ This scheme can be configured for interrupt triggering 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 columns are set to input+pullup+interrupt.  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 Any keypress will then pull at least one column line low.  Once the  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 interrupt is triggered, change to the logic described above, scan the keyboard  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-[over the [debounce period](#bounce) and then restore they keyboard  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+[over the [debounce period](#keyboard-bounce) and then restore they keyboard  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 to the "ready-for-interrupt" configuration. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-### 3x4 plus Ground [new design] <a name="3x4gnd" /> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+### 3x4 plus Ground [new design] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 If you are really tight on pins, you can ground one of the rows and still get a functioning keyboard. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -161,7 +160,6 @@ The resulting 4 groups of 3 bits indicate the state of the keyboard; 1=open, 0=k 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 Again, the same logic will allow you to manage a 4x4 keyboard with 7 GPIO pins. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-<a name="3x4diodes" /> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 ### 3x4 plus Ground and 2 Diodes [new design] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 Again, we can scrape off one more GPIO if we can add a pair of diodes to the row pins. 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -215,7 +213,6 @@ look for the symbol **FIVE_PIN**. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 This configuration is also interrupt-capable, by setting the row drivers to output-low, and the columns as  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 input+pullup+interrupt. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-<a name="3x4double_diodes" /> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 ### 3x4 plus Ground and 4 Diodes [new design] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 As an extreme, you can decode a 3x4 keypad with only 4 GPIO pins,  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -268,7 +265,7 @@ can happen. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 look for the symbol **FOUR_PIN**. Here is a working prototype.  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-### Keyboard Debounce <a name="bounce" /> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+### Keyboard Debounce  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 When a key is pressed, there is a short time during closure when the  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 physical elements almost-touch, and the  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -301,8 +298,12 @@ One involves using an SPI output and a shift register 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 <https://hackaday.com/2015/04/15/simple-keypad-scanning-with-spi-and-some-hardware/>    
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 This design requires an external chip ($0.10), 8 diodes and 4 resistors. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+### Interrupts 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-### Reading DIP switches <a name="dip_switches" /> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+### Reading DIP switches 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 In some situations, it's helpful to read an array of DIP switches,  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 perhaps to read a configuration at power-on. In this case,  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -325,7 +326,7 @@ and it's impossible to determine if the "5" key is pressed or not. Diodes are 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 required at each junction to disambiguate. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-### External keyboard processor <a name="external_hardware" /> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+### External keyboard processor 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 With the current cost of processors, consider adding an external coprocessor to your project.  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -364,7 +365,7 @@ It's U$0.11, 16k flash, 2k ram, 6gpio, sop8. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 | scan with diodes   | 6 for 4x4, 5 for 3x4     | 1 | 2 diodes |  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 | scan with 4 diodes | 5 for 4x4, 4 for 3x4     | 1 | 4 diodes |  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-### External References <a name="ext_ref" /> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+### External References 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 An excellent overview of keyboard scanning directly from a microcontroller.   
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 <https://ww1.microchip.com/downloads/aemDocuments/documents/MCU08/ApplicationNotes/ApplicationNotes/00003407A.pdf> 
			 |