Skip to content

1.2 Arduino IDE

John-Paul Chouery edited this page Aug 26, 2024 · 2 revisions

What is Arduino IDE?

Arduino IDE is a software tool used to write and upload code to Arduino boards, like the ones we use in our projects. It's beginner-friendly and helps you easily program and control the microcontrollers on our PCBs. The code written in Arduino is similar to C/C++, making it accessible yet powerful for embedded programming.

Setup and Loop Structure

When programming in Arduino IDE, the code is structured into two main parts: the setup() function and the loop() function.

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);

  // print to the serial monitor:
  Serial.println("Setup is starting...");

  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);

  // print to the serial monitor:
  Serial.println("LED pin has been set to OUTPUT.");
}

// the loop function runs over and over again forever
void loop() {
  Serial.println("LED is ON.");
  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(1000);                      // wait for a second

  Serial.println("LED is OFF.");
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
  delay(1000);                      // wait for a second
}

This is a simple example code that will be explained in detail later. But for now, note that:

  • Setup: The setup() function runs once when you press the reset button or power the board. It's used to initialize variables, pin modes, start using libraries, etc.
  • Loop: The loop() function runs continuously, allowing your program to change and respond. It's where the main logic of your program resides, repeating indefinitely as long as the board is powered.

Comments in Code

Comments are portions of the code that are not executed by the program. They are used to explain what certain parts of the code do, which makes it easier for others (and yourself) to understand the code later on. There are two types of comments in Arduino:

  • Single-line comments: Start with // and continue to the end of the line.
// This is a single-line comment
  • Multi-line comments: Start with /* and end with */.
/* This is a 
   multi-line 
   comment */

Compiling and Uploading Code

Once you write your code, it needs to be compiled (converted into machine code) and uploaded to the microcontroller. The Arduino IDE provides simple buttons for these tasks:

Compile Button (check mark symbol in top left of ide):

Click this button to compile your code and check for any errors.

Upload Button (right arrow symbol in top left of ide):

This button will compile your code and then upload it to the connected microcontroller.

Selecting Microcontroller (dropdown menu in top left of ide):

Make sure to select the correct microcontroller that you are using from the Tools menu before you compile and upload your code. For the purposes of this tutorial, you will have to select Arduino Uno.

Data Types in Arduino

In Arduino programming, understanding data types is crucial as they define the kind of data you can store in a variable. Here are the most commonly used data types for beginners:

1. int (Integer)

  • Description: The int data type is used to store whole numbers (both positive and negative) without decimal points.
  • Size: 2 bytes (16 bits).
  • Range: -32,768 to 32,767.
  • Example:
    int ledPin = 13;  // Variable to store the pin number for an LED
    int sensorValue = 0;  // Variable to store sensor readings

2. String (Text)

  • Description: The String data type is used to store sequences of characters or text.
  • Size: Varies depending on the length of the string.
  • Example:
    String welcomeMessage = "Hello, World!";  // Variable to store a welcome message
    String userName = "John";  // Variable to store a user's name

3. boolean (True/False)

  • Description: The boolean data type is used to store a value that can be either true or false.
  • Size: 1 byte (but only uses 1 bit).
  • Example:
    boolean isLEDOn = true;  // Variable to store the state of an LED (on/off)
    boolean isButtonPressed = false;  // Variable to check if a button is pressed

4. float (Floating Point)

  • Description: The float data type is used to store numbers with decimal points. This is useful for calculations that require precision, like sensor readings or mathematical operations.
  • Size: 4 bytes (32 bits).
  • Range: ±3.4028235E+38 (6-7 decimal digits of precision).
  • Example:
    float temperature = 24.7;  // Variable to store temperature readings
    float pi = 3.14159;  // Variable to store the value of Pi

Why Understanding Data Types is Important

Choosing the right data type for your variables is important because it affects memory usage and the accuracy of your program. For example, using an int for a value that will never exceed 255 might waste memory, while using a float for an exact count (like loop iterations) might lead to precision errors.

Print Statements

Print statements, like Serial.print() and Serial.println(), are used to send information from the Arduino to your computer. This can be very useful for debugging or simply understanding what your program is doing. We will explain how print statements work in the next page of the tutorial.

Clone this wiki locally