Go Down

Topic: Unable to use analogRead() with portenta (Read 1 time) previous topic - next topic

analogdude

Hi,

Is analogRead() function supposed to work with Arduino Portenta yet? My attempts doing so have failed so far. I tried to use the basic arduino examples (https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/) but I am only able to get zeros to the serial.

-analog

preynolds101

I have been trying with an application to provide an analog output using the Portenta but have failed to get anything other than zero output (see https://forum.arduino.cc/index.php?topic=693551.0).

Having read this post, I have just tried to make an analogRead using the Portenta H7 using the code from the analogRead reference page.

I can't get anything other than zero input, so I'm observing the same problem that was reported here. I wonder if the ADC and DAC problems are related...

analogdude

I seem to be going the same path as you did, as I'm currently trying to familiarize myself with stm32cube software...


ubidefeo

@analogdude @preynolds101

Sorry for the inconvenience.
There was a mishap during the merge of mbed 6.0 and pins A0-A3 ended up being ignored during a call to analogRead().

You can read about it here, the core will be updated today
https://github.com/arduino/ArduinoCore-mbed/issues/9#issuecomment-653539861

Enjoy :)

analogdude

Thank you @ubidefeo!

It works flawlessly now :)

analogdude

A follow up question:

Is external analog reference supported yet?

jerteach

#6
Jul 25, 2020, 04:12 pm Last Edit: Aug 03, 2020, 05:16 am by jerteach
Can someone check what I am doing wrong. I have MBED board version 1.2.1, I am running this simple blink, analogRead, serial code

Code: [Select]


/*  Blink, AnalogRead and Serial
 *  Arduino test program
 *  By Jeremy Ellis twitter @rocksetta
 *
 * Alternate connecting A0 to 3V3 and then to Ground
 *  Best to use a variable resistor the 3 wires to A0, GND, 3V3
 *
 */

void setup(){ 
 
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  // while (!Serial);  // Please don't do this it messes up beginners
 
}

void loop() {

  int sensorValue = analogRead(A0);
  Serial.print("A0 Analog Read max = 1023, Actual Value: ");
  Serial.println(sensorValue);

  // Flash LED 3 times
  digitalWrite(LED_BUILTIN, LOW);   
  delay(100);                     
  digitalWrite(LED_BUILTIN, HIGH);   
  delay(100);                   

  digitalWrite(LED_BUILTIN, LOW);   
  delay(100);                     
  digitalWrite(LED_BUILTIN, HIGH);   
  delay(100);     
               
  digitalWrite(LED_BUILTIN, LOW);   
  delay(100);                     
  digitalWrite(LED_BUILTIN, HIGH);   
  delay(3000);    // longer wait
                 
}





When I run it on a Nano 33 IOT I get sensible results from 0 to 1023, but when I run it on the Portenta. First and most important A5 crashes the board to flashing red and disconnects the serial monitor.





# Update Aug 1st. A5 no longer crashing. Looks like all the A0 to A6 are working fine with board manager 1.2.2



joshgoldberg

Jer,

I made the same mistake using A5 and crashing my board. Can you describe how you have reset yours?

I have managed to make analogRead() to work but the moment I try and include analogWrite() on a pin, neither of them work.

jerteach

#8
Jul 29, 2020, 10:59 pm Last Edit: Aug 03, 2020, 05:17 am by jerteach
Jer,

I made the same mistake using A5 and crashing my board. Can you describe how you have reset yours?

Josh, not sure if I did anymore than double push the reset button. I have crashed my Portenta so much. I am getting used to the double reset on first plugin and then it seems fairly good at finding the port after that.

P.S. mBed board manager version 1.2.2 fixes the A5 crash, now allows A0-A6 to analogRead, only with AREF connected to 3V3.







jwestmoreland

jerteach,

I confirmed A5 ok right now (1.2.2) as well:

Used this sketch - note - A5 is just floating (still have my H7 in a test harness...)

/*
  Analog input, analog output, serial output

  Reads an analog input pin, maps the result to a range from 0 to 255 and uses
  the result to set the pulse width modulation (PWM) of an output pin.
  Also prints the results to the Serial Monitor.

  The circuit:
  - potentiometer connected to analog pin 0.
    Center pin of the potentiometer goes to the analog pin.
    side pins of the potentiometer go to +5V and ground
  - LED connected from digital pin 9 to ground

  created 29 Dec. 2008
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/AnalogInOutSerial
*/

// These constants won't change. They're used to give names to the pins used:
const int analogInPin = A5;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(115200);
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);
  // map it to the range of the analog out:
  // outputValue = map(sensorValue, 0, 1023, 0, 255);
  // change the analog out value:
 //  analogWrite(analogOutPin, outputValue);

  // print the results to the Serial Monitor:
  Serial.print("sensor = ");
  Serial.println(sensorValue);
 // Serial.print("\t output = ");
 // Serial.println(outputValue);

  // wait 2 milliseconds before the next loop for the analog-to-digital
  // converter to settle after the last reading:
  delay(1000);
}

