tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/mcp3008/main.go (about)

     1  // Connects to a MCP3008 ADC via SPI.
     2  package main
     3  
     4  import (
     5  	"machine"
     6  	"time"
     7  
     8  	"tinygo.org/x/drivers/mcp3008"
     9  )
    10  
    11  var (
    12  	spi   = machine.SPI0
    13  	csPin = machine.D12
    14  )
    15  
    16  func main() {
    17  	spi.Configure(machine.SPIConfig{
    18  		Frequency: 4000000,
    19  		Mode:      3})
    20  
    21  	adc := mcp3008.New(spi, csPin)
    22  	adc.Configure()
    23  
    24  	// get "CH0" aka "machine.ADC" interface to channel 0 from ADC.
    25  	p := adc.CH0
    26  
    27  	for {
    28  		val := p.Get()
    29  		println(val)
    30  		time.Sleep(50 * time.Millisecond)
    31  	}
    32  }