Emonlib and ADS1115 on nodemcu

Hey guys and girls!

After reading a lot on the forum here i bit the bullet and signed up! I have a question for you!

I will describe what i have at this moment. I made a diy board which i can connect to four sct sensors. I can read this with my Arduino nano on four analog inputs. Then I wanted to put this on my nodemcu so i can send these current readings to emoncms. Ofcourse that board only has one analog input and i need to use a ADS to get more analog inputs with I2C.

I have the ADS1115 and i can read the voltage from the current sensors very good. But I want to implement the readings from the ads directly into the emonlib library. I tried to edit the emonlib.ccp file and changed the:
startV = analogRead(inPinV); to startV = ads1115.readADC_SingleEnded(inPinV);

And in the sketch I used emon1.current(0, 30); to create an instance that directs to the right pin.

This is where my mind gets puzzeled. How can i load the library of the ADS into the emoblib library?

If someone wants to point me in the right direction - maybe with an example?

Thanks!

Bramco's picture

Re: Emonlib and ADS1115 on nodemcu

Afraid I don't have a solution, but I was planning on doing the same. So if you do get a resolution, I'd be interested to see the code.

Simon

Robert Wall's picture

Re: Emonlib and ADS1115 on nodemcu

I haven't got time to search out the ADS library, if you tell me where it is, I might find time to look for you. I'm guessing that it does the I2C stuff for you, but remember that in emonLib, the pin is read in more places than just that. And you'll need to drive the multiplexer.

But one concern: although the 16 bits resolution might appeal to you, note that you'll only get a maximum of 17.2 samples per cycle from which to calculate the rms value, and that's using a single channel. If you switch channels to read voltage and current alternately, you'll get less than 8.6 sample pairs per cycle. (Meaning you'll only measure the fundamental 50 Hz and the third harmonic at 150 Hz - anything above will alias badly. That'll be more-or-less OK for voltage and nice resistive loads, anything that draws a distorted current waveform will be seriously in error). I don't think that's a good trade-off against 16 bits resolution. 

Bramco's picture

Re: Emonlib and ADS1115 on nodemcu

Hi Robert. The 1015 does up to 3.3k samples per second, so would be a better choice. I had a look at that ADS library, which seems to be from adafruit, but wasn't impressed. It's been a while since I last looked, but I think you'd need to make some changes to get things running in continuous mode. I think it was OK for single shot mode.

I'm hoping to get back to this some time soon.

Simon.

Robert Wall's picture

Re: Emonlib and ADS1115 on nodemcu

If you aren't using all the functions of emonLib, I'd almost suggest you should lift the appropriate sections out and paste them directly into your sketch. 

Having had a very quick look, it would appear that you must (obviously) connect the device using the I2C bus, then assuming you've defined an instance with Adafruit_ADS1015 ads, in your sketch's setup() you must initialise the library with ads.begin(), then you need to pick out the bits of emonLib you'll be using, and for all instances change analogRead(inPinV) and analogRead(inPinI) to become ads.readADC_SingleEnded(0) and ads.readADC_SingleEnded(1) respectively - that's reading voltage and CT1 for an emonTx V3. 

If you're thinking of adding it to one of the 'continuous' sketches, then life gets very interesting and I'd suggest for speed that you do the getting of the input samples that is in the ISR using the register calls extracted from the ADAfruit library to drive the I2C bus registers directly. In principle, you're still doing the same thing as the original (OEM) code, but you're addressing the external multiplexer and ADC via the bus instead of addressing the internal multiplexer and ADC directly.

If you want to change the gain "on the fly" if say the current is low, you probably need to set a fairly wide band and logic goes like "if the rms current is below 10%, change gain one step up" then "if the rms current is above 80%, change gain one step down" so that there's minimal chance of the gain change putting the new measure anywhere near the other switching point, thus preventing the gain switching continually between two adjacent values.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.