This is Wiring (and Arduino)

introducing the Wiring and Arduino i/o boards through simple examples
by Maarten Lamers

Part of the Hardware and Physical Computing course from the Media Technology MSc program of Leiden University, The Netherlands
Verify Stop    New Open Save Upload to I/O Board Serial Monitor

introduction

The Wiring i/o board is a small, cheap standalone computer with many connection capabilities. It can be easily programmed in a variant of the Processing language, with a similar programming environment. The board can control all kinds of sensors and actuators. Sensors allow the board to acquire information from the surrounding environment (temperature sensors, light sensors, distance sensors, etc). Actuators are devices that allow the board to create changes in the physical world (lights, motors, heating devices, etc). It can also interact easily with other devices and computers, such as your PC/Mac, GPS receivers, barcode readers.

Wiring is an open project initiated by Hernando Barragán (University of Los Andes, Architecture and Design School). It builds on Processing, the open source programming language and environment initiated by Ben Fry and Casey Reas.

Arduino was inpired by Wiring and is basically the same thing. It is programmed in the Wiring language. Wherever I mention Wiring on this page, you can just as easily read Arduino. I personally use Wiring boards, but within our "Hardware and Physical Computing" course students must solder their own Arduino board, and create an interactive object from it.

One great thing about these i/o boards are the available libraries. By using them, it becomes very easy to program difficult things or connect complex stuff. For example, there are libraries for serial communication with other devices, connecting LCD displays, driving stepper motors, etcetera.

This lecture introduces the Wiring and Arduino i/o boards to Media Technology students through simple examples that demonstrate how to connect and program the board. All examples are demonstrated live during the lecture. It should give you enough knowledge to decide if you can use the i/o board in future Media Technology installations. You need not be an excellent programmer to understand the examples, and no knowledge of electronics is assumed. The examples given here are borrowed or derived mainly from the Wiring website.

References to other websites and material are placed at the bottom of this page.


Wiring i/o board

Wiring i/o board

This image was borrowed from Massimo Banzi's website www.potemkin.org.
  • 42 digital i/o pins, 5 digital i/o ports (8 pins each)
  • an on-board led for debugging
  • 8 analog input pins
  • 6 analog (PWM) output pins
  • multiple serial ports (at the expense of digital i/o pins)
  • 8 external interrupt pins
  • power from USB or external power supply
  • Extras: 8 additional digital i/o pins, I2C communication interface

Arduino i/o board

Arduino Basic i/o board

Photograph by Nicholas Zambetti, borrowed from www.arduino.cc. Different versions of the Arduino i/o board exist, but the basic versions have
  • 14 digital i/o pins, of which 6 can be analog (PWM) output pins
  • 6 analog input pins
  • multiple serial ports (at the expense of digital i/o pins)
  • 2 external interrupt pins
  • power from USB or external power supply
  • Extras: I2C communication interface

Processing

This lecture assumes knowledge of the Processing programming language, or some experience in another (imperative) programming language. For those who are not familiar with Processing, here is the most important thing to know:

¡ Most programs continuously repeat the same code. For example, your program may repeatedly check where the mousepointer is and they act upon that. As a result, Processing supports two important procedures: setup() and draw(). Within setup() you write the code that should be executed only once when your program starts. After that, the code that you place in draw() is repeated continuously. !

For example, the following program creates a grey canvas in setup() and repeatedly draws one random white dot in draw(). The dots are drawn on a vertical line through the mousepointer. In the Wiring language, draw() is called loop(). Check out the Processing code for this example.

To view this content, you need to install Java from java.com

  // setup() is executed once, upon program startup
  void setup() {
    size(200,200);
    background(102);
    stroke(255);
  }

  // draw() is repeated continuously after setup() ends
  void draw() {
    float y = random(0,height);
    point(mouseX, y);
  }
		


programming the i/o board

  1. Write a program in the Wiring/Arduino programming environment,
  2. click the 'play' button to compile it,
  3. connect the i/o board to your computer via USB,
  4. click the 'upload' button to send your program to the i/o board,
  5. press the board's reset button to start the program, et voila!
     
  6. If power comes from an external power supply, you can disconnect the USB cable and the board keeps running your program.
  7. Even after all power was switched off, the i/o board retains your program in memory. When you reconnect the power, it starts running automatically.

digital output

This is the "Hello world!" example of the Wiring language, and the first thing you should try. It flashes several leds in sequence. The leds are connected to digital i/o pins 0-7. When in OUTPUT mode, these digital pins can supply either 0V (LOW) or 5V (HIGH).

pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
digitalWrite(pin, HIGH);
Source code, How to connect a led

digital input

A digital input signal can be either 0V (LOW) or 5V (HIGH). It can be read via a digital i/o pin, if that pin is set to INPUT mode. To generate digital input signals you must make or break an electrical circuit, such as with a button or switch.
This example uses a pushbutton to control the direction of the led-sequence: pressing it reverses the direction.

pinMode(s, INPUT);
if (digitalRead(s) == LOW) {
  p++;
} else {
  p--;
}
Source code, How to connect a button

If you connect a button like I did, then pressing it (closing the contact) makes the digital i/o pin go LOW, and releasing it sends it back to HIGH. This may be different from what you would expect.

