// Reference https://docs.wokwi.com/parts/wokwi-ntc-temperature-sensor
#define sensorPin A0
#define BETA 3950
int ledPins[] = {4,5,6,7,8,9,10,11,12,13};
// Define temperature range and LED levels
const float minTemp = -24.0; // Minimum temperature in Celsius
const float maxTemp = 80.0; // Maximum temperature in Celsius
const int numLeds = 10; // Total number of LEDs
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize all LED pins as outputs
for (int i = 0; i < numLeds; i++) {
pinMode(ledPins[i], OUTPUT);
}
}
void loop() {
// Read the analog value from the temperature sensor
int sensorValue = analogRead(A0);
// Convert the sensor value to a temperature in Celsius
// Assuming a 10-bit ADC (0-1023) and a reference voltage of 5V
float voltage = sensorValue * (5.0 / 1023.0);
// Convert voltage to temperature in Celsius (example formula, adjust based on your sensor)
float temperature = (voltage - 0.5) * 100.0; // Example formula
// Ensure the temperature is within the valid range
temperature = constrain(temperature, minTemp, maxTemp);
// Calculate the number of LEDs to light up
int numLedsOn = map(temperature, minTemp, maxTemp, 0, numLeds);
// Light up the corresponding number of LEDs
for (int i = 0; i < numLeds; i++) {
if (i < numLedsOn) {
digitalWrite(ledPins[i], HIGH);
} else {
digitalWrite(ledPins[i], LOW);
}
}
// Print temperature for debugging
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
// Wait a bit before taking the next reading
delay(10);
}
Loading
st-nucleo-c031c6
st-nucleo-c031c6