What are Arduino sketches?

The code that runs on Arduino is usually called “Sketch”. Getting started with Arduino programming is quite easy because the structure of the Sketches is clear. The Arduino programming language is derived from C++ programming. You only need a few lines of code for your first simple projects.

Arduino Program Structure

The structure of an Arduino program is divided into two parts: setup() and loop().

Arduino Sketch Structure

The setup function is executed only once when the Arduino board is started or after a reset. In this function basic settings like variable declarations, the configuration of ports, or initialization routines. Additionally, the inputs and outputs are set, as shown in the Blink example below, the led pin 13 is set as output.

The setup function is mandatory and must always be present, even if no declarations have to be made. In this case, the function remains empty.

The loop function is the second area of the basic structure of an Arduino program and has the task of the main program. After the setup function has been executed once, the loop function is run through – as its name implies, as an endless loop. In the loop, all further instructions and function calls are accommodated, which are needed in the normal operation for the desired application. As in the blinking example, it turns on the LED, waits one second, turns it off, waits one second, and repeats forever.

Arduino Examples

The easiest way to get started is to work through example programs and then modify and extend them for your own applications. Also, you can find a lot of Arduino sketches and even complete projects for your application on the internet. One Arduino code example is called AnalogInOutSerial shown above.

AnalogInOutSerial Arduino example

It sets the pulse width modulation (PWM) of an output pin (9) based on the analog input readings on pin (A0), after mapping from the ADC scale (0 to 1023) to the output PWM scale (0 to 255). It also prints out the results on the Serial Monitor. The circuit diagram for this code is shown below