Power measurements of A0 pin

We are giving power from signal generator to A0 pin and we are trying to read voltage across A0 pin using following program. We are not getting proper readings from program. I have attached a snapshot as well to show disharmony between readings between from signal generator and from code.

Question 1 :- Is it a problem in the code implementation

Question 2:- Does impedance changes across A0 pin and GND pin depending upon how much power you apply at the A0 pin.

Note:- Following code works fine if we provide AC or DC voltage across A0 pin.

SNapshot:-

Code:-

// EXAMPLE USAGE

int analogPin = A0;            // potentiometer connected to analog pin A0
float val = 0.00;                   // variable to store the read value
int AO_reading = 0;

void setup()
{
  Serial.begin(9600);
  Serial.println("Start..");
}

void loop()
{
  AO_reading = analogRead(analogPin);
  val = ((float)AO_reading / 4096) * 3.3;
  //val = val - 1.7;
  Serial.printlnf("value ---> %f", val);
  delay(1000);
}

I think that’s probably the correct result. Well, as implemented, not necessarily what you are expecting.

By default, analogRead() takes the mean of a bunch of samples to determine the resulting value. It think the default is 480 samples.

You probably need to use setADCSampleTime to reduce the number of samples.
https://docs.particle.io/reference/firmware/photon/#setadcsampletime-

An additional error might come from the calculation.
It should actually be

const double VRef = 3.19...; // this should be measured to calibrate the readings
...
void loop() 
{
  val = (AO_reading / 4095.0) * VRef;
}

The reason for 4095 is that this is the max value returned for VRef and only 4095/4095 equals 1.0 otherwise you’d calculate 4095/4096 * VRef.

Minor errors, but they will sum up.

Whenever we are not applying any voltage across A0 pin, still voltage across A0 pin comes around 0.78 V. My query is that why this is not 0 volts. Is there any problem OR it will come around same value only ?

Secondly, if we try to calibrate by subtracting 0.78 V from readings, our other values will give wrong interpretation.

I have attached a snapshot of Input power that we are giving to A0 pin from signal generator. Second column gives readings across circuit of harvester which we are reading using multi-meter and third column is reading across A0 pin. Our analysis is that second column and third column voltage readings should be same as we are giving power directly to A0 pin.

Our Updated code code :-

// EXAMPLE USAGE

int analogPin = A0;            // potentiometer connected to analog pin A0
float val = 0.00;                   // variable to store the read value
int AO_reading = 0;
const double VRef = 3.338; // this should be measured to calibrate the readings

int harvested_voltage[] =   {
                                 0.041, 0.051, 0.064, 0.079, 0.098, 
                                 0.121, 0.149, 0.184, 0.226, 0.272, 
                                 0.329, 0.395, 0.472, 0.553, 0.657, 
                                 0.772, 0.905, 1.054, 1.217, 1.407,
                                 1.620, 1.868, 2.134, 2.431, 2.757,
                                 3.106
                            };
                         
int input_power[] =        {
                                 -30, -29, -28, -27, -26,
                                 -25, -24, -23, -22, -21,
                                 -20. -19, -18. -17. -16,
                                 -15, -14, -13, -12, -11,
                                 -10,  -9,  -8,  -7,  -6,
                                  -5
                           };

void setup()
{
  Serial.begin(9600);
  Serial.println("Start..");
}

void loop()
{
  setADCSampleTime(ADC_SampleTime_1Cycles5);
  AO_reading = analogRead(analogPin);
  
  val = ((float)AO_reading / 4096) * VRef;
  //val = val - 1.7;
  Serial.printlnf("%d ---> %f", AO_reading, val);
  delay(1000);
}

If an analog input is not connected to anything, it will return random values, so that is not a good test.

What is the output impedance of your source? It must be significantly smaller than the input impedance of the analog inputs which run between 20 and 40 kohm. Are you using a terminating resistor on A0? You should read the processor data sheet where it talks about the primarily capacitive load that the analog inputs present.

It looks from the pictures that you are sending a 100 kHz tone into the input at some nominal power level, but again relative to what impedance? Power does not exist with just voltage–you need to measure current or know the resistance as well. As @rickkas7 said above, 100 kHz tone will be difficult to sample with the default settings on a Photon or Electron so you may need to change the speed.

Maybe you should tell us what your real goal is so we can help more?

No. We are not using any terminating resistor across A0 pin. We just want to make sure that A0 readings comes similar to harvested voltages.

If you see above attached code, we are providing power from source which is array int input_power, corresponding to this input_power, we are getting a harvested voltage which we are reading using multi-meter. You can check out these readings stored in int harvested_voltage. We are providing this voltage directly across A0 pin and measuring voltage across A0 pin. So we are not providing power directly. We are providing voltage that is directly feed to A0 pin. But, you can see in snapshot, the readings are not same. Another thing is that we are working with DC power.

@bko according to your comment, you are saying that VI product across output source should come equal to VI product across A0 pin. Is it???

Is there any resistor is connected between A0 pin and GND in core internally.? If it is connected, does it change values?

