4 - Arduino Sketch and Libraries
4 - Arduino Sketch and Libraries
Nauman H. Ansari
Mohammad Ali Jinnah University
Arduino Programming
• Arduino uses its own programming language, which is similar to C++.
However, it's possible to use Arduino with Python or another high-
level programming language.
• Arduino is easier to learn as a programming language as it is a
simplified version of the C++ programming language. Arduino is cross-
platform which makes it easy to run on any sort of device compared
to other microcontrollers which can only run on windows.
• The Arduino IDE does not yet support Python. Instead, we can use
OpenMV, a platform that supports programming Arduino boards with
MicroPython.
Arduino IDE
• Arduino programs are written in the
Arduino Integrated Development
Environment (IDE). Arduino IDE is a special
software running on your system that
allows you to write sketches (synonym for
program in Arduino language) for different
Arduino boards.
• After the sketch is written in the Arduino
IDE, it should be uploaded on the Arduino
board for execution.
• A sketch is the name that Arduino uses for
a program. It's the unit of code that is
uploaded to and run on an Arduino board.
The Sketch
• The structure of Arduino program is pretty simple. Arduino programs have a
minimum of 2 blocks:
Preparation & Execution
setup ( ) is the preparation block and loop ( ) is an execution block.
• Each block has a set of statements enclosed in curly braces.
• The setup function is the first to execute when the program is executed, and
this function is called only once. The setup function is used to initialize the pin
modes and start serial communication. This function has to be included even if
there are no statements to execute.
• After the setup ( ) function is executed, the execution block runs next. The
execution block or loop ( ) function hosts statements like reading inputs,
triggering outputs, checking conditions etc.
• // Single line comment
• / * Multi - line comment */
/*The text that is written after the two forward slashes are considered as a
single line comment. The compiler ignores the code written after the two
forward slashes.*/
Arduino Sketch
Exercise
Let’s do some experiments with Arduino:
Arduino Libraries
Arduino Libraries
• Libraries are files written in C or C++ which provide your sketches with
extra functionality (e.g. the ability to control an LED matrix, or read an
encoder, etc.).
• The Arduino environment can be extended through the use of
libraries, just like most programming platforms.
• Libraries provide extra functionality for use in sketches, e.g. working
with hardware or manipulating data.
• To use a library in a sketch, select it from Sketch > Import Library.
• A number of libraries come installed with the IDE, but you can also
download or create your own.
https://www.arduino.cc/reference/en/libraries/
Installing
Libraries
To install a new library into your
Arduino IDE you can use the
Library Manager (available from
IDE version 1.6.2). Open the IDE
and click to the "Sketch" menu
and then Include Library >
Manage Libraries.
https://docs.arduino.cc/software/ide-v1/tutorials/installing-libraries
10