analog input

Analog input and output signals can have many values, not just 0V (LOW) and 5V (HIGH). Voltage levels between 0 - 5V can be read via the analog input pins. Analog input is quite useful, because many different sensors can be used to easily control an input voltage. There are sensors for light, distance, stretch, magnetism, pressure, temperature, etcetera.
In this example, a variable resistor (a.k.a. potentiometer) is used to control the voltage on an analog input pin. The values read by the code are used to set the speed of the led sequence.

int v = analogRead(pin);  // value between 0 and 1023
Source code, How to connect a potentiometer

analog input

This is another example of analog input. A photoresistor is connected to an analog input pin. A photoresistor changes its electrical resistance, depending on how much light it detects. In this example, the detected level of ambient light determines the number of leds that light up.

int v = analogRead(pin);  // value between 0 and 1023
Source code, How to connect a photoresistor

analog output

Analog output pins can generate voltage levels between 0 - 5V, using a method called Pulse Width Modulation. PWM is basically a way of faking different voltages by very quickly alternating between 0V and 5V: the longer the 5V spikes take, the higher the output voltage appears.
This example makes a led connected to an analog output pin fade in and out, by changing the pin's voltage between 0 - 5V.

analogWrite(a, v);    // set analog out pin a to value v
Source code

LCD display

Most cheap character-based LCD displays contain the same controller chip (the HD44780, if you must know). The Wiring language contains a standard library to communicate with this controller chip in a very simple way.
This example demonstrates how to print text on an LCD display.

LiquidCrystal myLCD = LiquidCrystal(13,14,15,2);
myLCD.clear();
myLCD.home();                // send cursor to position (0,0)
myLCD.print("My name is:");
myLCD.setCursor(4, 1);       // send cursor to position (4,1)
myLCD.print("Maarten");
Source code, How to connect an LCD display

serial communication

The i/o board can communicate with other devices via several communication protocols. The serial port can be used to communicate with a PC/Mac, and can be emulated via the USB port. In this way the i/o board and your computer can interact.
This example sends the values read from an analog input pin over the serial port. It uses the "Serial Monitor" tool of the Wiring/Arduino programming environment to show how the data is received by the computer.

// open serial communication at 9600 bits-per-second
Serial.begin(9600);
// send a value, coded as decimal characters, e.g. "289" for value 289.
Serial.println(v, DEC);
Source code

This example does exactly the same, but codes the data as a byte value.

// send a value, coded as a byte value.
Serial.print(v, BYTE);
Source code

serial communication

Serial communication can act in two directions simultaneously. This example lets a Processing program on the PC react to data received from the i/o board, and vice versa. By controlling a potentiometer on the i/o board, the color of an object in a Processing application is changed. Meanwhile, mouse positions are sent from the Processing application to the i/o board, and make the leds light up.
The computer application can be written in any language that supports serial communication, such as Max/MSP, C, C++, Basic, Python, any language really.

if (Serial.available() > 0) { 
    int p = Serial.read();
}
Source code, Processing code

external interrupts

Interrupts are actions that are taken automatically when a specific event happens. These actions literally interrupt the running program. After the interrupt is over, program execution continues where it left off. External interrupts are triggered by changes in a digital input signal. They can be very useful if you want your code to react fast to external events, while keeping your code simple.
In the example below we connect a pushbutton to an external interrupt pin. In code, we attach a routine to the external interrupt: when the button is pressed, the value of the pin falls (from HIGH to LOW), and the routine is automatically called. Wiring supports 8 external interrupts, on digital pins 0-3 and 36-39. Arduino supports 2 external interrupts, on digital pins 2 and 3.

void setup() {  
  attachInterrupt(7, myRoutine, FALLING);
}

void myRoutine() {
  n++;
}
Source code (new!)

Using interrupts can be tricky. Two particular points of attention are:

  • While an interrupt routine is running, all other interrupts are blocked. As a result, timers will not work in interrupt routines and other functionality may not work as expected. Therefore, always keep your interrupt routines short and simple.
  • When you press a physical button, it 'bounces', generating a short sequence of alternating HIGH and LOW states. As a result, multiple external interrupts may be generated by one press of a button. Be aware of this.

eeprom

EEPROM (electrically erasable programmable read-only memory) is memory that retains the values stored in it when power is disconnected. This is actually where your program code is also stored on the i/o board.
It is possible for you application to store values in the EEPROM and read them out later, much later. When you upload new code into your i/o board, the values in this particular EEPROM section are not changed. This is very useful if you want your Wiring/Arduino application to remember its data or configuration for later use. Wiring has 4096 bytes of EEPROM memory available to store values in, Arduino has 512 bytes.

b = EEPROM.read(28);
EEPROM.write(28, b);
Source code

connecting other stuff

motors, servo's, mouse, barcode scanner's, GPS and other serial devices, radio communication... (I am working on a follow-up lecture to introduce these topics)


references

photographs


Preparations for the lecture about Wiring boards


Maarten (right, blue shirt) talking tech stuff with some Media Technology students


Students during the Hardware and Physical Computing course
(photograph by ikbenlisa)


Students during the Hardware and Physical Computing course
(photograph by ikbenlisa)