light-led.py 503 B

123456789101112131415161718
  1. import time
  2. from machine import ADC,Pin
  3. adc = ADC(Pin(34), atten=ADC.ATTN_11DB) # create an ADC object acting on a pin
  4. led = Pin(18, Pin.OUT)
  5. def ctrl_led(current):
  6. if current > 30000:
  7. led.off()
  8. if current < 27000:
  9. led.on()
  10. while True:
  11. val = adc.read_u16() # read a raw analog value in the range 0-65535
  12. ctrl_led(val)
  13. wdat = 'val:{}'.format(val)
  14. print(wdat, end='\x08' * len(wdat))
  15. time.sleep(0.03)
  16. print(' ' * len(wdat), end='\x08' * len(wdat))