#include <DHT.h> // Include DHT sensor library
#include <DHT_U.h> // Include Adafruit Unified Sensor library (DHT dependency)
// --- Define Pins ---
#define LIGHT_LED_PIN PB14 // Green LED for light control connected to PB14
#define MOTOR_RELAY_PIN PB15 // Relay for motor/fan control connected to PB15
#define DHT22_DATA_PIN PA1 // DHT22 data pin connected to PA1
#define LDR_ANALOG_PIN PA0 // LDR module Analog Output connected to PA0
// --- Sensor Type & Thresholds ---
#define DHTTYPE DHT22 // Define sensor type (DHT11, DHT21, DHT22)
// Set your desired thresholds
const int LIGHT_THRESHOLD = 500; // Analog value (0-1023). Adjust based on your LDR and ambient light. Lower value = darker.
const float TEMPERATURE_THRESHOLD = 28.0; // Celsius. Adjust as needed. (28.0 C is approx 82.4 F)
// --- Initialize Sensors ---
DHT dht(DHT22_DATA_PIN, DHTTYPE);
void setup() {
// Initialize Serial Communication for debugging
Serial.begin(115200); // Higher baud rate for faster output
while (!Serial); // Wait for Serial port to connect (only for some boards/debuggers)
Serial.println("STM32 Automated System Starting...");
// Set LED pin as OUTPUT
pinMode(LIGHT_LED_PIN, OUTPUT);
digitalWrite(LIGHT_LED_PIN, LOW); // Ensure LED is off initially
// Set Motor Relay pin as OUTPUT
pinMode(MOTOR_RELAY_PIN, OUTPUT);
digitalWrite(MOTOR_RELAY_PIN, LOW); // Ensure motor/relay is off initially (or whatever your module's "off" state is)
// Start DHT sensor
dht.begin();
Serial.println("DHT22 Sensor initialized.");
Serial.println("LDR Sensor initialized."); // LDR uses analogRead, no explicit begin
}
void loop() {
// --- Read Light Intensity (LDR) ---
int lightIntensity = analogRead(LDR_ANALOG_PIN);
Serial.print("Light Intensity (LDR): ");
Serial.println(lightIntensity);
// Decision for Light Intensity
if (lightIntensity < LIGHT_THRESHOLD) {
// It's dark, turn on LED
digitalWrite(LIGHT_LED_PIN, HIGH);
Serial.println(" --> Light below threshold. Light LED (PB14) ON.");
} else {
// It's bright enough, turn off LED
digitalWrite(LIGHT_LED_PIN, LOW);
Serial.println(" --> Light above threshold. Light LED (PB14) OFF.");
}
// --- Read Temperature (DHT22) ---
// Read temperature as Celsius (float)
float temperature = dht.readTemperature();
// Check if any reads failed and exit early (to try again next loop)
if (isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
} else {
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");
// Decision for Temperature
if (temperature > TEMPERATURE_THRESHOLD) {
// Temperature is too high, turn on Motor/Relay
digitalWrite(MOTOR_RELAY_PIN, HIGH); // Assuming HIGH activates your relay
Serial.println(" --> Temperature above threshold. Motor/Fan (PB15) ON.");
} else {
// Temperature is okay, turn off Motor/Relay
digitalWrite(MOTOR_RELAY_PIN, LOW); // Assuming LOW deactivates your relay
Serial.println(" --> Temperature below threshold. Motor/Fan (PB15) OFF.");
}
}
// Add a small delay before the next loop iteration
// This is important for sensor stability and not to overwhelm the serial monitor.
delay(2000); // Wait for 2 seconds
}Loading
stm32-bluepill
stm32-bluepill