Notifications
Clear all

ADS1115 AC voltage conversion to ESP8266

17 Posts
3 Users
0 Likes
141 Views
(@krassimir_db_forum)
Member
Joined: 2 weeks ago
Posts: 8
Topic starter  

Hi everyone, 

I am trying to measure AC 230 line voltage with ADS1115. I am using step-down transformer to abt 9 v and a load of 1.3 k resistor. In parallel I have a voltage divider with trim R for fine scaling adjustment. I also managed to simulate 230 voltage drop with about 5-10 V in order to test the final measurement. The measurement voltage is passed to the GND and pin 2 of the ADS1115 for single measurement. The settings are for continuous conversion (library <ADS1X15.h>). The code is as bellow:

float getVoltage(int counterADS) {

float result;
int16_t rawReading = 0;
int16_t maxRaw = 0;
float voltageReading;
float maxVoltage = 0;
float sum = 0;
int counter = 0;
const float scaleFactor = 250 ;
long timing = millis();
ADS_Device[counterADS]->setGain(4);
ADS_Device[counterADS]->setDataRate(0);
//float f = ADS_Device[counterADS]->toVoltage(1);
while (millis() - timing <= 1000) {
rawReading = ADS_Device[counterADS]->readADC_Differential_2_3();
voltageReading = ADS_Device[counterADS]->toVoltage(rawReading);
sum += sq(voltageReading);
counter = counter + 1;
}
result = sqrt(sum / counter) * scaleFactor;
Serial.println("Voltage = " + String(result, 3));
return result;

}

NB. 'ADS_Device[counterADS]' is an object member of an array of three ADS, counterADS is 0,1 or 2.

Here is the problem:
When the 230 V drops down from say 235 to 227, the converted voltage INCREASES instead to decrease! The measured voltage slightly goes down by some mV as expected (value about 3.9 V).

Any ideas of why this happens? Thanks....


   
Quote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7032
 

Posted by: @krassimir_db_forum

Hi everyone, 

I am trying to measure AC 230 line voltage with ADS1115. I am using step-down transformer to abt 9 v and a load of 1.3 k resistor. In parallel I have a voltage divider with trim R for fine scaling adjustment. I also managed to simulate 230 voltage drop with about 5-10 V in order to test the final measurement. The measurement voltage is passed to the GND and pin 2 of the ADS1115 for single measurement. The settings are for continuous conversion (library <ADS1X15.h>). The code is as bellow:

float getVoltage(int counterADS) {

float result;
int16_t rawReading = 0;
int16_t maxRaw = 0;
float voltageReading;
float maxVoltage = 0;
float sum = 0;
int counter = 0;
const float scaleFactor = 250 ;
long timing = millis();
ADS_Device[counterADS]->setGain(4);
ADS_Device[counterADS]->setDataRate(0);
//float f = ADS_Device[counterADS]->toVoltage(1);
while (millis() - timing <= 1000) {
rawReading = ADS_Device[counterADS]->readADC_Differential_2_3();
voltageReading = ADS_Device[counterADS]->toVoltage(rawReading);
sum += sq(voltageReading);
counter = counter + 1;
}
result = sqrt(sum / counter) * scaleFactor;
Serial.println("Voltage = " + String(result, 3));
return result;

}

NB. 'ADS_Device[counterADS]' is an object member of an array of three ADS, counterADS is 0,1 or 2.

Here is the problem:
When the 230 V drops down from say 235 to 227, the converted voltage INCREASES instead to decrease! The measured voltage slightly goes down by some mV as expected (value about 3.9 V).

Any ideas of why this happens? Thanks....

Screenshot 2024 04 26 at 07.22.10

 

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@davee)
Member
Joined: 3 years ago
Posts: 1698
 

Hi @krassimir_db_forum,

  I agree with Ron (@zander), it would be helpful if you posted the code properly. Others may spot a code bug.

  However, after my quick glance at the code as posted, I was wondering if the A/D (Analogue-to-Digital) process was working properly, perhaps with the A/D 'seeing' a voltage below its 'zero' or above its maximum, and hence giving clipped or weird values.

