Advertisement
belrey10

3 – Pico Night Light

Nov 5th, 2024 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.73 KB | Source Code | 0 0
  1. import machine
  2. import time
  3.  
  4. # Define LED Pin
  5. LED_PIN = 17
  6. led = machine.Pin(LED_PIN, machine.Pin.OUT)
  7.  
  8. # Define ADC Pin for LDR
  9. ADC_PIN = 26
  10. adc = machine.ADC(machine.Pin(ADC_PIN))
  11.  
  12. # Define a threshold for the LDR reading below which the LED should turn ON
  13. LIGHT_THRESHOLD = 50000  # This might need adjustment based on your LDR and environment
  14.  
  15. def night_light():
  16.     while True:
  17.         light_value = adc.read_u16()
  18.         print(light_value)
  19.         # If the reading is below the threshold, turn the LED on. Otherwise, turn it off.
  20.         if light_value < LIGHT_THRESHOLD:
  21.             led.value(1)
  22.         else:
  23.             led.value(0)
  24.        
  25.         time.sleep(0.5)  # Check every half second
  26.  
  27. night_light()
  28.  
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement