Scaling of ADC input - Page 1
Forum section: electronics / beginners

author: paul23
author_rank: Regular Contributor
timestamp: July 04, 2013, 03:10:51 pm
content: I am working on a circuit and I am a bit stuck on how to do the ADC part. I am using an ATTINY85 and using the internal ADC to take a measurement.There are 3 inputs to the circuit, VIN, Signal and GND. VIN can be a voltage between 4.5 - 12VDC, Signal is a voltage between 0-100% of VIN, this what I want to read, and GND is obvious.VIN goes through an LDO to power the ATTiny at 3.3V, so I need to turn my signal into a 0-3.3V value. For example, say VIN is 9V and Signal is 4.5V, then I need the input to the ADC to be 1.65V. How do I do that? If VIN was a fixed value I could do it easily, but I can´t figure out how to do it when VIN can change.Any help kindly appreciated.

author: Marco
author_rank: Super Contributor
timestamp: July 04, 2013, 03:47:56 pm
content: You could just measure both ... but if you absolutely want to do it in a single measurement, then you can divide VIN by ~4 and use it as an external voltage reference for the ADC.

author: Dave
author_rank: Super Contributor
timestamp: July 04, 2013, 03:49:01 pm
content: What resolution are you looking for? If you don't require a very high resolution, you could just use a voltage divider (factor of 3.7, 10k and 27k resistor, 12V gets scaled down to 3.24V) for both the input voltage and input signal. You then mathematically calculate (just a simple division) the ratio of the input signal to input voltage. But yes, while this solution will work, your resolution will suffer.

author: paul23
author_rank: Regular Contributor
timestamp: July 04, 2013, 04:12:50 pm
content: Resolution doesn´t really matter, I can get away with quite a low resolution, I only really need a 3 bit value from the ADC, so accuracy and resolution are not critical.The ATTINY uses VCC as its reference, i think. I am pretty sure there isn´t an AREF pin on that chip.With the voltage divider method, that works great at 12V, but doesn´t work at lower voltages, say 5V. If Vin was a fixed value then I would have done it with a voltage divider, but I don´t how to do it with a dynamic range, without an AREF pin.

author: Dave
author_rank: Super Contributor
timestamp: July 04, 2013, 04:46:21 pm
content: Why would it not work at 5V?5V / 3.7 = 1.35V1.35V on the input of a 10-bit ADC with 3.3V reference should read 419.So your full scale for the input signal at 5V supply would be 419, which is much better than your required 3 bits.Also, if your input signal would have a high impedance (for example if you had a potentiometer as the input), you should add a RRIO opamp on the input connected as a voltage buffer.

author: paul23
author_rank: Regular Contributor
timestamp: July 04, 2013, 05:05:41 pm
content: Quote from: Dave on July 04, 2013, 04:46:21 pmYes, but the value should be 1023 at 5V (if Vin is 5V too). The ADC has no way to know what the full scale value is.QuoteThat´s fine, but how do I set that automatically? That is what I am stuck on.Maybe I did not explain clear enough what the problem is. Vin can change, it´s not a fixed value, so it´s not always 5V or always 12V or whatever. I need a way to detect what Vin in and scale the measurements accordingly so that the signal (0-Vin) == 0-3.3V, but it has to be automatic and not user programmable/settable. I can´t figure out how to do that, maybe it is not possible?

author: Dave
author_rank: Super Contributor
timestamp: July 04, 2013, 05:22:56 pm
content: I understood exactly what your requirement is, maybe I didn't explain my suggestion well enough.You would be measuring 2 voltages: the supply voltage and the signal you are trying to quantize.Both these voltages would be divided by 3.7 and both of them then measured with an ADC.The reading you are getting from the signal voltage would be mathematically compared to the reading you are getting from the supply voltage. It's just division (signal/supply; you could also multiply the signal reading first, so you can avoid floating point calculations, which take more processing).So basically, you are measuring both unknown voltages and comparing the two. It doesn't matter if the supply voltage is 5, 7 or 12 volts, you are always able to calculate the ratio of the two.

author: fcb
author_rank: Super Contributor
timestamp: July 04, 2013, 05:23:34 pm
content: I'm not familar with the ATTiny, but some ideas:1. Use the ADC reference input. Feed your signal to a potential divider on the ADC input, and feed your DC rail into another potential divider on the ADC REF input. That will give you a ratiometric ADC (depending somewhat on the way the ADC works!).2. Use 2x ADC channels with potential dividers on them (one for signal and one for rail) - and then do a bit of scaling maths.3. Build a primitive multiplier and scale your signal according to the DC rail - you could probably do this with three-four transistors. Look-up voltage controlled amplifiers (VCA) - it's not qoing to be quick to design.I'd probably go for 2. But I don't know what your pin budget is like.

author: Marco
author_rank: Super Contributor
timestamp: July 04, 2013, 05:39:12 pm
content: Quote from: paul23 on July 04, 2013, 04:12:50 pmNot dedicated, but it is one of the many potential functions of PB0.

author: 4to20Milliamps
author_rank: Regular Contributor
timestamp: July 04, 2013, 06:15:38 pm
content: I'm not sure exactly what your application is, but it seems like if you know your resolution and your voltages you should be able to use a look up table to grab the adjusted value, there are folks here that are way more competent at figuring stuff out in software than I am so if there's an easier way to do it I'm sure they will point it out.References and example:http://www.eetimes.com/document.asp?doc_id=1276974< paragraph on software calibration.http://www.ni.com/white-paper/4806/en/http://www.phanderson.com/picaxe/picaxe_thermistor.html

author: paul23
author_rank: Regular Contributor
timestamp: July 04, 2013, 06:59:08 pm
content: Quote from: Dave on July 04, 2013, 05:22:56 pmThanks, I am such a spaz, I get what you mean and that is such an obvious solution, I should have thought of that myself. I didn´t realise you were meant using 2 ADC channels. But that would be a good solution. Thanks.Quote from: fcb on July 04, 2013, 05:23:34 pmAs I said, there is no ADC REF on the tiny85QuoteThat´s the winnerQuoteToo much mucking about, it´s easier with 2 ADC channels. Thanks for your suggestionsQuote from: Marco on July 04, 2013, 05:39:12 pmWell done, I hadn´t noticed that.

author: Dave
author_rank: Super Contributor
timestamp: July 04, 2013, 07:26:49 pm
content: Quote from: paul23 on July 04, 2013, 06:59:08 pmYou are very welcome.When doing the calculation, I think the fastest and simplest solution will be to put your signal reading into a long variable (longbeing the 32-bit variable type, not an adjective) and then shifting it 10 bits to the left. You then divide this variable by the reading you get from your supply voltage. You then shift the result to the right by 10-N places, where N represents the number of digits you want your final result to have. I'd say you have 6 reliable bits tops, but then again, you said you only need 3, so that should be fine.

author: richcj10
author_rank: Unknown
timestamp: July 05, 2013, 12:51:39 am
content: Quote from: paul23 on July 04, 2013, 04:12:50 pmThere is an optional Aref pin. But, I would use the internal reference tho.The voltage divider will work at "all" voltages.