Loading...

Learning   Examples | Foundations | Hacking | Links

Examples

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.

Note: these examples are written for Arduino 1.0 and later. Certain functions may not work in earlier versions. For best results, download the latest version.

Here's a style guide that helps with writing examples for beginners.

Core Functions

Simple programs that demonstrate basic Arduino commands. These are included with the Arduino environment; to open them, click the Open button on the toolbar and look in the examples folder.

1.Basics

  • BareMinimum: The bare minimum of code needed to start an Arduino sketch.
  • Blink: Turn an LED on and off.
  • DigitalReadSerial: Read a switch, print the state out to the Arduino Serial Monitor.
  • AnalogReadSerial: Read a potentiometer, print it's state out to the Arduino Serial Monitor.
  • Fade: Demonstrates the use of analog output to fade an LED.

2.Digital

  • 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.
  • Button State Change: counting the number of button pushes.
  • Tone: play a melody with a Piezo speaker.
  • Pitch follower: play a pitch on a piezo speaker depending on an analog input.
  • Simple Keyboard: a three-key musical keyboard using force sensors and a piezo speaker.
  • Tone4: play tones on multiple speakers sequentially using the tone() command.

3.Analog

  • AnalogInOutSerial: read an analog input pin, map the result, and then use that data to dim or brighten an LED.
  • Analog Input: use a potentiometer to control the blinking of an LED.
  • AnalogWriteMega: fade 12 LEDs on and off, one by one, using an Arduino Mega board.
  • Calibration: define a maximum and minimum for expected analog sensor values.
  • Fading: use an analog output (PWM pin) to fade an LED.
  • Smoothing: smooth multiple readings of an analog input.

4.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. There are also Max/MSP patches that can communicate with each Arduino sketch as well. For more on Max/MSP see Cycling 74. For Pd patches that can communicate with these sketches, see Scott Fitzgerald's examples.

  • ASCII Table: demonstrates Arduino's advanced serial output functions.
  • Dimmer: move the mouse to change the brightness of an LED.
  • Graph: send data to the computer and graph it in Processing.
  • Physical Pixel: turn a LED on and off by sending data to your Arduino from Processing or Max/MSP.
  • Virtual Color Mixer: send multiple variables from Arduino to your computer and read them in Processing or Max/MSP.
  • Serial Call Response: send multiple vairables using a call-and-response (handshaking) method.
  • Serial Call Response ASCII: send multiple variables using a call-and-response (handshaking) method, and ASCII-encode the values before sending.
  • SerialEvent: Demonstrates the use of SerialEvent().
  • Serial input (Switch (case) Statement): how to take different actions based on characters received by the serial port.
  • MIDI: send MIDI note messages serially.
  • MultiSerialMega: use two of the serial ports available on the Arduino Mega.

5.Control Structures

  • If Statement (Conditional): how to use an if statement to change output conditions based on changing input conditions.
  • For Loop: controlling multiple LEDs with a for loop and.
  • Array: a variation on the For Loop example that demonstrates how to use an array.
  • While Loop: how to use a while loop to calibrate a sensor while a button is being read.
  • Switch Case: how to choose between a discrete number of values. Equivalent to multiple If statements. This example shows how to divide a sensor's range into a set of four bands and to take four different actions depending on which band the result is in.
  • Switch Case 2: a second switch-case example, showing how to take different actions based in characters received in the serial port.

6.Sensors

  • ADXL3xx: read an ADXL3xx accelerometer.
  • Knock: detect knocks with a piezo element.
  • Memsic2125 : two-axis acceleromoter.
  • Ping: detecting objects with an ultrasonic range finder.

7.Display

Examples of basic display control

8.Strings

Libraries

Examples from the libraries that are included in the Arduino software.

EEPROM Library

  • 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.

Ethernet Library

Firmata Libraries

LiquidCrystal Library

  • Hello World: displays "hello world!" and the seconds since reset.
  • Blink: control of the block-style cursor.
  • Cursor: control of the underscore-style cursor.
  • Display: quickly blank the display without losing what's on it.
  • TextDirection: control which way text flows from the cursor.
  • Scroll: scroll text left and right.
  • Serial input: accepts serial input, displays it.
  • SetCursor: set the cursor position.
  • Autoscroll: shift text right and left.

SPI Library

Servo Library

  • Knob: control the shaft of a servo motor by turning a potentiometer.
  • Sweep: sweeps the shaft of a servo motor back and forth.

Software Serial Library

Stepper Library

  • Motor Knob: control a highly accurate stepper motor using a potentiometer.

Wire Library

Arduino as ISP Programmer

ArduinoISP turns your Arduino into an in-circuit programmer to re-program Atmega chips. Useful when you need to re-load the bootloader on an Arduino, if you're going from Arduino to an Atmega on a breadboard, or if you're making your own Arduino-compatible circuit on a breadboard.

More

For a huge list of examples from the Arduino community, see the interfacing with hardware page on the playground wiki. Also see the list of old examples.