The Arduino is programmed in a dialect of C++. Each Arduino program should consist of at least the functions setup()
and loop()
.
The setup()
function is executed when you power up the board, after uploading a new program to the board and after pressing the reset button.
Next, the loop()
function is executed over and over agan as long as the microprocessor is running your program. When there is nothing to be done during setup or in the loop the corresponding function has an empty body.
In the following sections, you will find tutorials to help get you started with the Arduino environment.
Hello World!
The “Hello World!” example for the Arduino blinks the onboard LED connected to digital pin 13
on the Arduino Uno.
This LED is the one pixel monochrome display of these boards.
Instead of writing the text Hello world!
to the display, we let this LED blink as the simplest demonstration program. The code for this can be found in the example below.
Explanation
To make the program portable to other boards it is preferred to use the symbolic name LED_BUILTIN
instead of the pin number of the board used, as the actual pin number is not the same on every board.
The pins of the Arduino are GPIO (General Purpose Input/Output) pins, meaning they can be used as both input and output. It is required to set the pin to the correct I/O mode before using it in that mode.
The setup()
function sets pin LED_BUILTIN
, connected to the onboard LED, in output mode with the function pinMode()
.
In the loop
function we make this output high and low voltage, to turn the LED on and off. After each write operation we wait 1000
milliseconds with the delay()
function.
On many boards the built-in LED is connected between a pin and positive power, Vcc
, rather than between the pin and the ground. This implies that you have to make the output LOW
on these boards to switch on the LED and HIGH
to switch it off.
Hello World! Without Delays
Since there is no operating system, there are no other threads or programs on the Arduino.
This implies that the delay(1000)
call blocks program execution for one second.
For more advanced programs it is better to prevent blocking by long delays.
By introducing some state variables and looking repeatedly at the clock
with millis()
, returning the milliseconds since startup,
the use of delay()
can easily be circumvented. This way of programming makes it much easier to execute multiple tasks
on the Arduino each at its own speed.
Explanation
In this version of “Hello World!”, instead of using the delay()
function like the previous example, we use a non-blocking timing method with the millis()
function. This function returns the time in milliseconds since the board started running. This method of delaying allows us to continue running the rest of the program while we wait for the LED state to switch.
In the loop()
function, we continuously check the current time (now
) using millis()
. If the difference between the current time and lastTime
is greater than the defined delay (DELAY
), we toggle the LED light.
Instead of using HIGH
and LOW
to define the state of the LED, we use a boolean value.
This is possible, due to HIGH
and a true boolean both signifying an integer with value 1
, and LOW
and a false boolean both signifying an integer with value 0
.
Serial Communication
The Arduino contains a serial port that can easily be accessed from the Arduino IDE. With this, you can send debug messages to the Serial Monitor or plot variables on the Serial Plotter.
In the Arduino IDE you can find the Serial Monitor/Plotter in the top right corner of the window or in the Tools
menu.
Serial communication on the Arduino is handled using the Serial
class, which manages the connection between your Arduino and your computer over the USB cable. This same cable that powers the board also carries the serial data.
Baud Rate
In serial communication, baud rate refers to the speed of data transmission, measured in bits per second (bps). Both the Arduino and the device it’s communicating with must be set to the same baud rate to be able to communicate.
In the following example, we initialize the serial port at a baud rate of 115200 bps, which is the communication rate of the Wemos D1 Mini:
Reading input
The serial port can also be used for reading input. This can be used to control the Arduino from your computer. The next example shows how we can read input from the serial port and use this to control the built-in LED.
Static Nature of Serial Methods
It’s important to note that all methods of the
Serial
class are static, meaning there is no need to instantiate aSerial
object. The methods are called directly on the class itself as we assume there is only one serial port on the Arduino.