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

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...

RFID + ESP8266 Based Web Interface System

  Project Title: RFID_ESP8266_Interface_by_ReponSheikh GitHub: View on GitHub ๐Ÿ“˜ Introduction In this project, I have built a simple yet powerful RFID-based system using the ESP8266 NodeMCU microcontroller. The system reads RFID cards using the MFRC522 module and sends the UID data to a web interface over Wi-Fi. It can be used in access control , attendance tracking , or any kind of RFID-based identification system. ๐Ÿงฐ Components Used ESP8266 NodeMCU (ESP-12E) MFRC522 RFID Reader RFID Tags or Cards (13.56 MHz) Jumper Wires USB Cable and Power Source ๐ŸŒ Features Web-based interface to view UID logs Automatic Wi-Fi configuration using WiFiManager LittleFS file system support to store configuration and logs Easy-to-understand code structure with comments Arduino IDE compatible ๐Ÿ”ง How It Works On boot, the ESP8266 tries to connect to the last used Wi-Fi. If it fails, it starts as an Access Point for WiFi setup. Once connect...

ESP8266 + Python GUI: Serial RS232 Communication Tool

  Project Title: ESP8266_Serial_Python_GUI_RS232_v3 GitHub Repository: View on GitHub ๐Ÿ“˜ Overview This project demonstrates a PC-to-ESP8266 serial communication system using a custom-built Python GUI . The tool is developed for RS232-based command exchange, making it ideal for debugging and interacting with embedded systems like microcontrollers, development boards, and IoT devices. ๐ŸŽฏ Project Goals Establish two-way communication between PC and ESP8266 Create a simple Python GUI for sending and receiving serial data Log all serial commands with timestamps Automatically save communication logs in a text file Provide clickable link to the developer’s profile from the GUI ๐Ÿงฐ Tools & Technologies ESP8266 NodeMCU Python 3.x PySerial for COM port communication Tkinter for GUI design RS232 Protocol Windows platform for executable (.exe) build ๐ŸŒŸ Key Features ✅ GUI Interface with Command Input Box ✅ Auto Serial Port Detection ✅ View...