Skip to content Skip to sidebar Skip to footer

Basic Syntax in Arduino Programming

Two Basic Functions You Should Know in Arduino Programming

Basic Syntax of C++ Programming on Arduino - After we know the types of Arduino boards and what types are suitable for beginners, how to write C++ programs that will be used in Arduino programming.

Basic Syntax in Arduino Programming

A. Sketch

In the Arduino IDE, the program code that we will create is called "Sketch". Sketch will be able to run if 2 conditions are met:

The setup() function is used to initialize variables, set Pin conditions, start a library and more. This function will be called only once when Sketch is first run. If the Arduino is reset or turned on again, then this function will be executed again.

The loop() function will be executed after the setup() function. The loop() function is used to run the main program repeatedly.

Look at to the following picture:

Sintaks Dasar Pemrograman C++ Pada Arduino

In this picture is the basic syntax needed in Arduino programming. Without these two, the program will error and cannot be used.

B. Basic Program Example

The following is an example program to turn on and off an LED alternately within 1 second:

void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH); 
  delay(1000);                       
  digitalWrite(13, LOW);   
  delay(1000);                     
}
 

Explanation:

In the setup() function, we set the pin to be used for the LED, which is 13. We set this pin as OUTPUT. That is, this pin will output a HIGH (5V) or LOW(0V) voltage.

After that, we will enter the main program, which is to turn on the LED for 1 second and turn off the LED for 1 second.

The flow, make pin 13 on HIGH condition, then pin 13 will have a voltage of 5V, where this 5V voltage is able to make the LED light up.

Then set the delay time for 1000ms (1 second), then the LED will light up to 1 second.

After that, make pin 13 in LOW condition, then pin 13 will have a voltage of 0V, where at this voltage the LED cannot turn on/off.

Then set the delay time for 1000ms (1 second), then the LED will be off for up to 1 second.

And the LED will be back on for 1 second, off for 1 second and so on.

I hope this article can add to your understanding and insight in learning Arduino. Thank you for visiting this website.

Post a Comment for "Basic Syntax in Arduino Programming"