Arduino Workshop


Workshop Notes taken during Arduino workshop given by David Cuarteilles and Massimo Banzi at Esdi in Barcelona, June 2006.
Further experiments by Artificialtourism.

Introduction

About Arduino

Arduino is an open-source physical computing platform based on a simple i/o board and a integrated development environment (IDE) that implements the Processing/Wiring language.

Arduino can be used to develop stand-alone interactive objects and installation or can be connected to software on your computer.

The project is the result of the collaboration Between David Cuartielles and Massimo Banzi, both teachers of physical computing, who saw the need for a Hands-on Prototyping tool for designers. The rest of the team is composed by David Mellis (Responsible for building the software engine), Tom Igoe (NYU Physical Computing teacher) and Gianluca Martino (who makes the boards).


Getting started

Downloading & Installing the Software

The Arduino IDE is a cross-platform tool for easy editing and uploading of Arduino programs. The front-end builds on Processing and is written in Java. The code that runs on the board is based on the Wiring API and makes use of the Procyon AVR-LIB.

Download

http://www.arduino.cc/en/Main/Software

All Mac OS X users will need to run the new macosx_setup.command file included in the distribution (and possibly reboot).

Select Correct Port

Pc: Menu/Control Panel/System/hardware/device manager Lookup device in ports (com & LPT) i.e COM4. Select port in arduino IDE: tools/serial port/…

Select the correct baud rate

In the Arduinos 2.0 normally 19200.


Writing Arduino code

Arduino code is similar to processing.
A typical code structure will include:

  1. // Comments
    they should state what the program does and include contact details of the programmer for debugging purposes
  2. variable or function declaration
    Variable types:

    • integers
    • Boolean
    • floating points (not yet supported properly).
  3. setup
    One of two main methods, which runs once at the start of the program
  4. loop
    The other main method, which runs continuously forever.

Commands include

  • pinMode (INPUT / OUTPUT)
  • digitalWrite
    (HIGH=ON=5V=1=True / LOW=OFF=0V=0=False)
  • digitalRead (HIGH / LOW)
  • analogRead
  • serialRead
  • serialSend
  • delay (for debugging and flow control – in milliseconds)


Arduino board overview

Starting clockwise from the top center:


(Board overview and image from http://www.arduino.cc/en/Tutorial/ArduinoBoard)
  • Analog Reference pin – AREF
  • Digital Ground – GND
  • Digital Pins 2-13
  • Digital Pins 0-1 – Serial In/Out – TX/RX – These pins cannot be used for digital i/o (digitalRead and digitalWrite) if you are also using serial communication (e.g. Serial.begin).
  • Reset Button – S1
  • In-circuit Serial Programmer – ICSP
  • Analog In Pins 0-5
  • Power – 5V 9V – and Ground Pins – GND
  • External Power Supply In (9-12VDC) – X1
  • Toggles External Power and USB Power – SV1 - Place jumper on two pins closest to desired supply.
  • USB - Used for uploading sketches to the board and for serial communication between the board and the computer; can be used to power the board.


Exercises

LEDs

  • Components that turn electricity into light.
  • Have polarity, only will light up if connected the right way around.
  • Need constant 1.5V voltage.
  • Use with a 220Ω pull-down resistor. (On Arduino board there’s a built-in one on pin 13)

Typically the long leg is positive, and should connect to pin 13. The short leg – with flag – connects to GND; If looked from above, the bulb will also normally have a flat edge indicating negative pole.

Light Emitting Diodes


led_blink example code

/* Blinking LED
* turns on and off a light emitting diode(LED) connected to a digital
* pin, in intervals of 2 seconds. Ideally we use pin 13 on the Arduino
* board because it has a resistor attached to it, needing only an LED
* Created 1 June 2005
* copyleft 2005 DojoDave
* http://arduino.berlios.de
* based on an orginal by H. Barragan for the Wiring i/o board
*/
int ledPin = 13; // LED connected to digital pin 13
void setup()
{
  pinMode(ledPin, OUTPUT);
// sets the digital pin as output
}
void loop()
{
  digitalWrite(ledPin, HIGH);
// sets the LED on
  delay(1000); // waits for a second
  digitalWrite(ledPin, LOW); // sets the LED off
  delay(1000); // waits for a second
}

Load program into Arduino board

  1. Compile program (verify any mistakes).
  2. Press reset button on board.
  3. Upload program to arduino board.

Program will start running after 7 seconds, once is loaded it can run stand-alone from the computer provided that it has its own power source


Making circuits using a breadboard

The breadboard is a device used to build circuits without the need of soldering. In the central part all the pins lined under a number are connected, while the ones on the top and bottom edges – usually marked with black and red lines – are connected horizontally.

By plugging components in the right holes, complex circuits can be built and modified in the space of a few minutes.

When plugging components remember electrons will always cross the minimum amount of components.



Resistive sensors
Relate to physical properties of material.

Switch

  • Simplest sensor
  • Measure user input
  • Micro-switches can detect collision
  • Tilt-switches: ON/OFF through gravity.

Photo-resistive sensor

  • Change resistance according to light received
  • Need external pull-up (located before sensor) resistor
  • Made from epoxy resin semiconductor and open circuit


Thermistor

  • Conductivity of carbon material changes depending on temperature
  • Results are non-linear

Flex sensor

  • Conductivity of carbon material changes when flexed

Slider


Piezo

  • Piezo-electric crystals produce a voltage when subjected to mechanical stress
  • also in reverse, an external voltage will cause them to change shape
  • Used in microphones, vibration or as pressure sensors

Potentiometer

  • Resistance between A-B is constant
  • R(AB)=10k
  • R(AC)!=R(AB)
  • R(AB)=R(AC)+R(CB)


Digital Input: Reading a push button
Feeding information from the environment to the microcontroller.

The Arduino pins are extremely sensitive, they can read electric noise from the environment. testing the following code with just a wire on pin 7 gives us random HIGH/LOW readings. In projects to use data from digitalRead the pin reading should be kept HIGH to measure change to LOW.

Connect three wires to the Arduino board. The first from one leg of the pushbutton through a pull-up resistor (here 10K Ω) to the 5 volt supply. The second goes from the corresponding leg of the pushbutton to ground. The third connects to a digital i/o pin (pin 7) which reads the button’s state.


Digital Read example code

/* Basic Digital Read
* turns on and off a light emitting diode(LED) connected to digital
* pin 13, when pressing a pushbutton attached to pin 7. It illustrates the
* concept of Active-Low, which consists in connecting buttons using a
* 1K to 10K pull-up resistor
* Created 1 December 2005
* copyleft 2005 DojoDave
* http://arduino.berlios.de
*/
int ledPin = 13; // choose the pin for the LED
int inPin = 7; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
void setup() {
  pinMode(ledPin, OUTPUT);
// declare LED as output
  pinMode(inPin, INPUT); // declare pushbutton as input
}
void loop(){
  val = digitalRead(inPin);
// read input value
  if (val == HIGH) { // check if the input is HIGH (button released)
   digitalWrite(ledPin, LOW); // turn LED OFF
  } else {
   digitalWrite(ledPin, HIGH);
// turn LED ON
  }
}

Change pushbutton to tilt sensor and light sensor.

Analogue input / output using a light sensor
Open communication channel between micro-controller and computer.

Use the same circuit as before, switching the button with the light sensor and placing the pin between the pull-up resistor and the LDR light sensor.

The function allows us to send numeric values to the computer. Digital variables will range from 0-1024 (1 Bit resolution).

Open serial monitor to read data from sensor.


Analog Read/Send + Serial communication example code

/* Analog Read Send
* turns on and off a light emitting diode(LED) connected to digital
* pin 13. The amount of time the LED will be on and off depends on
* the value obtained by analogRead(). In the easiest case we connect
* a potentiometer to analog pin 2. Sends the data back to a computer
* over the serial port.
* Created 1 December 2005
* copyleft 2005 DojoDave
* http://arduino.berlios.de
*/
int potPin = 2; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor
void setup() {
  pinMode(ledPin, OUTPUT);
// declare the ledPin as an OUTPUT
  Serial.begin(9600); // use the serial port to send the values back to the computer
}
void loop() {
  val = analogRead(potPin);
// read the value from the sensor
  Serial.println(val); // print the value to the serial port
  digitalWrite(ledPin, HIGH); // turn the ledPin on
  delay(val); // stop the program for some time
  digitalWrite(ledPin, LOW); // turn the ledPin off
  delay(val); // stop the program for some time
}

Complex sensors

Infrared proximity sensor

  • Paired infrared LED and detector, detect objects in near proximity
  • Can be confused by daylight – normally covered in red
  • Pulse of IR emited by emiter, light travels and either hits an object or keeps on going. If there is no collision reading shows no object, if light is reflected it returns creating a triangle. Receiver has a lens that transmits CCd array based on angle.

Passive infrared detector (PIR)

  • Measures changes in thermal heat patterns, given off by animals
  • 2 pyroelectric sensors connected to a comparator. If there is instantaneous change in the ouput of both it means movement
  • Most common motion detector


PING range finder – Ultrasound

  • Transmits ultrasonic bursts and provides an output pulse corresponding to time required to return
  • Best to be used outdoors in daylight over 1 meter, maximum 20 degrees angle

  • The sensor counts with 3 pins, two are dedicated to power and ground, while the third one is used both as input and output. Pin is set to OUTPUT to locate object / INPUT to read signal


Accelerometer

  • Measure 0-2g in either axis
  • Temperature compensated from 0-70 C
  • pulse output for x and y
  • analogue output for temperature
  • Sends pulse continuously


Reading a Parallax Ultrasound Sensor

Ultrasound Sensor example code

/* Ultrasound Sensor
* Reads values (00014-01199) from an ultrasound sensor (3m sensor)
* and writes the values to the serialport.
* http://www.xlab.se | http://www.0j0.org
* copyleft 2005 Mackie for XLAB | DojoDave for DojoCorp
*/
int ultraSoundSignal = 7; // Ultrasound signal pin
int val = 0;
int ultrasoundValue = 0;
int timecount = 0; // Echo counter
int ledPin = 13; // LED connected to digital pin 13
void setup() {
  beginSerial(9600);
// Sets the baud rate to 9600
  pinMode(ledPin, OUTPUT);// Sets the digital pin as output
}
void loop() {
  timecount = 0;
  val = 0;
  pinMode(ultraSoundSignal, OUTPUT);
// Switch signalpin to output
  /* Send low-high-low pulse to activate the trigger pulse of the sensor
  * ——————————————————————-
  */
  digitalWrite(ultraSoundSignal, LOW); // Send low pulse
  delayMicroseconds(2); // Wait for 2 microseconds
  digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
  delayMicroseconds(5); // Wait for 5 microseconds
  digitalWrite(ultraSoundSignal, LOW); // Holdoff
  /* Listening for echo pulse
  * ——————————————————————-
  */
  pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
  val = digitalRead(ultraSoundSignal); // Append signal value to val
  while(val == LOW) { // Loop until pin reads a high value
    val = digitalRead(ultraSoundSignal);
  }
  while(val == HIGH) {
// Loop until pin reads a low value
    val = digitalRead(ultraSoundSignal);
    timecount = timecount +1;
// Count echo pulse time
  }
  /* Writing out values to the serial port
  * ——————————————————————-
  */
  ultrasoundValue = timecount; // Append echo pulse time to ultrasoundValue
  serialWrite(‘A’); // Example identifier for the sensor
  printInteger(ultrasoundValue);
  serialWrite(10);
  serialWrite(13);

  /* Light up LED if any value is passed by the echo pulse
  * ——————————————————————-
  */
  if(timecount > 0){
    digitalWrite(ledPin, HIGH);
  }

  /* Delay of program
  * ——————————————————————-
  */
  delay(100);
}

Reading a Memsic 2125 Dual Axis Accelerometer

Accelerometer sensor example code

/* Accelerometer Sensor
* Reads an 2-D accelerometer attached to a couple of digital inputs
* and sends their values over the serial port;
* makes the monitor LED blink once sent
* http://www.0j0.org
* copyleft 2005 K3 – Malmo University – Sweden
* @author: Marcos Yarza
* @hardware: Marcos Yarza
* @project: SMEE – Experiential Vehicles
* @sponsor: Experiments in Art and Technology Sweden, 1:1 Scale
*/
int ledPin = 13;
int xaccPin = 7;
int yaccPin = 6;
int value = 0;
int accel = 0;
char sign = ‘ ‘;
int timer = 0;
int count = 0;
void setup() {
  Serial.begin(9600);
// Sets the baud rate to 9600
  pinMode(ledPin, OUTPUT);
  pinMode(xaccPin, INPUT);
  pinMode(yaccPin, INPUT);
}

/* (int) Operate Acceleration
* function to calculate acceleration returns an integer
*/
int operateAcceleration(int time1) {
  return abs(8 * (time1 / 10 – 500));
}

/* (void) readAccelerometer
* procedure to read the sensor, calculate acceleration and represent the value
*/
void readAcceleration(int axe){
  timer = 0;
  count = 0;
  value = digitalRead(axe);
  while(value == HIGH) {
// Loop until pin reads a low
    value = digitalRead(axe);
  }
  while(value == LOW) { // Loop until pin reads a high
    value = digitalRead(axe);
  }
  while(value == HIGH) {
// Loop until pin reads a low and count
    value = digitalRead(axe);
    count = count + 1;
  }
  timer = count * 18;
//calculate the teme in miliseconds
  //operate sign
  if (timer > 5000){
    sign = ‘+’;
  }
  if (timer < 5000){
    sign = ‘-’;
  }

  //determine the value
  accel = operateAcceleration(timer);

  //Represent acceleration over serial port
  if (axe == xaccPin){
    Serial.print(‘X’);
  } else {
    Serial.print(‘Y’);
  }
  Serial.print(sign);
  Serial.print(accel);
  Serial.println();
}
void loop() {
  readAcceleration(xaccPin);
//reads and represents acceleration X
  readAcceleration(yaccPin); //reads and represents acceleration Y
  digitalWrite(ledPin, HIGH);
  delay(300);
  digitalWrite(ledPin, LOW);
}

Motors

DC motors

  • Use magnets to create motion
  • Constant velocity
  • Pros: cheap, efficient, plentiful & easy to interface
  • Cons: too fast, draw lots of current
  • Switches to drive them:
    • Transistor – Electrical switch – ON/OFF + Speed in one direction
    • Relay – Electromecanical switch – ON/OFF only
    • H-Bridge – ON/OFF + Direction + Speed – Can drive up to 4 motors

  • Speed of motors is more or less proportional to voltage. Ideal way toi control speed is pulse width modulation (PWM).
  • To know position of motor use an encoder


Stepper Motors

  • Digitally controlled brush-less motor
  • Can rotate specific number of degrees – step- between 72-90
  • Pros: High precision, low voltage
  • Cons: Very hard to interface, use only if free


Servo motors

  • Closed loop control system: DC Motor + Gears + Potentiometer
  • Allow for precise positioning
  • Built-in control circuit uses varying external pulse width to set position (based on number of milliseconds)
  • Position from 60-180 degrees
  • Only reads one digital pin