Thursday, October 24, 2013

Introduction to Arduino (Workshop 1) - Detailed Guide

**If you have not purchased a kit and/or breadboard and would like to do so, please email me and we can arrange for you to do so. This workshop guide assumes you have purchased an Arduino kit or have an Arduino Uno.**

Workshop/Guide Contents:

I. Arduino Kit Contents
II. Introduction to the Arduino Uno
III. Installing the Arduino Software
IV. The Arduino IDE
V. Demo Part 1 - Hello World!
VI. Demo Part 2 - On-Board LED Blink
VII. Demo Part 3 - External LED Blink

I. Arduino Kit Contents

To start off, there are a number of different components included in your kit. They include:




1: Arduino Uno
The Uno is a microcontroller used to make almost any small embedded design you can think of. The Arduino is programmed via your computer and a USB cable and runs an infinite loop while power is provided to the board. We will discuss the board in further detail in the next section.


2: USB Cable
An A to B USB cable that connects your computer to your Arduino.

3: Mini Push Button Switch
A simple four pin button that is easy to connect to your Uno and used as a simple switch.

4: BJT Transistor
A small analog device that can be utilized both as an amplifier for small signals and as an analog switch.

5: Resistors (2 10K Ohm and 2 330 Ohm)
Resistors are analog components that restrict the current and/or voltage in your projects.

6: Diode
A diode is a simple two terminal device that allows current to pass through one direction but not the other direction.

7: LEDs (Light Emitting Diodes) (2 Red and 2 Green)
LEDs are just like normal diodes except they release light if current is run through them in the right direction. There is a longer lead (the anode) and a shorter lead (the cathode). The anode is connected to a voltage source and the cathode is connected to ground. LEDs almost always need a resistor between the source and the anode.

8: Servo
A small, simple analog stepper motor that can be moved anywhere inside 360 degrees but that cannot completely rotate. Servos are good levers and switches for having 2-3 positions for something on a rotational axis.

9: DC Motor
A DC motor takes a high and low signal and spins at a rate related the the voltage. DC motors are very common.

10: Temperature sensor (says TMP on it)
A temperature sensor reads the temperature around it and sends back analog data from which real temperatures can be calculated from.


II. Introduction to the Arduino Uno

The Arduino Uno is a very powerful of embedded prototyping technology. At a basic level, the Arduino allows you to program a combination of code and analog circuits together into a unified project and then run that project anywhere you want that has a power source. The important details of the Arduino include:



1: USB Connection - for your computer.
2: Wall Wart Power Connection - to power the board away from your computer.
3: Digital Pins - Configurable for either output or input if discrete digital values.
4: Analog Pins - same as the digital pins but for analog (voltage) values.
5: Power Pins - Vin (power the board through this), Ground (ground the whole board when powering or ground single components), and 5V and 3.3V (power your components).
6: Reset Button - to completely reset the program.
7: ON LED Indicator

Be careful about what you do with your Arduino - these boards are pretty robust and tolerable but can still be damaged.

III. Installing the Arduino Software

To install the Arduino Software, download your appropriate version here: http://arduino.cc/en/main/software

Once it's installed, open it up and configure it to your specific situation Go into the tools menu at the top:
        1) Board - select the type of Arduino you're using.
        2) Serial Port - select the port your Arduino is plugged into on your computer. If you don't know what port to select go to device manager on your computer and find your Arduino and the port it's connected to.



If you're having issues with one or both of these steps, you may need to install the drivers. This problem usually only occurs on Windows machines:
        1) Go to Device Manager and find your Arduino
        2) Go into Properties
        3) Go to the Hardware Tab
        4) Go to the Properties
        5) Go to the Driver Tab
        6) Select Update Driver
        7) Manually find the drivers folder. On my Windows 7 computer the path is something like C:/Program Files (x86)/Arduino 1.0.5/drivers
        8) Make sure that the subfolders option is selected and install the driver. This should automatically run and yield a message saying that the Arduino Uno driver has been installed.

IV. The Arduino IDE

The Arduino IDE is where you will program your Arduino. It is relatively simple to use. Key areas to pay attention to include:



         1) Main Program Body - where you write your program.
         2) Status/Error Bar - watch to check your verification and uploading statuses and any errors that might occur.
         3) Verify Button - use this to compile your code. This essentially takes you code, analyzes it and checks for any syntax or logic errors, and then compacts it into a form ready to be sent to your Arduino.
         4) Upload Button - verifies your code and then uploads it and programs it on your Arduino. Once you take this step that particular code will remain on the Arduino until you upload something else.
         5) Serial Monitor - a helpful place to print and enter text for your program.

