github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/examples/adc/adc.go (about) 1 package main 2 3 import ( 4 "machine" 5 "time" 6 ) 7 8 // This example assumes that an analog sensor such as a rotary dial is connected to pin ADC0. 9 // When the dial is turned past the midway point, the built-in LED will light up. 10 11 func main() { 12 machine.InitADC() 13 14 led := machine.LED 15 led.Configure(machine.PinConfig{Mode: machine.PinOutput}) 16 17 sensor := machine.ADC{machine.ADC2} 18 sensor.Configure(machine.ADCConfig{}) 19 20 for { 21 val := sensor.Get() 22 if val < 0x8000 { 23 led.Low() 24 } else { 25 led.High() 26 } 27 time.Sleep(time.Millisecond * 100) 28 } 29 }