const int PIN_NTC = 34;
const int PIN_LED = 2;
QueueHandle_t antreanSuhu;
unsigned long terakhirKedip = 0;
bool ledNyala = false;
float bacaSuhu() {
int adc = analogRead(PIN_NTC);
float v = adc * 3.3 / 4095.0;
float r = 10000.0 * v / (3.3 - v);
float suhu = 1.0 / (1.0 / 298.15 + log(r / 10000.0) / 3950.0) - 273.15;
return suhu;
}
void tugasSensor(void* parameter) {
for (;;) {
float suhu = bacaSuhu();
xQueueSend(antreanSuhu, &suhu, portMAX_DELAY);
vTaskDelay(pdMS_TO_TICKS(2000));
}
}
void setup() {
Serial.begin(115200);
pinMode(PIN_LED, OUTPUT);
antreanSuhu = xQueueCreate(5, sizeof(float));
xTaskCreatePinnedToCore(tugasSensor, "sensor", 4096, NULL, 1, NULL, 1);
Serial.println("Tugas sensor dibuat.");
}
void loop() {
float suhu;
if (xQueueReceive(antreanSuhu, &suhu, 0) == pdTRUE) {
Serial.printf("Diterima dari antrean: %.1f C\n", suhu);
}
if (millis() - terakhirKedip >= 250) {
terakhirKedip = millis();
ledNyala = !ledNyala;
digitalWrite(PIN_LED, ledNyala);
}
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4