Skip to main content

Arduino Programming Tutorial


In this tutorial, we'll cover the basics of Arduino programming, focusing on variable types, functions, function return types, and constant declarations. We will use simple examples to demonstrate how each concept works.



1. Variables in Arduino

Variables are used to store data that your Arduino program can use. There are different types of variables based on the data they store.

Common variable types:

  • int: Stores integer values (whole numbers).
  • float: Stores decimal numbers.
  • char: Stores a single character.
  • boolean: Stores true or false.
  • String: Stores a sequence of characters (text).

Example:


int ledPin = 13; // Integer variable to store LED pin number float sensorValue = 0.0; // Float variable to store sensor value char myChar = 'A'; // Char variable to store a character boolean isOn = true; // Boolean variable to store true/false String myText = "Hello"; // String variable to store a string

2. Constants

Constants are values that do not change during the program's execution. In Arduino, you can declare a constant using the const keyword or the #define directive.

  • const: Used for typed constants.

  • #define: Used for constant macros without data types.

Example:


const int buttonPin = 2; // Declaring a constant with a type #define PI 3.14159 // Defining a constant macro

3. Functions

Functions allow you to break your code into reusable blocks. A function usually performs a specific task.

The structure of a function:

  • Return type: What type of value the function returns (e.g., int, void, float).
  • Function name: The name used to call the function.
  • Parameters: Inputs to the function (optional).
  • Body: The code inside the function.

Example of a simple function:


void blinkLED() { // `void` means this function doesn't return any value digitalWrite(ledPin, HIGH); // Turn the LED on delay(1000); // Wait for 1 second digitalWrite(ledPin, LOW); // Turn the LED off delay(1000); // Wait for 1 second }

4. Function Return Types

A function can return a value to the part of the code that called it. You specify the return type when defining the function.

Example of a function with a return type:


int addTwoNumbers(int a, int b) { // The function returns an `int` int sum = a + b; return sum; // Returning the sum } void setup() { Serial.begin(9600); int result = addTwoNumbers(5, 3); // Calling the function Serial.println(result); // Output will be 8 } void loop() { // Your code here }

In the above code:

  • The function addTwoNumbers takes two int parameters and returns their sum.
  • The function return type is int, meaning it will return an integer value.

5. Global vs Local Variables

  • Global variables: Declared outside any function and can be accessed from any function.
  • Local variables: Declared inside a function and can only be accessed within that function.

Example:


int ledPin = 13; // Global variable void setup() { int sensorValue = 0; // Local variable pinMode(ledPin, OUTPUT); } void loop() { digitalWrite(ledPin, HIGH); // Can access the global variable delay(1000); }

6. Using Functions in Arduino

Arduino has two main functions that are essential for every sketch:

  • setup(): Runs once when the program starts, typically used for initialization.
  • loop(): Runs repeatedly after the setup() function completes.

Example Arduino Sketch:


const int ledPin = 13; // Constant to define the LED pin void setup() { pinMode(ledPin, OUTPUT); // Set LED pin as an output } void loop() { blinkLED(); // Call the blinkLED function } // Function to blink the LED void blinkLED() { digitalWrite(ledPin, HIGH); // Turn the LED on delay(1000); // Wait for 1 second digitalWrite(ledPin, LOW); // Turn the LED off delay(1000); // Wait for 1 second }

Conclusion

In this tutorial, we covered:

  • Different variable types (int, float, char, etc.).
  • Declaring constants using const and #define.
  • Writing and using functions, including those with return types.
  • Global and local variables, and the structure of an Arduino program with setup() and loop().

You can expand these concepts to build more complex Arduino projects!

For more in-depth tutorials and detailed project guides, stay tuned to ResearchTape!

Author: Repon Sheikh

Comments

Popular posts from this blog

AI Technology & Medical Service Integration is very Essential for Whom?

For whom AI and medical services have become urgent?  the name of the country, Bangladesh may come upstairs in response to the question. Bangladesh is a very important country in South Asia and most of the sides of this country are surrounded by the Indian border. According to the data published the  year of 2018  in the UK-based medical and public health magazine Lancet, Bangladesh is ranked 133rd among 195 countries in respect of medical and health services. Not only in Bangladesh but financially underdeveloped or developing countries the quality of medical care is weak. Getting these skilled doctors also becomes difficult. On the other hand, Bangladesh's position in the production and export of drugs is at the top of the underdeveloped country, but according to data obtained by 2020, Bangladesh was ranked 72nd in global drug exports. Although Bangladesh has achieved a lot of supremacy in the production of medicine, it is viewed, but it is far behind in medical care. Today's

Embedded system programming & development interest is becoming more popular

  Over the years there has been a growing interest in embedded systems development and programming as part of science technology and computer research. Due to the obligation to present a project or research in science in many schools, colleges, or universities, this subject is becoming necessary and acceptable day by day. In most cases, science projects are gaining importance as electronics and embedded system programming. This is because of their easy availability and the availability of lots of information on the Internet. photo: Arduino board, an example of an embedded board  Arduino Board is a top choice for beginners of electro-embedded designers because of this less complexity and availability of lots of resources online. easy and attractive beginning to an embedded programming platform is Arduino.  to be continued...  writer: Repon Sheikh 

Students' interest in science fair is increasing

At present, students are becoming interested in science fairs at the school and college levels. photo credit : fair-bd.org