Introduction to Arduino

Thanks!!!

Cloudspace

Adafruit

FamiLAB Volunteers

Agenda

  • Introduction to Arduino
  • Setting up your Arduino Environment
  • Your first Arduino sketch
  • Basic digital input and output
  • Making LEDs glow and blink on command
  • How to read knobs & switches

Special thanks to Tod E. Kurt, Lutz Hamel, and John Duksta for reuse of their Arduino class slides and graphics...  

Version UNO-22-1

A Word On Safety

• Electronics can hurt you

• Lead in some of the parts

• Wash up afterwards

• You can hurt electronics

• Static-sensitive: don’t shuffle your feet & touch

• Wires only bend so much

Photoseizure warning!

Unplug before wiring

Wash hands before (and after) picking nose

What Is Arduino?

The word "Arduino" can mean 3 things:

A physical piece of hardware

A programming environment (IDE)

A Community and a Philosophy

"An Arduino"

"Open up Arduinio"

"Arduino is awesome"

Arduino Hardware

  • Arduino is a microcontroller platform
  • Arduino consists of standardized hardware, bootloader, peripherals (shields)
  • Uses the Atmel AVR ATmega328 chip

Per Wikipedia: "A microcontroller (sometimes abbreviated µC, uC or MCU) is a small computer on a single integrated circuit containing a processor core, memory, and programmable input/output peripherals."

Microcontrollers are used in automatically controlled products and devices, such as automobile engine control systems, implantable medical devices, remote controls, office machines, appliances, power tools, toys and other embedded systems. By reducing the siz

Bootloader allows easy updating without the need for specialized programming hardware.

Peripheral connection allows plug and play "shields".

Estimate of several hundred thousand units in the field

Arduino Shields

  • Standard interface for expansion
  • Breaks out power, input/output pins

Arduino Bootloader Compatability

    

Arduino UNO Board

Digital input/output "pins"

USB interface

Test LED on pin 13

TX/RX pins

ATmega 328

Analog input "pins"

reset button

Power

Power Jack

Note: Things hooked up to TX and RX will affect ability to load sketches.

Arduino Uno Stats

  • Microcontroller - ATmega328
  • Operating Voltage - 5V
  • Input Voltage (recommended) - 7-12V
  • Input Voltage (limits) - 6-20V
  • Digital I/O Pins - 14 (of which 6 provide PWM output)
  • Analog Input Pins - 6 (can also be used as digital inputs)
  • DC Current per I/O Pin - 40 mA
  • DC Current for 3.3V Pin - 50 mA
  • Flash Memory - 32 KB (ATmega328) of which 0.5 KB used by bootloader
  • SRAM - 2 KB (ATmega328)
  • EEPROM - 1 KB (ATmega328)
  • Clock Speed - 16 MHz

Arduino Terminology

“sketch” – a program you write to run on an Arduino board 

“pin” – an input or output connected to something. 

               e.g. output to an LED, input from a knob. 

 “digital” – value is either HIGH or LOW. 

               (aka on/off, one/zero) e.g. switch state 

“analog” – value ranges, usually from 0-255. 

               e.g. LED brightness, motor speed, etc. 

Enough talking...

Let's make it DO something!

Arduino Software

Arduino Software

Tools

- Set Serial Port

- Board Type

Buttons

- Verify/Compile, Stop

- Upload

- Serial Monitor

Arduino Software

Arduino Software

Arduino Software

Verify/Compile

Upload

1K

Note: Pin 13 is a digital pin

I made an LED blink, so what?

• Most actuators are switched on and off with 

    a digital output 

• The digitalWrite() command is the software

    portion of being able to control just about anything 

• LEDs are easy, motors come next!

 

• Arduino has up to 13 digital outputs, and 

    you easily can add more with helper chips 

Sketch (Program) Structure

  • Declare variables at the top
  • Initialize
    • setup() function - runs once at the beginning, set pins, start serial for debugging, etc.
  • Running
    • loop() - run repeatedly, after setup()

Arduino "Language"

Language is standard C (but made easy)

