Programming In Arduino — The Basics

Nandan Ghawate
4 min readMar 7, 2023

--

The Arduino platform is a very popular choice for both — hobbyists and professionals alike. Arduino IDE provides an interface to program various 8-bit, 16-bit, and 32-bit microcontrollers (from Microchip, Espressif, STM etc) using C and C++. It is a great tool to start with programming and creating interactive electronic and IOT devices. In this article, we will explore the basics of using C language in Arduino, including how to write and upload code, as well as some important concepts.

C Language in the Arduino Environment :

The first step will be downloading and installing the Arduino IDE from the official website. The Arduino IDE is equipped with a code editor, a compiler, and a serial monitor for communicating with any selected Arduino board such as Atmega328 based “Arduino Uno” board (or any selected Arduino-core compatible board such as ESP32).

The Arduino IDE automatically generates a simple program template when you start or create a new “sketch” (Sketch refers to a new code / program). This template includes the setup and loop functions, which are the two main functions in an Arduino program.

The setup function is executes only once when the Arduino board is powered on or reset. This function is typically used to initialise variables, set pin modes, and configure any necessary hardware. The loop function executes continuously (just like an infinite loop or endless loop). This function is used to write the main logic of your project.

Basic Syntax in C Language :

In an overview of C language Syntax, here are some key points to keep in mind:

  • The main() function is where the Program starts.
  • All statements end with a semicolon ;
  • { Curly brackets are used to create loops or set of statements }
  • Variables must be declared before using.
  • Comments can be added to code using //the double slash or /*slash-asterisk*/ notation.

Variables and Data Types :

Variables can be declared using data types such as int, long, byte, float, etc. Boolean data type can also be used to store 1 or 0 or true or false values.

The syntax for declaring a variable :

data_type variable_name;

For example : to declare an integer variable called “my_int”, you can use the following code:

int my_int;

You can also initialise a variable after declaration :

int my_int = 10;

Functions :

Functions are very basic building blocks of a C code/program that can be executed repeatedly by calling them from the declared part of the program. Functions are used to perform a specific task by grouping a set of statements together. Data can also be passed for processing or can be returned (fetched) in to variables by calling the functions in a certain way.

Here is an example of a simple function that adds two numbers :

int add(int a, int b) {
int c = a + b;
return c;
}

Control Structures :

C language includes several control structures that allow you to modify the flow of your program based on specific conditions. These control structures include:

  • If/else statements: allows you to perform conditional testing to decide which operation should be executed.
  • For loops: allows you to execute an operation or a set of operations repeatedly until a certain condition is met.
  • While loops: allows you to execute an operation or a set of operations as long as a certain condition is true.

Here is an example of an Arduino C program that makes an Led blink with a variable delay which increases from 1 second to 20 seconds, and then resets to 1 , running in an infinite loop :

// Pin 13 has an led connected on most Arduino boards.
int led = 13;
int myDelayValue = 1000; // initial delay time is 1 second

void setup() {
pinMode(led, OUTPUT); // set the led pin as an output
}

void loop() {
digitalWrite(led, HIGH); // turn the led on
delay(myDelayValue); // wait for delayTime milliseconds
digitalWrite(led, LOW); // turn the led off
delay(myDelayValue); // wait for delayTime milliseconds

myDelayValue += 1000; // increase delay time by 1 second

if (myDelayValue > 20000) { // if delay time is greater than 20 seconds
delay(1000); // wait for 1 second
delayTime = 1000;
}
}

Uploading Code to an Arduino Board :

  1. Connect the board to your computer using a USB cable.
  2. Select the appropriate board.
  3. Select correct COM port assigned to your board
  4. Click the “Upload” button, and wait till the upload is complete.

Logs can be seen in the status bar at the bottom of the Arduino IDE. “Done uploading” message can be seen after completion.

Programming in Arduino is a great way to learn the basics of C programming concepts. With practice and experience, you can explore advanced features of the Arduino based programming. This will enable you to create complex and exciting Embedded systems and IOT applications and projects.

Photo by Sahand Babali on Unsplash

--

--

Nandan Ghawate

Electronics Product Designer | Project Manager | Embedded Systems | Internet Of Things (IOT) | Battery Management Systems (BMS)