Fundamentals of Arduino Programming
Introduction
Arduino is a popular open-source platform for building electronics projects. It’s centered around a microcontroller and a software environment where you can write and upload code to your board. Arduino programming allows you to interact with the hardware, enabling creativity in building interactive devices.
In this article, we’ll cover the basics of Arduino programming. These fundamentals will be crucial as we dive deeper into actual projects in the following lessons.
What is a Variable?
A variable in Arduino programming is a placeholder in memory where data can be stored, retrieved, and manipulated. Think of it as a container containing different information like numbers, text, or sensor readings. You define a variable to store information that changes during the program’s execution.
int ledPin = 13; // Stores the pin number of the LED
Data Types
Data types specify the type of data a variable can store. Common data types in Arduino programming include:
- int: Stores integer numbers (whole numbers).
int temperature = 25;
- float: Stores decimal numbers.
float voltage = 5.12;
- char: Stores single characters.
char letter = 'A';
- boolean: Stores true or false values.
boolean isOn = true;
How to Use Comments
Comments are lines of text in your code ignored by the compiler but help make your code easier to understand. In Arduino, comments start with // for single lines and /* */ for multi-line comments.
1. // This is a single-line comment
2.
3. /*
4. This is a multi-line comment.
5. It can span across several lines.
6. */
7.
Operators
Operators are symbols that tell the compiler to perform specific mathematical or logical operations. Some common operators in Arduino are:
- Arithmetic Operators: +, -, *, /, %
- Assignment Operator: =
- Comparison Operators: ==, !=, <, >, <=, >=
If-Else Statements
The if-else statement allows you to execute certain parts of the code based on a condition. If the condition is proper, the program runs a specific code block; otherwise, it runs an alternative block.
if (temperature > 30) {
// Turn on fan
digitalWrite(fanPin, HIGH);
} else {
// Turn off fan
digitalWrite(fanPin, LOW);
}
Loops
Loops allow you to run a block of code repeatedly. There are two main types of loops in Arduino:
- For Loop: Used when you know how many times you want to repeat a code block.
for (int i = 0; i < 10; i++) {
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
- While Loop: Repeats a code block if a condition is true.
while (buttonPressed == false) {
digitalWrite(ledPin, LOW);
}
Functions
A function is a block of code that performs a specific task. In Arduino, there are two core functions you must include:
- Setup(): This function runs once the board starts and sets initial conditions (e.g., pin modes).
- Loop(): This function runs repeatedly and contains the code that keeps executing throughout the program’s lifetime.
1. void setup() {
2. pinMode(ledPin, OUTPUT); // Configures pin as an output
3. }
4.
5. void loop() {
6. digitalWrite(ledPin, HIGH); // Turns the LED on
7. delay(1000); // Waits for a second
8. digitalWrite(ledPin, LOW); // Turns the LED off
9. delay(1000); // Waits for a second
10. }
- pinMode(): Sets a pin as INPUT or OUTPUT.
- digitalWrite(): Sends a HIGH or LOW signal to a digital pin.
- digitalRead(): Reads a digital pin’s state (HIGH/LOW).
For analog functions:
- analogWrite(): Writes an analog value (PWM) to a pin.
analogWrite(ledPin, 128); // Sets LED brightness to 50%
- analogRead(): Reads the value from an analog pin.
int sensorValue = analogRead(A0);
delay(): Pauses the program for a specified number of milliseconds.
Libraries
Libraries are collections of pre-written code that add extra functionality to your program. Arduino libraries make working with sensors, displays, motors, and other hardware easier.
Examples:
- Servo library: Controls servo motors.
- LiquidCrystal library: Controls LCDs.
To include a library, use:
#include <Servo.h>
Conclusion
These basic concepts—variables, data types, operators, loops, and functions—are the building blocks of Arduino programming. Understanding them will help you unlock the potential of creating your projects. Don’t worry if this feels overwhelming; in the following lessons, you’ll apply these concepts hands-on, making learning Arduino fun and practical! Stay tuned!