Introduction to Arduino

From XinCheJian
Jump to navigation Jump to search

Overview

Requirements

Overview Run Time: 2-3 Hours

  • Overview of the Arduino wiring - Analog vs Digital, GND, 3.3V, 5V, VIN.
  • Breadboards
  • LEDS
  • Servos
  • Button
  • Buzzer
  • Photosensor
  • Temperature Sensor
  • Potentiometer

Arduino

Arduino-example-arduino.png

Pins to pay attention too:

  • 5v
  • 3v3
  • gnd
  • vin
  • pwm (pulse width modulation)
  • digital
  • analog


Breadboards

Arduino-example-breadboard.png


LEDS

WARNING: Never attach a LED without a Resistor! *boom* - trust me on this one :)

Color Resistor Value
Red (1.7v) 330 Ohm
Yellow (2.1v) 300 Ohm
Blue (3.2v) 91 Ohm
Green (2.2v) 270 Ohm


Arduino-example-blink.png

What you'll need:

  • Breadboard
  • RED LED
  • 330 Ω Resistor

Open the following example: Arduino > examples > Basics > Blink


void setup() {                
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);     
}

void loop() {
  digitalWrite(13, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
  delay(1000);              // wait for a second
}


Now lets try PWM (Pulse Width Modulation) to fade up and down..

Arduino-example-fade.png

Open the following Example: Arduino > Examples > Basics > Fade


int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

void setup()  { 
  // declare pin 9 to be an output:
  pinMode(9, OUTPUT);
} 

void loop()  { 
  // set the brightness of pin 9:
  analogWrite(9, brightness);    

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade: 
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ; 
  }     
  // wait for 30 milliseconds to see the dimming effect    
  delay(30);                            
}

Servos

Arduino-example-servo.png

You'll need the following items:

  • Servo

Open the following example: Arduino > Example > Servo > Sweep


#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
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() 
{ 
  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>=1; 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 
  } 
} 

Mention power/current output limits:-

           - of 3.3 & 5v power Pins
           - of digital pins output
           - digital pins - input sink/source



Button

Arduino-example-button.png


You'll need the following items:

* Breadboard
* LED attached from pin 13 to ground 
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground


open the following example in Arduino: Examples > Digital > Button

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }
}


Buzzer

Arduino-example-piezo.png

What you'll need:

  • 100 ohm resistor (NOTE: Seems like the new version from DF Robot works better WITHOUT RESISTOR)
  • Piezo speaker


  • NOTE: open the following: Arduino > Examples > Digital > ToneMelody
    • It is better to get the code from the Examples. Do not just copy the code, otherwise it will be missing the library.
  • NOTE: If it is too quiet, remove the resistor and remove the cover.

 #include "pitches.h"

// notes in the melody:
int melody[] = {
  NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = { 4, 8, 8, 4,4,4,4,4 };

void setup() {
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 8; thisNote++) {

    // to calculate the note duration, take one second 
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }
}

void loop() {
  // no need to repeat the melody.
}


Photosensor

Arduino-example-LDR.png

What you'll need:

  • 10K ohm Resistor 1K Resistor
  • LDR/Photosensor Photoresistor (?)
  • Breadboard
  • KEY NOTE: "Ambient Light Sensor" seems to work better backwards. The long leg goes towards GND, and the short leg towards 5V.


For this Example we'll need to make our own, here's one I made earlier:


int ldr = A0;             //analog pin to which LDR is connected
int ldr_value = 0;        //variable to store LDR values, we'll set this to zero first.

void setup()
{
  Serial.begin(9600);   //Start the serial monitor
  pinMode(ldr, INPUT);  //Tell arduino that this pin is for input
}

void loop()
{
  ldr_value = analogRead(ldr);   //reads the LDR values
  Serial.println(ldr_value);     //prints the LDR values to serial monitor
  delay(50);                     //lets not make your computer go crazy
}

Open the Serial Monitor and move your hand over the top. Even better, use your phone flashlight and shine it on the photosensor.
With the latest DF Robot "Ambient Light Sensor" you should receive a range of values roughly 0 - 1024. The darker it is, the higher the number. The brighter it is the lower the number.

Temperature Sensor

This should get your Arduino to give you the temperature.

What you'll need:

  • Just the Arduino
  • Temperature Sensor


For this example we'll use the Temperature Sensor that's included in the Arduino Starter Kit:



float tempC;
int reading;
int tempPin = A0;

void setup()
{
Serial.begin(9600);
analogReference(INTERNAL);
}

void loop()
{
reading = analogRead(tempPin);
tempC = reading / 9.31;
Serial.println (tempC);
}



Potentiometer

Arduino-example-potentiometer.png

You'll need the following:

  • breadboard
  • potentiometer


Open the following example: Arduino > Examples > Analog > smoothing



// Define the number of samples to keep track of.  The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input.  Using a constant rather than a normal variable lets
// use this value to determine the size of the readings array.
const int numReadings = 10;

int readings[numReadings];      // the readings from the analog input
int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average

int inputPin = A0;

void setup()
{
  // initialize serial communication with computer:
  Serial.begin(9600);                   
  // initialize all the readings to 0: 
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;          
}

void loop() {
  // subtract the last reading:
  total= total - readings[index];         
  // read from the sensor:  
  readings[index] = analogRead(inputPin); 
  // add the reading to the total:
  total= total + readings[index];       
  // advance to the next position in the array:  
  index = index + 1;                    

  // if we're at the end of the array...
  if (index >= numReadings)              
    // ...wrap around to the beginning: 
    index = 0;                           

  // calculate the average:
  average = total / numReadings;         
  // send it to the computer (as ASCII digits) 
  Serial.println(average, DEC);               
}





Next Steps

Now that you've completed the Introduction to Arduino...

See also