Ultimate_Arduino_Sensors_Guide
Ultimate_Arduino_Sensors_Guide
This document contains explanations and example codes for over 40 commonly used
sensors and modules with Arduino. Each section provides a description of the component
along with a sample code to implement it.
Example Code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
void setup() {
lcd.begin(16, 2);
lcd.print("Hello, Arduino!");
}
void loop() {
lcd.setCursor(0, 1);
lcd.print(millis() / 1000);
delay(1000);
}
OLED Display
Displays text and graphics using an OLED screen.
Example Code:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 10);
display.println("Hello OLED!");
display.display();
}
void loop() {}
Example Code:
#include <Adafruit_GFX.h>
#include <Adafruit_TFTLCD.h>
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4
void setup() {
tft.begin(0x9325);
tft.fillScreen(0x0000);
tft.setCursor(50, 50);
tft.setTextColor(0xFFFF);
tft.setTextSize(2);
tft.print("Hello, TFT!");
}
void loop() {}
Servo Motor
A servo motor allows precise angular control.
Example Code:
#include <Servo.h>
Servo myServo;
void setup() {
myServo.attach(9);
}
void loop() {
myServo.write(0);
delay(1000);
myServo.write(90);
delay(1000);
myServo.write(180);
delay(1000);
}
Stepper Motor
A stepper motor rotates in small steps for precise movement.
Example Code:
#include <Stepper.h>
#define STEPS 200
void setup() {
stepper.setSpeed(60);
}
void loop() {
stepper.step(200);
delay(1000);
stepper.step(-200);
delay(1000);
}
Example Code:
const int trigPin = 9;
const int echoPin = 10;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
Example Code:
void setup() {
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int motionDetected = digitalRead(pirPin);
if (motionDetected) {
digitalWrite(ledPin, HIGH);
Serial.println("Motion detected!");
} else {
digitalWrite(ledPin, LOW);
}
delay(500);
}
Example Code:
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT11
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
float temp = dht.readTemperature();
float hum = dht.readHumidity();
Serial.print("Temperature: ");
Serial.print(temp);
Serial.println(" *C");
Serial.print("Humidity: ");
Serial.print(hum);
Serial.println(" %");
delay(2000);
}
void setup() {
Serial.begin(9600);
}
void loop() {
int gasLevel = analogRead(mq2Pin);
Serial.print("Gas Level: ");
Serial.println(gasLevel);
delay(1000);
}
Relay Module
Controls high-power devices with Arduino.
Example Code:
int relayPin = 7;
void setup() {
pinMode(relayPin, OUTPUT);
}
void loop() {
digitalWrite(relayPin, HIGH);
delay(5000);
digitalWrite(relayPin, LOW);
delay(5000);
}
Example Code:
#include <SoftwareSerial.h>
SoftwareSerial BT(10, 11);
void setup() {
Serial.begin(9600);
BT.begin(9600);
}
void loop() {
if (BT.available()) {
char data = BT.read();
Serial.write(data);
}
}