#include

Description

#include is used to include outside libraries in your sketch. This gives the programmer access to a large group of standard C libraries (groups of pre-made functions), and also libraries written especially for Arduino.

The main reference page for AVR C libraries (AVR is a reference to the Atmel chips on which the Arduino is based) is here.

Note that #include, similar to #define, has no semicolon terminator, and the compiler will yield cryptic error messages if you add one.

Syntax

#include <LibraryFile.h>

#include "LocalFile.h"

Parameter Values

  • LibraryFile.h: When the angle brackets syntax is used, the Arduino IDE seaches for the LibraryFile.h in the libraries paths[1].
  • LocalFile.h: When the double quotes syntax is used, the Arduino IDE searches for the LocalFile.h in the sketch's folder[2]. If the file is not found, the Arduino IDE will searches for the file in libraries paths.

Where:

  • [1]: libraries paths includes
    • The path that contains the built-in libraries of Arduino IDE. It is located at where Arduino IDE is installed.
    • The path that contains the libraries installed via Library Manager of Arduino IDE. In Windows, it usually located at C:\Users\YOUR_ACCOUNT\Documents\Arduino\libraries.
  • [2]: sketch's folder[2] is the folder that your sketch (.ino file) is located.

Example Code

This example includes the Servo library so that its functions may be used to control a Servo motor.

#include <Servo.h> Servo myservo; // create servo object to control a servo void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object } void loop() { for (int 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 (int pos = 180; pos >= 0; 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 } }

This example includes the pitches.h file, which is placed in the same directory with the sketch (the sketch's folder).

/* * This example code is in the public domain * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-piezo-buzzer */ #include "pitches.h" int melody[] = { NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4 }; int noteDurations[] = { 4, 8, 8, 4, 4, 4, 4, 4 }; void setup() { for (int thisNote = 0; thisNote < 8; thisNote++) { int noteDuration = 1000 / noteDurations[thisNote]; tone(8, melody[thisNote], noteDuration); int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); noTone(8); } } void loop() { }

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

※ OUR MESSAGES