0% found this document useful (0 votes)
27 views10 pages

Sensors

The document discusses how to install libraries for sensors in Arduino. It provides steps to search for and install libraries from the Arduino IDE. It then provides code examples for 10 different sensors: ultrasonic sensor, LM35 temperature sensor, touch sensor, LM393 light sensor, soil moisture sensor, water level sensor, PIR motion sensor, DHT11 humidity and temperature sensor, force sensor, and MQ-135 air quality sensor. The code examples show how to read sensor values and print them to the serial monitor.

Uploaded by

16-Najeeb Ahmed
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)
27 views10 pages

Sensors

The document discusses how to install libraries for sensors in Arduino. It provides steps to search for and install libraries from the Arduino IDE. It then provides code examples for 10 different sensors: ultrasonic sensor, LM35 temperature sensor, touch sensor, LM393 light sensor, soil moisture sensor, water level sensor, PIR motion sensor, DHT11 humidity and temperature sensor, force sensor, and MQ-135 air quality sensor. The code examples show how to read sensor values and print them to the serial monitor.

Uploaded by

16-Najeeb Ahmed
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/ 10

Arduino

How to install libraries for certain sensors:


Certain sensors require additional libraries to be installed for this working.
1. In Arduino IDE, click sketch. Then click Include Library.
2. Click Manage Libraries.

3. Then, you will get the below window.


4. You need to type the name of the required library.
5. You will get few results. Choose the suitable one and click them.
6. Press install.
1. Ultrasonic sensor

const int trigPin = 2;

const int echoPin = 3;

void setup() {

Serial.begin(9600);

// Set the trigger pin as output and the echo pin as input

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

void loop() {

// Generate a short pulse to trig digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// Measure the duration of the pulse from the echo pin

long duration = pulseIn(echoPin, HIGH);

// Calculate the distance based on the speed of sound

int distance = duration * 0.034 / 2;

Serial.print("Distance: ");

Serial.print(distance);
Serial.println(" cm");

delay(500);

2. LM35 sensor

const int lm35Pin = A0;

void setup() {

Serial.begin(9600);

void loop() {

int sensorValue = analogRead(lm35Pin);

// Convert the sensor value to temperature in Celsius

float temperature = (sensorValue * 5.0 / 1023.0) * 100.0;

Serial.print("Temperature: ");

Serial.print(temperature);

Serial.println(" °C");

delay(1000);

3. Touch sensor
const int touchPin = 2;

void setup() {

Serial.begin(9600);

// Set the touch pin as input

pinMode(touchPin, INPUT);

void loop() {

int touchStatus = digitalRead(touchPin);

if (touchStatus == HIGH) {

Serial.println("Touch Detected");

} else {

Serial.println("No Touch Detected");

delay(1000);

4. LM393 light sensor


const int lightPin = A0;

void setup() {

Serial.begin(9600);

void loop() {

// Read the analog value from the LM393 light sensor

int sensorValue = analogRead(lightPin);

Serial.print("Light Intensity: ");

Serial.println(sensorValue);

delay(1000);

5. Soil moisture sensor


const int moisturePin = A0;

void setup() {

Serial.begin(9600);

void loop() {

// Read the analog value from the soil moisture sensor

int sensorValue = analogRead(moisturePin);

Serial.print("Moisture Level: ");

Serial.println(sensorValue);

delay(1000);

6. Water Level Sensor

const int sensorPin = A0;

void setup() {

Serial.begin(9600);

void loop() {

// Read the analog value from the water level sensor


int sensorValue = analogRead(sensorPin);

// Map the sensor value to a water level percentage (adjust the min and max values as needed)

int waterLevel = map(sensorValue, 0, 1023, 0, 100);

Serial.print("Water Level: ");

Serial.print(waterLevel);

Serial.println("%");

delay(1000);

7. PIR sensor

const int pirPin = 2;

void setup() {

Serial.begin(9600);

pinMode(pirPin, INPUT);

void loop() {

// Read the motion status from the PIR sensor

int motionStatus = digitalRead(pirPin);

if (motionStatus == HIGH) {

Serial.println("Motion Detected");

} else {
Serial.println("No Motion Detected");

delay(500);

8. DHT11 Sensor

#include <DHT.h>

// DHT11 sensor pin

const int dhtPin = 2;

// Initialize the DHT sensor

DHT dht(dhtPin, DHT11);

void setup() {

Serial.begin(9600);

// Initialize the DHT sensor

dht.begin();

void loop() {

float temperature = dht.readTemperature();

float humidity = dht.readHumidity();

// Print the temperature and humidity values to the serial monitor

Serial.print("Temperature: ");

Serial.print(temperature);
Serial.print(" °C\tHumidity: ");

Serial.print(humidity);

Serial.println(" %");

delay(2000);

9. Force sensor

const int forcePin = A0;

void setup() {

// Initialize serial communication

Serial.begin(9600);

void loop() {

// Read the analog value from the force sensor

int forceValue = analogRead(forcePin);

Serial.print("Force Sensor Value: ");

Serial.println(forceValue);

delay(1000);

}
10. MQ-135 air sensor

const int mqPin = A0;

void setup() {

Serial.begin(9600);

void loop() {

int airQuality = analogRead(mqPin);

// Print the air quality value to the serial monitor

Serial.print("Air Quality: ");

Serial.println(airQuality);

delay(1000);

You might also like