I2C keypad using PIC18F4550 microcontroller

Summary of I2C keypad using PIC18F4550 microcontroller


This project demonstrates interfacing a 4x4 keypad with a PIC microcontroller via an I2C bus to save GPIO pins. It uses PCF8574 chips as I/O expanders to manage the keypad input and optionally drive a 7-segment display output, simplifying prototyping for electronics projects.

Parts used in the I2C Keypad:

  • PCF8574 I2C chip
  • 4x4 Keypad
  • 2,2 kOhm pull-up resistors
  • PIC18F4550 microcontroller
  • 7 segment display
  • 330 Ohm resistor
  • Serial to USB dongle

I2C keypad

This instructable explains how to interface with a 4×4 keypad over an I2C bus.

I started this project because I wanted some plug and play input and output modules for easy and fast prototyping of my electronics projects (mostly with PIC microcontrollers). Of course you can use leds and dip switches but they require lots of pins on your microcontroller and you only have binary I/O.

I2C keypad
I2C keypad

To solve this problem I decided to use I2C.
I2C is a 2 wire bidirectional bus to which addressable devices can be connected.
I had some I2C remote I/O expander chips laying around that I could use for this purpose.
You give them an address and then you can read or write to their 8 I/O pins.
I also recovered a 4×4 keypad from a broken telephone to be used as input device.
And I connected a 7 segment display to the I2C bus as an output device.

In a next iteration of the project I’ll move the components to PCB.

Step 1

Components

Required components:

-1x PCF8574  I2C chip (remote 8-bit I/O expander for I2C-bus)
-1x 4×4 Keypad
-2x 2,2 kOhm pull-up resistors
-1x PIC18F4550 (or other programmable I2C master device)

Optional components (depends on output):

-1x PCF8574  I2C chip
-1x 7 segment display
-7x 330 Ohm resistor
-1x serial to usb dongle

Step 2

Circuit

I2C keypad Schematic
I2C keypad Schematic

Because the I2C bus only uses 4 wires (power, ground, clock and data) the circuit is very simple.The microcontroller (MCU) and the I2C chip are both connected to the 5V from the power source. In my case the circuit is powered by my MCU programmer which gets it power from the USB port of my computer.

The 8 I/O pins of the I2C chip are connected to the 8 pins of the keypad. It’s easier for writing the code if we make the connections in a logical order. I connected the column pins on one side of the chip and the row pins on the other side of the chip.

Data (SDA) and clock (SCL) from the I2C chip are connected to the data and clock pin of the MCU.

Both SCL and SDA lines are “open drain” drivers. What this means is that the chip can drive its output low, but it cannot drive it high. For the line to be able to go high you must provide pull-up resistors to the 5v supply. There should be a 2,2 kOhm resistor from the SCL line to the 5v line and another from the SDA line to the 5v line. You only need one set of pull-up resistors for the whole I2C bus.

3 bits of the 8bit address of the device are set by connecting the address pins to 5V or to ground.

Output:

The serial output (UART/rs232) goes to my serial to usb dongle that is connected to my pc.

The second PCF8574 chip is connected to the I2C bus by connecting the data and clock line to the data and clock line from the first chip.

For the second chip we need a different address so I connected the first address pin to 5V instead of the ground.

The common anode of the 7 segment display is connected to the 5V from the power source. The other pins are connected with the I/O pins of the I2C chip (using 330 Ohm current limiting resistors).

Step 3: Code

The code implements the standard keyboard scanning routine by reading and writing to the I2C chip. The keypad is scanned by selecting a column and then reading the rows to see wich one of the buttons is pressed.

For more detail: I2C keypad using PIC18F4550 microcontroller

Quick Solutions to Questions related to I2C Keypad:

  • How many wires does the I2C bus use?
    The I2C bus only uses 4 wires including power, ground, clock, and data.
  • What is the function of the PCF8574 chip?
    It acts as a remote 8-bit I/O expander for the I2C-bus allowing read or write access to its 8 I/O pins.
  • Why are pull-up resistors required on the I2C lines?
    Because SCL and SDA are open drain drivers that cannot drive high, requiring resistors to connect to the 5V supply.
  • How do you set different addresses for multiple I2C devices?
    You set bits of the 8-bit address by connecting specific address pins to either 5V or ground.
  • What components are needed for the optional output section?
    An additional PCF8574 chip, a 7 segment display, seven 330 Ohm resistors, and a serial to USB dongle.
  • How is the keypad scanned in the code?
    The routine selects a column and then reads the rows to detect which button is pressed.
  • Can this project be adapted for other microcontrollers?
    Yes, the PIC18F4550 can be replaced with any other programmable I2C master device.
  • How is the 7 segment display connected to the circuit?
    The common anode connects to 5V while other pins link to I/O pins via current limiting resistors.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter