Arduino Serial I/O

Read time: 10 minutes (2739 words)

There will be times when your Arduino code is not working, and you need a way to debug things. The traditional way to do this as a beginning programmer is to sprinkle print statements in your code to see what is happening.

Let’s tackle finding out if a Joystick is actually working.

SparkFun Joystick

Here is a simple device, made up of two potentiometers and a few buttons. The buttons are a simple input devices that can figure out if they are pressed or not. Seems simple enough until you learn about something called switch bouncing, then it gets a little harder.

The potentiometers are different. Basically they are a variable resistor which oyu connect to 5-volts and ground on two of the pins. The third pin is connected to a “wiper” that sweeps across the resistor as you turn a shaft. The output on this pin will be a voltage varying between the “reference voltage” attached to one pin (5-v in this case) and ground (0-volts). That means that the pin delivers some voltage between 0 and 5 volts, and we need to figure out that that voltage is.

The actual joystick gadget is just two potentiometers connected through a mechanism that moves the shafts on both “pots” as you maneuver the stick around. You need to read both analog signals to figure out where the joystick os positioned, then do something with that information.

Starting Point

Like most projects involving the Arduino, it is a good idea to start with a sample “sketch” (basically a stripped down c/c++ program) and make sure you can actually talk to the device. THe SparkFUn Joystick is :1 designed to attach on top of the Arduino, which is a bit annoying because it hides all the lights on the board.


We need to find the details about how this gadget is wired up, so we know what pins to control in the Arduino (and the AVR). Time to go to SparkFun:

This guide includes assembly instructions, in case the Joystick you acquired is a kit. Since my boards are assembled, we skip past this and fine the code needed to test the device:


This file has a pde extension. Newer Arduino IDEs use an ino extension. Just rename it and it should run fine.

This code tells you exactly what pins on the board are doing, which is an important thing to find out!

Test in IDE

With this code in hand, it is time to test the device. CAREFULLY, plug in the board.


Carefully, because if you do this wrong, you can bend the pins that need to slide into each pllug on the board. When removing the board, pry a little amount on one sode, then a little amount on the opposite side so you remove the board as “straight up: as you can. If you bend a pin, carefully straightn it back up. If this happens a lot, the pins mak weaken and snap off, then you are done, unless you like electronic surgery!