You can see circuit diagram as well :-

You can suggest if there are any mistakes from our interpretation.

Hi @jdsarode

You don’t say what the source impedance of your source is, but if it is really DC, it is likely to be quite low. What kind of source is it? In your screenshot above in your first post, the PC oscilloscope is showing a 100 kHZ frequency AC signal. Is that what your are using? That is not a DC voltage.

With the corrections mentioned by others above for 4095.0 and the actual measured value of the 3.3V Photon supply that is used an ADC reference, the value you get from the Photon should be +/- 1 ADC level or +/- 0.0008 V with a perfect 3.3V supply and a stable and accurate DC source.

I do not understand what you mean by “harvested voltage” except that you are measuring the voltage across pins A0 and GND which is OK for a DC signal but probably wrong for an AC signal, especially if the frequency is not 50 or 60 Hz. I do not understand what you mean by “input power” in your code; the examples vales are similar to dBm levels but they do not calculate correctly for that scale either.

You can read the datasheet for the processor used in Photon, particularly from pages 124 on regarding the ADC. On page 126 you will see that the input impedance of the ADC is modeled as a series resistor (Radc) and a capacitor to ground (Cadc) both of which vary with the sample rate you select. This is important for AC signals (which your first screenshot indicates you have).

Can you please elaborate process how ADC is read on A0 to A7 pins. And what does it means when you set setADCSampleTime(duration),

ADC_SampleTime_41Cycles5: Sample time equal to 41.5 cycles ===> what does it means of cycles here ?

Hi @bko,

Just ignore first screenshot. My mistake that I asked other problem in similar thread.

We are working with DC power so DC voltage is constant here. But we are not getting similar results on A0 pin. Does this provide whole scenario to you ? If you want, i can start other thread and explain all the process to you in that thread.

Hi @jdsarode

Read the datasheet I pointed you to earlier to understand the 41.5 cycles. This is the sample rate of the ADC and is settable by your program.

You have never said either (1) what the source is or at least (2) what its nominal output impedance is. If that impedance is high, you will not get good readings. The input of the ADC is a switched capacitor circuit and so the input impedance varies with sample rate; again the ST datasheet will clear this up.

Please do not start another thread since I or one of the other elites will just combine them again if it is the same problem.

1 Like

@bko But even if change samples why it should matter as I am trying to read DC voltage. So it should remain same irrespective of cycles. Isn’t it ?

Nope sorry, DC input does not matter. The input to the ADC is switched. It has an inherent sample-and-hold function, so it takes a measurement via sampling and then converts it, takes another measurement and converts it, etc., etc. This is important since the input impedance of the ADC varies with the sampling rate your code sets.

Do you not know the answers to the questions about your voltage source? I can’t help without more information from you that don’t seem to want to provide.

We are using analog signal generator and we are providing RF power at 50 ohm output impedance. For your info, i have attached snapshot of input source and those are dbm levels in the code.

I’m having trouble following what you are trying to do, and how you are trying to accomplish it, others might be having the same problem - maybe you can try again from the beginning.

Having said that, the following is based on my best guess of what you are trying to accomplish and how.

It looks like you are trying to measure some RF signal levels by reading A0. In order to do that, you are going to need an envelope detector and a low pass filter. That would consist of a diode, a resistor and a capacitor, at a bare minimum.

What kind of accuracy & resolution do you think you will need to accomplish your goals ? Due to the physical package used for the STM32 processor, the ADC uses the 3.3V rail as it’s reference, and it may be too noisy to support your goals.You really cannot take a spot measurement of the 3.3V rail and expect to use that to calibrate the ADC results, the instantaneous value seen by the ADC during conversions will be constantly changing within a (hopefully small) range. The built in ADC on the photon is best suited to ratiometric readings based on a proportion of the 3.3V rail, not absolute measurements of an external voltage.

However, if this turns out to be the case, you can use an external, high performance ADC that uses I2C or SPI to communicate with the photon. Your example code suggests you are expecting mV resolution & accuracy, and the photon simply will not deliver that in this configuration. Note: you will still need the envelope detector circuit, even with an external ADC.

2 Likes

There are many things you could be trying to measure from an RF signal generator, but as @AndyW points out, none of them are directly measurable with an analog input (or a multimeter for that matter).

Your captured voltages and power levels are presumably from the multimeter and are not correct either–try to calculate a few values by hand and see. Multimeters are typically designed to measure 50/60 Hz AC signals, not RF signals and the measurements will not be accurate. You can use a scope or specialized RF equipment.

That signal generator is designed to drive a 50 ohm load. That load could be an antenna with an equivalent impedance or a terminating resistor. Either way you cannot get valid measurements without a load.

1 Like

If you are trying to measure RF power, there are ICs that are designed for this purpose, and some have digital outputs.

One example I am familiar with is the LTC5587 from Linear, although there are plenty of others.

I only suggest this because I have a hunch that you are going to need an outboard ADC anyway, so you might want to consider this approach.

1 Like