Common functions

  • pinMode() – set a pin as input or output
  • digitalWrite() – set a digital pin HIGH / LOW
  • digitalRead() – read a digital pin’s state
  • delay() – wait an amount of time (in milliseconds)
  • analogRead() – read an analog pin
  • analogWrite() – write an “analog” value 
  • millis() – get the current time (in milliseconds since boot)

/*

  Blink

  Turns on an LED on for one second, then off for one second, repeatedly.

 

  This example code is in the public domain.

 */

void setup() {                

  // initialize the digital pin as an output.

  // Pin 13 has an LED connected on most Arduino boards:

  pinMode(13, OUTPUT);     

}

void loop() {

  digitalWrite(13, HIGH);   // set the LED on

  delay(1000);              // wait for a second

  digitalWrite(13, LOW);    // set the LED off

  delay(1000);              // wait for a second

}

"Leveraging Existing Code"

Have class change delay values.

/*

  Blink

  Turns on an LED on for one second, then off for one second, repeatedly.

 

  This example code is in the public domain.

 */

void setup() {                

  // initialize the digital pin as an output.

  // Pin 13 has an LED connected on most Arduino boards:

  pinMode(13, OUTPUT);     

}

void loop() {

  digitalWrite(13, HIGH);   // set the LED on

  delay(1000);              // wait for a second

  digitalWrite(13, LOW);    // set the LED off

  delay(1000);              // wait for a second

}

Analog Output

PWM Signals

  • Microcontrollers cannot generate analog output, but we can fake it by creating digital Pulse Width Modulate signals with different “duty cycles” - signals with different pulse widths.
  • To the analog world the different duty cycles create different effective voltages

On an Arduino UNO board, the PWM capable pins are noted with the "~" symbol

File

   Examples

       Analog

           Fading

"Fading" Example Sketch

/*  Fading -  This example shows how to fade an LED using the analogWrite() function. */

int ledPin = 9;    // LED connected to digital pin 9

void setup()  { 

  // nothing happens in setup 

void loop()  { 

  // fade in from min to max in increments of 5 points:

  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 

    // sets the value (range from 0 to 255):

    analogWrite(ledPin, fadeValue);         

    // wait for 30 milliseconds to see the dimming effect    

    delay(30);                            

  } 

  // fade out from max to min in increments of 5 points:

  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 

    // sets the value (range from 0 to 255):

    analogWrite(ledPin, fadeValue);         

    // wait for 30 milliseconds to see the dimming effect    

    delay(30);                            

  } 

}

Things to Try with "Fading"

  • Make it go really fast or really slow
  • Fading from half- to full-bright
  • Try other PWM pins
  • Multiple fading LEDs, at different rates

Analog Input

File

   Examples

       Analog

           AnalogInput

int sensorPin = A0;    // select the input pin for the potentiometer

int ledPin = 13;      // select the pin for the LED

int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {

  // declare the ledPin as an OUTPUT:

  pinMode(ledPin, OUTPUT);  

}

void loop() {

  // read the value from the sensor:

  sensorValue = analogRead(sensorPin);    

  // turn the ledPin on

  digitalWrite(ledPin, HIGH);  

  // stop the program for <sensorValue> milliseconds:

  delay(sensorValue);          

  // turn the ledPin off:        

  digitalWrite(ledPin, LOW);   

  // stop the program for for <sensorValue> milliseconds:

  delay(sensorValue);                  

}

Serial Output

int sensorPin = A0;    // select the input pin for the potentiometer

int ledPin = 13;      // select the pin for the LED

int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {

  // declare the ledPin as an OUTPUT:

  pinMode(ledPin, OUTPUT); 

  Serial.begin(9600); 

}

void loop() {

  // read the value from the sensor:

  sensorValue = analogRead(sensorPin); 

  Serial.println(sensorValue, DEC);

  // turn the ledPin on

  digitalWrite(ledPin, HIGH);  

  // stop the program for <sensorValue> milliseconds:

  delay(sensorValue);          

  // turn the ledPin off:        

  digitalWrite(ledPin, LOW);   

  // stop the program for for <sensorValue> milliseconds:

  delay(sensorValue);                  

}

Digital Input

