This circuit is designed to control two LEDs (one red and one green) using a Raspberry Pi 3B based on temperature readings from a temperature sensor. The red LED is connected to GPIO14 and is controlled by the temperature: it turns on when the temperature is equal to or above 60°F and turns off when below this threshold. The green LED is connected to GPIO15 but does not have a control mechanism defined in the provided code. Both LEDs are connected to ground through individual resistors. The temperature sensor is powered by the 3.3V pin on the Raspberry Pi and communicates via GPIO4.
"""
This Python script controls an LED connected to GPIO14 of a Raspberry Pi 3B.
The LED turns on when the onboard temperature is >= 60°F and turns off when
the temperature is < 60°F.
"""
import RPi.GPIO as GPIO
import time
ledPin = 14 # GPIO14
thresholdTempF = 60.0 # Temperature threshold in Fahrenheit
def setup():
GPIO.setmode(GPIO.BCM)
GPIO.setup(ledPin, GPIO.OUT)
def getTemperature():
# Placeholder function to simulate temperature reading
# Replace with actual temperature sensor code
return 32.0 # Return a dummy temperature in Celsius
def loop():
while True:
tempC = getTemperature() # Get the temperature in Celsius
tempF = (tempC * 9.0 / 5.0) + 32.0 # Convert to Fahrenheit
if tempF >= thresholdTempF:
GPIO.output(ledPin, GPIO.HIGH) # Turn on the LED
else:
GPIO.output(ledPin, GPIO.LOW) # Turn off the LED
time.sleep(1) # Wait for 1 second before checking again
def destroy():
GPIO.cleanup() # Release all GPIO
if __name__ == '__main__':
try:
setup()
loop()
except KeyboardInterrupt:
destroy()
Filename: sketch.ino
Description: The code sets up the Raspberry Pi GPIO pins and enters a loop where it continuously checks the temperature. If the temperature is above the threshold, it turns on the red LED; otherwise, it turns it off. The temperature reading function is a placeholder and should be replaced with actual sensor reading code.