0% found this document useful (0 votes)
3 views18 pages

Ultimate_Arduino_Sensors_Modules_Guide

The document is a comprehensive guide detailing over 40 sensors and modules commonly used with Arduino, each accompanied by descriptions and example codes for implementation. It covers various components such as displays, motors, sensors, and communication modules, providing essential information for users to integrate these components into their projects. The guide serves as a valuable resource for both beginners and experienced Arduino enthusiasts looking to expand their knowledge and skills.

Uploaded by

badkiaan123
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
3 views18 pages

Ultimate_Arduino_Sensors_Modules_Guide

The document is a comprehensive guide detailing over 40 sensors and modules commonly used with Arduino, each accompanied by descriptions and example codes for implementation. It covers various components such as displays, motors, sensors, and communication modules, providing essential information for users to integrate these components into their projects. The guide serves as a valuable resource for both beginners and experienced Arduino enthusiasts looking to expand their knowledge and skills.

Uploaded by

badkiaan123
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 18

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);
}
}

Soil Moisture Sensor


Measures soil moisture levels for smart irrigation.

Example Code:

int sensorPin = A0;

void setup() {
Serial.begin(9600);
}

void loop() {
int moisture = analogRead(sensorPin);
Serial.print("Soil Moisture: ");
Serial.println(moisture);
delay(1000);
}

Rain Sensor
Detects the presence of rain using a conductive plate.

Example Code:

int rainPin = A0;

void setup() {
Serial.begin(9600);
}
void loop() {
int rainValue = analogRead(rainPin);
Serial.print("Rain Intensity: ");
Serial.println(rainValue);
delay(1000);
}

UV Sensor (GUVA-S12SD)
Measures ultraviolet light intensity.

Example Code:

int uvPin = A0;

void setup() {
Serial.begin(9600);
}

void loop() {
int uvValue = analogRead(uvPin);
Serial.print("UV Level: ");
Serial.println(uvValue);
delay(1000);
}

MPU6050 (Accelerometer & Gyroscope)


Measures acceleration and rotation.

Example Code:

#include <Wire.h>
#include <MPU6050.h>

MPU6050 mpu;

void setup() {
Serial.begin(9600);
Wire.begin();
mpu.initialize();
}
void loop() {
int ax, ay, az;
mpu.getAcceleration(&ax, &ay, &az);
Serial.print("Accel: "); Serial.print(ax); Serial.print(", ");
Serial.print(ay); Serial.print(", "); Serial.println(az);
delay(500);
}

Sound Sensor
Detects ambient sound levels.

Example Code:

int soundPin = A0;

void setup() {
Serial.begin(9600);
}

void loop() {
int soundLevel = analogRead(soundPin);
Serial.print("Sound Level: ");
Serial.println(soundLevel);
delay(500);
}

Flame Sensor
Detects flame presence and alerts.

Example Code:

int flamePin = A0;

void setup() {
Serial.begin(9600);
}

void loop() {
int flameDetected = analogRead(flamePin);
if (flameDetected < 500) {
Serial.println("Flame Detected!");
}
delay(500);
}

RFID Module (MFRC522)


Reads RFID tags for identification.

Example Code:

#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9
MFRC522 rfid(SS_PIN, RST_PIN);

void setup() {
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
}

void loop() {
if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
Serial.print("RFID Tag: ");
for (byte i = 0; i < rfid.uid.size; i++) {
Serial.print(rfid.uid.uidByte[i], HEX);
}
Serial.println();
}
}

NFC Module
Reads NFC tags for data transfer and security.

Example Code:

#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>
PN532_I2C pn532_i2c(Wire);
PN532 nfc(pn532_i2c);

void setup() {
Serial.begin(9600);
nfc.begin();
}

void loop() {
uint8_t success;
uint8_t uid[] = {0, 0, 0, 0, 0, 0, 0};
uint8_t uidLength;

success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);

if (success) {
Serial.print("NFC Tag UID: ");
for (uint8_t i=0; i < uidLength; i++) {
Serial.print(uid[i], HEX);
}
Serial.println();
delay(1000);
}
}

ESP8266 WiFi Module


Allows Arduino to connect to WiFi for IoT applications.

Example Code:

#include <ESP8266WiFi.h>

const char* ssid = "your_SSID";


const char* password = "your_PASSWORD";

void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {


delay(500);
Serial.print(".");
}
Serial.println("Connected!");
}

void loop() {}

GSM Module (SIM900)


Allows Arduino to send SMS and make calls.

Example Code:

#include <SoftwareSerial.h>

