0% found this document useful (0 votes)
20 views5 pages

Define The Pins For The Ultrasonic

Uploaded by

Sriram S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views5 pages

Define The Pins For The Ultrasonic

Uploaded by

Sriram S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

ULTRASONIC SENSOR

const int triggerPin = 23; // Trigger pin connected to GPIO 23


const int echoPin = 22; // Echo pin connected to GPIO 22
long duration; // Duration for the pulse to travel
int distance; // Distance calculated from the duration

// Define the threshold for "Near" and "Far"


const int nearThreshold = 20; // Distance in cm for "Near"
const int farThreshold = 100; // Distance in cm for "Far"

void setup() {
// Start the serial communication at 115200 baud rate
Serial.begin(115200);

// Set the triggerPin as OUTPUT and echoPin as INPUT


pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop() {
// Send a 10-microsecond HIGH pulse to trigger the sensor
digitalWrite(triggerPin, LOW); // Ensure the trigger pin is LOW initially
delayMicroseconds(2); // Wait for a short time
digitalWrite(triggerPin, HIGH); // Send a trigger pulse
delayMicroseconds(10); // Wait for 10 microseconds
digitalWrite(triggerPin, LOW); // End the trigger pulse

// Measure the time it takes for the echo to return


duration = pulseIn(echoPin, HIGH); // Read the pulse duration in microseconds

// Calculate the distance (speed of sound is 343 m/s or 0.0343 cm/us)


distance = duration * 0.0343 / 2; // Divide by 2 to get the distance

// Print the distance to the Serial Monitor


Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" cm - ");

// Determine if the object is Near or Far based on the distance


if (distance <= nearThreshold) {
Serial.println("Near");
} else if (distance >= farThreshold) {
Serial.println("Far");
} else {
Serial.println("Moderate");
}

// Wait for a short time before the next reading


delay(500);
}

LDR:

// Define the analog pin where the LDR is connected


int ldrPin = 34; // Analog pin 34 (ESP32 ADC pin)
int ldrValue = 0; // Variable to store LDR reading
int threshold = 2000; // Set threshold to decide light or dark

void setup() {
// Start the serial communication at 115200 baud rate
Serial.begin(115200);

// Configure the LDR pin as an input


pinMode(ldrPin, INPUT);
}

void loop() {
// Read the analog value from the LDR (0 to 4095 for 12-bit ADC on ESP32)
ldrValue = analogRead(ldrPin);

// Print the LDR value to the Serial Monitor


Serial.print("LDR Value: ");
Serial.println(ldrValue);

// Check if the light level is above or below the threshold


if (ldrValue > threshold) {
Serial.println("Light (Bright)");
} else {
Serial.println("Dark");
}

// Add a small delay between readings


delay(500);
}
LM35:
int sensorPin = 34; // Analog pin 34 (ESP32 has ADC pins from 34 to 39)
float temperature; // Variable to store the temperature value

void setup() {
// Start the serial communication at 115200 baud rate (recommended for ESP32)
Serial.begin(115200);

// Configure the sensor pin


pinMode(sensorPin, INPUT);
}

void loop() {
// Read the analog value from the temperature sensor (LM35)
int sensorValue = analogRead(sensorPin);

// Convert the analog value to a voltage (0-3.3V for ESP32)


float voltage = sensorValue * (3.3 / 4095.0); // 12-bit ADC on ESP32 (0-4095 range)

// Convert the voltage to temperature (LM35: 10mV per °C)


temperature = voltage * 100.0;

// Print the temperature value to the Serial Monitor


Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");

// Wait for a second before the next reading


delay(1000);
}

You might also like