//push button replace pressure sensor
const int b = 33;
//thermistor
const int t = 26; // Analog pin connected to the NTC thermistor
const int resistorValue = 10000; // Resistance value of the resistor in ohms
const float referenceVoltage = 3.3; // Reference voltage for ESP32 in volts
//motion sensor
const int m = 25;
//led
const int red = 18;
const int green = 5;
int b_val = 0;
int m_val = 0;
void setup() {
Serial.begin(115200);
pinMode(b, INPUT);
pinMode(t, INPUT);
pinMode(m, INPUT);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
}
void loop() {
// Read the analog voltage across the NTC thermistor
int sensorValue = analogRead(t);
// Convert the analog reading to voltage
float voltage = sensorValue * (referenceVoltage / 4095.0);
// Calculate the resistance of the NTC thermistor using the voltage divider formula
float thermistorResistance = (resistorValue * voltage) / (referenceVoltage - voltage);
// Use the Steinhart-Hart equation or a lookup table to convert resistance to temperature
float temperature = calculateTemperature(thermistorResistance);
Serial.print("Temperature: ");
Serial.print(temperature, 2); // Print temperature with 2 decimal places
Serial.println(" °C");
b_val = digitalRead(b);
m_val = digitalRead(m);
Serial.print("Motion: ");
Serial.println(m_val);
Serial.print("Pressure: ");
Serial.println(b_val);
if(b_val == HIGH && temperature < 20){
digitalWrite(red, HIGH);
}
else{
digitalWrite(red, LOW);
}
if(b_val == HIGH && m_val == LOW){
digitalWrite(green, HIGH);
}
else{
digitalWrite(green, LOW);
}
// Delay for a moment before taking the next reading
delay(1500);
}
// Function to calculate temperature using the Steinhart-Hart equation
float calculateTemperature(float resistance) {
const float A = 0.001129148;
const float B = 0.000234125;
const float C = 0.0000000876741;
// Steinhart-Hart equation
float temperatureKelvin = 1.0 / (A + B * log(resistance) + C * pow(log(resistance), 3));
float temperatureCelsius = temperatureKelvin - 273.15;
return temperatureCelsius;
}