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 detected if (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 melody int 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 detected if (digitalread(detector) == HIGH) { playtune(); 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); delay(250);