Introducing Arduino – a simple Raspberry Pi alternative
Finally, take one last black jumper wire and connect it to the common ground, in the same column as the black jumper wires connected to the switches. The other end should go into one of the Arduino’s ground pins, marked GND. Now the circuit is complete: each switch is connected to a digital pin on the Arduino, and to the Arduino’s ground, which provides everything that’s required for the Arduino to start detecting when the buttons are pushed – once the device has been programmed.
2. Installing the software
Programming the Arduino Leonardo requires an integrated development environment (IDE), also known as Arduino. Available for Windows, OS X and Linux, the Arduino IDE provides a simple method for writing and uploading programs to the microcontroller chip that’s at the heart of the Arduino.
The Arduino software can be downloaded for free from the official website. Pick the version for your operating system, download the archive file and extract it somewhere on your system – the desktop is as good a place as any. If you’re a Linux user, make sure you pick the right binary format (32-bit or 64-bit) for your distribution.
The Arduino IDE requires no real installation: once the files have been extracted from the ZIP archive, it’s ready to use. Browse to the folder and double-click on the Arduino file to load the IDE. If you already have the Arduino IDE installed, make sure you’re running the latest release. The Leonardo is the latest of the mainstream Arduino models; older Arduino IDE versions can’t connect correctly.
3. Writing the sketch
The text editor window of the Arduino IDE is where the code is written. As in many programming languages, program code should begin with definitions of the variables and constants that will be used later. In this case, we’ll begin our code by setting up the physical buttons we’ll be making use of:
These lines of code tell the Arduino which pins have switches connected, and set up the initial status of a quartet of variables designed to monitor the status of each switch. This ensures each macro triggers only once when a button is pressed.
An Arduino program has a segment labelled “setup”. Each time the Arduino is powered on, the contents of this segment are executed once and then ignored until power is lost again. Type the following:
The pinMode instruction tells the Arduino whether a pin is an input or output, while INPUT_PULLUP tells it to turn on its internal pull-up resistors. These provide a known voltage on each pin, which will drop when the button is pressed and the connection is made to ground. Without the pull-up resistors, the circuit would require external resistors connected between the +5V pin and each of the buttons. Finally, the code tells the Arduino to emulate a keyboard.
The next section of an Arduino program is the “loop”, which runs continuously as long as the Arduino has power. Start this section as follows: