Introduction to Arduino Workshop Notes

arduinoUnoBoard

On Wednesday, Oct. 16, 2013, we had an introduction to Arduino workshop. Here are the notes from my presentation.

An Arduino is a hardware board that lets us connect hardware to our computers or mobile devices. We can take sensors or inputs, like a button, and “map” it to a behaviour on our computer. It acts as both an input and output, sometimes simultaneously. Simply, the Arduino board lets us plug “things” into our computers.

The Arduino board works with simple software “sketches” that are created using the Arduino IDE (Integrated Development Environment). The IDE is derived from a prototyping platform called Wiring, which itself s derived from Processing.

Processing took a complex programming language, Java, and “wrapped” it in a simpler approach. The goal was to help people learn by creating collections of programs – a kind of sketchbook of graphical programs for exploring ideas. The Arduino IDE and its markup continues the metaphor of a sketchbook of physical programs and devices.

connectors
The board needs power to operate. This can be done by either plugging in via USB, an AC-to-DC adapter (wall-wart) or a battery. You don’t necessarily need a computer to run the Arduino – it can run by itself depending on what you want to do. Currently, when using a battery or adapter, the recommended power range is 7 to 12 volts. If you overload your board, the Arduino has a fuse built into it to protect your computer. Additionally, it’s common for USB ports to offer some protection against unhealthy surges.

To begin, you need:
– Arduino board
– The Arduino IDE downloaded from http://arduino.cc/en/Main/Software
– A USB A to B cable (rectangle to square) – depending on your Arduino model

To compile and upload a sketch to your board you need to specify the correct Serial Port and Board from the Tools menu of the IDE.

The IDE launches with many example sketches. Most often, creating a sketch is a matter of hacking the supplied examples.

Arduino Pins

The pins on the Arduino board are tiny slots on the board to which you can connect wires or the “legs” of electronic components. All pins can be used as input (real world to your computer) or output (your computer to real world). The pins are categorised as being Digital or Analogue by default. You can specify what the pins do in your Arduino sketch – all Analogue In pins can be used as digital pins as well.

Digital Pins have 2 states – they’re either on or off. This is good for reading things like light switches or turning on/ off outputs like an LED light. Input digital pins either get power or they don’t. Output digital pins either supply power or they don’t.

Analogue Pins let you read inputs or sensors that have multiple states. For example, you can read an analogue pin to read the level of light in a room or the value of a dimmer switch or potentiometer (pot). Analogue In pins read the level of voltage that makes it through the sensor to the pin. e.g. a Photodiode has more or less voltage based on the level of light. An Arduino sketch supplies the voltage as a number between 0-1023 (10 bit resolution).

You’ll notice there are no “Analogue Out” pins. The board can’t output different levels of power by itself.

pwm
Six of the digital pins can be used in a special mode called PWM or pulse wavelength moderation. If you have an electronic component like an LED it really only has 2 states – “on” or “off”. Also, the Arduino board can only output “on” or “off” – it is a digital component after all.  Normally, if you want to brighten or darken a light you would limit the amount of power going to it. Often “limiting” the amount of power is wasteful. Also, if you have too little power with an LED it stops lighting up instead of dimming. We get around this by using PWM – a way of turning something on and off so quickly it looks like it’s brighter or darker. PWM lets you dim or make things that are only on/off behave more like analogue things – you use 2-states so quickly they appear to be multiple states.

Floating? The Digital Pins are incredibly sensitive and they are constantly looking for a HIGH (5V) or LOW (0V) signal. They can easily pick up interference from environmental noise – such as charges in the air. This causes the circuit to vary between 0 and 5v. This is called floating and can cause simple Digital Input pins to behave in seemingly random ways. Because this is such a common issue, the Arduino has special component that reduces the amount of noise called a pullup resistor. A Pull-up resistors lets you change your circuit so instead of floating, the circuit is only ever HIGH or LOW. Unfortunately, a pull-up resistor causes your circuit to invert – on becomes off. On the Arduino Uno R3, any digital pins that you set to INPUT and give a value of HIGH automatically use a built in pullup resistor (try not to use pin 13 for this – it’s got some extra functionality that might cause issues).

pinMode(pin, INPUT);           // set pin to input
digitalWrite(pin, HIGH);       // turn on pullup resistors

Programming in the Arduino IDE

The documentation for programming on the Arduino is comprehensive and helpful. Here are some highlights:

Comments:
Block comments are wrapped in /* and */. Single line comments are preceded by //

Variables:
Declare global (access from anywhere in the sketch) scoped variables in the beginning of your sketch. A variable is a word that is used to set or retrieve a piece of data. A declaration is comprised of a name, type and value.
e.g.
int pin = 13;
This creates a variable named “pin” of the type integer with the value of 13.

