To code a temperature sensor and LED for an Arduino Uno, begin by connecting the temperature sensor (like the LM35) to an analog pin (e.g., A0) and the LED to a digital pin (e.g., pin 9). In the Arduino IDE, use the analogRead() function to read the temperature sensor's output, convert the analog value to Celsius, and then use digitalWrite() to turn the LED on or off based on a temperature threshold. Here's a basic example:
<code class="language-cpp">const int tempPin = A0;const int ledPin = 9; void setup() { pinMode(ledPin, OUTPUT); Serial.begin(9600); } void loop() { float temp = analogRead(tempPin) * 0.488; // Convert to Celsius if (temp > 25) { // Threshold digitalWrite(ledPin, HIGH); // Turn on LED } else { digitalWrite(ledPin, LOW); // Turn off LED } Serial.println(temp); delay(1000); }
</code>
Make sure to adjust the threshold and conversion based on your specific sensor.
Copyright © 2026 eLLeNow.com All Rights Reserved.