analogRead showing a digital behaviour

Post here first, or if you can't find a relevant section!
Post Reply
tyrooryt
Posts: 8
Joined: Fri Jul 24, 2020 11:36 am

analogRead showing a digital behaviour

Post by tyrooryt »

Hello everyone,

I have seen an earlier post about this, and tried reading voltage using several pins, but am unable to figure out the problem.

The board I am using is STM32F103C8T6 and the core is the STM32 official core. I want to measure an analog voltage with a 12 bit resolution. I am using the following code to write voltage to a breadboard and measuring the same voltage:

Code: Select all


void setup() {
  Serial.begin(115200);
  pinMode(PB0, OUTPUT);
  pinMode(PA7,INPUT_ANALOG);
  analogReadResolution(12);
  analogWriteResolution(12);
}


void loop() {

  analogWrite(PB0,2100);            //for all values above 2100, the voltage is is 3.3, and for all values below this, voltage is 0
  uint16_t val = analogRead(PA7);
  Serial.print(val);
  Serial.print("  ");
  float voltage = 3.3 * (float(val)/4096);
  Serial.println(voltage);
  delay(1000);
}
When I measure the voltage between PB0 and ground using a multi-meter, it is showing the correct analog value. Therefore I am thinking if this is a problem with analogRead.

Can anyone please tell me how to resolve this issue?
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: analogRead showing a digital behaviour

Post by dannyf »

analogwrite() produces a digital output whose duty cycle carries the analog information.

what you observed is expected.
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

Re: analogRead showing a digital behaviour

Post by GonzoG »

F103C8 does not have DAC (analog output), only PWM so what you get is correct.
If you want analog output you need MCU with DAC or external DAC.
mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: analogRead showing a digital behaviour

Post by mrburnette »

tyrooryt wrote: Sun Sep 20, 2020 3:47 pm ...
When I measure the voltage between PB0 and ground using a multi-meter, it is showing the correct analog value. Therefore I am thinking if this is a problem with analogRead.
...

TYROORYT:

Your homework is to read the text here on analogWrite()
tyrooryt
Posts: 8
Joined: Fri Jul 24, 2020 11:36 am

Re: analogRead showing a digital behaviour

Post by tyrooryt »

Thank you all for the replies.
I did not realize that there is no DAC in the board, so I was under the assumption that I can simply use analogWrite.

@mrburnette thanks for the link! It mentions that an analog output can be obtained by filtering the digital output, so I believe a series RC will suffice to get an analog voltage.
mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: analogRead showing a digital behaviour

Post by mrburnette »

tyrooryt wrote: Tue Sep 22, 2020 5:37 am Thank you all for the replies.
I did not realize that there is no DAC in the board, so I was under the assumption that I can simply use analogWrite.

@mrburnette thanks for the link! It mentions that an analog output can be obtained by filtering the digital output, so I believe a series RC will suffice to get an analog voltage.
You are correct, an RC circuit will "integrate" the digital pulses.
The (approximate) average voltage will appear across the capacitator during the +3.3V high (Source event) and will decay when the pulse is low (sink event.) As the CMOS output of the uC is complimentary, care must be exercised to ensure the chosen RC values do not exceed the maximum DC sink/source current for the output pin.

You can work the RC out manually or use an online circuit simulator to model.
RC.jpg
RC.jpg (42.19 KiB) Viewed 3814 times
In the circuit, the 100K resistance represents the DC impedance of the measuring device/meter and all leakage resistances.

Code: Select all

$ 1 0.000005 24.46919322642204 97 5 50
R 112 112 64 112 0 2 1000 3.3 0 0 0.5
g 112 240 112 288 0
r 112 112 192 112 0 1000
c 192 112 192 240 0 1.0000000000000001e-7 -0.5543658984576727
w 112 240 192 240 0
w 192 112 304 112 0
r 304 112 304 240 0 100000
w 192 240 304 240 0
p 304 112 384 112 1 0 0
o 8 64 0 4098 5 0.1 0 1
tyrooryt
Posts: 8
Joined: Fri Jul 24, 2020 11:36 am

Re: analogRead showing a digital behaviour

Post by tyrooryt »

Thank you @mrburnette for the detailed reply. Thanks for the warning too, I might have missed that and damaged the board.
I will try out the simulator first.
Post Reply

Return to “General discussion”