Programming Arduino

A computer program is a coded series of instructions that tells the computer what to do. The programs that run on Arduino are called sketches.

The sketches used in this book mostly tell Arduino to read data from one of the pins, such as the one connected to a sensor; and to write information to a different pin, such as the pin connected to an LED or display unit.

Sometimes the sketches also instruct Arduino to process that information in a certain way: to combine data streams, or compare the input with some reference, or even place the data into a readable format.

An Arduino program has two parts: setup() and loop().

The setup() part tells Arduino what it needs to know in order to do what we want it to do. For example, setup() tells Arduino which pins it needs to configure as input, which pins to configure as output, and (by default) which won’t be doing anything. If we’re going to use a special type of output to show our results, such as a four-character display, setup() is where we tell Arduino how that output works. If we need to communicate with the outside world through a serial port or an Ethernet connection, all the instructions necessary to make that connection go here.
loop() tells Arduino what to do with the input or output. Unlike some other computers, it never stops; once the instructions in a loop have been executed, Arduino goes right back to the top of the loop() and starts executing instructions all over again.

Get Environmental Monitoring with Arduino now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.