Tutorial: Reading a 12-Button Keypad

Difficulty Level = 1 [What’s this?]

Most keypads like this are wired so it makes it straightforward to figure out what button is being pressed. With 3 columns and 4 rows of buttons, you only need 7 wires. Typically all the buttons in a column are connected together with the same wire, and all the buttons in a row are connected together with the same wire. To determine which button is pressed, you apply a voltage to the wire attached to a column and then check the wires attached to each row to see if current is flowing through any of them. If so, then the switch for a particular button is closed (button pressed). Then you proceed to the next column and try each row again, etc. Not rocket science — just scanning a bunch of switches to see which one is closed. In fact, there is a keypad library in the Arduino Playground that makes it easy to do this.

Well, the keypad I bought here has 10 wires instead of 7, and it’s wired in a really goofy way. I’m not sure if this is common, but I thought keypads were generally wired as described above. Here’s the schematic that shows how this one is actually wired:

Schematic of my non-standard keypad

Notice that the gray wire is used only for the 9 key. And the orange wire is only used for the * key. And the brown wire is only used for the # key. Why is this built so inefficiently? I have no idea!

Regardless, here is Arduino code that I used to scan this keypad. You can do something similar if you have a keypad that is not wired in a straightforward way.

// Pins
#define BLACK 2
#define WHITE 3
#define GRAY 4
#define PURPLE 5
#define BLUE 6
#define GREEN 7
#define YELLOW 8
#define ORANGE 9
#define RED 10
#define BROWN 11

#define STAR 10
#define POUND 11

