12B – Arduino 1

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.
  • We are using the uno board.

COMPONENTS AND PINS:

Arduino allows us to control componenets like LED lights, motors, buzzers and other ‘actuators’ with voltage changes. It also allows us to get information from the world through ‘sensors’ – for light, sound, motion, etc. The sensor detects a change and this is translated into a change in voltage. The pins on the Arduino can be set to either listen to voltage changes from sensor circuits (INPUTS), or they can be set to send out voltage changes to actuators to control things like lights and motion (OUTPUTS).

Both sensors and actuator can be digital (off or on) or analog (giving or receiving a range of values). Different pins on the Arduino can be programmed to be either digital or analog input or output pins – see the diagrams below.

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 an alternative to HIGH and LOW. What is it?
  • 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)

Arduino doesn’t have a screen so it can be really hard to see what is going on on your board. We can get Arduino to communicate with the computer via serial communication. This is really helpful for debugging.
To use serial communication, in void setup declare:

Serial.begin(9600);

And then you can use the serial functions to send data to the computer:

Serial.println
Serial.print
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");
}

To see the data on your computer, run the code above and open the serial port on your computer. You have to make sure the baud rate matches the one you set with Serial.begin(9600). In this case it is 9600.

serialport

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

Notes on building circuits using the breadboard:

We may want to use 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

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.
http://arduino.cc/en/Tutorial/PWM

hackpad.com_Q2FrBtpZBVA_p.251586_1418051721541_pwm

PWM – FADE EXERCISE

Build this circuit on the Breadboard .

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).

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

Arduino functions we have now used.

In summary the Arduino functions we have covered in this tutorial are:

'+
1
'+
2 - 3
4 - 5
6 - 7
8 - 9
10 - 11
12 - 13
13 - 14
[x]