0
\$\begingroup\$

I have a joystick module for Arduino connected to my PA0 pin (that pin is set as ADC1_IN0). I would like to read the voltage from that pin but there is a problem. The voltage on which the joy-stick module operates is 5V and HAL functions I'm using to read Analog values are referencing it against 3.3V, which renders part of the module (which is essentially a potentiometer) useless and nonfunctional as the value given measured is gonna be simply 4095. Here is the code that I have in the while(1) function in main():

      HAL_ADC_Start(&hadc1);
      HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
      raw = HAL_ADC_GetValue(&hadc1);

How can I make it reference this voltage against 5V so I can get the full reading?

\$\endgroup\$
4
  • \$\begingroup\$ You can't measure 5V analog inputs directly with that part. Datasheet page 67, Table 45 "ADC charactersitics" - Vdda max is 3.6V and Vref cannot be higher than Vdda. If you've already tried to put 5V on any of your analog inputs it's likely that you've damaged the part, since none of them are 5V-tolerant pins (Datasheet, page 26, Table 5, look for pins with 'FT' in the I/O Level column). \$\endgroup\$
    – brhans
    Commented Apr 20, 2020 at 1:35
  • \$\begingroup\$ @brhans So is there a way for me to measure the whole spectrum of my joystick? I'm very new to STMs. I read on a few pots about similar things like that, that what I would have to do is to measure it against a different voltage provided through some pin. But This (I guess) would require me to use somehow Vref and as I see now from what you linked it cannot exceed 3.6. So Do I have to just cope with it being the maximum or do you maybe see a different solution? \$\endgroup\$
    – John
    Commented Apr 20, 2020 at 1:36
  • \$\begingroup\$ You need to reduce the range of your input to match what the STM's ADC can handle. Use a unity-gain rail-to-rail opamp (with 5V supply) to buffer the input then feed it thru a voltage divider (for example a 3k3 + 6k8 would turn 5V into about 3.3V). \$\endgroup\$
    – brhans
    Commented Apr 20, 2020 at 1:41
  • \$\begingroup\$ @brhans okay, I understand. I was really hoping that I could solve this just with code. Thank you very much for the help, I'm new to this and I really appreciate your help :D \$\endgroup\$
    – John
    Commented Apr 20, 2020 at 1:45

1 Answer 1

2
\$\begingroup\$

The STM32 ADC does not support reading voltages that are larger than the supply voltage. Either change the joystick reference voltage to match the STM32 voltage or divide the joystick voltage down with resistor divider.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you. I think I will go with the voltage divider :) \$\endgroup\$
    – John
    Commented Apr 24, 2020 at 22:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.