1
\$\begingroup\$

I am a beginner in Arduino programming.

I want to acquire two analog signals using an Arduino Portenta.

In the technical specification of the Portenta H7, it mentions that it has 3 ADCs. Since 1st analog read command consumes some time before reading the 2nd analog read, how do I get away with the time-gap between the reading of these two analog signals? What should I incorporate through code that it will dedicate 1st ADC for 1st analog signal acquisition and 2nd for the 2nd sensor so that both of them are processed at once?

Normal code for Analog input reading

Arduino portenta specification

\$\endgroup\$
3
  • 1
    \$\begingroup\$ You might have to access the registers directly instead of using the analogRead function. \$\endgroup\$
    – user253751
    Feb 2, 2021 at 12:27
  • 1
    \$\begingroup\$ What time gap can you tolerate? With three adcs, you should be able to synchronise the sampling, but you might have to write your own code to do that. \$\endgroup\$
    – Kartman
    Feb 2, 2021 at 12:27
  • 1
    \$\begingroup\$ Thank you, Kartman. Can you refer me to some example code I can take help from? I am aiming for the maximum possible sample rate e.g. up to 100KSa/sec so this might be very helpful for achieving the desired sample rate. \$\endgroup\$
    – Basit Ali
    Feb 2, 2021 at 12:39

1 Answer 1

3
\$\begingroup\$

The Arduino analogRead() function is a blocking function. That means when you call it the code initiate an ADC conversion and then waits for it to complete and return you the value. So using this method you cannot read two channels simultaneously.

To do what you are asking is a more advanced topic but basically the process is:

  1. Setup timers in the STM32 to trigger conversions on ADC0 and ADC1 starting at the same time.
  2. Have the ADCs generate an interrupt when the conversions are complete.
  3. Read the ADC values in your interrupt routine directly from the ADCs registers without using analogRead().

Again, this is a more advanced topic. As a beginner you may just want to live with the difference in times between the two readings until you build up some experience with the STM32.

\$\endgroup\$
6
  • \$\begingroup\$ Thank you. This might be advanced to me, however, could you please refer me to some reference or example code I can take guidance from. \$\endgroup\$
    – Basit Ali
    Feb 2, 2021 at 12:33
  • 2
    \$\begingroup\$ This is an excellent site for more advanced STM32 programming. Note that it uses the STM CubeIDE instead of Arduino but you can still adapt these techniques to the Arduino environment. deepbluembedded.com/… \$\endgroup\$
    – jwh20
    Feb 2, 2021 at 12:51
  • \$\begingroup\$ Thank you. It will be very helpful. I have one follow-up question from the website link you shared. ''This trigger function can cause high CPU loading, Especially when you’re doing so many conversions per second. ''. So is it an efficient approach when you need a high sample rate? I mean, will this trigger function cause a negative effect on the sample rate, if used? \$\endgroup\$
    – Basit Ali
    Feb 2, 2021 at 12:59
  • 2
    \$\begingroup\$ The CPU loading depends on the conversion rate. How often do you intend to sample your inputs? Since the ADCs in the STM32 are quite fast, you can overwhelm the CPU if you interrupt too often. That depends on what else you are doing. You also have the DMA option to gather ADC results without any interrupts. \$\endgroup\$
    – jwh20
    Feb 2, 2021 at 13:03
  • 2
    \$\begingroup\$ At 100KSa/sec my first impression is that the STM32 CPU should have no issues handling that interrupt rate. Also keep in mind that the Portentia has a dual-CPU architecture which lets you easily dedicate the 2nd CPU to handing the ADC interrupts and the 1st CPU doing you other stuff. \$\endgroup\$
    – jwh20
    Feb 2, 2021 at 13:27

Your Answer

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

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