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.

void setup() {
	pinMode(LED_BUILTIN, OUTPUT);       // initialize pin as output
}
 
void loop() {
	digitalWrite(LED_BUILTIN, HIGH);    // switch LED on (HIGH voltage)
	delay(1000);                        // wait 1000 ms, i.e. one second
	digitalWrite(LED_BUILTIN, LOW);     // switch LED off (LOW voltage)
	delay(1000);                        // wait a second
}

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.

#define DELAY 1000                    // define length of delay
bool ledOn = false;                   // status of LED
unsigned long lastTime = 0;           // last status switch
 
void setup() {
	pinMode(LED_BUILTIN, OUTPUT);
}
 
void loop() {
	unsigned long now = millis();	// set now to the current time
	if (now - DELAY > lastTime) {  // has enough time passed since the last call?
		ledOn = !ledOn;            // switch the state of the led 
		digitalWrite(LED_BUILTIN, ledOn); 
		lastTime = now;            // set last time we switched to current time
	}
}

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:

void setup() {
	Serial.begin(115200);			// Open the serial port at 115200 bps
	Serial.println("Hello World!");	// Print the message with a newline
}
void loop() {
}

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.

void setup() {
  Serial.begin (115200);                    // Start serial communication
  pinMode(LED_BUILTIN, OUTPUT);             // Set the LED pin to output mode
}
void loop() {
  if (Serial.available() > 0) {             // Check if input has been submitted on the serial monitor
    char c = (char)Serial.read();           // Read the first byte of input as a character
    switch (c) {
      case '1':
      case 'i':
      case 'I':                             // For the input '1', 'i', or 'I'
        digitalWrite(LED_BUILTIN, HIGH);    // Turn off the LED
        Serial.println("In");               // Print "In" to the serial monitor
        break;
      case '0':
      case 'o':
      case 'O':                             // For the input '0', 'o', or 'O'
        digitalWrite(LED_BUILTIN, LOW);     // Turn on the LED
        Serial.println("Out");              // Print "Out" to the serial monitor
        break;
    }
    while (Serial.available() > 0) {        // Read the rest of the input
      Serial.read();                        
    }
  delay (250);                              // Wait 250ms
  }
}

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 a Serial object. The methods are called directly on the class itself as we assume there is only one serial port on the Arduino.