How read channels 1-8 on ADC1

Post here first, or if you can't find a relevant section!
Post Reply
abc_dude
Posts: 3
Joined: Mon Jun 21, 2021 9:03 pm

How read channels 1-8 on ADC1

Post by abc_dude »

I am using a nucleo board with a ST32 103RB micro and the Arduino IDE to upload my sketch.

I have 8 ADC lines I want to read from PA0 - PA7. When I read from PA2 it will show the same reading as PA1 and when I read from PA3 it will show the same ADC reading as PA0. I am not sure why that is or I don't understand the pin mapping correlation between Arduino and ST to make this work. Any help as to what is wrong and/or guidance?

Code: Select all

#include <stm32yyxx_ll_adc.h>
#include <stdio.h>
#include <math.h>

const int totalSensingLines = 8;    
const int currentLines[totalSensingLines] = { PA0, PA1, PA2, PA3, PA4, PA5, PA6, PA7 }; 

void setup()
{
  analogReadResolution(12); // set resolution to 12-bit
  for (int i = 0; i < totalSensingLines; i++) {
    pinMode(currentLines[i], INPUT);
  }
}


void loop()
{
  read_lines(currentReadings, totalSensingLines);
}


void read_lines(double *buf, int buffSize) {
  double samples[buffSize] = {0.0};
  double vout[buffSize] = {0.0};
  int totalSamples = 10;
  double thisPeak = 0.0;

  // Take Samples
  for (int i = 0; i < totalSamples; i++) {
    for (int k = 0; k < buffSize; k++) {
      thisPeak = analogRead(currentLines[k]);
      if (thisPeak > samples[k]) {
        samples[k] = thisPeak;
      }
    }
  }

  // Calculate Reading
  for (int i = 0; i < buffSize; i++) {
    vout[i] = samples[i] * (3.3 / 4096.0);
    buf[i] = (abs((1.65 - vout[i]) / 0.044)) / sqrt(2); 
  }
}
mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: How read channels 1-8 on ADC1

Post by mrburnette »

abc_dude wrote: Mon Jun 21, 2021 9:20 pm ...
I have 8 ADC lines I want to read from PA0 - PA7. When I read from PA2 it will show the same reading as PA1 and when I read from PA3 it will show the same ADC reading as PA0.
...
If you leave the analog pins open, they are floating at a very high impedance; static charges on the circuit board and the very high resistance on the circuit board causes the charges to be distributed.

It is common rule of thumb to have analog pins pulled to ground by 10K resistor before sampling the analog pin(s). Use resistors with a precision appropriate to the accuracy you need from the analog readings.

Clamping prevents the analog pin from drifting above Vdd (less the Vd of the junction.)
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: How read channels 1-8 on ADC1

Post by ag123 »

some faqs may be relevant
viewtopic.php?f=2&t=3
viewtopic.php?f=2&t=301

and normally to use the adc it is

Code: Select all

pinMode(pin, INPUT_ANALOG);
uint16_t adc_val = analogRead(pin);
abc_dude
Posts: 3
Joined: Mon Jun 21, 2021 9:03 pm

Re: How read channels 1-8 on ADC1

Post by abc_dude »

mrburnette wrote: Tue Jun 22, 2021 12:38 am
abc_dude wrote: Mon Jun 21, 2021 9:20 pm ...
I have 8 ADC lines I want to read from PA0 - PA7. When I read from PA2 it will show the same reading as PA1 and when I read from PA3 it will show the same ADC reading as PA0.
...
If you leave the analog pins open, they are floating at a very high impedance; static charges on the circuit board and the very high resistance on the circuit board causes the charges to be distributed.

It is common rule of thumb to have analog pins pulled to ground by 10K resistor before sampling the analog pin(s). Use resistors with a precision appropriate to the accuracy you need from the analog readings.

Clamping prevents the analog pin from drifting above Vdd (less the Vd of the junction.)
They aren't floating. When I print the pin numbers from PA0-PA7 I get 53, 54, 1, 0 , 55, 61 ,60, 59. Not quite sure why 1 and/or 0 get assigned here. Is there an incorrect pin mapping going on for PA2 & PA3?
abc_dude
Posts: 3
Joined: Mon Jun 21, 2021 9:03 pm

Re: How read channels 1-8 on ADC1

Post by abc_dude »

ag123 wrote: Tue Jun 22, 2021 1:24 am some faqs may be relevant
viewtopic.php?f=2&t=3
viewtopic.php?f=2&t=301

and normally to use the adc it is

Code: Select all

pinMode(pin, INPUT_ANALOG);
uint16_t adc_val = analogRead(pin);
INPUT_ANALOG is not documented in the Arduino docs. Only options are INPUT, OUTPUT, INPUT_PULLUP. I tried it anyways and same result as my original problem.
mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: How read channels 1-8 on ADC1

Post by mrburnette »

Honestly, this is from the early days of STM32duino (Roger's time) and was tested on a Maple Mini clone... it may not even compile on the STM official core. Arduino 1.7.8 on Linux Mint 17.3 tested on 20160215

Code: Select all

/*
  hacked by Ray Burnette to loop through all analog values
  Board view: http://letsmakerobots.com/files/maplemini2.jpg  
  Quote from old Maple Mini doc by Leaflabs "Maple Mini has an electrically 
    isolated analog power plane with its own regulator, and a geometrically
    isolated ground plane, connected to the digital plane by an inductor. 
    Its analog input pins, D3 — D11, are laid out to correspond with these 
    analog planes, and our measurements indicate that they generally offer
    low noise ADC performance. However, analog performance may vary depending 
    upon the activity of the other GPIOs. Consult the Maple Mini hardware 
    design files for more details."
    Arduino 1.7.8 on Linux Mint 17.3 tested on 20160215
      Sketch uses 13,948 bytes (12%) of program storage space. Maximum is 110,592 bytes.
      Global variables use 2,560 bytes of dynamic memory.
*/

int aValue ;
int settlingmS = 5000;

void setup() {
    // Configure the ADC pins
    for (int x =3; x < 12; x++) {
      pinMode(x, INPUT_ANALOG);
    }
  delay(settlingmS);
}

void loop() {
  for ( int analogInPin = 3; analogInPin < 12; analogInPin++)
  {
    // read the analog in value:
    aValue = analogRead(analogInPin);
    // print the results to the serial monitor:
    Serial.print("Analog pin#");
    Serial.print(analogInPin);
    Serial.print("\t = ");
    Serial.println(aValue);
  }
  delay(settlingmS);
}


mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: How read channels 1-8 on ADC1

Post by mrburnette »

Sooo .... I messed up and posted my response here: viewtopic.php?p=7682#p7682

But, since the topic was analog, I will just leave things and try and be more careful in the future :shock:

Ray
Post Reply

Return to “General discussion”