V. Demo Part 1 - Hello World!

Now, we're going to program three simple demos for the Arduino. The first will use the serial monitor exclusively. Copy the following code into a new file in the Arduino IDE at take a look at it.

void setup() {                
  Serial.begin(9600);  // initialize serial port
}

void loop() {
  Serial.println("Hello World!");  //print to the serial port
  delay(1000);              // maintain current conditions for 1 second
  
  Serial.println("");      // print a blank line
  delay(1000);              // maintain current conditions for 1 second
}

What do you think it does?

(We won't be getting too deeply into the code in this workshop, so follow along and if you don't understand something that's okay.)

The first thing to look at is the two parts to the code. There is the setup part and the loop part. The setup part runs once when the Arduino is powered on and never again unless the program is reset. In this demo our setup method sets the communication speed of the serial port.

In the loop method, there are four lines that do one of two things. There are two types of line - Serial.println statements and delay statements.

The Serial.println lines print whatever is in the parenthesis to the serial monitor. That's why the line starts with 'Serial.' Println means that it prints the content and then makes a new line. If we wanted to print the text only, using 'print' there instead of 'println' would work for this.

The delay(1000); commands delays the program for a certain amount of time - in this case 1000 milliseconds, or one second. When the program reaches this line it simply sits there for a second and then moves on to the next line.

These four lines are in the loop method. The loop runs infinitely while the board is on after the setup routine is run once at the very beginning.

Now what do you think the program does? Click the Verify button and once that's done click the Upload button. Then select the Serial Monitor. Make sure the baud rate at the bottom right corner of the pop-up window is 9600, the value you established in the program.

As you can see, the program prints out text, waits a second, prints a blank line, waits a second, then starts over again.

VI. Demo Part 2 - On-Board LED Blink

Open a new file and paste the follow code in. What does it do?

void setup() {                
  pinMode(13, OUTPUT);  // establish pin 13 as an outpin pin
}

void loop() {
  digitalWrite(13, HIGH);   // turn on the on-board LED
  delay(1000);              // maintain current conditions for 1 second
  
  digitalWrite(13, LOW);    // turn the on-board LED off
  delay(1000);              // maintain current conditions for 1 second
}

Like the last program, the setup and loop parts are there. The setup method now sets pin 13 on the Arduino Uno to be an output pin. The loop method sets pin 13 to HIGH, waits a second, sets pin 13 to LOW, waits a second, and then repeats. Verify and Upload the code and see what happens.

See that little light on your board that's blinking? That's an LED on pin 13. You can still plug things into pin 13, but as a default that LED is there as a helful on-board tool to use. We turn on the LED, wait a second, turn it off, wait a second, and repeat. It's very similar to the first program, but with that LED.

VII. Demo Part 3 - External LED Blink

**This part of the demo requires a breadboard.**

Copy the code below into a new file. It's pretty similar to the last demo with only three new lines. Now get out your breadboard, an LED, and a 330 ohm resistor (the one without a black band in the middle). Connect one end of the resistor to the digital pin on the Arduino, connect the other end into the breadboard, connect the anode (longer) lead of the LED to the same row as the resistor and the cathode (shorter) end into another another row and from that row connect a jumper wire to a ground terminal on your Uno. Make sure it looks like this:




void setup() {                
  pinMode(12, OUTPUT);  // establish pin 12 as an output pin
  pinMode(13, OUTPUT);  // establish pin 13 as an outpin pin
}

void loop() {
  digitalWrite(13, HIGH);   // turn on the on-board LED
  digitalWrite(12,LOW);  // turn off the analog LED
  delay(1000);              // maintain current conditions for 1 second
  
  
  digitalWrite(13, LOW);    // turn the on-board LED off
  digitalWrite(12,HIGH);  //turn the analog LED on
  delay(1000);              // maintain current conditions for 1 second
}

Verify and upload the code and see what it does! Now you have two LEDs that are alternating on and off.

For any of these demos, take some time to alter and play with the code. The best way to learn about programming is to experiment with it and see what you can do to alter it.

That's it for Workshop 1! If you have any questions or issues email me at cbloom8 (at) gmail.com.

No comments:

Post a Comment