Hello. For my project, I would like to make a heat pad that turns on automatically by sensing a presence, and when it reaches a specific temperature, the heat pad will turn off. For this project, I'm using an Arduino Uno, a DHT 11 for the temperature sensor, an i2c display (so it can display the temperature), a relay, and an HC-SR04 ultrasonic sensor (for automation purposes).
The condition wherein it will turn off after reaching a specific temperature works fine, but the ultrasonic sensor part doesn't work and I'm kind of stuck.
The step-by-step of what my project should be like:
- The ultrasonic sensor will sense a presence, and it will turn the whole system on
- The DHT11 will sense if the temperature is below or above 40 degrees Celsius. If it's below, the heat pad will turn on, else, it will turn off.
- When the presence is gone, the whole system will also turn off.
Here's the schematic diagram:
Here's the coding:
#include <Wire.h>
#include <DHT.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define DHTPIN 7
#define DHTTYPE DHT11
int relay1 = 3;
int relay2 = 4;
int temp = 7;
int dist = 9;
const int trigPin = 8;
const int echoPin = 9;
DHT dht(DHTPIN, DHTTYPE);
float duration, distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
dht.begin();
lcd.init();
lcd.clear();
lcd.backlight();
}
void loop() {
one();
two();
three();
delay(2000);
}
void one() {
if (dist > 50.0) {
digitalWrite(relay1, HIGH);
}
else {
digitalWrite(relay1, LOW);
}
}
void two() {
float dhtHumidity = dht.readHumidity();
float dhtTemperature = dht.readTemperature();
delay(1000);
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.print(dhtTemperature);
lcd.print(" C");
lcd.setCursor(0,1);
lcd.print("Humid: ");
lcd.print(dhtHumidity);
lcd.print(" H");
}
void three() {
temp = dht.readTemperature();
if (temp < 40) {
digitalWrite(relay1, LOW);
}
else {
digitalWrite(relay1, HIGH);
}
}
void four() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration*.0343)/2;
delay(100);
}
Any replies will be highly appreciated!
Note: I'm a high school STEM student who doesn't have any background in coding. I'm just doing this for a capstone project