Pin is not configured as analog channel

I’m working on getting InlineCurrentSense working on esp32-s3-devkitc-1 and a SimpleFOCShield v2.0.4.

Here’s the constructor:

InlineCurrentSense current_sense = InlineCurrentSense(0.01, 50.0f, (int)CURRENT_SENSE_A, (int)CURRENT_SENSE_B);  

I’m seeing this error at runtime:

[  1012][E][esp32-hal-adc.c:172] __analogChannelConfig(): Pin is not configured as analog channel
[  1021][E][esp32-hal-adc.c:172] __analogChannelConfig(): Pin is not configured as analog channel

Here’s the full output:

Starting...
ESP32-DRV: Configuring 3PWM in group: 0 on timer: 0
ESP32-DRV: Configuring 2 operators.
ESP32-DRV: Configuring 3 comparators.
ESP32-DRV: Configuring 3 generators.
ESP32-DRV: Configuring center-aligned pwm.
ESP32-DRV: Enabling timer: 0
ESP32-DRV: MCPWM configured!
MOT: Monitor enabled!
[  1012][E][esp32-hal-adc.c:172] __analogChannelConfig(): Pin is not configured as analog channel
[  1021][E][esp32-hal-adc.c:172] __analogChannelConfig(): Pin is not configured as analog channel
Current sense init success!

I found this post but was not able to determine if this message requires attention.

Here’s the full code:

#include <Arduino.h>
#include <SimpleFOC.h>
#include <Wire.h>

#define I2C_SDA 4 // SDA on encoder (encoder yellow)
#define I2C_SCL 5 // SCL on encoder (encoder orange)
#define CURRENT_SENSE_A 6  // GPIO6 - ADC1_5
#define CURRENT_SENSE_B 7  // GPIO7 - ADC1_6
#define PWM_A 15 // 9 on simplefoc shield v2 (red)
#define PWM_B 16  // 5 on simplefoc shield v2 (orange)
#define PWM_C 17 // 6 on simplefoc shield v2 (yellow)
#define ENABLE 18 // 8 on simplefoc shield v2 (green)


BLDCMotor motor = BLDCMotor(7);
BLDCDriver3PWM driver = BLDCDriver3PWM(PWM_A, PWM_B, PWM_C, ENABLE);
MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0x0E, 4);

InlineCurrentSense current_sense = InlineCurrentSense(0.01, 50.0f, (int)CURRENT_SENSE_A, (int)CURRENT_SENSE_B);  

float target_velocity = 0.0f;

Commander command = Commander(Serial);

void doMotor(char* cmd) { command.motor(&motor, cmd); }
void doTarget(char* cmd) { 
  command.scalar(&target_velocity, cmd); 
  // Serial.print("Target velocity set to: ");
  // Serial.println(target_velocity);
}

void setup() {
  
  Serial.begin(115200);
  _delay(1000);
  
  Serial.println("Starting...");

  SimpleFOCDebug::enable(&Serial);

  Wire.begin(I2C_SDA, I2C_SCL);
  Wire.setClock(400000);

  sensor.init(&Wire);

  motor.linkSensor(&sensor);
  
  driver.voltage_power_supply = 12;
  if(!driver.init()){
    Serial.println("Driver init failed!");
    return;
  }

  current_sense.linkDriver(&driver);

  motor.linkDriver(&driver);
  motor.controller = MotionControlType::velocity;
  motor.PID_velocity.P = 0.04f;
  motor.PID_velocity.I = 0.7f;
  motor.PID_velocity.D = 0.0f;
  motor.LPF_velocity.Tf = 0.01f;
  motor.voltage_limit = 12;   // [V]
  motor.velocity_limit = 1000; // [rad/s]
  motor.voltage_sensor_align = 1; // [V]
  motor.velocity_index_search = 3;
  motor.useMonitoring(Serial);
  
  if (current_sense.init())  Serial.println("Current sense init success!");
  else{
    Serial.println("Current sense init failed!");
    return;
  }

  // driver.pwm_frequency = 20000;
  Serial.println(driver.pwm_frequency);

  Serial.print("Current sense pins: ");
  Serial.print(CURRENT_SENSE_A);
  Serial.print(", ");
  Serial.println(CURRENT_SENSE_B);

  if(!motor.init()){
    Serial.println("Motor init failed!");
    return;
  }
  motor.initFOC();

  command.add('T', doTarget, "target velocity");
  command.add('M', doMotor,"my motor");
  
  _delay(1000);
}