SoftwareSerial gsm(7, 8);

void setup() {
gsm.begin(9600);
Serial.begin(9600);
delay(1000);
gsm.println("AT");
delay(1000);
}

void loop() {
if (Serial.available()) {
gsm.write(Serial.read());
}
if (gsm.available()) {
Serial.write(gsm.read());
}
}

4x4 Capacitive Touchpad (16 Keys)


A capacitive touchpad with 16 keys for input.

Example Code:

#include <Wire.h>
#include "Adafruit_MPR121.h"
Adafruit_MPR121 touchpad = Adafruit_MPR121();

void setup() {
Serial.begin(9600);
if (!touchpad.begin(0x5A)) {
Serial.println("Touchpad not found!");
while (1);
}
}

void loop() {
uint16_t touched = touchpad.touched();
for (uint8_t i = 0; i < 12; i++) {
if (touched & (1 << i)) {
Serial.print("Key ");
Serial.print(i);
Serial.println(" Touched");
}
}
delay(200);
}

CO2 Sensor (MG811)


Measures carbon dioxide concentration in the air.

Example Code:

int co2Pin = A0;

void setup() {
Serial.begin(9600);
}

void loop() {
int co2Level = analogRead(co2Pin);
Serial.print("CO2 Level: ");
Serial.println(co2Level);
delay(1000);
}
Water Level Sensor
Detects the level of water in a tank or container.

Example Code:

int waterLevelPin = A0;

void setup() {
Serial.begin(9600);
}

void loop() {
int level = analogRead(waterLevelPin);
Serial.print("Water Level: ");
Serial.println(level);
delay(1000);
}

Tilt Sensor
Detects tilt and movement.

Example Code:

int tiltPin = 2;

void setup() {
pinMode(tiltPin, INPUT);
Serial.begin(9600);
}

void loop() {
if (digitalRead(tiltPin)) {
Serial.println("Tilt Detected!");
}
delay(500);
}

IR Line Follower Sensor


Detects black and white surfaces for robotics.

Example Code:
int irPin = 2;

void setup() {
pinMode(irPin, INPUT);
Serial.begin(9600);
}

void loop() {
if (digitalRead(irPin)) {
Serial.println("White Surface Detected!");
} else {
Serial.println("Black Line Detected!");
}
delay(500);
}

Laser Sensor
Detects laser beams for security or distance measurement.

Example Code:

int laserPin = A0;

void setup() {
Serial.begin(9600);
}

void loop() {
int laserValue = analogRead(laserPin);
Serial.print("Laser Intensity: ");
Serial.println(laserValue);
delay(1000);
}

Magnetic Reed Switch


Detects the presence of a magnetic field.

Example Code:

int reedPin = 2;
void setup() {
pinMode(reedPin, INPUT);
Serial.begin(9600);
}

void loop() {
if (digitalRead(reedPin)) {
Serial.println("Magnet Detected!");
}
delay(500);
}

Pulse Sensor (Heart Rate Monitor)


Measures heart rate using a pulse sensor.

Example Code:

int pulsePin = A0;

void setup() {
Serial.begin(9600);
}

void loop() {
int pulse = analogRead(pulsePin);
Serial.print("Heart Rate: ");
Serial.println(pulse);
delay(1000);
}

Real-Time Clock (RTC DS3231)


Keeps track of real-world time and date.

Example Code:

#include <Wire.h>
#include "RTClib.h"

RTC_DS3231 rtc;
void setup() {
Serial.begin(9600);
rtc.begin();
}

void loop() {
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.println(now.second(), DEC);
delay(1000);
}

GPS Module (NEO-6M)


Retrieves location coordinates using GPS.

Example Code:

#include <SoftwareSerial.h>
SoftwareSerial gpsSerial(4, 3);

void setup() {
Serial.begin(9600);
gpsSerial.begin(9600);
}

void loop() {
while (gpsSerial.available()) {
Serial.write(gpsSerial.read());
}
}
Air Quality Sensor (MQ-135)
Measures air pollution levels.

Example Code:

int airQualityPin = A0;

void setup() {
Serial.begin(9600);
}

void loop() {
int airQuality = analogRead(airQualityPin);
Serial.print("Air Quality Level: ");
Serial.println(airQuality);
delay(1000);
}

pH Sensor
Measures the pH level of liquids.

Example Code:

int pHpin = A0;

void setup() {
Serial.begin(9600);
}

void loop() {
int pHValue = analogRead(pHpin);
Serial.print("pH Level: ");
Serial.println(pHValue);
delay(1000);
}

You might also like