Keypad controller

From ZeroPhone
(Redirected from ZeroPhone keypad controller)
Jump to: navigation, search

ZeroPhone uses a microcontroller to offload some tasks and to get some peripherals that Pi Zero doesn't have.

ZeroPhone Gamma/Delta/Delta-B


An ATMega328P is used, in a TQFP-32 package. The ATMega is connected to Pi Zero through I2C (shared with MCP23017 and expansion headers), and it also has an interrupt line to avoid having to poll the key state all the time. The pinout is as follows:


3.3V Power
3.5V-5V power
Keypad matrix pins
User-exposed pins
Auxiliary functions
Pi Zero-connected pins
Internal use
Pin Number ATMega pin Arduino pin Function Used for Firmware support
1 PD3 3 COL5 Keypad scanning Yes
2 PD4 4 COL4 Keypad scanning Yes
7 PB6 XTAL1 16MHz crystal connection
8 PB7 XTAL2 16MHz crystal connection
9 PD5 5 ATMEGA_GPIO User-exposed (opt-in) No
10 PD6 6 COL3 Keypad scanning Yes
11 PD7 7 COL2 Keypad scanning Yes
12 PB0 8 COL1 Keypad scanning Yes
13 PB1 9 KEYPAD_BL Keypad backlight No
14 PB2 10 DISPLAY_BL Display backlight (i.e. TFT) No
15 PB3 11 VIBRO_PWM Vibromotor control No
16 PB4 12 ROW1 Keypad scanning Yes
17 PB5 13 ROW2 Keypad scanning Yes
19 ADC6 A6 ADC6 User-exposed ADC No
22 ADC7 A7 ADC7 Measuring VSYS No
23 PC0 A0 ROW3 Keypad scanning Yes
24 PC1 A1 ROW4 Keypad scanning Yes
25 PC2 A2 ROW5 Keypad scanning Yes
26 PC3 A3 KEYPAD_INT Pi Zero communication Yes
27 PC4 A4 SDA Pi Zero communication Yes
28 PC5 A5 SCL Pi Zero communication Yes
29 PC6 RST Keypad scanning
30 PD0 0 ATMEGA_RXD User-exposed; programming No
31 PD1 1 ATMEGA_TXD User-exposed; programming No
32 PD2 2 COL6 Keypad scanning Yes

Parts involved and substitutes

  • ATMega328P/168P in TQFP package
  • 16MHz/8MHz crystal, one of these:
    • CSCTE crystal (small footprint, SMD, 3 pads under the crystal)
    • HC-49 through-hole crystal + 2x 18pF capacitors

Sourcing the parts

There are two ways to get the parts necessary:

  • Buy them separately from Digikey/Mouser/eBay/etc.
  • Get an Arduino Pro Mini clone board and transplant the necessary components with a heat gun

Second way is the preferred way, since you get the ATMega with the right bootloader already flashed and it's going to be cheaper. However, you do need access to a heat gun (or to be skilled enough to desolder TQFP with a soldering iron). Also, you do need to make sure that the Pro Mini board has the TQFP ATMega, though you should be able to see the chip on the photos provided by seller.

Compiling the firmware

  • Opening the library manager
  • Installing the Keypad library

Current firmware is Arduino-based, so Arduino IDE can be used to compile and flash the firmware. You need to add the Keypad library in the library manager, as pictured in the two pictures on the left.

Flashing the firmware

Flashing using Arduino Serial Light

You can use the 6-pin header and a USB-UART- the 6-pin ATMega programming header has the same pinout as Arduino Pro Mini, so you can use either general-purpose USB-UARTs or the "FTDI header" (GND-GND-VCC-TX-RX-RST) adapters (like Arduino Serial Light or some Chinese USB-UARTs).

  • You can use 5V-only ATMega programming adapters, such as Arduino Serial Light. However, you need to make sure that, when a 5V adapter is connected to the ATMega328P programming header, front board is not connected to the Pi Zero and the back board
  • Do not connect the adapter in wrong polarity (the other way around), as that will likely destroy the ATMega.

Current state of firmware


  • Scanning keys and sending key values, enabling the interrupt line on each received key

Not implemented:

  • Vibromotor control
  • VIN voltage reading
  • Keypad backlight control
  • Screen backlight control (will only be necessary if TFT screens will be used with ZeroPhone)
  • User-accessible ADC and GPIO control
  • Allowing ATMega UART usage for some other things (custom sensors, RFID, POGSAC etc.) (needs some way of bridging I2C to UART in firmware)

Possible problems

  • When an LED is connected to Pin 13, certain keys misbehave.
    • KEY_LEFT is interpreted as KEY_ANSWER
    • KEY_VOLUMEUP is interpreted as KEY_VOLUMEDOWN
    • KEY_F3 (KEY_PAGEUP) is interpreted as KEY_1
    • KEY_8 is interpreted as KEY_4
    • KEY_* is interpreted as KEY_PROG1
    • KEY_0 is interpreted as KEY_F5

To avoid this, you can desolder the resistor in series with the LED (as it's much easier to desolder than the LED).

  • Connecting a USB serial adapter in reverse polarity appears to burn the ATMega328P (confirmed with 5V Arduino Serial Light - happens almost instantaneously). This might be solved in Delta revisions, but the solution wasn't tested. For now, be very careful about connecting the serial programmer in the wrong way.