introduction to arduino - meetupfiles.meetup.com/19087205/arduino_class.pdf•connect the leads to...

16
Introduction To Arduino

Upload: others

Post on 12-Jun-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Introduction To Arduino

What is Arduino?

• Hardware• Boards / microcontrollers• Shields

• Software• Arduino IDE• Simplified C

• Community• Tutorials• Forums• Sample projects

Arduino Uno

• Power: 5v (7-12v input)• Digital Pins: 14 (6 support PWM)• Analog Pins: 6• Memory: 32 kb• Size: 69 x 53 mm

Open Source

Arduino IDE

• Develop• Compile• Deploy• Serial Monitor• Support for additional boards• Library support

Interfacing with the Outside World

• General Purpose Input Output (GPIO) Pins• Read (Input) or Write (Output)

• Digital

• Analog

• Pulse Width Modulation (PWM)

• I2C

• SPI

Anatomy of an Arduino Sketch

• References and Variable Declaration

• Setup (required)

• Loop (required)

• Functions

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

void setup() {

// initialize digital pin 13 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}

Basic Syntax Rules

• Setup and Loop functions are required

• All lines of code are terminated with a semicolon except:• #define and #include• After { or }

• Use { and } to group code into a singular code block

• Comments are ignored by the complier but are extremely helpful to the programmer• // for a single line comment• /* … */ for multi-line comments

• Indentation and carriage returns are ignored by the complier but are extremely helpful to the programmer

Language Reference

Blink

• Define a constant for the GPIO pin the onboard LED is attached to

• In setup, define the digital pin with “pinMode” and set it as “OUTPUT” (as opposed to “INPUT”)

• In loop, write to the digital pin with “digitalWrite” and alternate “HIGH” and “LOW” signals with a one thousand millisecond (or 1 second) “delay” in between

#define ledPin 13

void setup() {

pinMode(ledPin, OUTPUT);

}

void loop() {

digitalWrite(ledPin, HIGH);

delay(1000);

digitalWrite(ledPin, LOW);

delay(1000);}

Breadboarding

Wiring the Project

• Polarity – check the device or datasheet

• Pin order – check the device or datasheet

• Logic Voltage – check datasheet

• Pin Voltage – check datasheet

PIR Motion Sensor

• 5v required to power the sensor

• Output pin works at 3.3v

• Output value is HIGH when motion is detected

• Output value is LOW when no motion

• Single trigger means the “same motion” doesn’t continue to trigger HIGH

• Repeat trigger outputs HIGH as long as motion is detected

• Working voltage range 4.5 – 20v• High output level 3.3v / low 0v• 110 degree angle sensor• 7m maximum sensing distance• Operating temp -15 to 70 degrees

Security System Sketch

• Define constants for the motion sensor and the buzzer pins

• Define the pins and their direction

• Check the motion sensor for movement

• Turn on the buzzer accordingly

• Wait a second before looping through again

#define detector 12#define buzzer 8

void setup() {pinMode(detector, INPUT);pinMode(buzzer, OUTPUT);

}

void loop() {// check to see if motion is detectedif (digitalRead(detector) == HIGH)

digitalWrite(buzzer, HIGH);else

digitalWrite(buzzer, LOW);

delay(1000);}

Powering the Project

• Rechargeable 5v portable power packs• Most commonly used for cellphones• Have a USB connector• Usually provide more than enough ( > 1A) current

• 9v battery• Connect the leads to the Arduino’s Vin and GND pins or

get a barrel plug• As long as you don’t draw more than 500 milliamps

• AC/DC adapter – aka wall wart• Between 9 – 12 volts• Atleast 500 milliamps• 2.1 mm plug• Center pin positive• http://playground.arduino.cc/Learning/WhatAdapter

Using tone()#define detector 12#define buzzer 8

// notes in the melodyint melody[] = {262, 196, 196, 220, 196, 0, 247, 262};

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

void setup() {pinMode(detector, INPUT);pinMode(buzzer, OUTPUT);

}

void loop() {// check to see if motion is detectedif (digitalRead(detector) == HIGH) {playTune();

}

delay(250);}

void playTune() {// 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(buzzer, 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(buzzer);

}}