Also, I note the ADS1115 has a PGA (Programmable Gain Amplifier), which might give funny effects if the applied input voltage exceeds the range it is expecting.

I suggest you start trying to examine the individual values, perhaps plotting them using the serial plotter, looking for distortion from the sine wave shape, checking how it changes with different voltages.

You don't say how the 230Vac voltage is changing ... perhaps you have an autotransformer, or maybe you are just looking at different times?

If you are looking at different times, which may be tedious for tracking down the bug, then for a convenient experiment connect the transformer output across a lowish-value potentiometer, say 1kOhm, and then measure the voltage from one end of the track to the slider.

If that isn't showing any problems, try making single-ended measurements, rather than differential, and also, try measuring dc voltages in the same voltage range, and examine the digital values, possibly using the serial monitor.

Sorry, this is a rather vague strategy, that you will need to adapt as you proceed, but there are lots of ways the signal can become distorted by the measuring system, and if it only prints a single number, then it may not be obvious that the underlying amplifier and/or A/D measurement system has a problem.

Good luck and best wishes, Dave


   
ReplyQuote
(@krassimir_db_forum)
Member
Joined: 2 weeks ago
Posts: 8
Topic starter  

@davee Hi. Thanks for your reply. 
First, I apologize for non-uploading the code as required, further more, the example code is not exactly the one - I've been in hurry and swapped the snippets.
Then, let's go in details one-by-one of yours above:

Monitoring: I am using O-scope Pico2000 and multimeters. All values are as required at the pin 2 of the ADC - increments are increments and decrements are decrements.

Values: I am not providing any values herewith, as I have been playing to scale the source voltage thru the whole allowable input values (they should not exceed the Vdd here 5V). Also I was testing all PGA settings. To eliminate hardware reason I swapped 3-4 ADC devices. 

AC 230 V drop simulation: I use a separate cable from the receptacle and a load of a heater (1500 W). The power cable is of slightly smaller diameter that is not quite sufficient for the heater and switching it on and off causes the local voltage drop-down of about 7-10 V, which is quite enough. Of course this is not safe, but I am here and I am aware of what I am doing (the cable slightly heats to about 40 C, but for sure will not burn).

Other ways of simulation: I do not want to directly connect a N or any other L or G grid cables directly to the circuitry, you know why no doubt. That's why I use a dropdown transformer trusting the proportion of the transformed voltage to the measured one.

Measurement mode: As I apologized above, the sketch snipped is not exactly what is used in the case: The voltage for conversion is connected to pin 2 of ADC and GND as the datasheet reads. The conversion line is <uint16_t rawReading = ADS_Device[counterADS]->getMaxVoltage(2) * f;> where <float f = ADS_Device[counterADS]->toVoltage(1);>
As I said above, I have tested all PGA settings to the device. The probe voltage is rectified before pin 2 and resistor divider only (+) half-sine is passed for conversion. Then during 1000 millis the samplings are averaged and passed to MCU.

What is curious here is, that at the same time, the other two pins (0 and 1) of the ADS1115 are connected to a current transformer SCT013 in differential mode in order to measure the current and that part of the code is working perfect and I get about 6 amps RMS that is for the load of 1500 W heater (the link is split and I am also using standard clamp meter for testing).

As you can see, before I get rid of the problem for voltage conversion I cannot proceed any further with scaling etc:

 


   
ReplyQuote
(@davee)
Member
Joined: 3 years ago
Posts: 1698
 

Hi @krassimir_db_forum,

  Please be careful ... I do not want to feel I am in any way linked to a premature decrease Bill's (@dronebot-workshop) subscriber numbers. I certainly do not recommend using an overloaded mains cable as a 'dropper resistor'.

