tinygo-org / tinygo

Go compiler for small places. Microcontrollers, WebAssembly (WASM/WASI), and command-line tools. Based on LLVM.
https://tinygo.org
Other
15.49k stars 913 forks source link

ADC with Raspberry Pico returns 16-bits value instead of 12-bits #4595

Closed mbe81 closed 1 week ago

mbe81 commented 1 week ago

According to the datasheet for the Raspberry Pi Pico the ADC has a resolution of 12-bits. However, if I read the value via adc.Get() I get a value of of 16-bits.

func main() {
    machine.InitADC()
    adc := machine.ADC{Pin: machine.ADC0}
    err := adc.Configure(machine.ADCConfig{})
    if err != nil {
        log.Fatalf("Error configuring ADC input: %v", err)
    }
    fmt.Println(adc.Get()) // Gives 16-bit value
}

This is in conflict with the documentation of ADCConfig:

// ADCConfig holds ADC configuration parameters. If left unspecified, the zero
// value of each parameter will use the peripheral's default settings.

Or do I miss something? Where can I find the peripheral's default settings?

aykevl commented 1 week ago

This is working as intended. The idea is that code can assume that the ADC returns a 16-bit value in all cases, no matter the underlying resolution of the ADC. This makes code more portable across chips. If you specifically want 12 bits, you can of course shift the result.

I agree this should be better documented.

mbe81 commented 1 week ago

Thanks for your response! I will close this issue.