Hello everyone, in this article we are going to talk about how can we read analog voltage with STM32F407 with Standart Periphals.

First we need to have a STM32F407 processor or STM32F4-DISCO development board. Also we need uVision Keil installed on our computer, We are going to use a potantiometer to set the voltage level between 0VDC – 3VDC. STM32F407 has 3V ADC, arduino has 5V ADC. So we need to be careful to set maximum voltage value is 3V in our project.

So what can we do with analog voltage reading ?

We can read voltage value with ADC. Sometimes we need to use some potentiometers or LVDT’s or something like that… in our project to decide position information. Thrust lever is the best example of this. Connect a potentiometer to the moving part of a thrust lever and when lever move voltage level will be changed.

Now Lets get started to code program. I will share all my code and explain part by part…

Now let me explain what I have done part by part:

In this code block we have a function to read our ADC data from the pin that connected to variable voltage source. This source may be a potantiometer or a LVDT etc… with ADC_Channel_0, we have selected the reading channel of ADC hardware. ADC_SampleTime_xxCycles is a value to set the how many cycle need to be counted to read analog value again. So we can set the sampling time with this value. After making first configurations we are starting the software conversation and than waiting for the reading.

After creating this read method now we need to configure inside of main function. Lets get started to code main function.

Now we have three variables top of in main fonction. We will use these variables to configure and set our pins and ADC values.

In above code we are setting the which buses will be activated. If we forget this section our pins and hardwares will not work. Because these buses are providing oscilated wave and this keeps chips hardwares to work.

Now let me configure GPIO pins for LEDs and ADC.

In this section we have set the PD12, PD13, PD14, PD15 pins to the GPIO_OUTPUT. We are setting these pins. On the BOARD configuration these pins connected to the LEDs. These pins are setted as Open Drain.

Also In second section we have set the PA0 pin to analog hardware port. We will connect this pin to our potentiometer. We have set the mode to GPIO_Mode_AN.

In the last section we are using the adc_data variable to show LEDs. You can set these LEDs whatever you want.

So this is the end of the article.

Keep following my articles.

Have a nice coding.

Burak Hamdi TUFAN…

LEAVE A REPLY

Please enter your comment!
Please enter your name here