Getting Started with Arduino

Written by Husam Wadi

Introductory Terms

Here are some basic hardware items that we will go over.

Some Hardware Vocab:

Jumping into the Basics

  • You will need to install the Arduino IDE.

The Blink Example

https://www.arduino.cc/en/tutorial/blink

Simplest thing possible- Blink an LED

Supplies:

  • Resistor
  • Arduino
  • Led
  • Breadboard

Setup

  1. To build the circuit, attach a 220-ohm resistor to pin 8.
    Then attach the long leg of an LED (the positive leg, called the anode) to the resistor.

  2. Attach the short leg (the negative leg, called the cathode) to ground.
    Then plug your Arduino board into your computer, start the Arduino program, and enter the code below.

  3. In the program below, the first thing you do is to initialize pin 13 as an output pin with the line

    pinMode(13, OUTPUT);
    
  4. In the main loop, you turn the LED on with the line:

    digitalWrite(13, HIGH);
    
  5. This supplies 5 volts to pin 13. That creates a voltage difference across the pins of the LED, and lights it up. Then you turn it off with the line:

    digitalWrite(13, LOW);
    

Code Finalized

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

  This example code is in the public domain.
 */

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
 digitalWrite(led, HIGH);   // turn the LED on (HIGH is the  voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the    voltage LOW
  delay(1000);               // wait for a second
}

Servo Motion

https://www.arduino.cc/en/Tutorial/Sweep
Sweeps the shaft of a RC servo motor back and forth across 180 degrees.
This example makes use of the Arduino servo library.

Hardware Required:

Arduino Board
(1) Servo Motor hook-up wire
Servo Layout

Background
  1. Servo motors have three wires: power, ground, and signal:
    The power wire is typically red, and should be connected to the 5V pin on the Arduino board.

  2. The ground wire is typically black or brown and should be connected to a ground pin on the Arduino board.

  3. The signal pin is typically yellow, orange or white and should be connected to pin 9 on the Arduino board.

Servo Code
 /* Sweep
 by BARRAGAN <http://barraganstudio.com>
 This example code is in the public domain.

 modified 8 Nov 2013
 by Scott Fitzgerald
 http://www.arduino.cc/en/Tutorial/Sweep
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the   servo object
}

void loop() {   
// goes from 0  degrees to 180 degrees

  // in steps of 1 degree
  for (pos = 0; pos <= 180; pos += 1) {          
    myservo.write(pos);     // tell servo to go to  position in variable 'pos'
    delay(15);               // waits 15ms for the servo to reach the position
  }

  for (pos = 180; pos >= 0; pos -= 1) {       // goes from 180 degrees to 0 degrees
    myservo.write(pos);        // tell servo to go to position in variable 'pos'
    delay(15);                // waits 15ms for the servo to reach the position
    }
}