const int temperaturePin = A0; // Analog pin to which temperature sensor is connected
const int buzzerPin = 8; // Pin connected to the buzzer
const int ledPin = 9; // Pin connected to the LED
void setup() {
Serial.begin(9600);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
// Read the analog input from the temperature sensor
int sensorValue = analogRead(temperaturePin);
// Convert the sensor value to temperature in degrees Celsius
float temperatureCelsius = (sensorValue * 0.00488) * 100; // LM35 output is 10 mV/°C
// Print the temperature to serial monitor
Serial.print("Temperature: ");
Serial.print(temperatureCelsius);
Serial.println(" °C");
// Check if temperature is greater than 50°C
if (temperatureCelsius > 50) {
// Activate buzzer and LED
digitalWrite(buzzerPin, HIGH);
digitalWrite(ledPin, HIGH);
} else {
if (temperatureCelsius < 50)
// Deactivate buzzer and LED
digitalWrite(buzzerPin, LOW);
digitalWrite(ledPin, LOW);
}
delay(1000); // Delay for stability
}
Loading
st-nucleo-c031c6
st-nucleo-c031c6