Arduino Uno Programming Instructions

From NYC Resistor Wiki
Jump to navigation Jump to search

These are scratch notes in preparation for June 2012 Arduino Uno 101 class

Grab the 1.0.1 release of the Arduino IDE (Interactive Development Environment <- take that you acronym happy programmers)

Mac OS:[edit]

  1. Download the arduino-1.01-macosx.zip file from the Arduino website at [1]
  2. No need to install separate drivers for Mac OSx (Arduino UNO uses 8u2 atmel chip for USB communication, instead of the ftdi chip)

Linux:[edit]

  1. You'll need to do a bit of pre-work to get the Arduino IDE working in your specific flavor of linux. If you're running linux then you should be more than capable of handling this yourself. Here's some guidelines on the Arduino playground site: [2]
  2. Download the arduino-1.0.1-linux.tgz or the arduino-1.0.1-linux64.tgz file from the Arduino website at [3]
  3. No Driver needed for Linux (Arduino UNO uses 8u2 atmel chip for USB communication, instead of the ftdi chip)

Windows PC:[edit]

  1. Download the arduino-1.0.1-windows.zip file from the Arduino website at [4]
  2. Unzip the file, preserving the folder structure.
  3. Plugin your board and wait for it to fail. Failure is good.
  4. Install the drivers for Uno, per the specific steps over on the Arduino website: [5]. Note that you should install the ArduinoUNO.inf driver, not any of the ftdi drivers.

For extra help on any of the platforms check the Arduino pages proper: http://arduino.cc/en/Guide/HomePage

It Lives[edit]

The UNO comes preloaded with a blink program. If your onboard LED marked "L" is blinking then your UNO lives. It lives! mwah-ha-ha-ha.

Load the blink program[edit]

The LED blink example will be in File > Examples > 1.Basics > Blink. Open it!
Confirm you are targeting the right board: Tools > Board > Arduino UNO
Select the correct serial port for your environment :
- MacOSx : it's not the Bluetooth ports. It's probably the usbmodem port.
- Linux : ports are numbered in the order they were connected /dev/ttyACM0, /dev/ttyACM1 etc
- Windows: try one and see if it works [any thing else?]
Upload the program! (Click the right arrow button) You'll see the little TX and RX onboard LEDs blink as the program transfers to the board.

The LED on the board (at pin 13) should start to blink.

Make the LED blink to your will[edit]

Change the program to make the led blink faster or slower. Save the program. Upload the program! Laugh with an evil maniacal laugh!

Hook up an External LED and make it Fade[edit]

Hook your UNO up to a bread board.
Plug in an LED. See images!

http://www.flickr.com/photos/88427305@N07/8082945932/

Also, it's the Fade sketch which is in Examples/Basics/Fade

Try out a digital input: Pushbutton is Pushy[edit]

Get to know and love your breadboard.
Put your push button on the breadboard.
Connect switch input (white on reference board) to pin 2
Connect ground (blue on reference board) to ground on the UNO
Connect ground to input using a pull-down resistor of about 10-22k (red-red-orange)
Connect switch power (red on reference board) to 5v power (second from last pin)

See images:
http://www.flickr.com/photos/88427305@N07/8082945788/

Compile and upload DigitalReadSerial from Examples >> Basics.
Open the serial monitor (little magnifying glass button)

You should see a beautiful stream of zeros
Push and hold the button: see a beautiful stream of ones!

Doesn't work? Turn that switch around.

Success!


Now, modify the program to use the digital in to turn your onboard led on and off.

Add a name for the 8 pin as a digital output:

  // digital pin 8 has the LED attached to it. Give it a name:
  int led = 8;

Add a new variable to keep track of the button state the last time it was read:

  int lastButtonState = 0;     // previous state of the button

In the setup method, initialize the LED pin as a digital output

    pinMode(led, OUTPUT);

In the read loop, right after reading the button input, make a decision based on it:

   // compare the buttonState to its previous state
   if (buttonState != lastButtonState) {
      // if the button is pushed set the output pin to high
      if (buttonState == 1) {
         digitalWrite(led, HIGH); 
      } else {
         digitalWrite(led, LOW); 
      }
      lastButtonState = buttonState;
   }

Save it, compile it and upload it to the UNO.

Open the serial monitor. When you push the button down the serial monitor goes to ones and the LED goes on.

Try out an analog output[edit]

