This is Wiring (and Arduino)introducing the Wiring and Arduino i/o boards through simple examples
Part of the Hardware and Physical Computing course from the
Media Technology MSc program of Leiden University, The Netherlands |
|||
|
|||
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 |
![]() This image was borrowed from Massimo Banzi's website www.potemkin.org.
|
||
Arduino 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
|
||
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: For example, the following program creates a grey canvas in
|
||
programming the i/o board |
If power comes from an external power supply, you can disconnect the USB cable and the board keeps running your program. 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 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 (
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 |
||
analog input |
Analog input and output signals can have many values, not just 0V ( int v = analogRead(pin); // value between 0 and 1023Source 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 1023Source 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. analogWrite(a, v); // set analog out pin a to value vSource 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.
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. // 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.
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.
void setup() {
attachInterrupt(7, myRoutine, FALLING);
}
void myRoutine() {
n++;
}
Source code (new!)
Using interrupts can be tricky. Two particular points of attention are:
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. 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 |
|