Introduction to Arduino
What is Arduino?
Arduino is an open-source platform used for creating electronic projects. It includes both programmable boards and an integrated development environment (IDE).
Getting Started:
To begin, download and install the Arduino IDE from the official website. Then, connect your Arduino board to your computer via USB and select the correct model and port in the IDE.
void setup() {
// initialization
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
// main loop
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
Exercise:
Write a program to blink the built-in LED of the Arduino board.
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
}
Sensors and Actuators
Temperature Sensors:
Arduino can be connected to various sensors to read environmental data. For example, a temperature sensor can be used to read ambient temperature.
Example:
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
delay(2000);
}
Actuators:
Actuators like motors can be controlled by Arduino to perform physical actions. For example, you can use a servo motor to move a robotic arm.
Example:
#include <Servo.h>
Servo myservo;
void setup() {
myservo.attach(9);
}
void loop() {
myservo.write(90);
delay(1000);
myservo.write(0);
delay(1000);
}
Exercise:
Use a temperature sensor to read the temperature and display the values on the serial monitor.
Here's an explanatory video on sensors and actuators:
Serial Communication
Introduction:
Serial communication allows Arduino to communicate with other devices like computers or Bluetooth modules. This communication is typically done via the RX and TX pins or via USB.
Serial Communication Example:
Here's an example code to send data from Arduino to a computer via serial communication.
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("Hello, World!");
delay(1000);
}
Exercise:
Write a program that sends a personalized message to the serial monitor every two seconds.