19:57:07.438 -> sensor = 233
19:57:08.413 -> sensor = 233
19:57:09.416 -> sensor = 227
19:57:10.437 -> sensor = 230
19:57:11.432 -> sensor = 225
19:57:12.402 -> sensor = 229
19:57:13.402 -> sensor = 233
19:57:14.403 -> sensor = 224
19:57:15.408 -> sensor = 229
19:57:16.413 -> sensor = 226
19:57:17.408 -> sensor = 232
19:57:18.396 -> sensor = 226
19:57:19.397 -> sensor = 225
19:57:20.396 -> sensor = 232
19:57:21.396 -> sensor = 235
19:57:22.413 -> sensor = 240
19:57:23.396 -> sensor = 241
19:57:24.393 -> sensor = 226
19:57:25.399 -> sensor = 237
19:57:26.405 -> sensor = 247
19:57:27.383 -> sensor = 239
19:57:28.380 -> sensor = 233
19:57:29.384 -> sensor = 222
19:57:30.388 -> sensor = 224

I'll try the rest.

Regards,
John W.

jwestmoreland

#10
Jul 30, 2020, 05:23 am Last Edit: Jul 30, 2020, 05:27 am by jwestmoreland
For reference:

Using this sketch:

Code: [Select]
/*
  Analog input, analog output, serial output

  Reads an analog input pin, maps the result to a range from 0 to 255 and uses
  the result to set the pulse width modulation (PWM) of an output pin.
  Also prints the results to the Serial Monitor.

  The circuit:
  - potentiometer connected to analog pin 0.
    Center pin of the potentiometer goes to the analog pin.
    side pins of the potentiometer go to +5V and ground
  - LED connected from digital pin 9 to ground

  created 29 Dec. 2008
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/AnalogInOutSerial
*/
unsigned short counter = 0;
// These constants won't change. They're used to give names to the pins used:
int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(115200);
}

void loop() {

   // read the analog in value:

  switch ( counter++ )

  {

    case 0:
    analogInPin = A0;
//    counter++;
    break;
   
    case 1:
    analogInPin = A1;
    break;
   
    case 2:
    analogInPin = A2;
    break;
   
    case 3:
    analogInPin = A3;
    break;
   
    case 4:
    analogInPin = A4;
    break;
   
    case 5:
    analogInPin = A5;
    break;
   
    case 6:
    analogInPin = A6;
    counter = 0;
    break;

    default:
    analogInPin = A0;
    counter = 0;
    break;
   
  }
  sensorValue = analogRead(analogInPin);
  // map it to the range of the analog out:
  // outputValue = map(sensorValue, 0, 1023, 0, 255);
  // change the analog out value:
 //  analogWrite(analogOutPin, outputValue);

  // print the results to the Serial Monitor:
  Serial.print(counter);
  Serial.print(" sensor = :");
  Serial.println(sensorValue);
 // Serial.print("\t output = ");
 // Serial.println(outputValue);

  // wait 2 milliseconds before the next loop for the analog-to-digital
  // converter to settle after the last reading:
  delay(1000);
}


I got this output:

20:20:44.694 -> 0 sensor = :490
20:20:45.713 -> 1 sensor = :21
20:20:46.693 -> 2 sensor = :11
20:20:47.697 -> 3 sensor = :18
20:20:48.703 -> 4 sensor = :14
20:20:49.672 -> 5 sensor = :230
20:20:50.664 -> 6 sensor = :235
20:20:51.672 -> 0 sensor = :560
20:20:52.683 -> 1 sensor = :18
20:20:53.681 -> 2 sensor = :16
20:20:54.683 -> 3 sensor = :13
20:20:55.693 -> 4 sensor = :14
20:20:56.663 -> 5 sensor = :238
20:20:57.682 -> 6 sensor = :229
20:20:58.671 -> 0 sensor = :565
20:20:59.676 -> 1 sensor = :17
20:21:00.681 -> 2 sensor = :19
20:21:01.656 -> 3 sensor = :16
20:21:02.679 -> 4 sensor = :16
20:21:03.649 -> 5 sensor = :240
20:21:04.651 -> 6 sensor = :232
20:21:05.655 -> 0 sensor = :561
20:21:06.657 -> 1 sensor = :14
20:21:07.646 -> 2 sensor = :19
20:21:08.665 -> 3 sensor = :15
20:21:09.673 -> 4 sensor = :16
20:21:10.662 -> 5 sensor = :236
20:21:11.632 -> 6 sensor = :249

HTH,
John W.



Go Up