void loop() {
  motor.loopFOC();
  //motor.monitor();
  motor.move(target_velocity);  // use the target velocity here

  command.run();
  
}

// columtarget, voltage.q, velocity, angle

and PlatformIO config:

[env:esp32-s3_i2c_simplefoc]
build_src_filter = +<esp32-s3_i2c_simplefoc.cpp> 
platform = espressif32
board = esp32-s3-devkitc-1
framework = arduino
board_build.mcu = esp32s3
board_build.f_cpu = 240000000L
board_build.partitions = default_8MB.csv
board_build.arduino.memory_type = qio_qspi
build_flags = 
    -DARDUINO_USB_MODE=1
    -DARDUINO_USB_CDC_ON_BOOT=1
    -DBOARD_HAS_PSRAM
upload_protocol = esptool
monitor_speed = 115200
lib_deps = 
	askuric/Simple FOC@^2.3.4
	Wire

Hi,

I run in the same issues with:

__analogChannelConfig(): Pin is not configured as analog channel

I tried different esp32 boards, SimpleFOC Releases and platform-espressif32 releases.

Does anyone have an idea to this?

platform.ini:

platform = https://github.com/pioarduino/platform-espressif32/releases/download/53.03.12/platform-espressif32.zip
board = esp32-s3-devkitc-1
framework = arduino
monitor_speed = 115200
lib_ldf_mode = deep
lib_deps =
askuric/Simple FOC@^2.3.4

Same issue here

[env]
framework = arduino
lib_archive = false
lib_deps =
    SPI
    Wire
    askuric/Simple FOC@^2.3.4
    simplefoc/SimpleFOCDrivers @ ^1.0.8

[env:sparkfun_thing_plus_esp32s3]
platform = https://github.com/pioarduino/platform-espressif32.git
board = sparkfun_esp32s3_thing_plus

The error is coming from this function in the arduino-esp32 framework. It is checking to see if the pin type is ESP32_BUS_TYPE_ADC_ONESHOT. Perhaps we are using the wrong pins. However, looking at the pin selection guide, this doesn’t seem to be the case. I am using pins 11 & 12 on an ESP32s3 which should be fine.

Minimal code to replicate on my Sparkfun Thing Plus ESP32s3:

#include "Arduino.h"

void setup() {
  Serial.begin(115200);
  delay(1000); // Interestingly, the error isn't printed if we remove this delay (but the other printouts are)

  int pin = 11;
  analogSetPinAttenuation(pin, ADC_11db); // Error occurs here after reset button is pressed on Sparkfun Thing Plus ESP32s3
}

void loop() {
}

So it appears the problem is not in the SimpleFOC library itself.

Interestingly, the error no longer occurs if I do an analogRead before setting the attenuation:

#include "Arduino.h"

void setup() {
  Serial.begin(115200);
  delay(1000); // Interestingly, the error isn't printed if we remove this delay

  int pin = 11;
  analogRead(pin);
  analogSetPinAttenuation(pin, ADC_11db); // Error no longer occurs
}

void loop() {
}

Delving into arduino-esp32 a bit more deeply… it appears that the ESP32_BUS_TYPE_ADC_ONESHOT setting is only applied to a pin in the __analogInit function, which in turn is only ever invoked during an analogRead (or analogReadMilliVolts, which SimpleFOC doesn’t use).

This looks like a bug in arduino-esp32, since analogSetPinAttenuation should be able to work before an analogRead is called. But we can work around it in the SimpleFOC library by reversing the order of the analogSetPinAttenuation and the analogRead calls in the hardware specific driver for ESP32 here.

Pull request here: Fix `Pin is not configured as analog channel` error by jeremiahrose · Pull Request #458 · simplefoc/Arduino-FOC · GitHub

We can’t work around it in our sketches by manually calling analogRead, unfortunately, because the pins are reset by a pinMode(pin, ANALOG) within the current_sense.init().

2 Likes

I suppose that this error is introduced in the new releases of the epressif (after 5.1.4). I did not really see while migrating the current sensing code to v3.0.1 arduino-esp32.

@Jeremiah_Rose Very nice catch! We are gonna integrate this in the next release.

From what I was able to figure out this error does not have any real consequences right?
The current sense still works right?

1 Like

Yep, it looks like it is failing to set the ADC attenuation to 11db, but the attenuation is already defaulted at 11db. So it doesn’t actually matter if it fails.