Functions:
Perform the same actions repeatedly by declaring a function that consists of a type, name, parameters, opening and closing curly braces and a return value that matches the type.
var a = newValue(3);
int newValue(int x){
int offset = 5;
return offset*x;
}

The setup function is run once when the board is powered up or reset. It’s a great place to declare how pins will be used and set initial values. Setup should always be included in a sketch.
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
}

The “loop” function is run repeatedly as long as the board is functioning properly.
void loop(){
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000); }

Serial Communications

You can send text messages, just like SMS, between your arduino and your computer. This is an easy way to have the arduino control a program or vice versa. Many programming/ scripting options let us communicate with the Arduino via Serial, such as Python, Java, Processing, NodeJS, AS3 etc. We can even do this from the command prompt of most Operating Systems:
e.g.
mode COM13 BAUD=9600 PARITY=n DATA=8
echo hello arduino > COM13
OR
demo% export PORT=/dev/tty.usbserial-A3000Xv0
demo% stty -f $PORT 9600 raw -parenb -parodd cs8 -hupcl -cstopb clocal
demo% printf “1” > $PORT

To add Serial communication functionality to your sketch you must initialise the serial communications and then either listen for or send data. Whenver you use Serial communications you specify the speed at which you want to communicate.
e.g.
void setup(){
//Initialise serial communications
Serial.begin(115200);
//Print a message
Serial.println(“Test Serial Communications”);
}

Example Project

The following device and sketch lets you control the brightness of an LED using a 10k pot (knob) and make the led flash by tapping the tact switch

Needed:
– Arduino, connection cable, computer
– 10 kOhm Potentiometer (knob)
– 220 Ohm Resistor
– Tact Switch
– White LED
– Bread board
– Jumper cables

potFade_bb

1. Connect the components as illustrated above
2. Upload the sketch supplied below
3. Turn the dial to change the brightness of the LED or tap the tact switch to change the speed that the LED flashes at

/*
Uses a pot on pin gnd, 5v and A0 to control an led on pins gnd and 9.
Flash an led and a beat that is hit into a switch.

Turning the pot fades the LED in and out.
Christopher Lewis
cttoronto.com
majorcallisto.com
gomangostudios.net

Oct. 21, 2013
*/

int fading_led = 9;
int brightness = 0;

//Used for determining the delaytime
unsigned long lasttime;
int delaytime = 2000;

//Track the current state of the led
//seperate this out so we can toggle the LED independant of
//the manual timer results
int ledState = LOW;

//Track the previous state of the switch so we can
//tell when someone has immediately pressed in stead of press/hold
//(switch down)
int lastSwitchState = LOW;

//the previous Millis for manually
//tracking time instead of
//using delay
unsigned long previousMillis;

void setup() {
  //Arbitrary baud
  Serial.begin(19200);
  
  //Set the pin that the led is attached to as
  //output
  pinMode(fading_led, OUTPUT);
  
  //Input pin for switch
  //Set to pull up to avoid floating
  pinMode(7, INPUT);           // set pin to input
  digitalWrite(7, HIGH);       // turn on pullup resistors
}

void loop() {
  
  //Read the pot attached to pin A0
  int sensorValue = analogRead(A0);

  //The current time
  unsigned long currentMillis = millis();
  
  //Brightness is 0-255. analog pins return 0-1023, so divide
  //by 4 to get brightness
  brightness = sensorValue/4;
  
  //If the button on pin 7 is LOW
  //then it's "on". Value is reversed because
  //of use of pullup
  if (digitalRead(7) == LOW){
    //if the switch was "off" but is now "on"
    //it's a "switch down" event
    if (lastSwitchState == HIGH){
      //Set the delaytime. Divide it by 2 so the light blinks off
      //and on in one beat
      delaytime = (currentMillis - lasttime)/2;
      //track the current millis so I can calculate again next time
      lasttime = currentMillis;
    }
    //Set brightness to off while button is pressed
    brightness = 0;
    lastSwitchState = LOW;
  }else {
    //It's not LOW so lets track that it's HIGH
   lastSwitchState = HIGH; 
  }
  
  //If I've gone past the delaytime
  //fire "event"
  if(currentMillis - previousMillis > delaytime) {
    previousMillis = currentMillis;  
    onDelayEvent()
  }
  //Set the light value every loop
  //so that the brightness can be adjusted
  //without waiting for the next onDelayEvent
  if (ledState == LOW){
      analogWrite(fading_led, 0);
  }else{
      analogWrite(fading_led, brightness);
  }
  delay(1);
}

function onDelayEvent(){
    //Toggle the light
    if (ledState == LOW){
      ledState = HIGH;
    }else{
      ledState = LOW;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Bitnami