// Define pins for PIR sensor and LDR
#define PIR_PIN PA1
#define LDR_ANALOG_PIN A1
#define LDR_DIGITAL_PIN PB0
void setup() {
// Initialize serial communication at 9600 baud rate
Serial.begin(9600);
// Initialize analog input pin for TMP36 sensor (PA0)
pinMode(A0, INPUT);
// Initialize PIR sensor pin (PA1)
pinMode(PIR_PIN, INPUT);
// Initialize analog input pin for LDR sensor (A1)
pinMode(LDR_ANALOG_PIN, INPUT);
// Initialize digital input pin for LDR (D0)
pinMode(LDR_DIGITAL_PIN, INPUT);
}
void loop() {
// Read analog value from TMP36 sensor connected to A0
int sensorValue = analogRead(A0);
// Convert analog value to voltage (STM32 operates at 3.3V)
float voltage = sensorValue * (3.3 / 1023.0);
// Convert voltage to temperature (for TMP36 sensor)
float temperature = (voltage - 0.5) * 100.0;
// Read PIR sensor value
int pirState = digitalRead(PIR_PIN);
// Read LDR analog value from A1
int ldrAnalogValue = analogRead(LDR_ANALOG_PIN);
// Read LDR digital value from D0
int ldrDigitalValue = digitalRead(LDR_DIGITAL_PIN);
// Print temperature to the UART (Serial monitor)
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
// Print LDR analog value (light intensity)
Serial.print("LDR Analog Value: ");
Serial.println(ldrAnalogValue);
// Print LDR digital value (threshold detection)
Serial.print("LDR Digital Value: ");
if (ldrDigitalValue == HIGH) {
Serial.println("Light Detected");
} else {
Serial.println("No Light Detected");
}
// Check PIR sensor state and print motion detection status
if (pirState == HIGH) {
Serial.println("Motion Detected!");
} else {
Serial.println("No Motion");
}
// Delay for a while before reading the next value
delay(1000); // 1 second delay
}
Loading
stm32-bluepill
stm32-bluepill