analogRead() issue

Fri Jan 22, 2016 8:56 pm
Hello, guys!

When I read one analog pin everything looks normally.
But when I try to read pins in sequence like:

void setup() {
Serial.begin(115200); // Ignored by Maple. But needed by boards using hardware serial via a USB to Serial adaptor
for (int i=0; i < 8; i++)
pinMode(i, INPUT_ANALOG);
delay (5000);

void loop() {
for (int i=0; i < 8; i++)
Serial.print(analogRead(i), DEC);
Serial.print(' ');

Sat Jan 23, 2016 6:08 pm

So, I’m going on the “old” Maple Mini documentation by Leaflabs, and providing the following sketch that does work as I would expect:

hacked by Ray Burnette to loop through eight analog values A0 through A7
Board view:
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."

int aValue ;
int settlingmS = 5000;

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

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("\t = ");

/* Sample console output:

Analog pin#3 = 2009
Analog pin#4 = 2061
Analog pin#5 = 2077
Analog pin#6 = 2536
Analog pin#7 = 2678
Analog pin#8 = 2681
Analog pin#9 = 2697
Analog pin#10 = 2135
Analog pin#11 = 1984

Sat Jan 23, 2016 8:42 pm
I didnt get around to replying to the original post, but I wondered if all the pins you used can be used as analog input pins.

Im also not sure if the STM32 can have that many pins simultaneously configured as analog inputs.

The STM32 is a fantastically flexible and powerful device, but some combinations of configurations (in fact many combinations) probably wont work due to internal bus conflicts in the chip.

You probably need to download the programming manual RM0008 from STMs site and look in detail about how this works.

Sat Jan 23, 2016 8:48 pm
i wonder if a post with the links to the main st f1, f4 & f7 ref and prog manuals downloads would be useful?
6 links?
pulling the docs to be local might or might not be allowed or be too sensible?


hopefully someone with some html experience can tidy this up.
i tried a link, took me to the right place to find out more

i went raiding, searched for each of stmf1, stm32f4 and stm32f7; snaffled the source of the table it gives you from the search.
ok, maybe overkill, losing stm32f100/101/102 etc easy, pls keep 107 as i’ve got some:D
i’ll add it as a zip 4.5k, about 100k uncompressed.
(4.04 KiB) Downloaded 350 times

Sat Jan 23, 2016 8:50 pm
probably need to do a faq thread

Sat Jan 23, 2016 8:52 pm
zmemw16 wrote:i wonder if a post with the links to the main st f1, f4 & f7 ref and prog manuals downloads would be useful?
6 links?
pulling the docs to be local might or might not be allowed or be too sensible?


Sat Jan 23, 2016 8:52 pm
I guess we could stick my Analog example into the github examples, too. Of all the stuff I did, I failed to do one single analog.


Sat Jan 23, 2016 11:07 pm
Hmmm… I try it on STM “maple mini clone” and “blue pill” – all works ok.
If I apply some voltage on some analog pin – I can read this voltage on this pin. On numbers of analog pin in sequence…
But on GD if I try sequential read, I get in two first readings same value on analog channel 0 and 1, and then “rolled” readings.
On PA0 read voltage applyed to PA7
On PA1 read voltage applyed to PA0
On PA2 read voltage applyed to PA1

And I spent some time to deal with pin-mapping, numbering and naming and I understand a difference between “maple mini”, “blue/red pill” and others, I hope :)

Sat Jan 23, 2016 11:14 pm

Are you using a GD32 or STM32 ?

Sat Jan 23, 2016 11:17 pm
That’s just the point that everything works on STM, and on “GD” – doesn’t.
I have some “Zoo” of boards :)

Sat Jan 23, 2016 11:45 pm
TomaTLAB wrote:That’s just the point that everything works on STM, and on “GD” – doesn’t.
I have some “Zoo” of boards :)

Sun Jan 24, 2016 12:01 am

GD are supposed to be compatible with STM32 but I have not verified all the functionality

There are definitely some differences, but I wasnt aware of any ADC related things.

It could be the hardware of the board thats the issue, can you post a link to your board

Sun Jan 24, 2016 11:44 am
My GD32 board
Any digital read/write works correctly and readings one analog channel too. It’s analog MUX issue, IMO.

Can anybody check my sketch on GD to be convinced, what it is not a single failure on my chip?

Sun Jan 24, 2016 9:23 pm
I will try to run your sketch this evening.

BTW. There is a separate thread on the forum for the GD32 as it does appear to have some differences with the STM32

I have not managed to get SPI working, when I connected it to an ILI9341 display.
But it could just be a wiring problem.

BTW. AFIK, only 4 people on this forum have a GD32 board, including me and you.

Mon Jan 25, 2016 7:58 am
RogerClark wrote:I will try to run your sketch this evening.

Mon Jan 25, 2016 10:26 pm
Thanks for testing SPI

I’m glad i just had a wiring fault.

I’ve been trying to upload to my GD32 board, but it looks like my new PC which is a rather fast core i7 machine is possibly too fast for the bootloader.

I’ll need to work out which pin, if any I assigned as the perpetual bootloader enable pin on GD32

So I’ll need to get back to you when this is working

PS. I don’t even know if I can upload to a Maple mini :-(

Tue Jan 26, 2016 3:22 am
I tried the sketch on the GD32 and I tried pulling each pin to GND, individually and it was fine.

The strange thing however is that to read PA0 (pin 0) I have to connect GND to PA7, and pin 1 is PA0 pin 2 is PA1 etc, i.e its all shifted down by own.

I’ve looked in the PIN MAP and the ADC channel number is being defined correctly i.e Channel 0 is PA0, but this is not what the code is reading.

I’ve not tried this on a red pill to see if it has the same effect, or even on a Maple Mini by addressing the pin by its STM32 name i.e PA0

The code that reads the ADC is quite straight forward

adc_reg_map *regs = dev->regs;

adc_set_reg_seqlen(dev, 1);

regs->SQR3 = channel;
regs->CR2 |= ADC_CR2_SWSTART;
while (!(regs->SR & ADC_SR_EOC))

return (uint16)(regs->DR & ADC_DR_DATA);

Tue Jan 26, 2016 1:37 pm
Thanx, Roger!
I’ll also try to do some experiments.
It’s meanwhile not strongly important for me yet. But I think that this issue needs to be cleared up somehow, IMO.

I played with modbus library which was provided by André Sarmento, writing down a value from analog inputs to the modbus registers in cycle.
And all works perfecly on Arduino Mini & Due, STM32 maple mini & blue pill boards with arduinoish code style, but with GD board I get this “surprize” :)

I like arduinoish style for the portability and uniformity of code.
I have an idea to build several sensors and actuators for a little home automation, like it becomes in the industrial distributed systems. On a various platforms, depending on load.

Tue Jan 26, 2016 7:54 pm
Hi TomaTLab

Unfortunately have anither strange problem on my GD32. The USB reset is not working on my new PC.

I tried by old Core i5 PC and the GD32 resets the USB bus, but on the new Core i7 machine ( Skylake chipset) it does not reset the USB.

I tried various pull up resistors on PA12, including 1.5, 1k and 2k, but it didn’t make any difference.

I tried a generic STM32F103V board, which has a 1.5k pull-up on USB, on my new machine, and it worked OK.

I think perhaps the GD32 does not pull PA12 low for long enough. I will need to rebuild the bootloader with a longer delay and try it again

Leave a Reply

Your email address will not be published. Required fields are marked *