Exercise 1: The Darkness Sensor
Goal: The LED should turn on when the sensor detects that it is dark.
int led = 2;
int ldr = 34;
void setup() {
pinMode(led, INPUT);
Serial.begin(115200);
}
void loop() {
int light = analogRead(ldr);
if (light < 500) {
digitalWrite(led, LOW);
} else {
digitalWrite(led, HIGH);
}
Serial.println(ldr);
delay(5000);
}
Exercise 2: The Irrigation Servo
Goal: If the potentiometer (humidity) drops below 30, the servo should open to 90 degrees.
#include <ESP32Servo.h>
Servo myservo;
void setup() {
myservo.attach(18);
}
void loop() {
int value = analogRead(32);
int humidity = map(value, 0, 4095, 0, 100);
if (humidity = 20) {
myservo.write(90);
} else {
myservo.write(0);
}
delay(1);
humidity = 100;
Serial.print(humidity);
}
Exercise 3: Temperature Alarm
Goal: If the temperature rises above 30 degrees, the LED should blink as an alert.
#include "DHT.h"
DHT dht(15, DHT22);
void setup() {
dht.begin();
pinMode(2, OUTPUT);
}
void loop() {
float temp = dht.readTemperature();
while (temp > 30) {
digitalWrite(2, HIGH);
delay(200);
digitalWrite(2, LOW);
delay(200);
}
if (temp < 30) {
digitalWrite(2, LOW);
}
delay(20);
}
Exercise 4: Motion Sensor Door
Goal: When motion is detected (PIR), the servo should open the door (180 degrees).
#include <ESP32Servo.h>
Servo door;
void setup() {
pinMode(13, OUTPUT);
door.attach(12);
}
void loop() {
int pir = digitalRead(12);
if (pir == LOW) {
door.write(180);
} else {
door.write(0);
}
return;
delay(100);
}
No hay comentarios:
Publicar un comentario