Note that mains cable wiring is often PVC insulated, which is fine at 'room temperatures', but if the copper wire gets too 'warm', it will gradually 'migrate' through the plastic, and eventually short to one of the other conductors, or possibly even become exposed at the outside of the cable. Although the outside of the cable may only be 40C or so, as plastic is a poor conductor of heat, the copper temperature can be much higher. At the very least, I would suggest that you mark the length of cable that has been 'overheated', for replacement at the end of this experimental phase, because I suspect it may be damaged and more likely to fail in the future, when you have forgotten its historical sacrifices to experimental research.

------

To give yourself 'variable' AC voltages, how about a connecting a potentiometer track of say 1kOhm across the 10V transformer, and use connections to one end of the track and the slider for a 'controllable' voltage to measure? As you describe, you already have a meter and a 'scope to determine the voltage that your A/D is sampling at the same time? (Unless you can see a reason why that will not work.)

If you don't have a pot, then a couple (or more) of resistors, say 1KOhm and 100 Ohm in series, could be used for roughly 91% and 9% of 10V, and so on.

------

Sorry, but without setting up a similar test set, which for me would include procuring an A/D, etc., I don't think I could find any subtle software bugs

But my suggestion is to look for a clipping effect, which could be at integer limits (e.g. -31768/32767 for 16-bit signed) in the A/D or software, or it could be in the analogue parts, such as PGA output, or A/D input, approaching a rail voltage.

Also remember, with differential measurements, which by definition is the difference of two values, again in both analogue or digital sections, the difference of two voltages or numbers may be well within range, but one of the voltages or numbers may be clipped.

Your comment that your current measurement works, which I presume uses the same RMS calculation method, suggests you have the basic code structure correct, and also suggests a 'raw' measurement error, or possibly something caused by range limited numbers in the calculation, that (by chance) the current values do not encounter.

Hence, I hoped, that plotting the values may reveal some clues.

Of course, the cause could be something else, but 'raw' measurement errors are a common stumbling block, particularly when measuring a voltage that is not a 'simple' voltage referenced to ground.

Good luck, stay safe and best wishes, Dave


   
ReplyQuote
(@davee)
Member
Joined: 3 years ago
Posts: 1698
 

Hi @krassimir_db_forum,

As a supplement to the diatribe above, remember that in addition to the requirement that the differential voltage applied to the input stays comfortably inside range, it is also necessary that both input pins forming the differential input stay comfortably inside the range 0V to Vdd, with respect to the A/D's 0V line.

As you are feeding 'rectified and divide' ~10Vac from an isolating transformer, it is unclear to me, how the 'ground/0V' reference of the A/D is related to the 0V of your incoming rectified AC waveform.

NB If you are still stuck, sometimes posting a circuit diagram of the 'exact' wiring in use shows up the problem. Textual descriptions of wiring are often insufficient to spot the problem.

Best wishes, Dave


   
ReplyQuote
(@krassimir_db_forum)
Member
Joined: 2 weeks ago
Posts: 8
Topic starter  

Hi @davee and thanks again for the reply.

I appreciate very much your concern regarding the safety. Please do not worry, this is standard, for domestic purposes adapter (1 in - 3 out), some call it adapter, some call it extension cord, purchased on the market duly marked for max load of 10 amps to use for some home machines as a grinder etc. As I said that is heated at about 40 C I did not measure, just felt by hand, maybe less. Also, I know what I am doing and will never take an excessive risk.... I just found out that for the purpose, the drop-down of some 5-7 volts is Ok and sufficient to judge if the measured values compare. Also please note that the cable is heating very close to the socket, so most probably there is a bad contact inside and I promise that will change it as soon as my tests are completed. However, the desired effect is on hand.... 🙂 ...

Further, I explain again the circuitry (last, made yesterday):