Digital Input

  • Switches make or break a connection
  • Arduino wants to see a voltage
    • Specifically, a "HIGH" (5 volts)
    • or a "LOW" (0 volts)
  • With no connection, digital inputs "float" between 0 & 5 volts
  • Solution - "Pulldown" resistor
    • Resistor "pulls" input to ground (0 volts)
    • Pushing a switch "pushes" input to 5 volts
      • Pressed is HIGH
      • Not Pressed is LOW

File

   Examples

      Digital

          Button

Using digitalRead() 

  • In setup(): pinMode(myPin,INPUT) makes a pin an input
  • In loop(): digitalRead(myPin) gets switch’s position
    • If doing many tests, use a variable to hold the output value of digitalRead().
    • e.g. val = digitalRead(myPin)

"Button" Sketch

Examples->Digital->Button

/* Button -  Turns on and off a light emitting diode(LED) connected to digital  

 pin 13, when pressing a pushbutton attached to pin 2. */

 

const int buttonPin = 2;     // the number of the pushbutton pin

const int ledPin =  13;      // the number of the LED pin

// variables will change:

int buttonState = 0;         // variable for reading the pushbutton status

void setup() {

  pinMode(ledPin, OUTPUT);   // initialize the LED pin as an output   

  pinMode(buttonPin, INPUT);   // initialize the pushbutton pin as an input    

}

void loop(){

  

  buttonState = digitalRead(buttonPin);  // read the state of the pushbutton value

  // check if the pushbutton is pressed.

  // if it is, the buttonState is HIGH:

  if (buttonState == HIGH) {     

    // turn LED on:    

    digitalWrite(ledPin, HIGH);  

  } 

  else {

    // turn LED off:

    digitalWrite(ledPin, LOW); 

  }

}

Resources

arduino.cc - Official homepage. Check out the Playground & Forums

ladyada.net/learn/arduino/ - Great Arduino tutorials

freeduino.org - Index of Arduino knowledge

adafruit.com - Arduino starter kits, shields, tons of parts

sparkfun.com - Sells Arduino and lots of sensors & break-out boards

ruggedcircuits.com - "10 ways to destroy an Arduino" (Don't do these!)

Books:

“Physical Computing”, Dan O’Sullivan & Tom Igoe

“Making Things Talk”, Tom Igoe

People:

    FamiLAB

    User Groups

File->Examples->Analog->Fading

"Fading" Example Sketch With Analog Read

/*  Fading -  This example shows how to fade an LED using the analogWrite() function. */

int ledPin = 9;    // LED connected to digital pin 9

int sensorPin = A0;    // select the input pin for the potentiometer

int sensorValue = 0;  // variable to store the value coming from the sensor

void setup()  { 

  // nothing happens in setup 

void loop()  { 

  sensorValue=analogRead(sensorPin);

  // fade in from min to max in increments of 5 points:

  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 

    // sets the value (range from 0 to 255):

    analogWrite(ledPin, fadeValue);         

    // wait for 30 milliseconds to see the dimming effect    

    delay(sensorValue);                           

  } 

  // fade out from max to min in increments of 5 points:

  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 

    // sets the value (range from 0 to 255):

    analogWrite(ledPin, fadeValue);         

    // wait for 30 milliseconds to see the dimming effect    

    delay(sensorValue);                            

  } 

}

Using the Serial Monitor

/*  Fading -  This example shows how to fade an LED using the analogWrite() function. */

int ledPin = 9;    // LED connected to digital pin 9

int sensorPin = A0;    // select the input pin for the potentiometer

int sensorValue = 0;  // variable to store the value coming from the sensor

void setup()  { 

  Serial.begin(9600);

void loop()  { 

  sensorValue=analogRead(sensorPin);

  Serial.println(sensorValue, DEC);

  // fade in from min to max in increments of 5 points:

  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 

    // sets the value (range from 0 to 255):

    analogWrite(ledPin, fadeValue);         

    // wait for 30 milliseconds to see the dimming effect    

    delay(sensorValue);                           

  } 

  // fade out from max to min in increments of 5 points:

  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 

    // sets the value (range from 0 to 255):

    analogWrite(ledPin, fadeValue);         

    // wait for 30 milliseconds to see the dimming effect    

...                         

Intro To Arduino - Google Slides