adc-current-monitor.ino

BeagleConnect QuadESC: 4-Channel BLDC Controller · File : adc-current-monitor.ino · Language : C · Use case : Lire la tension d'une phase moteur via ADC pour surveiller le courant.

Periodic ADC channel reading to estimate motor phase voltage.

Configures a specific ADC channel. Example assumes 12-bit resolution and 3.3V reference. Adjust scaling factor based on current shunt and op-amp. Use west build and flash to board.

adc-current-monitor.ino

#include <zephyr/kernel.h>
#include <zephyr/drivers/adc.h>

#define ADC_NODE DT_ALIAS(adc0)
#define ADC_CHANNEL 0
static const struct adc_dt_spec adc_channel = ADC_DT_SPEC_CHANNEL_GET(ADC_NODE, ADC_CHANNEL);

void main(void) {
    int16_t buf;
    struct adc_sequence sequence = { .channels = 1, .buffer = &buf, .buffer_size = sizeof(buf) };
    if (adc_channel_setup_dt(&adc_channel)) return;
    while (1) {
        if (!adc_read_dt(&adc_channel, &sequence)) {
            float voltage = buf * 3.3f / 4095.0f; // Exemple 12-bit
            printk("Phase Voltage: %.3f V\n", voltage);
        }
        k_msleep(100);
    }
}

← Back to the board