There is a step-down transformer 230 to 9 V AC. Then is a rectifier bridge. Then is a resistor load in parallel to (+) and (-) of the bridge of 10 K. Then I have 1 resistor of 22 K in series with a trim pot (multi-turn) of 10 K, both in parallel to the above as a voltage divider. One end of this group (that is (-)) is connected to the GND and the pot slide goes to pin 2 of the ADC. So, to the ADC for conversion I am passing the adjusted voltage, as you can reckon, from 0 to about 4 volts (on o-scope peak). Later I can place here the schematic, but it is very simple and I do not think it will be necessary...

And now, some good news (but I do not think that the problem is closed): 
At ADC.setGain(2); and lower (these are 1 and 2/3) I got good results: the reading decreases when the main AC drops, and opposite.
However, at ADC.setGain(4); and higher (these are 8 and 16), the problem appears again.

Then, I found that the standard float f = ADC.toVoltage(1) conversion factors (15 bit reading is of int16_t and is converted to voltage by * f) have to be re-calculated for the case and to be placed in the code as a const.

NB. From existing alternatives finally I selected the code to extract max sampled value instead of average one for a time of 1000 millis.

Thanks again and if you would have any comments on the above, please revert.. 


   
ReplyQuote
(@krassimir_db_forum)
Member
Joined: 2 weeks ago
Posts: 8
Topic starter  

@davee Hi, fully agree on your last (the supplement to 'diatribe'), especially about the behavior of ADC within the limits of rated voltage values. I have also felt something sort of, but the data sheet and library could not guide mе to that clearly...
Despite that the schematic is quite simple, I will provide an image later tomorrow as I do not have it drawn nicely and suitable for uploading....


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7032
 

@krassimir_db_forum Since I am seeing 230VAC plus I am not familiar enough with most of the devices you mentioned AND since Dave (@davee) is assisting, I am dropping out. All the best in your endeavours.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@davee)
Member
Joined: 3 years ago
Posts: 1698
 

Hi @krassimir_db_forum,

  It sounds like you are starting to get a grip of the problem. It maybe you have more than 1 issue to deal with .. that is pretty common.

I suspect that with some hardware settings you are driving beyond the electrical limits, which was also my original suspicion. You may also be pushing integer ranges, or similar, in the software under some conditions. These are all 'typical' problems to look out for.

I think you are probably at the point where you can follow it through and do the necessary analysis. I suggest, you try to analyse each stage at a time, and characterise its limits. In most cases, I think the reason for the limits will be fairly obvious, once you figure out how to distinguish between (say) the PGA inputs exceeding 0 to VDD (or whatever the data sheet says), from the PGA output exceeding 0 to Vdd, and so on for the A/D converter as well. This can be a bit tedious and hard to begin with, but I think you will get the hang of it with a little practice.

----------------------

Note, when reading the data sheet, be careful to understand what each voltage limit is implying. e.g. Absolute maximum ratings tend to mean if you exceed this value, you may damage the chip. Hence, you might see something like [VDD + 0.3V].

However, that does not mean that the chip will measure correctly, when measuring a voltage that takes it to (say) [VDD + 0.1V]. It is probable that you need to keep it to a bit less than Vdd; sometimes as much as a volt or more, for accurate measurements. These sorts of 'margins' are sometimes explained clearly in the data sheet, but not always. Sometimes you have to do the tests and find out yourself, the hard way. For a reliable design, you should always check in this way, even if the data sheet appears to be sufficient.

Note, I do not have any experience of this particular chip, so the two last paragraphs are just general things to beware of for any chip like this, from any manufacturer, not a specific chip review.

--------------

If you publish a circuit diagram, then I'll have a look at it, but from your text above, I have a general impression. I may be able to spot something, but I am beginning to think your wiring is generally, OK (excepting perhaps your choice of 230Vac extension cables 🤔 🤔), and maybe your problems are driving the system to its limits in one or more places.

Best wishes and I hope you finish your project successfully, Dave


   
ReplyQuote
(@krassimir_db_forum)
Member
Joined: 2 weeks ago
Posts: 8
Topic starter  

Hi @davee and thanks again for your concern on the topic.