Connect long leg of LED to digital output pin 8
Connect short leg of LED a current limiting resistor of about 10-22k (red-red-orange)
Connect resistor (red on reference board) to 5v power

Compile and upload Fade.
Watch the pretty light breathe.

Doesn't work? Flip the LED legs.

See something else: Swap for a very very very low resistor e.g. 10Ohms (brown-black-brown), See how bright it is!

Why does it work: Averaging! Not the LED, your eyes! POV. Prove it with Raf's oscilloscope.

Success!

Try out an analog input: Potentiometer[edit]

We like to call them pots.
right to 5v
left to ground.
Middle Pin (wiper) to Analog In Zero (A2)
Keep your LED setup

http://www.flickr.com/photos/88427305@N07/8083177651/

Compile and upload AnalogInOutSerial.
Open the serial monitor (little magnifying glass button)

Turn the little stick, See the numbers fly!

Let's display the voltage level instead of the PWM output!


Let's Move a servo![edit]

Connect the Brown Wire of the Servo to GND
Connect the Red Wire to 5V
Connect the Orange/Yellow Wire to pin 9

See Images:
http://www.flickr.com/photos/88427305@N07/8075766957/
http://www.flickr.com/photos/88427305@N07/8075771054/

Open Examples->Servo->sweep

Compile and upload! Your servo will move back and forth!

Not For Today: Let's play a song![edit]

Connect one of the buzzer or speaker to GND Connect the other end to Pin 8

Load the toneMelody sketch from Examples->Digital->toneMelody

Compile and Upload!

The song will play!

Try changing pitches to make a new song.


Not For Today: Try out an analog input: Sense some light[edit]

Hook a photoresistor up to GND and pin A0 - the Analog In Zero pin. Hook up a resistor to the A0 end of the light sensor and to 5V Hook up an LED and a current limiting resistor to pin 13 and ground.

Compile and upload AnalogInput from Examples >> Analog.

http://www.flickr.com/photos/26889555@N00/7427214164/

Save it, compile it and load it. As your hand moves closer, the LED flickers brighter.

Now lets modify the code so we can see the analog values we're reading in from photoresistor.

in setup() add

  Serial.begin(9600)

in loop() after we've read the analog pin add

  Serial.println(SensorValue)


Not for Today: Try Another Analog Input and Use a Community Library[edit]

Connect a resistor between pins 2 and 4 Put one end of a wire connected to pin2 Fold a small piece of foil in half and hang it on the other end of the wire. let the foil sit loosely on the pin. don't wrap it too tight.

Set up our LED circuit : Connect the long LED lead to Pin13 Connect a resistor to the short lead of the LED and to GND

Download CapSense04.zip from [6] Unzip the file and add the CapSense folder to your arduino libraries folder. On Windows this is In the folder where Arduino is installed Arduino->libraries On Mac it is Documents->Arduino->libraries (if this folder does not exist, create it) On linux, the libraries folder is in the folder where arduino is installed

In a moment we're going to restart your Arduino IDE First, if you want to save any of the changes we've made to files, you'll have to save them in a new location since the example files are read only. Make a folder next to the Arduino->libraries folder called sketches. Save any changed files to Arduino->sketches

Now Open the Arduino IDE again. Go to File->Examples. You should see the CapSense library in the list now. Open Examples->CapSense->CapSenseSketch

This example is set up to read 3 capacitive sensors, but we're only going to read 1. We'll comment out the other sensors. To comment out a line, add // in the front of it. Comment out the sensor read lines:

  // long total2 =  cs_4_6.capSense(30);
  // long total3 =  cs_4_8.capSense(30);

and also the print lines for these sensors and the delay:

   //Serial.print("\t");
   //Serial.print(total2);                  // print sensor output 2
   //Serial.print("\t");
   //Serial.println(total3);                // print sensor output 3
   //delay(10);                             // arbitrary delay to limit data to serial port 

now we still want one line per sensor read so change Serial.print(total1); to Serial.println(total1);

Compile and Upload! Launch the serial terminal! pinch the foil with your fingers. You'll see the read value jump up high!

Now lets turn on an led with our sensor. Add our LED code to the sketch! in setup() add the pin initialization:

pinMode(13, HIGH);

in loop(), after we print the sensed value, add code to turn on and off the LED:

  if (total1 > 1000){
     digitalWrite(13,HIGH);
   }
   else{
     digitalWrite(13, LOW);
   }

Compile and upload!

Now the led will turn on when you grasp the foil!



Success!