News:

;) This forum is the property of Proton software developers

Main Menu

How to use 2 adc pins

Started by Amod, Aug 13, 2021, 09:47 PM

Previous topic - Next topic

Amod

Hello,

Iam making an amp meter and voltmeter using a common display. How can i use an0 and an1 pin of 16f886 at the same time. Thanks

david

Hi,
You have multiple ADC inputs available but they are multiplexed (time shared) into a single sample and hold and ADC converter so you can not sample two inputs simultaneously.  You can sample them sequentially with a very short time lag between them - would this suffice for your application?   You need to consider how fast changing the two individual voltage sources are (worst case) and then determine from the datasheet if you can make an ADC reading on one channel, change channels and take another ADC reading all within that worst case time.   Depending on the noise expected you may also want to consider taking several samples from each source and averaging.

Cheers,
David

Amod

Thanks David,
I need to display ampere for 5 seconds then volts for 5 seconds and so on. I am able to display voltage from an0 pin. How can i use an1 pin also for ampere?

david

Hi,
I may not be understanding your requirement correctly.  You can display a value for as long as you like but it will be the value at the time you sampled that ADC input and it may change during the 5 second interval. 
You may want to consider doing an ADC conversion on AN0 (AN0_val) and then quickly changing to AN1 and doing another conversion (AN1_val) and then displaying both values on the display. After about 1 second you could sample both inputs again.  It depends on what the source of the two ADC inputs is as to how fast you may need to sample them.
If AN1 is being used to measure current you will need a current shunt (ground referenced) to measure the voltage drop across.  You may also need some gain to amplify the value as usually you don't want too much voltage drop when reading current.

Cheers,
David

Giuseppe

Other 2 possibilities to measure the current while maintaining the insulation are: ACS712 5A / 20A / 30A or a current transformer.

JonW

#5
I presume you are using a current sense amplifier for the current measurement?  We used to use a discrete method for a while in production and getting accurate results can be difficult. Under short conditions we had transients that would damage the current sense circuity.  Our previous design used a Zetex current sense amplifier, many of these have a really high output impedance so be careful as errors can creep in when the ADC cap charges so you will then need to buffer the output with a secondary opamp but this will need low input current and low offset otherwise more errors creep in. 
In a recent redesign I opted for the LTC4151 and these are brilliant  https://www.analog.com/media/en/technical-documentation/data-sheets/4151ff.pdf

These fully integrated IC's are perfect as all of the offsets etc are taken care of within the IC.  Just quirt I2C etc.  Pay close attention to the current sense resistor tolerance and layout. They are a few dollars but well worth the money for the accuracy.  You can get current, voltage at the current measurement node and an external voltage input too.


See_Mos

Hi Amod,

Do you need help with the hardware or writing the code

RGV250

Hi,
You have not said if you are looking at AC or DC and what voltage current range you require.
This module can handle 36V and 15A and will interface on 3 - 5v.
https://coolcomponents.co.uk/products/ina260-high-or-low-side-voltage-current-power-sensor?_pos=1&_sid=c6851aa7e&_ss=r
I have written some code for the INA219 which will probably be similar if you decide to use it.

Regards,
Bob


Amod

Thanks everyone for valuable suggestions.

I had made a voltage divider circuit for voltage measurement and current transformer (30A) with a burden resistance. I have  successfully done all the millivolt mesurement to read 10 bit Adc. I had written a code only for using 1 adc pin (AN0). I don't know how to use two pins in code. (AN0 & AN1).

david

Hi,
Ensure these AN0 and AN1 pins are defined as inputs.  You then need to configure them as analog inputs using the ANSEL register (ANalog SELect register)
If you have only the 2 ADC inputs then ANSEL=%00000011. 

Do your first ADC conversion on AN0 then you need to change the analog input channel to AN1.
To select AN1 on the multiplexer you need to set bits 2,3,4,5 of the ADCON0 register to ADCON0=%xx0001xx.
Add a short delay (~100uS) then do another ADC read as per your first conversion.  The result should be the ADC count representing the DC voltage applied to AN1.

Current transformer? That can wait for another question.....

Cheers,
David

Stephen Moss

A few years ago I was looking at updating the circuits used in the Watt meters at work, I did not get started with the PIC side as they were replaced but I was looking at the 18F2431 because it has "Two sample and hold inputs for dual-channel simultaneous sampling". As I recall it was not quite as good as it initially sounded as while both inputs are sampled simultaneously they are then sent sequentially a single ADC for sample conversion, but it would still have given a still more accurate result of W when multiplying V by I due to the samples being taken at the same time as opposed to sequentially.
That may not be of concern in your application but if it is you may want take a look at that device. Although the ADC setup is more complicated than generally found on PICs due to being able to set it up as either single shot or continuous loop sampling combined with sequential or simultaneous sampling and knowing which sample is stored where in the result buffer.

Quote from: Amod on Aug 14, 2021, 04:19 AMI need to display ampere for 5 seconds then volts for 5 seconds and so on. I am able to display voltage from an0 pin. How can i use an1 pin also for ampere?
If you are not already intending to do so I suggest setting up two arrays (Voltage & Current, minimum 10 elements each), take alternating voltage and current samples in sequence (i.e. V1, A1, V2, A2, V3, A3) to store in the respective array and then taking an average of each for display. Generally, that would give a better overall indication of what is happening than a single spot sample taken every 5 seconds.
 
As David indicated switching between ADC input channels (pins) for measurements is generally only a case of writing the relevant value to the ANSEL register at the appropriate time, wait the necessary acquisition time and then set the Go/Done bit of the ADCON register to start a new conversation.

Amod

Many thanks to all. I successfully completed my project.