← flaviocopes.com

Introduction to the Arduino Programming Language

Published Jan 24 2020

Psssst! The 2023 WEB DEVELOPMENT BOOTCAMP is starting on FEBRUARY 01, 2023! SIGNUPS ARE NOW OPEN to this 10-weeks cohort course. Learn the fundamentals, HTML, CSS, JS, Tailwind, React, Next.js and much more! ✨

How can you write programs for your Arduino board?

Arduino, natively, supports a language that we call the Arduino Programming Language, or Arduino Language.

This language is based upon the Wiring development platform, which in turn is based upon Processing, which if you are not familiar with, is what p5.js is based upon. It’s a long history of projects building upon other projects, in a very Open Source way. The Arduino IDE is based upon the Processing IDE, and the Wiring IDE which builds on top of it.

When we work with Arduino we commonly use the Arduino IDE (Integrated Development Environment), a software available for all the major desktop platforms (macOS, Linux, Windows), which gives us 2 things: a programming editor with integrated libraries support, and a way to easily compile and load our Arduino programs to a board connected to the computer.

The Arduino Programming Language is basically a framework built on top of C++. You can argue that it’s not a real programming language in the traditional term, but I think this helps avoiding confusion for beginners.

A program written in the Arduino Programming Language is called sketch. A sketch is normally saved with the .ino extension (from Arduino).

The main difference from “normal” C or C++ is that you wrap all your code into 2 main functions. You can have more than 2, of course, but any Arduino program must provide at least those 2.

One is called setup(), the other is called loop(). The first is called once, when the program starts, the second is repeatedly called while your program is running.

We don’t have a main() function like you are used to in C/C++ as the entry point for a program. Once you compile your sketch, the IDE will make sure the end result is a correct C++ program and will basically add the missing glue by preprocessing it.

Everything else is normal C++ code, and as C++ is a superset of C, any valid C is also valid Arduino code.

One difference that might cause you troubles is that while you can spawn your program over multiple files, those files must all be in the same folder. Might be a deal breaking limitation if your program will grow very large, but at that point it will be easy to move to a native C++ setup, which is possible.

Part of the Arduino Programming Language is the built-in libraries that allow you to easily integrate with the functionality provided by the Arduino board.

Your first Arduino program will surely involve making a led turn on the light, and then turn off. To do so, you will use the pinMode(), delay() and digitalWrite() functions, along with some constants like HIGH, LOW, OUTPUT.

Like this, the canonical first Arduino project (the “Hello, World!”):

#define LED_PIN 13

void setup() {
    // Configure pin 13 to be a digital output
    pinMode(LED_PIN, OUTPUT);
}

void loop() {
    // Turn on the LED
    digitalWrite(LED_PIN, HIGH);
    // Wait 1 second (1000 milliseconds)
    delay(1000);
    // Turn off the LED
    digitalWrite(LED_PIN, LOW);
    // Wait 1 second
    delay(1000);
}

This is all part of the Arduino Programming Language, or we’d better call it suite or library.

Support for other language

As a reminder, I want to note that you are not limited to using this language and IDE to program an Arduino. Projects exist, among others, to let you run Node.js code on it using the Johnny Five project, Python code using pyserial and Go code with Gobot, but the Arduino Programming Language is definitely the one you’ll see most tutorials based upon, since it’s the native and canonical way to work with these devices.

The Arduino Programming Language Built-in constants

Arduino sets two constants we can use to

HIGH equates to a high level of voltage, which can differ depending on the hardware (>2V on 3.3V boards like Arduino Nano, >3V on 5V boards like Arduino Uno) LOW equates to a low level of voltage. Again, the exact value depends on the board used

Then we have 3 constants we can use in combination with the pinMode() function:

The other constant we have is LED_BUILTIN, which points to the number of the on-board pin, which usually equates to the number 13.

In addition to this, we have the C/C++ constants true and false.

Arduino Math Constants

The Arduino Programming Language Built-in Functions

In this section I am going to make a reference for the built-in functions provided by the Arduino Programming Language.

Program lifecycle

Handling I/O

The following functions help with handling input and output from your Arduino device.

Digital I/O

Analog I/O

Time functions

Math functions

Note: there are more built-in mathematical functions if you need them, documented here.

Working with alphanumeric characters

Random numbers generation

In Arduino, like in most languages, it’s impossible to get really random numbers, and the sequence is always the same, so you seed it with the current time or (in the case of Arduino) you can read the input from an analog port.

Working with bits and bytes

Interrupts



I write books for developers 👇🏼

© 2023 Flavio Copes Flavio Copes made in Italy 🇮🇹 using Notion to Site Notion to Site