As I promised, please find the schematic attached herewith.

I am trying different approaches, from differential measurement thru single conversion and measuring the average value or, just detecting the max value etc etc. For example, while measuring the average value, I found that on the o-scope the half-sine is changing from 0 to max as expected, but the ADC values read on the serial monitor all values of 0 or close to 0 were missing (this for sampling of the half-sine). Illustration in the attached file.

Finally, I decided to measure the max value of the half-sine and the code snipped is also attached.

Of course, I can use the analog pin of the MCU as ADC, but as far as I use much more precise device as ADS1115, especially designed for the purpose, this is better alternative. I reckon that the 15-bit is quite enough to provide sufficient resolution. The data sheet reads that I have 2 mV per bit at a gain (1), and this in decimal and scaled would produce say scale factor of 250 / 4 = 62, then 62 * 2 mV = 124 mV and the accuracy that satisfies me is +/- 0.5 V which is quite OK. 250 is the max value that is scaled to 4 V to ADC, so the 230 will correspond to some 3.68 V and eg 230.5 would go to 3.69. Of course, I can go to higher gain, but then comes the problem that I faced to and topic itself!!! Or maybe I am wrong with these calculations?

Unfortunately, digging in the net provides any other info but not the one that I need. That's why I am looking for help here.....

Have a good weekend, Krassimir

 

NB. Do you know if I can address the problem directly to Bill and how to do this, and will that be a good decision?


   
ReplyQuote
(@davee)
Member
Joined: 3 years ago
Posts: 1698
 

Hi @krassimir_db_forum,

  Thanks for the update. I have looked at your circuit, and I can't see any 'obvious' mistakes, but as I discuss below, the problems may be that the circuit is 'too simple', due to limitations of the chips, etc.

--------

  Sorry, I can't reproduce your experiment, as I have neither the time, nor the A/D etc., so the best I can manage is give you some guesses as to what you might look for. Your situation is essentially the same as any engineer would find themselves when presented with a completely new problem, working with new chips, etc. The data sheets will tell you part of the story, but you have to use experience, patience and a general 'questioning', 'what if' and so on approach, to discover all the little details that are far from obvious when you start.

Personally, I would begin with trying to find out more about the A/D. I would probably start with just a battery and a pot, to get a much better 'feel'.

Your observation that the 'low' values are missing leaves me to think that the amplifier may be introducing a voltage offset, so that every voltage is (say) 100mV less than would be expected if it was 'perfect'. So that, when the input should give 50mV to the A/D, then the offset will try to shift it to +50 -100 ... which is mathematically -50 mV. But of course, the lowest voltage the chip has available, is 0V, so that 0V is the voltage the A/D 'sees', and in turn digitises.

Of course, the last paragraph, although based on my general experience and facts, is presently little more than a fairy tale with respect to your system, that I just made up, because I haven't looked at your system on a bench, with meters, scopes and so on. But it is intended to give you an idea as to how you might approach the problem. That is, start with a plausible 'story' of what might be happening, devise some measurements to try to determine if the 'story' is true, and if it proves to be true, find a method of overcoming the problem. 

I haven't looked into the possibilities of how to fix such a problem, but if my 'story' proved to be 'true', then you might need to find a way of adding a known dc offset to the voltage being measured, so that all the readings were of  'positive' voltage. Or perhaps you will find an alternate approach.

I hope that gives you some clues as how you might proceed.

BTW in terms of accuracy, bear in mind that a rectifying diode has a forward voltage drop, typically around 0.6V for a silicon diode, but different types of diodes have different values, so that the voltage from a bridge rectifier as shown in your circuit, might be about 1.2V less than the transformer output. Of course, if this voltage drop will also apply to scopes, meters, etc. if they are measuring the voltage at the output of the bridge, so this is probably in addition to the other effects you have observed.

-------------------------------

