ARDUINO 1: Intro

DOWNLOAD INSTALL AND SETUP

  • Follow the download and installation instructions for getting the Arduino software here.
  • If you are having difficulty getting your computer to recognize the Arduino, you may need to manually install a driver on your computer – download here. If you have a mac and are confused as to whether your computer is 32bit or 64bit architecture. See this guide here.
  • Download this circuit design software Fritzing here: http://fritzing.org/download/

WHAT IS IT?

  • Arduino is a single chip computer on a single micro-controller board.
  • Atmel Microcontroller (Uno= AVR atmega 328) with bootloader.
  • Separate ATmega controller deals with serial communication. (Except on Leonardo and a few other boards which operate on only one microcontroller).
  • We are using the uno board.

COMPONENTS AND PINS:

Screen Shot 2015-02-19 at 6.39.05 PM

Screen Shot 2015-02-19 at 6.38.56 PM

Screen Shot 2015-02-19 at 6.38.49 PM

 

How is it different from other small computers?

Screen Shot 2015-02-19 at 11.59.51 AM

Comparison article:

http://makezine.com/2013/04/15/arduino-uno-vs-beaglebone-vs-raspberry-pi/

POWER

  • Input Voltage on Jack: between 6 and 20v (7-12v recommended)
  • The board is 5v regulated- digital standard.
  • There is also a 3.3v power pin. – Lower energy consumption.  Good for lower voltage / small battery projects
  • Mixing 5v and 3.3v can be tricky.
  • Arduino uses 5mA-25mA current on its own.  4 AA pack (6v) will power approx. 60hr (assuming 1500mAH/25mA AA)

LANGUAGE

Don’t ignore the learning power of the example files!!!! And for help and instruction on what the corresponding circuits look like, see the fritzing software examples.

Screen Shot 2015-02-19 at 12.04.09 PM

BASIC SKETCH STRUCTURE:

Screen Shot 2015-02-19 at 1.05.35 PM

TROUBLESHOOTING?

Trouble uploading to the board? Always check the following things:

  • Is the board plugged in?
  • Are the power indicator lights on the board on?
  • In the Arduino IDE – go to the Tools > Board. Check that the UNO board is selected.
  • Then also in Tools>Port check that dev/cu.usbmodem641 (Arduino Uno) is selected from the list of ports. If it is not, you have a problem reading from the USB port of your computer. If this is the first time you are running Arduino you may need to install a driver. See the install guide here. 
  • Check you code compiles. Click the tick button to make sure you do not have errors in your program.

EXERCISES

We will do some basic circuits here. There are also excellent examples in the guides that come with your kits. See the online version here:

http://ardx.org/src/guide/2/ARDX-EG-ADAF-WEB.pdf 

EXERCISE 1: BLINK EXAMPLE (10min)

Set up an LED like follows:

Blinking-LED

 

Grab the code from the Arduino example files.

 

Screen Shot 2015-02-19 at 12.14.49 PM

>>>>>Important: PinMode declares PINS as input or output in setup. 

void setup() {                
  // initialize the digital pin as an output.
  pinMode(13, OUTPUT);     
}

digitalWrite sets pin HIGH or LOW (this means they have a charge or they don’t ie. they are on or off.)

  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)

CHALLENGES

  • 1.  We can use two other alternatives to HIGH and LOW.  What are they?
  • 2. Port the external LED to another pin and change the rate of flash.
  • 3. What’s the fastest you can flash an external LED and still see the flashing?  (try changing delay to a float variable to get super precise).
  • 4. What if we want the light to flash two times when the microcontroller starts up when we turn it on and then never again until restart?  What’s the easiest solution ?

Advice:

Blink is always the first program to run on a new Arduino or an Arduino that you are unsure of its health.  Built in LEDs can vary.  You can always blink the TX LED (usually PIN 1) if you are not sending data back to the host computer.

 

EXERCISE 2: Serial output for debugging.  (10min)

void setup() {
// initialize serial communication at 9600 bits per second:
 Serial.begin(9600);
}
 
void loop() {
Serial.print("I've been running for ");
Serial.print(millis()); 
Serial.println(" mS");
}

Important:

  • Declaring the baud rate. This is the rate at which your arduino sends data to the computer. The baud rate declared in your code must match the baud rate you select in your serial monitor.
  • More information is here: http://arduino.cc/en/Serial/begin
  • To output data to the serial port we use the functions print(); or println(). http://arduino.cc/en/Serial/Print
  • print prints data in one line, println includes carriage return!
Challenges:
  • 1.  Print the state of an LED
  • 2. Print runtime in millis, seconds, minutes, hours

EXERCISE 3: control external LED with PWM (pulse-duration modulation)

