// Reference https://docs.wokwi.com/parts/wokwi-ntc-temperature-sensor
// Constants
const int sensorPin = A0; // Analog input pin for temperature sensor
const int numLeds = 10; // Number of LEDs in the array
const int ledPins[numLeds] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; // Digital output pins for LEDs
void setup() {
// Initialize LED pins as outputs
for (int i = 0; i < numLeds; i++) {
pinMode(ledPins[i], OUTPUT);
}
}
void loop() {
// Read analog input from temperature sensor
int sensorValue = analogRead(sensorPin);
// Convert analog reading to temperature in Celsius
float voltage = sensorValue * (5.0 / 1023.0);
float resistance = (5.0 * 10000.0 / voltage) - 10000.0;
float temperature = 1.0 / (log(resistance / 10000.0) / 3977.0 + (1.0 / 298.15)) - 273.15;
// Map temperature to LED levels
int level = map(temperature, -24, 80, 0, numLeds);
// Illuminate LEDs according to the mapped level
for (int i = 0; i < numLeds; i++) {
digitalWrite(ledPins[i], i < level ? HIGH : LOW);
}
}