Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: Measure mV

  1. #1
    Member
    Join Date
    Nov 2020
    Location
    Czech Republic
    Posts
    49

    Measure mV

    Hi all,
    I have function generator (Joy-it JDS6600 lite) and I want generate sine wave 0,02V RMS = on generator I have to setup 0.06V because 0.06 is peak-peak, then divided 2 to have 0-peak and multiply by 0,707 (RMS). So for 0.02V RMS I have to setup 0.06V peak-peak. I hope this is ok.
    Now I read 4096 samples. Every sample every 50micros via ADC. ADC has only positive values = on plotter I can see half sine.
    When I try find max value = 0-peak then I can see 0.024.

    I need to measure volts in RMS - I want to see on Serial monitor value 0.02V - any idea how to do it? Thanks!


    My code:

    Code:
    #include <ADC.h>
    const int readpin = A1;
    ADC *adc = new ADC();
    #define AVERAGING 8
    #define RESOLUTION 12
    #define ADCSAMPLES 4096
    float adcbuffer[ADCSAMPLES];
    float volt = 0;
    float mmax = 0;
    float adc_val = 0;
    int k;
    
    void setup() {
      Serial.begin(115200);
      adc->adc0->setAveraging(AVERAGING); 
      adc->adc0->setResolution(RESOLUTION); 
      adc->adc0->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED);  
      adc->adc0->setSamplingSpeed(ADC_SAMPLING_SPEED::LOW_SPEED);      
    }
    
    volatile uint32_t adcidx; 
    
    void adc0_isr()  {
      
      
      adc_val = adc->adc0->readSingle();
      if (adcidx < ADCSAMPLES) {  
        adcbuffer[adcidx] = adc_val;
        adcidx++;
      }
    }
    
    void GetADCData(void) {
      delay(100); // wait for usb serial to finish
      adc->adc0->stopTimer();
      adc->adc0->startSingleRead(readpin);
      delay(1);
      adc->adc0->readSingle();
      adcidx = 0;
      adc->adc0->startTimer(SAMPRATE); 
      adc->adc0->enableInterrupts(adc0_isr);
      do {
    
      } while (adcidx < ADCSAMPLES);
      adc->adc0->stopTimer();
    }
    
    void loop() {
    
    GetADCData();
    
    for (k = 0 ; k < ADCSAMPLES; k++)
      {
        volt = adcbuffer[k] * (3.3 / 4095);
        if(volt > mmax)
        {
          mmax = volt;
        }
       }
      Serial.print("0 - Peak: "); //I will print 0-peak value
      Serial.println(mmax,4);
      mmax = 0;
      delay(1000);
    
    }

  2. #2
    Senior Member
    Join Date
    Jul 2020
    Posts
    1,235
    You need to shift the signal to mid-way between the voltage rails. The negative voltages cannot be measured by the ADC,
    and if more than 0.3V or so could damage the chip permanently.

    Normally a capacitor and 1:1 voltage divider are used to shift the voltage to mid-rail, and a series resistor can improve
    robustness to over-voltage.

  3. #3
    Senior Member BriComp's Avatar
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    412
    Can you expand on that, perhaps with a diagram. I googled but got nothing like you explained.

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    25,057
    How about 2 diagrams?

    You can find one similar to MarkT's suggestion on this audio library design tool page. Look at the right side documentation column and scroll down to Hardware.

    https://www.pjrc.com/teensy/gui/?info=AudioInputAnalog

    This circuit is slightly more complex. The first 47K resistor at the input can be omitted if your signal already has a well defined DC level. The other extra parts try to filter away noise which may be present from the power supply. If you don't care about that, just 1 capacitor and 2 resistors can do the same thing.


    The downside to this circuit is it acts as a high pass filter. Usually that's fine if you wish to measure a sine wave or other signals which well are above the filter frequency. Choosing the capacitor large enough lets you get the filter frequency low, so it's usually not an issue.

    But if you want to measure DC or very low frequencies, here is another way.

    https://www.pjrc.com/control-voltage...log-input-pin/

    Using just 3 resistors is simple, but if your signal source isn't strong (technically speaking, low impedance) the resistors can create a load which causes inaccuracy. You can find a link on that page to old forum conversation about how to do it better (but more work, more complexity) with opamps.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •