Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

analogRead and analogReadResolution #36

Open
KurtE opened this issue Jan 18, 2025 · 1 comment · May be fixed by #39
Open

analogRead and analogReadResolution #36

KurtE opened this issue Jan 18, 2025 · 1 comment · May be fixed by #39

Comments

@KurtE
Copy link

KurtE commented Jan 18, 2025

Describe the bug
analogRead(A0) - is returning a value that does not fit within 10 bits, like 3349. Guessing that analogRead
is returning 12 bits, whereas the document:
https://docs.arduino.cc/language-reference/en/functions/analog-io/analogRead/
says that it should default to 10 bit resolution.

analogReadResolution(int bit) - is not defined within the system (i.e. compile error)

Target board + cli verbose compilation output
Arduino Zephyr - GIGA...

Optional: attach the sketch
Started with the AnalogInput.ino example sketch, with a few edits to use the built in LED and prints out the
values returned, if the value changed from the previous call.

Also checked for Serial input and used the input for which analog pin to use 0 is A0...

/*
  Analog Input

  Demonstrates analog input by reading an analog sensor on analog pin 0 and
  turning on and off a light emitting diode(LED) connected to digital pin 13.
  The amount of time the LED will be on and off depends on the value obtained
  by analogRead().

  The circuit:
  - potentiometer
    center pin of the potentiometer to the analog input 0
    one side pin (either one) to ground
    the other side pin to +5V
  - LED
    anode (long leg) attached to digital output 13 through 220 ohm resistor
    cathode (short leg) attached to ground

  - Note: because most Arduinos have a built-in LED attached to pin 13 on the
    board, the LED is optional.

  created by David Cuartielles
  modified 30 Aug 2011
  By Tom Igoe

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/AnalogInput
*/

int sensorPin = A0;   // select the input pin for the potentiometer
int ledPin = LED_BUILTIN;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor
int sensorValuePrev = -1;

void setup() {
  // declare the ledPin as an OUTPUT:
  //analogReadResolution(10);
  pinMode(ledPin, OUTPUT);
  Serial.begin(115200);
}


void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
  if (sensorValue != sensorValuePrev) {
    Serial.println(sensorValue);
    sensorValuePrev = sensorValue;
  }
  // turn the ledPin on
  digitalWrite(ledPin, HIGH);
  // stop the program for <sensorValue> milliseconds:
  delay(sensorValue);
  // turn the ledPin off:
  digitalWrite(ledPin, LOW);
  // stop the program for <sensorValue> milliseconds:
  delay(sensorValue);

  if (Serial.available()) {
    int pin_num = Serial.parseInt();
    while (Serial.available()) Serial.read();
    static const int analog_pins[] = {A0, A1, A2, A3, A4, A5, A6, A7/*, A8, A9, A10, A11*/};
    sensorPin = analog_pins[pin_num];
    Serial.print("New pin A");
    Serial.print(pin_num);
    Serial.print(" : ");
    Serial.println(sensorPin);
    sensorValuePrev = -1;

  }
}

The above works for A0-A7
uncommenting A8-A11, gives compile errors.

C:\Users\kurte\Documents\Arduino\hardware\arduino-git\ArduinoCore-zephyr\variants\arduino_giga_r1_m7/pure_analog_pins.h:40:13: error: call to 'PureAnalogPin::operator int' declared with attribute error: Change me to a #define
   40 | #define A8  A8_PURE
      |             ^~~~~~~
C:\Users\kurte\Documents\Arduino\hardware\arduino-git\ArduinoCore-zephyr\variants\arduino_giga_r1_m7/pure_analog_pins.h:40:13: note: in definition of macro 'A8'
   40 | #define A8  A8_PURE
      |             ^~~~~~~
C:\Users\kurte\Documents\Arduino\hardware\arduino-git\ArduinoCore-zephyr\variants\arduino_giga_r1_m7/pure_analog_pins.h:41:13: error: call to 'PureAnalogPin::operator int' declared with attribute error: Change me to a #define
   41 | #define A9  A9_PURE
      |             ^~~~~~~
C:\Users\kurte\Documents\Arduino\hardware\arduino-git\ArduinoCore-zephyr\variants\arduino_giga_r1_m7/pure_analog_pins.h:41:13: note: in definition of macro 'A9'
   41 | #define A9  A9_PURE
      |             ^~~~~~~
C:\Users\kurte\Documents\Arduino\hardware\arduino-git\ArduinoCore-zephyr\variants\arduino_giga_r1_m7/pure_analog_pins.h:42:14: error: call to 'PureAnalogPin::operator int' declared with attribute error: Change me to a #define
   42 | #define A10  A10_PURE
      |              ^~~~~~~~
C:\Users\kurte\Documents\Arduino\hardware\arduino-git\ArduinoCore-zephyr\variants\arduino_giga_r1_m7/pure_analog_pins.h:42:14: note: in definition of macro 'A10'

Will look at later...

@KurtE
Copy link
Author

KurtE commented Jan 18, 2025

I personally don't like it, but looks like A8-A11 are objects PureAnalogPin - which appears to be consistent with the MBED version.
I did not see any reference to this in documentation... At least the cheat sheet.
https://docs.arduino.cc/tutorials/giga-r1-wifi/cheat-sheet/#analog-pins

Hacked up sketch to allow it to run on those as well:

/*
  Analog Input

  Demonstrates analog input by reading an analog sensor on analog pin 0 and
  turning on and off a light emitting diode(LED) connected to digital pin 13.
  The amount of time the LED will be on and off depends on the value obtained
  by analogRead().

  The circuit:
  - potentiometer
    center pin of the potentiometer to the analog input 0
    one side pin (either one) to ground
    the other side pin to +5V
  - LED
    anode (long leg) attached to digital output 13 through 220 ohm resistor
    cathode (short leg) attached to ground

  - Note: because most Arduinos have a built-in LED attached to pin 13 on the
    board, the LED is optional.

  created by David Cuartielles
  modified 30 Aug 2011
  By Tom Igoe

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/AnalogInput
*/

int sensorPin = A0;   // select the input pin for the potentiometer
int ledPin = LED_BUILTIN;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor
int sensorValuePrev = -1;
const int analog_pins[] = {A0, A1, A2, A3, A4, A5, A6, A7, 0, 1, 2, 3};
const PureAnalogPin *pure_pins[] = {&A8, &A9, &A10, &A11};

void setup() {
  // declare the ledPin as an OUTPUT:
  //analogReadResolution(10);
  pinMode(ledPin, OUTPUT);
  Serial.begin(115200);
}


void loop() {
  // read the value from the sensor:
  sensorValue = (sensorPin >= A0)? analogRead(sensorPin) : analogRead(*pure_pins[sensorPin]);
  if (sensorValue != sensorValuePrev) {
    Serial.println(sensorValue);
    sensorValuePrev = sensorValue;
  }
  // turn the ledPin on
  digitalWrite(ledPin, HIGH);
  // stop the program for <sensorValue> milliseconds:
  delay(sensorValue);
  // turn the ledPin off:
  digitalWrite(ledPin, LOW);
  // stop the program for <sensorValue> milliseconds:
  delay(sensorValue);

  if (Serial.available()) {
    int pin_num = Serial.parseInt();
    while (Serial.available()) Serial.read();
    sensorPin = analog_pins[pin_num];
    Serial.print("New pin A");
    Serial.print(pin_num);
    Serial.print(" : ");
    Serial.println(sensorPin);
    sensorValuePrev = -1;

  }
}

Checked on 8-11 and verified that they also return 0-4095

@KurtE KurtE linked a pull request Jan 18, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant