"""
JETSON NANO - 1. LED BLINK
WIRING:
Pin 11 -> resistor (220 ohm) -> LED long leg
LED short leg -> GND (pin 6)
"""
import Jetson.GPIO as GPIO
from time import sleep
LED_PIN = 11
GPIO.setmode(GPIO.BOARD)
GPIO.setup(LED_PIN, GPIO.OUT, initial=GPIO.LOW)
try:
while True:
GPIO.output(LED_PIN, GPIO.HIGH)
print("LED ON")
sleep(1)
GPIO.output(LED_PIN, GPIO.LOW)
print("LED OFF")
sleep(1)
except KeyboardInterrupt:
print("\nStopping...")
finally:
GPIO.output(LED_PIN, GPIO.LOW)
GPIO.cleanup()
print("LED OFF - Program stopped.")"""
JETSON NANO - 2. LED + BUTTON + EMAIL
WIRING:
LED : pin 11 -> resistor (220 ohm) -> LED -> GND
BUTTON : one leg -> pin 13, other leg -> GND
PULL-UP RESISTOR (REQUIRED): pin 13 -> resistor -> 3.3V (pin 1)
The Jetson Nano IGNORES the software pull-up setting, so the pull-up
must be a real resistor. Without it the pin floats and the LED will
toggle/blink by itself.
"""
import Jetson.GPIO as GPIO
from time import sleep
from threading import Thread
import smtplib
from email.message import EmailMessage
LED_PIN = 11
BUTTON_PIN = 13
EMAIL = "yourgmail@gmail.com"
APP_PASSWORD = "YOUR_APP_PASSWORD"
GPIO.setmode(GPIO.BOARD)
GPIO.setup(LED_PIN, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(BUTTON_PIN, GPIO.IN) # external pull-up resistor does the work
led_state = False
def send_email(subject, message):
email = EmailMessage()
email["From"] = EMAIL
email["To"] = EMAIL
email["Subject"] = subject
email.set_content(message)
try:
with smtplib.SMTP_SSL("smtp.gmail.com", 465, timeout=10) as smtp:
smtp.login(EMAIL, APP_PASSWORD)
smtp.send_message(email)
print("Email sent!")
except Exception as e:
print("Email failed:", e)
def send_email_async(subject, message):
# Runs in the background so the program never freezes while emailing
Thread(target=send_email, args=(subject, message), daemon=True).start()
def toggle_led(channel):
global led_state
led_state = not led_state
if led_state:
GPIO.output(LED_PIN, GPIO.HIGH)
print("LED ON")
send_email_async(
"Jetson Nano - LED ON",
"The LED was turned ON using the button."
)
else:
GPIO.output(LED_PIN, GPIO.LOW)
print("LED OFF")
send_email_async(
"Jetson Nano - LED OFF",
"The LED was turned OFF using the button."
)
GPIO.add_event_detect(
BUTTON_PIN,
GPIO.FALLING,
callback=toggle_led,
bouncetime=200
)
print("LED + Button + Email")
print("Press the button to toggle the LED.")
print("Press Ctrl+C to stop.")
try:
while True:
sleep(1)
except KeyboardInterrupt:
print("\nStopping...")
finally:
GPIO.output(LED_PIN, GPIO.LOW)
GPIO.cleanup()
print("Program stopped safely.")"""
JETSON NANO - 4. LED + LDR + EMAIL
WIRING:
LED : pin 11 -> resistor (220 ohm) -> LED -> GND
LDR module : VCC -> 3.3V (pin 1), GND -> GND, OUT -> pin 12
LDR output: 1 = DARK (night), 0 = BRIGHT (day)
"""
import Jetson.GPIO as GPIO
from time import sleep
from threading import Thread
import smtplib
from email.message import EmailMessage
LED_PIN = 11
LDR_PIN = 12
EMAIL = "yourgmail@gmail.com"
APP_PASSWORD = "YOUR_APP_PASSWORD"
# Reading must stay the same for 2 seconds (20 x 0.1 s) before we act.
# This also stops a flickering sensor from flooding your inbox.
STABLE_READS = 20
GPIO.setmode(GPIO.BOARD)
GPIO.setup(LED_PIN, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(LDR_PIN, GPIO.IN)
def send_email(subject, message):
email = EmailMessage()
email["From"] = EMAIL
email["To"] = EMAIL
email["Subject"] = subject
email.set_content(message)
try:
with smtplib.SMTP_SSL("smtp.gmail.com", 465, timeout=10) as smtp:
smtp.login(EMAIL, APP_PASSWORD)
smtp.send_message(email)
print("Email sent!")
except Exception as e:
print("Email failed:", e)
def send_email_async(subject, message):
Thread(target=send_email, args=(subject, message), daemon=True).start()
confirmed_state = None
candidate = None
stable_count = 0
print("LED + LDR + Email")
print("Press Ctrl+C to stop.")
try:
while True:
reading = GPIO.input(LDR_PIN)
if reading == candidate:
stable_count += 1
else:
candidate = reading
stable_count = 1
if stable_count >= STABLE_READS and candidate != confirmed_state:
confirmed_state = candidate
if confirmed_state == GPIO.HIGH:
print("Night detected -> LED ON")
GPIO.output(LED_PIN, GPIO.HIGH)
send_email_async(
"Jetson Nano - Night Detected",
"The LDR detected darkness. LED turned ON."
)
else:
print("Day detected -> LED OFF")
GPIO.output(LED_PIN, GPIO.LOW)
send_email_async(
"Jetson Nano - Day Detected",
"The LDR detected daylight. LED turned OFF."
)
sleep(0.1)
except KeyboardInterrupt:
print("\nStopping...")
finally:
GPIO.output(LED_PIN, GPIO.LOW)
GPIO.cleanup()
print("Program stopped safely.")"""
JETSON NANO - 5. PIR MOTION SENSOR
WIRING:
PIR : VCC -> 5V (pin 2), GND -> GND, OUT -> pin 16
(PIR output is 3.3V, safe for the Jetson)
"""
import Jetson.GPIO as GPIO
from time import sleep
PIR_PIN = 16
# PIR sensors give false triggers right after power-on.
# Lower this number if you are short on time.
WARMUP_SECONDS = 30
GPIO.setmode(GPIO.BOARD)
GPIO.setup(PIR_PIN, GPIO.IN)
previous_state = GPIO.LOW
print("PIR Motion Sensor")
print("Press Ctrl+C to stop.")
try:
print(f"Warming up for {WARMUP_SECONDS} seconds - keep still...")
for remaining in range(WARMUP_SECONDS, 0, -1):
print(f" {remaining}...", end="\r", flush=True)
sleep(1)
print("\nReady. Waiting for motion...")
while True:
current_state = GPIO.input(PIR_PIN)
if current_state == GPIO.HIGH and previous_state == GPIO.LOW:
print("Motion detected!")
elif current_state == GPIO.LOW and previous_state == GPIO.HIGH:
print("Motion stopped.")
previous_state = current_state
sleep(0.1)
except KeyboardInterrupt:
print("\nStopping...")
finally:
GPIO.cleanup()
print("PIR program stopped.")"""
JETSON NANO - 6. HOME AUTOMATION (IOT)
WIRING:
LED : pin 11 -> resistor (220 ohm) -> LED -> GND
BUTTON : one leg -> pin 13, other leg -> GND
+ PULL-UP RESISTOR (REQUIRED): pin 13 -> resistor -> 3.3V (pin 1)
(Jetson Nano ignores the software pull-up setting)
LDR module : VCC -> 3.3V, GND -> GND, OUT -> pin 12 (1 = night, 0 = day)
PIR : VCC -> 5V (pin 2), GND -> GND, OUT -> pin 16
LOGIC:
NIGHT starts : LED ON, PIR armed
DAY starts : LED OFF, PIR ignored
Button : toggles LED any time, sends email
Motion (night only) : sends email (max one every 30 seconds)
"""
import Jetson.GPIO as GPIO
from time import sleep, time
from threading import Thread
import smtplib
from email.message import EmailMessage
# ==========================================
# SETTINGS
# ==========================================
LED_PIN = 11
BUTTON_PIN = 13
LDR_PIN = 12
PIR_PIN = 16
EMAIL = "yourgmail@gmail.com"
APP_PASSWORD = "YOUR_APP_PASSWORD"
LOOP_DELAY = 0.1 # main loop speed (seconds)
STABLE_READS = 20 # LDR must hold steady 20 x 0.1 s = 2 seconds
ALERT_COOLDOWN = 30 # seconds between motion emails
PIR_WARMUP_SECONDS = 30 # ignore PIR right after start-up (false triggers)
# ==========================================
# GPIO SETUP
# ==========================================
GPIO.setmode(GPIO.BOARD)
GPIO.setup(LED_PIN, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(BUTTON_PIN, GPIO.IN) # external pull-up resistor does the work
GPIO.setup(LDR_PIN, GPIO.IN)
GPIO.setup(PIR_PIN, GPIO.IN)
# ==========================================
# EMAIL (runs in background, 10 s timeout)
# ==========================================
def send_email(subject, message):
email = EmailMessage()
email["From"] = EMAIL
email["To"] = EMAIL
email["Subject"] = subject
email.set_content(message)
try:
with smtplib.SMTP_SSL("smtp.gmail.com", 465, timeout=10) as smtp:
smtp.login(EMAIL, APP_PASSWORD)
smtp.send_message(email)
print("Email sent!")
except Exception as e:
print("Email failed:", e)
def send_email_async(subject, message):
Thread(target=send_email, args=(subject, message), daemon=True).start()
# ==========================================
# SYSTEM STATE
# ==========================================
is_night = False
led_state = False
# ==========================================
# BUTTON CONTROL
# ==========================================
def button_pressed(channel):
global led_state
led_state = not led_state
if led_state:
GPIO.output(LED_PIN, GPIO.HIGH)
print("Button -> LED ON")
send_email_async(
"Jetson Nano - LED ON",
"The LED was turned ON using the button."
)
else:
GPIO.output(LED_PIN, GPIO.LOW)
print("Button -> LED OFF")
send_email_async(
"Jetson Nano - LED OFF",
"The LED was turned OFF using the button."
)
GPIO.add_event_detect(
BUTTON_PIN,
GPIO.FALLING,
callback=button_pressed,
bouncetime=200
)
# ==========================================
# STARTUP
# ==========================================
print("======================================")
print(" JETSON NANO HOME AUTOMATION")
print("======================================")
print()
print("LDR: 0 = DAY, 1 = NIGHT")
print()
print("DAY : LED OFF, button can toggle LED")
print("NIGHT: LED ON, button can toggle LED")
print(" PIR ACTIVE (intrusion email alert)")
print()
print(f"PIR warms up for {PIR_WARMUP_SECONDS} seconds - keep away from it.")
print("Press Ctrl+C to stop.")
print("======================================")
# ==========================================
# MAIN LOOP
# ==========================================
candidate = None
stable_count = 0
last_alert = 0
prev_motion = GPIO.LOW
pir_ready_at = time() + PIR_WARMUP_SECONDS
try:
while True:
# ---------- LDR (with stability filter) ----------
light = GPIO.input(LDR_PIN)
if light == candidate:
stable_count += 1
else:
candidate = light
stable_count = 1
if stable_count >= STABLE_READS:
# NIGHT (LDR = 1)
if candidate == GPIO.HIGH and not is_night:
is_night = True
led_state = True
GPIO.output(LED_PIN, GPIO.HIGH)
print("\nNIGHT DETECTED -> LED ON, PIR ACTIVE")
# DAY (LDR = 0)
elif candidate == GPIO.LOW and is_night:
is_night = False
led_state = False
GPIO.output(LED_PIN, GPIO.LOW)
print("\nDAY DETECTED -> LED OFF, PIR IGNORED")
# ---------- PIR (night only, never blocks) ----------
if is_night:
motion = GPIO.input(PIR_PIN)
if time() >= pir_ready_at:
if motion == GPIO.HIGH and prev_motion == GPIO.LOW:
print("Motion detected!")
if time() - last_alert > ALERT_COOLDOWN:
last_alert = time()
send_email_async(
"Jetson Nano - Motion Detected",
"Motion has been detected during nighttime."
)
elif motion == GPIO.LOW and prev_motion == GPIO.HIGH:
print("Motion stopped.")
prev_motion = motion
else:
prev_motion = GPIO.LOW
sleep(LOOP_DELAY)
except KeyboardInterrupt:
print("\n\nStopping system...")
finally:
GPIO.output(LED_PIN, GPIO.LOW)
GPIO.cleanup()
print("LED OFF")
print("GPIO cleaned up")
print("Program stopped safely.")