Reading Analog Voltage on STM32F103C8T6

Generic boards that are not Maple or Maple mini clones, and don't contain the additional USB reset hardware
Post Reply
adembudak
Posts: 6
Joined: Fri Feb 02, 2018 1:12 am
Location: Turkey

Reading Analog Voltage on STM32F103C8T6

Post by adembudak » Wed Feb 14, 2018 2:39 am

Hello Everyone,

I need to measure voltage for 5V and 12V on STM32F103C8T6 in most accurate values possible. Do you guys dare to help me on this? I have pretty much idea of programming side of things but am really blunder on electronics.

Any idea or tips is welcome. :) :D

User avatar
mrburnette
Posts: 2193
Joined: Mon Apr 27, 2015 12:50 pm
Location: Greater Atlanta
Contact:

Re: Reading Analog Voltage on STM32F103C8T6

Post by mrburnette » Wed Feb 14, 2018 3:21 am

First, read this application note: http://www.st.com/resource/en/applicati ... 211314.pdf

You will need a voltage divider for any voltage over Vcc, if Vref==Vcc
A very stable Vref will be required. Proper bypassing of Vref/Vcc is imperative.

You may find this Web app of use: http://www.falstad.com/circuit/

Andy did a lots of design work on the Pig Scope. viewtopic.php?t=107

Ray

adembudak
Posts: 6
Joined: Fri Feb 02, 2018 1:12 am
Location: Turkey

Re: Reading Analog Voltage on STM32F103C8T6

Post by adembudak » Wed Feb 14, 2018 6:27 am

Thank you @mrburnette, I will read the doc, and try to share my experiences in here.

adembudak
Posts: 6
Joined: Fri Feb 02, 2018 1:12 am
Location: Turkey

Re: Reading Analog Voltage on STM32F103C8T6

Post by adembudak » Fri Feb 16, 2018 1:28 am

Lots of words but no code :( anyone can post sample code. Do I need to read pages of docs to write a function? Why reinvent the wheel? What is the best practice?

User avatar
mrburnette
Posts: 2193
Joined: Mon Apr 27, 2015 12:50 pm
Location: Greater Atlanta
Contact:

Re: Reading Analog Voltage on STM32F103C8T6

Post by mrburnette » Fri Feb 16, 2018 2:32 am

adembudak wrote:
Fri Feb 16, 2018 1:28 am
Lots of words but no code :( anyone can post sample code. Do I need to read pages of docs to write a function? Why reinvent the wheel? What is the best practice?
I do not know what you think a forum is supposed to do, but "we" do not write your code. There are a zillion of examples on the Internet to get you started with Arduino analog scaling of voltage measurements. There are a number of examples that get installed with the stm32duino to help with the syntax:
https://github.com/rogerclarkmelbourne/ ... les/Analog

Here is code for a real Arduino to create a voltmeter. https://www.allaboutcircuits.com/projec ... e-arduino/

You are going to have to learn a bit of electronics because the big part of what you want to do involves an external resistor voltage divider. You can do the math by hand or using a online circuit simulator. I did a quick Google and found numerous examples; an example is here.


Ray
Global Administrator

User avatar
Pito
Posts: 1734
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: Reading Analog Voltage on STM32F103C8T6

Post by Pito » Fri Feb 16, 2018 8:29 am

Analog voltage to read is easy:
Example (provided as-is, no warranties of any kind, use at your own risk):

Code: Select all

// Example how to measure 5-12V DC with BluePill
// You must have a voltage divider wired at the stm32 analog input
// Example of a voltage divider R1=33k, R2=10k for max 12V input
// Provided as-is, no warranties of any kind, use at your own risk
// Pito Feb-2018

uint32_t adcin;                     // data from ADC
float voltage, r_divider, r1, r2;

void setup() {
delay(3000);                        // wait on USB ready
Serial.begin(115200);
r1 = 33000.0;                       // R1 in ohm, 33k = 33000.0
r2 = 10000.0;                       // R2 in ohm, 10k = 10000.0
r_divider = r2 / (r1 + r2);         // calculate the voltage divider ratio R2/(R1+R2)
}
void loop() {
adcin = analogRead(PA0);            // read ADC from PA0 or other PAx pin
voltage = 3.3 / 4095 * adcin;       // voltage at the PA0, ref voltage is 3.3V and 12bit ADC is 4095 steps
voltage = voltage / r_divider;      // calculate the voltage at the divider's input
Serial.println(voltage, 3);         // Print out the voltage in Volts, 3 decimal places
delay(1000); // wait 1 sec
}
.
The ADC in stm32 measures from 0-3.3V (3.3V is maximum voltage you can apply at the input pin with stm32).
You need a voltage divider at the pin input PA0 in order to measure higher DC voltage. Also mind the ADC input is noisy thus you may add a capacitor, for example a ceramic capacitor 10-100nF.
Voltage Divider for STM32.JPG
Voltage Divider for STM32.JPG (16.95 KiB) Viewed 138 times
Last edited by Pito on Fri Feb 16, 2018 9:48 am, edited 1 time in total.
Pukao Hats Cleaning Services Ltd.

adembudak
Posts: 6
Joined: Fri Feb 02, 2018 1:12 am
Location: Turkey

Re: Reading Analog Voltage on STM32F103C8T6

Post by adembudak » Fri Feb 16, 2018 9:46 am

Thank you for you help @Pito, I guess other users also use that scheme and code as a blueprint and also big thanks @mrburnette for sharing that links to direct to the point. I learn from them so much. :D :D

fredbox
Posts: 98
Joined: Tue Jul 07, 2015 4:44 pm

Re: Reading Analog Voltage on STM32F103C8T6

Post by fredbox » Sat Feb 17, 2018 4:17 am

You can also use the map() function to scale an a/d input.
Based on the 33k/10k voltage divider, the maximum count of 4095 occurs around 14.19V.

Code: Select all

Serial.println(map(adcin, 0, 4095, 0, 14190));
scales the value without the decimal point.

Pito's version uses floating point and shows the decimal point correctly.

Post Reply