Arduino Programming
Arduino sketches are written in the C language with extensions from C++. We will only describe the most common and immediately useful features of C for two reasons. First, we only need to use a handful of the basic features of the language. Second, learning C as an end goal takes lots of time and practice. Recognizing these two aspects, we can fairly quickly learn enough about a subset of C to write sketches to perform fun and useful tasks.
Jump to detail pages on …
- Code structures:
if
, loops, functions - Microcontroller functions: analog input, digital I/O, PWM
- On-board features of CPX: NeoPixels, sensors, buttons
- Connect to External Devices with I2C
Contents of this page
Essential Components
As you build your C/C++ programming skill, you will need to learn about variable types, operations and assignments. These concepts are introduced on the Programming Fundamentals page, where you will also find links to additional information.
Our immediate goal is to introduce these ideas so that you can recognize that terms like “variable types”, “operations” and “assignments”, have precise meanings that can affect how your program runs. As you learn, you will revisit these concepts and gain understanding how they are both simple and at times subtle.
What’s a sketch?
Variables, operations and assignments are essential building blocks of computer programs in any language. To program an Arduino, we also have to supply another key ingredient called a sketch.
A sketch is the C/C++ statements that you provide to give the Arduino program its unique set of tasks. The Arduino IDE combines your sketch with other components (think boilerplate) to create the instructions that are uploaded to the Arduino board.
setup
and loop
All Arduino sketches must have a setup
function and a loop
function. The setup
function is executed once when the Arduino board is first powered-up or when the reset button is pressed. The loop
function is repeated indefinitely after the first and only execution of the setup
function.
Programming Constructs
There are a few basic programming constructs that help us write more compact, efficient programs that are easier to write and maintain.
Loops
A programming loop is a block of code that repeats. The two common types of loops are for
loops and while
loops. Typically, a for
loop is used when the number of repetitions is known in advance and a while
loop is used when an unknown number of repetitions continue until a condition is met.
Read more about for
and while
loops on this page.
if
constructs
Programs that respond to changing conditions need to have ways of making decisions. When one set of conditions is true, a predefined block of code is executed. If alternative conditions are present, a different block of code is executed. The general concept of responding to changes in data is called branching. This logical decision-making is implemented with if
constructs and is described on this page.
functions
In programming, functions are modules of code. In a complex code that performs several distinct tasks, it is usually a good idea to write a function for each major task (or set of tasks). Functions can have inputs that control how they work. Functions can also return values.
Programming languages have a set of built-in functions for performing common tasks. Examples are mathematical functions like cos(x)
and sin(x)
and functions that print values to an external device.
Read more about functions on this page.
Arduino board functions
The microprocessor at the the heart of an Arduino boards can perform functions that allow it to interact with the environment, including
- Sending messages to the Serial Monitor.
- Analog input
- Digital input and output
- PWM
Circuit Playground functions
The Adafruit Circuit Playground Express has built-in components that are controlled via additional functions supplied in a library by Adafruit. Those components include
- NeoPixel programmable LEDs
- Three-axis accelerometer
- Temperature sensor
- Light sensor
- Microphone
- Speaker
- Infrared transmitter and receiver
- Two momentary (clickable) buttons
- Slide switch