




























































































Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!





























































































Buy | Download | Getting Started | Learning | Reference | Hardware | FAQ Blog » | Forum » | Playground »
Learning Examples | Foundations | Hacking | Links
See the foundations page for in-depth description of core concepts of the Arduino hardware and software; the hacking page for information on extending and modifying the Arduino hardware and software; and the links page for other documentation.
Simple programs that demonstrate the use of the Arduino board. These are included with the Arduino environment; to open them, click the Open button on the toolbar and look in the examples folder. (If you're looking for an older example, check the Arduino 0007 tutorials page.)
Digital I/O
Blink: turn an LED on and off. Blink Without Delay: blinking an LED without using the delay() function. Button: use a pushbutton to control an LED. Debounce: read a pushbutton, filtering noise. Loop: controlling multiple LEDs with a loop and an array.
Analog I/O
Analog Input: use a potentiometer to control the blinking of an LED. Fading: uses an analog output (PWM pin) to fade an LED. Knock: detect knocks with a piezo element. Smoothing: smooth multiple readings of an analog input.
Communication
These examples include code that allows the Arduino to talk to Processing sketches running on the computer. For more information or to download Processing, see processing.org.
ASCII Table: demonstrates Arduino's advanced serial output functions. Dimmer: move the mouse to change the brightness of an LED. Graph: sending data to the computer and graphing it in Processing. Physical Pixel: turning on and off an LED by sending data from Processing. Virtual Color Mixer: sending multiple variables from Arduino to the computer and reading them in Processing.
EEPROM Library
These are more complex examples for using particular electronic components or accomplishing specific tasks. The code is included on the page.
Miscellaneous
TwoSwitchesOnePin: Read two switches with one I/O pin Read a Tilt Sensor Controlling an LED circle with a joystick 3 LED color mixer with 3 potentiometers
Stopwatch
Complex Sensors
Read an ADXL3xx accelerometer Read an Accelerometer Read an Ultrasonic Range Finder (ultrasound sensor) Reading the qprox qt401 linear touch sensor
Sound
Play Melodies with a Piezo Speaker Play Tones from the Serial Connection MIDI Output (from ITP physcomp labs) and from Spooky Arduino
Interfacing w/ Hardware
Multiply the Amount of Outputs with an LED Driver Interfacing an LCD display with 8 bits LCD interface library Driving a DC Motor with an L293 (from ITP physcomp labs). Driving a Unipolar Stepper Motor Build your own DMX Master device Implement a software serial connection RS-232 computer interface Interface with a serial EEPROM using SPI Control a digital potentiometer using SPI Multiple digital outs with a 595 Shift Register X10 output control devices over AC powerlines using X
EEPROM Clear: clear the bytes in the EEPROM. EEPROM Read: read the EEPROM and send its values to the computer. EEPROM Write: stores values from an analog input to the EEPROM.
Stepper Library
Motor Knob: control a stepper motor with a potentiometer.
Edit Page | Page History | Printable View | All Recent Site Changes
Buy | Download | Getting Started | Learning | Reference | Hardware | FAQ Blog » | Forum » | Playground »
Learning Examples | Foundations | Hacking | Links
Arduino examples, tutorials, and documentation elsewhere on the web.
Making Things Talk (by Tom Igoe): teaches you how to get your creations to communicate with one another by forming networks of smart devices that carry on conversations with you and your environment.
Arduino Booklet (pdf): an illustrated guide to the philosophy and practice of Arduino.
Tutorials created by the Arduino community. Hosted on the publicly-editable playground wiki.
Board Setup and Configuration: Information about the components and usage of Arduino hardware.
Interfacing With Hardware: Code, circuits, and instructions for using various electronic components with an Arduino board.
Output Input Interaction Storage Communication
Interfacing with Software: how to get an Arduino board talking to software running on the computer (e.g. Processing, PD, Flash, Max/MSP).
Code Library and Tutorials: Arduino functions for performing specific tasks and other programming tutorials.
Electronics Techniques: tutorials on soldering and other electronics resources.
Learn electronics using Arduino: an introduction to programming, input / output, communication, etc. using Arduino. By ladyada.
Lesson 0: Pre-flight check...Is your Arduino and computer ready? Lesson 1: The "Hello World!" of electronics, a simple blinking light Lesson 2: Sketches, variables, procedures and hacking code Lesson 3: Breadboards, resistors and LEDs, schematics, and basic RGB color-mixing Lesson 4: The serial library and binary data - getting chatty with Arduino and crunching numbers Lesson 5: Buttons & switches, digital inputs, pull-up and pull-down resistors, if/if-else statements, debouncing and your first contract product design.
Tom Igoe's Physical Computing Site: lots of information on electronics, microcontrollers, sensors, actuators, books, etc.
Example labs from ITP
Spooky Arduino: Longer presentation-format documents introducing Arduino from a Halloween hacking class taught by TodBot:
class 1 (getting started) class 2 (input and sensors) class 3 (communication, servos, and pwm) class 4 (piezo sound & sensors, arduino+processing, stand-alone operation)
Bionic Arduino: another Arduino class from TodBot, this one focusing on physical sensing and making motion.
Wiring electronics reference: circuit diagrams for connecting a variety of basic electronic components.
Schematics to circuits: from Wiring, a guide to transforming circuit diagrams into physical circuits.
Examples from Tom Igoe
Examples from Jeff Gray
Edit Page | Page History | Printable View | All Recent Site Changes
Interfacing w/ Hardware
Multiply the Amount of Outputs with an LED Driver Interfacing an LCD display with 8 bits LCD interface library Driving a DC Motor with an L293 (from ITP physcomp labs). Driving a Unipolar Stepper Motor Implement a software serial connection RS-232 computer interface Interface with a serial EEPROM using SPI Control a digital potentiometer using SPI Multiple digital outs with a 595 Shift Register Multiple digital inputs with a CD4021 Shift Register
Example labs from ITP Examples from Tom Igoe Examples from Jeff Gray
Edit Page | Page History | Printable View | All Recent Site Changes
Buy | Download | Getting Started | Learning | Reference | Hardware | FAQ Blog » | Forum » | Playground »
Learning Examples | Foundations | Hacking | Links
Examples > Digital I/O
In most programming languages, the first program you write prints "hello world" to the screen. Since an Arduino board doesn't have a screen, we blink an LED instead.
The boards are designed to make it easy to blink an LED using digital pin 13. Some (like the Diecimila and LilyPad) have the LED built-in to the board. On most others (like the Mini and BT), there is a 1 KB resistor on the pin, allowing you to connect an LED directly. (To connect an LED to another digital pin, you should use an external resistor.)
LEDs have polarity, which means they will only light up if you orient the legs properly. The long leg is typically positive, and should connect to pin 13. The short leg connects to GND; the bulb of the LED will also typically have a flat edge on this side. If the LED doesn't light up, trying reversing the legs (you won't hurt the LED if you plug it in backwards for a short period of time).
The example code is very simple, credits are to be found in the comments.
/* Blinking LED
Buy | Download | Getting Started | Learning | Reference | Hardware | FAQ Blog » | Forum » | Playground »
Learning Examples | Foundations | Hacking | Links
Examples > Digital I/O
Sometimes you need to blink an LED (or some other time sensitive function) at the same time as something else (like watching for a button press). That means you can't use delay(), or you'd stop everything else the program while the LED blinked. Here's some code that demonstrates how to blink the LED without using delay(). It keeps track of the last time it turned the LED on or off. Then, each time through loop() it checks if a sufficient interval has passed - if it has, it turns the LED off if it was on and vice-versa.
int ledPin = 13; // LED connected to digital pin 13 int value = LOW; // previous value of the LED long previousMillis = 0; // will store last time LED was updated long interval = 1000; // interval at which to blink (milliseconds)
void setup() { pinMode(ledPin, OUTPUT); // sets the digital pin as output }
void loop() { // here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, is the difference // between the current time and last time we blinked the LED bigger than // the interval at which we want to blink the LED. if (millis() - previousMillis > interval) { previousMillis = millis(); // remember the last time we blinked the LED
// if the LED is off turn it on and vice-versa. if (value == LOW) value = HIGH; else value = LOW;
digitalWrite(ledPin, value); } }
Edit Page | Page History | Printable View | All Recent Site Changes
Buy | Download | Getting Started | Learning | Reference | Hardware | FAQ Blog » | Forum » | Playground »
Learning Examples | Foundations | Hacking | Links
Examples > Digital I/O
The pushbutton is a component that connects two points in a circuit when you press it. The example turns on an LED when you press the button.
We connect three wires to the Arduino board. The first goes from one leg of the pushbutton through a pull-up resistor (here 2.2 KOhms) to the 5 volt supply. The second goes from the corresponding leg of the pushbutton to ground. The third connects to a digital i/o pin (here pin 7) which reads the button's state.
When the pushbutton is open (unpressed) there is no connection between the two legs of the pushbutton, so the pin is connected to 5 volts (through the pull-up resistor) and we read a HIGH. When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to ground, so that we read a LOW. (The pin is still connected to 5 volts, but the resistor in-between them means that the pin is "closer" to ground.)
You can also wire this circuit the opposite way, with a pull-down resistor keeping the input LOW, and going HIGH when the button is pressed. If so, the behavior of the sketch will be reversed, with the LED normally on and turning off when you press the button.
If you disconnect the digital i/o pin from everything, the LED may blink erratically. This is because the input is "floating" - that is, it will more-or-less randomly return either HIGH or LOW. That's why you need a pull-up or pull-down resister in the circuit.
int ledPin = 13; // choose the pin for the LED int inPin = 2; // choose the input pin (for a pushbutton) int val = 0; // variable for reading the pin status
void setup() { pinMode(ledPin, OUTPUT); // declare LED as output
Buy | Download | Getting Started | Learning | Reference | Hardware | FAQ Blog » | Forum » | Playground »
Learning Examples | Foundations | Hacking | Links
Examples > Digital I/O
This example demonstrates the use of a pushbutton as a switch: each time you press the button, the LED (or whatever) is turned on (if it's off) or off (if on). It also debounces the input, without which pressing the button once would appear to the code as multiple presses. Makes use of the millis() function to keep track of the time when the button is pressed.
A push-button on pin 7 and an LED on pin 13.
int inPin = 7; // the number of the input pin int outPin = 13; // the number of the output pin
int state = HIGH; // the current state of the output pin int reading; // the current reading from the input pin int previous = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds, // will quickly become a bigger number than can be stored in an int. long time = 0; // the last time the output pin was toggled long debounce = 200; // the debounce time, increase if the output flickers
void setup() { pinMode(inPin, INPUT); pinMode(outPin, OUTPUT); }
void loop()
reading = digitalRead(inPin);
// if we just pressed the button (i.e. the input went from LOW to HIGH), // and we've waited long enough since the last press to ignore any noise... if (reading == HIGH && previous == LOW && millis() - time > debounce) { // ... invert the output if (state == HIGH) state = LOW; else state = HIGH;
// ... and remember when the last button press was time = millis(); }
digitalWrite(outPin, state);
previous = reading; }
Edit Page | Page History | Printable View | All Recent Site Changes
pinMode(pins[i], OUTPUT); // set each pin as an output }
void loop() { int i;
for (i = 0; i < num_pins; i++) { // loop through each pin... digitalWrite(pins[i], HIGH); // turning it on, delay(timer); // pausing, digitalWrite(pins[i], LOW); // and turning it off. } for (i = num_pins - 1; i >= 0; i--) { digitalWrite(pins[i], HIGH); delay(timer); digitalWrite(pins[i], LOW); } }
Edit Page | Page History | Printable View | All Recent Site Changes
Buy | Download | Getting Started | Learning | Reference | Hardware | FAQ Blog » | Forum » | Playground »
Learning Examples | Foundations | Hacking | Links
Examples > Analog I/O
A potentiometer is a simple knob that provides a variable resistance, which we can read into the Arduino board as an analog value. In this example, that value controls the rate at which an LED blinks.
We connect three wires to the Arduino board. The first goes to ground from one of the outer pins of the potentiometer. The second goes from 5 volts to the other outer pin of the potentiometer. The third goes from analog input 2 to the middle pin of the potentiometer.
By turning the shaft of the potentiometer, we change the amount of resistence on either side of the wiper which is connected to the center pin of the potentiometer. This changes the relative "closeness" of that pin to 5 volts and ground, giving us a different analog input. When the shaft is turned all the way in one direction, there are 0 volts going to the pin, and we read
int potPin = 2; // select the input pin for the potentiometer int ledPin = 13; // select the pin for the LED
Buy | Download | Getting Started | Learning | Reference | Hardware | FAQ Blog » | Forum » | Playground »
Learning Examples | Foundations | Hacking | Links
Examples > Analog I/O
Demonstrates the use of analog output (PWM) to fade an LED.
An LED connected to digital pin 9.
int value = 0; // variable to keep the actual value int ledpin = 9; // light connected to digital pin 9
void setup() { // nothing for setup }
void loop() { for(value = 0 ; value <= 255; value+=5) // fade in (from min to max) { analogWrite(ledpin, value); // sets the value (range from 0 to 255) delay(30); // waits for 30 milli seconds to see the dimming effect } for(value = 255; value >=0; value-=5) // fade out (from max to min) { analogWrite(ledpin, value); delay(30); } }
Edit Page | Page History | Printable View | All Recent Site Changes
Buy | Download | Getting Started | Learning | Reference | Hardware | FAQ Blog » | Forum » | Playground »
Learning Examples | Foundations | Hacking | Links
Examples > Analog I/O
Here we use a Piezo element to detect sound, what will allow us to use it as a knock sensor. We are taking advantage of the processors capability to read analog signals through its ADC - analog to digital converter. These converters read a voltage value and transform it into a value encoded digitally. In the case of the Arduino boards, we transform the voltage into a value in the range 0..1024. 0 represents 0volts, while 1024 represents 5volts at the input of one of the six analog pins.
A Piezo is nothing but an electronic device that can both be used to play tones and to detect tones. In our example we are plugging the Piezo on the analog input pin number 0, that supports the functionality of reading a value between 0 and 5volts, and not just a plain HIGH or LOW.
The other thing to remember is that Piezos have polarity, commercial devices are usually having a red and a black wires indicating how to plug it to the board. We connect the black one to ground and the red one to the input. We also have to connect a resistor in the range of the Megaohms in parallel to the Piezo element; in the example we have plugged it directly in the female connectors. Sometimes it is possible to acquire Piezo elements without a plastic housing, then they will just look like a metallic disc and are easier to use as input sensors.
The code example will capture the knock and if it is stronger than a certain threshold, it will send the string "Knock!" back to the computer over the serial port. In order to see this text you can use the Arduino serial monitor.
Example of connection of a Piezo to analog pin 0 with a resistor
/* Knock Sensor