Basic Arduino Tutorial

Andrew Todd Marcus

Circuit #1A: LED Blink

Ryan Ferguson

// the setup function runs once when you press reset or power the board

void setup() {

  // initialize digital pin LED_BUILTIN as an output.

  pinMode(13, OUTPUT);

}


// the loop function runs over and over again forever

void loop() {

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

  delay(1000);                       // wait for a second

  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW

  delay(1000);                       // wait for a second

}

Circuit #1B: Fading an LED

Ryan Ferguson


// the setup routine runs once when you press reset:

void setup() {

}


// the loop routine runs over and over again forever:

void loop() {


  for(int i=0; i<255; i++){

    analogWrite(11, i);

    delay(10);

  }

  for(int i=0; i>0; i--){

    analogWrite(11, i);

    delay(10);

  }

}

Circuit #2: Potentiometer

Ryan Ferguson

// the setup routine runs once when you press reset:

void setup() {

  // initialize serial communication at 9600 bits per second:

  Serial.begin(9600);

}


// the loop routine runs over and over again forever:

void loop() {

  // read the input on analog pin 0:

  int sensorValue = analogRead(A0);

  // print out the value you read:

  Serial.println(sensorValue);

  delay(1);        // delay in between reads for stability

}

Circuit 3: LED & Potentiometer

Ryan Ferguson

int ledPin = 11;      // select the pin for the LED

int brightness = 0;


// the setup routine runs once when you press reset:

void setup() {

  // declare the ledPin as an OUTPUT:

  pinMode(ledPin, OUTPUT);

  // initialize serial communication at 9600 bits per second:

  Serial.begin(9600);

}


// the loop routine runs over and over again forever:

void loop() {

  // read the input on analog pin 0:

  int sensorValue = analogRead(A0);

  // print out the value you read:


  


  //remap sensorValue from 0-1024 to 0-255

  brightness = map(sensorValue, 0, 1024, 0, 255);

  // set the brightness of pin 9:

  analogWrite(ledPin, brightness);


  Serial.println(brightness);

  delay(1);        // delay in between reads for stability

}

Circuit #4A: Servo Position

Ryan Ferguson

#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(11);  // attaches the servo on pin 9 to the servo object

}


void loop() {


    myservo.write(45);              // tell servo to go to position 45 Degrees


  }

Circuit #4B: Servo Sweep

Ryan Ferguson

#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(11);  // attaches the servo on pin 9 to the servo object

}


void loop() {

  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees

    // in steps of 1 degree

    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

  }

}

Circuit #5: Potentiometer & Servo

Ryan Ferguson

#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(11);  // attaches the servo on pin 9 to the servo object

  // initialize serial communication at 9600 bits per second:


  Serial.begin(9600);

}


void loop() {


  // read the input on analog pin 0:

  int sensorValue = analogRead(A0);


  //remap sensorValue from 0-1024 to 0-255

  pos = map(sensorValue, 0, 1024, 0, 179);


  // tell servo to go to position in variable 'pos'

  myservo.write(pos);


  // print the value of variable "pos"

  Serial.println(pos);

}

PIR Motion Sensor

Liam Brady

PIR sensors (or passive infrared sensors) are motion sensors that measure subtle changes in infrared light in the room and register that change as movement. The processing is done on the chip and it outputs a current based on whether or not it detects motion.

int pin = 2;

void setup(){
  pinMode(pin, INPUT);

  pinMode(13, OUTPUT);
}

void loop(){
  int pirVal = digitalRead(pin);

  if (pirVal == 0) {
    digitalWrite(13, HIGH);
  } else {
    digitalWrite(13, LOW);
  }

  delay(100);
}