// Reference https://docs.wokwi.com/parts/wokwi-ntc-temperature-sensor
#define sensorPin A0
#define BETA 3950
int ledArray[] = {4,5,6,7,8,9,10,11,12,13};
void setup() {
// initialize LEDs and sensor pin
pinMode(sensorPin, INPUT);
for (int i = 0; i < 10; i++) {
pinMode(ledArray[i], OUTPUT);
}
}
void loop() {
// Read the sensor value , calculate the temperature, light up the leds
int analogValue = analogRead(sensorPin);
float celsius = 1 / (log(1 / (1023.0 / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
int ledLevel = (int)((celsius + 24) * 10 / 104.0);
if (ledLevel < 0) {
ledLevel = 0;
}
if (ledLevel > 10) {
ledLevel = 10;
}
for (int i = 0; i < 10; i++) {
if (i < ledLevel) {
digitalWrite(ledArray[9-i], HIGH);//took 9-i since it also codes the color of the sensor with the corresponding temp since cold implies blue and hot implies red
} else {
digitalWrite(ledArray[9-i], LOW);
}
}
delay(10); // this speeds up the simulation
}
Loading
st-nucleo-c031c6
st-nucleo-c031c6