Cant build zephyr battery example

Hi,

I was trying to build the zephyr battery example Battery Voltage Measurement — Zephyr Project Documentation (nordicsemi.com) . But when I was building the project, it raises an error like this:

note: in expansion of macro 'DEVICE_DT_GET'

78 | .adc = DEVICE_DT_GET(DT_IO_CHANNELS_CTLR(ZEPHYR_USER)),

What should I do? Or is there any way to read/calculate/measure the input voltage to a pin?

Parents Reply
  • I tried to build relative to instructions, but unfortunately I have no such "west" command. 

    So, I have an alternative code that measures the voltage. But this code also does not work (I couldnt build). What should I do?

    #include <stdio.h>
    #include <nrf.h>
    #include <nrfx_saadc.h>
    #include <nrfx.h>
    int main(void)
    {
        // Configure the ADC to use the 0.13 pin as the input channel
        NRF_SAADC->CH[0].PSELP = 13; // Pin 0.13
        NRF_SAADC->CH[0].PSELN = 0xFFFFFFFF; // Not using negative input
    
        // Configure the ADC's reference voltage
        NRF_SAADC->REF = SAADC_REF_INTERNAL;
    
        // Start the ADC conversion
        NRF_SAADC->TASKS_START = 1;
    
        // Wait for the ADC conversion to complete
        while (NRF_SAADC->EVENTS_END == 0) {}
    
        // Read the ADC result
        uint16_t result = NRF_SAADC->RESULT.RESULT;
    
        // Convert the ADC result to a voltage value
        float voltage = (float)result / (1 << 12) * 1.2;
    
        // Convert the voltage value to a battery level
        float battery_level = voltage / 4.0 * 100.0;
    
        // Print the battery level to the console
        printf("Battery level: %.2f%%\n", battery_level);
    
        return 0;
    }
    

Children
No Data
Related