void setup() {

  // Rows
  pinMode(BLACK, INPUT);
  digitalWrite(BLACK, HIGH);  // set pull-up resistors for all inputs

  pinMode(WHITE, INPUT);
  digitalWrite(WHITE, HIGH);

  pinMode(GRAY, INPUT);
  digitalWrite(GRAY, HIGH);

  pinMode(PURPLE, INPUT);
  digitalWrite(PURPLE, HIGH);

  pinMode(ORANGE, INPUT);
  digitalWrite(ORANGE, HIGH);

  pinMode(BROWN, INPUT);
  digitalWrite(BROWN, HIGH);

  // Columns
  pinMode(BLUE, OUTPUT);
  digitalWrite(BLUE, HIGH);

  pinMode(GREEN, OUTPUT);
  digitalWrite(GREEN, HIGH);

  pinMode(YELLOW, OUTPUT);
  digitalWrite(YELLOW, HIGH);

  pinMode(RED, OUTPUT);
  digitalWrite(RED, HIGH);


void loop() {
  int key = scanKeypad();

  if (key != -1) {
    if (key == STAR) {
    } else {
      if (key == POUND) {
      } else {

int scanKeypad() {
  int key = -1;

  // Pull the first column low, then check each of the rows to see if a
  // button is pressed.
  digitalWrite(BLUE, LOW);
  if (digitalRead(BLACK) == LOW) {
    key = 1;
  if (digitalRead(WHITE) == LOW) {
    key = 4;
  if (digitalRead(PURPLE) == LOW) {
    key = 7;
  digitalWrite(BLUE, HIGH);

  // Moving on to the second column....
  digitalWrite(GREEN, LOW);
  if (digitalRead(BLACK) == LOW) {
    key = 2;
  if (digitalRead(WHITE) == LOW) {
    key = 5;
  if (digitalRead(PURPLE) == LOW) {
    key = 8;
  digitalWrite(GREEN, HIGH);

  // Third "column".  Note that the 0 key is wired to this column even though
  // the 0 is really in the second column.
  digitalWrite(YELLOW, LOW);
  if (digitalRead(BLACK) == LOW) {
    key = 3;
  if (digitalRead(WHITE) == LOW) {
    key = 6;
  if (digitalRead(GRAY) == LOW) {
    key = 9;
  if (digitalRead(PURPLE) == LOW) {
    key = 0;
  digitalWrite(YELLOW, HIGH);

  // Last "column" is not really it's own column.  Only wired to * and #
  digitalWrite(RED, LOW);
  if (digitalRead(ORANGE) == LOW) {
    key = STAR;
  if (digitalRead(BROWN) == LOW) {
    key = POUND;
  digitalWrite(RED, HIGH);

  return key;

Published by Michael, on April 3rd, 2010 at 1:29 pm. Filed under: Arduino,Level 1. | 3 Comments |

Arduino-Controlled Mood Lamp Made with LEDs and Glass Vials

Difficulty Level = 6 [What’s this?]

Arduino Mood Lamp

This is a mood lamp I build using 16 LEDs of different colors and small glass vials. The square bottoms of the vials look a lot like glass block, and the glass diffuses and scatters the light in beautiful ways. The software shows random patterns of light and the brightness of each LED can vary — they aren’t simply “on” or “off”.

The Arduino code is pretty complex because it implements PWM (pulse-width modulation) for all 16 LEDs. The Arduino board only has 5 PWM-capable pins, so providing PWM for all 16 pins is accomplished purely in the code. The lamp randomly displays different lighting patterns and can be really mesmerizing. Ok, I know you want to see it in action, so here it is (note that the music is just in the background — the lights are not reacting to it):


The base of the lamp is a piece of plexiglass about 5 inches square, and all of the wiring is on the underside of the plexi. Each of the 16 LEDs goes into a small socket made from two pins of a female header. I used sockets instead of soldering the LEDs directly so that I could rearrange the colors any way I like.

LED sockets

Read more…

Published by Michael, on March 14th, 2010 at 6:08 pm. Filed under: Arduino,Art,Level 6. | 2 Comments |

Wireless Robotics Platform: Cheap R/C Vehicle + Arduino + XBee + Processing

Difficulty Level = 8 [What’s this?]

UPDATE: Check out the new robotics platform project!

I built a wireless robotics platform from a cheap R/C car, an Arduino with XBee shield, small microswitch sensors, and a Processing program running on a remote computer to control the vehicle. The vehicle is completely controlled by the code running on the remote computer which allows very rapid prototyping of the code to tell the vehicle what to do and how to react to the sensor events received from the vehicle. I’m hoping this is a good way to teach my 9-year old son about programming.

Wireless computer-controlled robotics platform built on cheap RC vehicle, Arduino microcontroller, and XBee radios

Before I get into details, here’s an overview of the features:

  • All logic controlling the vehicle is performed in a Processing program running on remote computer. The Arduino program listens for commands from the remote computer.
  • Bi-directional wireless communication over XBee radios with (theoretical) 1-mile range. I’ve accomplished 1/4 mile range with these radios.
  • Sensor events are transmitted from the vehicle to the controlling computer. This vehicle has 3 microswitches – two on front bumper and one at the rear.
  • Original circuitry of vehicle replaced with dual H-Bridge circuit to control drive motor and turn motor. Drive motor is controlled with variable speed.
  • Power: Vehicle motors powered by 4 AA batteries. Arduino with XBee shield powered by 9V battery mounted at front of vehicle.
  • Simple communications protocol: 2 byte commands from controller to vehicle, one byte sensor readings from vehicle to controller.

The Hardware

There’s nothing special about the configuration of the XBee radios. They are running the AT firmware (“transparent mode”) which allows them to simply exchange serial data. The Libelium XBee shield on top of the Arduino makes it easy to read/write serial data from Arduino code.

Arduino and XBee shield on top of the vehicle

Inside the vehicle is a simple circuit board with an SN754410 quadruple half-H driver to drive the motors. The drive motor and turn motor are connected. I had to rip out the original circuit board (but I saved it!).

Read more…

Published by Michael, on March 4th, 2010 at 9:47 pm. Filed under: Arduino,Level 8,Processing,Robotics,XBee. | 43 Comments |

EZ-Expander Shield for Arduino

I’m happy to finally announce my new product, the EZ-Expander shield. After several months of hard work of sourcing parts, designing the PCB, and setting up a store, my first product is now available for purchase in the nootropic design store!

For all the technical details, go to the EZ-Expander page.

EZ-Expander shield

EZ-Expander on an Arduino

Published by Michael, on March 3rd, 2010 at 8:21 pm. Filed under: Arduino. | No Comments |