Introduction: Arduino and Python Serial Communication - Keypad Display

This Project is made for mac users but it can also be implemented for Linux and Windows, the only step that should be different is the installation.

Step 1: Software Installation

  1. Download and Install Arduino ->
  2. Download and Install Python 2.7 ->
  3. Download Python library "pyserial-2.7.tar.gz" ->
  4. Unzip pyserial-2.7.tar.gz
  5. Open Terminal and type:
   cd /users/"Your-User-Account"/Downloads/pyserial-2.7
   sudo python install

Software installation ready!

Step 2: Wiring

  1. Arduino Uno
  2. Sparkfun 12 Button keypad

The wiring is done without external resistors, instead I used the internal Pullup-Resistors of the microcontroller (internal Pullup-Resistors of Arduino have a value of 20K-Ohm to 50K-Ohm)

To activate the internal Pullup-Resistors set the INPUT-Pins HIGH in the code

If you use another Keypad look at the data sheet for the right wiring, otherwise it could damage your microcontroller

Step 3: Arduino Code

  • First we define a chars-Matrix for the Keys
  • The keypad uses normal switch connectors that are arranged in 4 rows (Pins 7,2,3 and 5) and 3 columns (Pins 6,8 and 4), are defined as Arrays rowPins and colPins
  • The setup() function
    • Open the serial gate with Serial.begin();
    • Set columns as OUTPUT-Pins HIGH
    • Activate the Pullup-Resistors, to do this set rows as INPUT-Pins HIGH;
  • The getkey() function
    • Set every row LOW and test if one of the columns are LOW. Because of the Pullup-Resistors are all rows HIGH until one key is pushed down. The pushed key generate a LOW-Signal at the INPUT-Pin. This LOW indicates the pushed key in this row and column
    • Wait until the key is released and returns the char of the keymap-Array or 0 if no key was pushed
    • Use a delay(debounceTime) to stabilise the signal

Step 4: Python_2.7 Code

  • Import the Serial Library
  • Define a variable connected=FALSE, later is this variable used to test if the serial connection is available or not
  • Open the Serial Port with serial.Serial("Name of your Serial Port", baud)
    • To get the name of your serial port click -> Tools/Serial Port in the Arduino IDLE
    • the baud should be the same as in the Arduino Code
  • In a while loop test if the connection is available or not reading the serial signal and setting the variable connected=TRUE, it loops until it gets serial connection
  • After the connection read the serial in a while loop and put this input in a new variable "var"
  • close the port with ser.close()
Arduino All The Things! Contest

Participated in the
Arduino All The Things! Contest