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

BJT vs. MOSFET: Understanding the Key Differences

  When it comes to semiconductor devices, BJT (Bipolar Junction Transistor) and MOSFET (Metal-Oxide-Semiconductor Field-Effect Transistor) are two of the most commonly used transistors. They both play a critical role in amplifying or switching electronic signals, but their underlying principles and applications are quite different. In this post, we’ll explore the fundamental differences between BJT and MOSFET to help you understand their unique features and decide which one to use in your project. What is a BJT? A Bipolar Junction Transistor (BJT) is a current-controlled device that has three terminals: Collector (C) , Base (B) , and Emitter (E) . BJTs can be either NPN or PNP types, depending on the arrangement of their semiconductor layers. BJTs work by using a small current at the base to control a larger current flowing between the collector and emitter. How BJT Works: Current-Controlled : In a BJT, the amount of current that flows from the collector to the emitter is cont...

Troubleshooting an EEPROM Write Issue in a Microcontroller – A Real-World Debugging Experience

 Troubleshooting an EEPROM Write Issue in a Microcontroller—A Real-World Debugging Experience ๐Ÿ”ง Problem Statement While working on an embedded system at an R&D firm, I encountered a puzzling problem with EEPROM data writing. The EEPROM write operation was perfectly simulated, but when the compiled HEX file was uploaded to the actual microcontroller, the written data became unrecognizable. ๐Ÿงช Initial Assumptions At first, I assumed there might be a logical bug in my code. However, after carefully reviewing my EEPROM write functions and re-running simulations multiple times, everything appeared to be correct. ๐Ÿ›  Step-by-Step Debugging Process 1. Verification via Custom Tool To confirm whether the EEPROM was actually writing incorrect data, I built a simple tool that could: Read the EEPROM data from the microcontroller Convert and display the raw bytes into human-readable formats like integer and character Upon reading the EEPROM content using this tool, I conf...