Ultimate Arduino Sensors & Modules 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.
LCD (16x2) Display
Displays text using a Liquid Crystal Display.
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>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
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() {}
Touch Screen (TFT Display)
A touchscreen display that allows user interaction.
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
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
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
Stepper stepper(STEPS, 8, 9, 10, 11);
void setup() {
stepper.setSpeed(60);
}
void loop() {
stepper.step(200);
delay(1000);
stepper.step(-200);
delay(1000);
}
Ultrasonic Sensor (HC-SR04)
Measures distance using ultrasonic waves.
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);
long duration = pulseIn(echoPin, HIGH);
int distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
PIR Motion Sensor
Detects motion using infrared radiation.
Example Code:
int pirPin = 2; // PIR sensor input pin
int ledPin = 13; // LED output
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);
}
DHT11 Temperature & Humidity Sensor
Measures temperature and humidity levels.
Example Code:
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
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);
}
Gas Sensor (MQ-2)
Detects gases like methane, LPG, and smoke.
Example Code:
int mq2Pin = A0;
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);
}
Bluetooth Module (HC-05)
Allows wireless communication via Bluetooth.
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);
}
}