You mention you are trying different approaches, including calculating RMS, and measuring peak values and calculating RMS, assuming it's a sine wave. Both of these methods are found in commercial equipment, and the reputable manufacturers will specify which one they are using. I think 'cheap' multimeters are likely to measure the peak, or some kind of 'average', and assume it is a sine wave, whilst some of the more expensive models may claim to be 'true RMS' or similar. There is no absolute rule that covers all cases. It is the responsibility of the person doing the measurement, to know the capabilities of their test equipment and act accordingly. Of course, all such measurements will also be affected by frequency, in that the test equipment will only be able to accurately measure waveforms over a certain frequency range.

-------------------------------

I am sorry, but I think I am approaching the point where I have given as much help as I reasonably can, with regard to problems you are seeing when attempting to digitise a waveform.

You mention contacting Bill (@dronebot-workshop). Whilst I in no way represent him, my feeling is that in general, this is not an appropriate approach. If you look at his (very impressive) YouTube statistics, he has 590,000 subscribers, and his recent discussion on Diodes has already had 26,000 views, whilst an older introduction to ESP32 has had 1,500,000 views. So I think you will realise that it is extremely unlikely that he will be in a position to offer advice on personal projects.

However, he does have a section on the forum, which members are invited to use to suggest topics for a specific video + blog, should you see one that you think would be appropriate. (Before posting on that section, I recommend you have a look at the existing library that he has created, to get a good idea of the type and style that he covers.)

I wish you well with your project. Whilst your query is not of the type that I can answer in a couple of sentences, I hope I have given you some insight into how to proceed.

Best wishes, Dave


   
ReplyQuote
(@krassimir_db_forum)
Member
Joined: 2 weeks ago
Posts: 8
Topic starter  

@davee Hi Dave, 

Thanks again for your above reply.

I feel happy to advise you that I solved the problem today and now all works smooth and fine....!!!

Finally, it came out that the sampling speed was very low and there is no time the device to succeed to read the close to 0 values. 

It is interesting that all information on this ADC device just do not pay enough attention on this parameter setting of the device (ADS.setDataRate() in the library). For the previous modification the setting values are from 0 to 7 while on the latest modification they can be only 0, 4 or 7 and, even the recommendation is that the value 7 should not be used as it is open door for any kind of noise...
So, I just neglected the value of 7 and that was the mistake (which cost me a huge amount of wasted time!).

Otherwise, as I pre-calculated, the max voltage level should be at gain(1) +4V referred to the GND for single conversion scaled to 250 V and thus the value for typical grid voltage of 230 V came out to be 3.68 V. In practice power company standard fluctuations are normally from 218 to 235 V, but local trafo of my place some times drops down even to about 210 V, especially evening time during winter....So, I removed all unnecessary resistors after the rectifier bridge (thanks for reminding me to count also on voltage drop on diodes!) and only two limit resistors from both ends of the pot were left, one of 10 K to prevent increasing the voltage above the upper limit and one of about 2 K from the other end of the pot, in order to 'extend' as much as practicable the active adjust range of the pot (the voltage drop on diodes is also compensated here along with other errors).

Well, FYI I changed the 220 V plug that I used to simulate voltage drop - there was a bad contact inside and it was of solid constriction 😀  so now all is safe...

Thanks and best regards,

Krassimir

NB. As I am new here, could you please shortly guide me shall I do something to 'close' the topic or it will no harm if I just leave it as it is?


   
ReplyQuote
(@davee)
Member
Joined: 3 years ago
Posts: 1698
 

Hi @krassimir_db_forum,

 Once again, thanks for your reply. I am pleased to see you are now getting better results, and hopefully you are in a position to complete your project.

As for closing a topic, I think if you look at the latest post of this thread, towards the top left you will see a button labelled 'Solved', which I presume you click. (Sorry, I haven't been in the position to use it!)

It looks like this:

image

Wishing you all the best for the future,  Dave

 


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7032
 

@krassimir_db_forum FYI @davee It's on your original post where you mark Solved.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Page 1 / 2