If we want to have an LED fade, we need to be able to send it the equivalent of an analog signal, so this means we want to vary its voltage gradually, not in the on/off mode of a digital signal.

Analog vs Digital 
hackpad.com_Q2FrBtpZBVA_p.251586_1418051699703_analog_digital

What is PWM (pulse width modulation)?

Pulse width modulation is the way that arduino imitates an analog signal. So if we want an LED to light up at 1/4 strength, we send it a digital signal that is flickering on and off more rapidly than we can see with our eyes. If the digital signal is on for 25% or the time and off for 75% of the time, then we perceive it as on at 25% strength. This simulation of an analogy signal is called Pulse Width Modulation. See in the graphs below and the tutorial for more info.

hackpad.com_Q2FrBtpZBVA_p.251586_1418051721541_pwm

PWM – FADE EXERCISE

Build this circuit on the Breadboard .

simplefade_pin9_schem

Argrhhh! Wondering what that weird diagram is? Well electrical components have special symbols. Here’s a starting guide:

 

Screen Shot 2015-02-19 at 12.40.21 PM

 

Still confused by that diagram? This is where Fritzing comes in handy. Most of the Arduino example circuits can be looked up in the Fritzing software. This makes it much easier to construct the circuits and gain an understanding of how circuits are represented. The circuit above is shown in Fritzing like this:

 

Screen Shot 2015-02-19 at 12.50.15 PM
Notes on resistors and LEDS:
This circuit is built with an resistor. Remember we need to design our circuit so that we use up all the current (otherwise we will eventually burn out our LED). This means we need to couple our LED with a resistor as shown above. (You dont have to do this on Pin 13 as it has a special on board resistor. But here we can’t use 13 as we need to use an PWM pin – one with a ~ sign).
You have 3 different resistors in your kits. For your LEDs you need to use either the the 560 Ohm Resistor (stripes of Green-Blue-Brown) or the 220 Ohm resistor (stripes of red, red, red, gold).

Notes on building circuits using the breadboard:

Notice that we are now using our bread board to construct the circuit. As Arduino does’t have any other space to assemble other electronic components directly, the breadboard is a great tool to do that. You can easily insert and take out components without soldering therefore it has holes in grid. The holes are connected by following a certain rule. See the figures below. The left one show schematic view of the breadboard. the red and blue lines show the connectivity between holes. 5V always goes to 5V pin on Arduino. GND does to GND pin.
 breadboard
THE CODE FOR AN LED FADE IS HERE:
Screen Shot 2015-02-19 at 12.51.38 PM
Can all pins be PWM pins?  No!!! – You need to see the  ~  next to a pin.

Challenges:

  • 1. Change fade speed
  • 2. Fade one LED on as another fades off
  • 3. Build the RGB LED circuit and fade between colors on RGBled. The circuit will look something like this:

Screen Shot 2015-02-19 at 6.37.32 PM

—-CODE —— (fixed up since class)

/*
/*
 Fade for a 3 color LED
 
 This example shows how to fade an LED on pin 9
 using the analogWrite() function.
 
 This example code is in the public domain.
 */
 
int r = 9;           // the pin that the R pin of the LED is attached to
int g = 10;    // the pin that the G pin of the LED is attached to
int b = 11;    // the pin that the B pin of the LED is attached to
int brightnessR = 0;    // how bright the R LED is
int brightnessG = 0;    // how bright the G LED is
int brightnessB = 0;    // how bright the B LED is
int fadeAmountR = 5;    // how many points to fade the LED by
int fadeAmountG = 7;    // how many points to fade the LED by
int fadeAmountB = 9;    // how many points to fade the LED by
 
// the setup routine runs once when you press reset:
void setup() {
  // declare pin 9, 10, 11 to be an output:
  pinMode(r, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(b, OUTPUT);
}
 
// the loop routine runs over and over again forever:
void loop() {
  // set the brightness of pin 9, 10, 11:
  analogWrite(r, brightnessR);
  analogWrite(g, brightnessG);
  analogWrite(b, brightnessB);
 
  // change the brightness for next time through the loop:
  brightnessR = brightnessR + fadeAmountR;
  brightnessG = brightnessG + fadeAmountG;
  brightnessB = brightnessB + fadeAmountB;
 
  // reverse the direction of the fading at the ends of the fade:
  if (brightnessR == 0 || brightnessR == 255) {
    fadeAmountR = -fadeAmountR ;
  }
    // reverse the direction of the fading at the ends of the fade:
  if (brightnessG == 0 || brightnessG == 255) {
    fadeAmountG = -fadeAmountG ;
  }
    // reverse the direction of the fading at the ends of the fade:
  if (brightnessB == 0 || brightnessB == 255) {
    fadeAmountB = -fadeAmountB ;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(30);
}
 
}