Notifications
Clear all

Arduino resistor testing

51 Posts
5 Users
7 Likes
4,415 Views
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2533
 

@davee

Thanks for the information, I was assuming that the voltage didn't matter since your formula uses only the measured value from the analog pin. 

Because it seemed to make the original voltage immaterial (along sit didn't damage the device) I wasn't concerned about the drop.

Anything seems possible when you don't know what you're talking about.


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

Hi @will,

   If you look carefully, the analysis is a bit sneaky about the voltage. Perhaps I didn't explain it clearly.

In effect, the raw ADC reading is a voltage, but measured in 'private' units of 1/1024 of the reference voltage.

Within the device limits, it doesn't matter what that reference voltage is, but to determine the unknown resistance as a simple ratio, the reference voltage for the ADC must be the same as the voltage fed to the pair of resistors.

(The situation is similar to the Wheatstone bridge, which you might recall from the past .. physics lessons at school perhaps?)

Using the shift register outputs introduces a voltage drop whose magnitude is rather poorly defined, and hence tricky to compensate for.

Using the analogue switches is not a perfect solution, as they also introduce some resistance, but it is better specified and fairly small .. say 60 Ohms .. maybe less with alternate devices... (in principle you could use FETs with an Ron of a less than 1 milliOhm, but that solution will be much more complicated and expensive).

So the switch resistance can either be ignored or compensated for with a much higher confidence.

Hope it is interesting and helpful. Best wishes, Dave


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2533
 
Posted by: @davee

Hi @will,

   If you look carefully, the analysis is a bit sneaky about the voltage. Perhaps I didn't explain it clearly.

In effect, the raw ADC reading is a voltage, but measured in 'private' units of 1/1024 of the reference voltage.

Yes, I understood that, but as you point out the actual voltage is immaterial since we're just considering the ratio of the unknown resistance to the total resistance.

Within the device limits, it doesn't matter what that reference voltage is, but to determine the unknown resistance as a simple ratio, the reference voltage for the ADC must be the same as the voltage fed to the pair of resistors.

Which it would be, the connection is made from GND to the shift register pin through both resistors. The comparison voltage is whatever is fed out of the SR, but I still see it as consistent with your formula. 

Using the shift register outputs introduces a voltage drop whose magnitude is rather poorly defined, and hence tricky to compensate for.

OK, I think the gist of the matter here is that I don't understand why you think the value of the test voltage is important. Can you explain that to me, please.

Hope it is interesting and helpful. Best wishes, Dave

It is both interesting and helpful to me because I seem to be missing an important point (and I want to understand it properly).

Thanks.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
(@jeffreyjene)
Member
Joined: 4 years ago
Posts: 56
Topic starter  

As frustrating as this gets, I'm sincerely enjoying the project and all I'm learning. @Will and @DaveE, thanks for all the input and help.

Something I don't understand in this same subject range as I believe it affects my resistor results. I wanted to test my analog pins, so I set up a voltage divider (actually a small common module with two resistors valued R1=30000 and R2=7500). I measured a voltage in from my power source at different levels as well as a 9V battery and a 6V lantern battery. All of the readings came in almost a full volt higher than it should have. It really doesn't make sense. I'm setting the pins at INPUT but it still seems there is a signal at that pin (from what I understand it should be about 0) even when nothing is plugged into it according to my serial monitor. I've tried other pins with the same results. I'm using an external reference voltage module at 4.096. Here's the code, but it's fairly simple:

//for timing purposes to avoid delay()
unsigned long Timer;

//Floats for ADC voltage & Input voltage
float adc_voltage = 0.0;
float in_voltage = 0.0;
 
// Floats for resistor values in divider (in ohms)
float R1 = 30000.0;
float R2 = 7500.0; 

//Float for Reference Voltage
float ref_voltage = 4.096;

void setup(){
   analogReference(EXTERNAL);
//Floats for ADC voltage & Input voltage
float adc_voltage = 0.0;
float in_voltage = 0.0;
 
// Floats for resistor values in divider (in ohms)
float R1 = 30000.0;
float R2 = 7500.0; 

//Float for Reference Voltage
float ref_voltage = 4.096;

pinMode(ANALOG_IN_PIN, INPUT);
}

void loop(){

if (millis() - Timer >= 1000UL){
      Timer = millis();
      //Read the Analog Input
      adc_value = analogRead(ANALOG_IN_PIN);
      
      //Determine voltage at ADC input
      adc_voltage  = (adc_value * ref_voltage) / 1024.0; 
      
      //Calculate voltage at divider input
      in_voltage = adc_voltage / (R2/(R1+R2)); 


      
      //Print results LCD
      //lcd.setCursor(0, 1);
      //lcd.print(in_voltage);
     // lcd.print("V");
    }

}

   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2533
 

@jeffreyjene 

Where do you assign a pin number for ANALOG_PIN_IN ?

 

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
(@jeffreyjene)
Member
Joined: 4 years ago
Posts: 56
Topic starter  

@will Oops, missed that when I copied:

#define ANALOG_IN_PIN A0

   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2533
 

@jeffreyjene 

Can you please:

- tell us what order the resistors are in (e.g. 9V, 30K, A0, 7.5K, GND)

- give us some example raw values from the 6V battery test

- give us some example raw values from the 9V battery test

So we have at least something besides your description to work from.

 

As for declaring the pins as INPUT, check out the Arduino Foundations document at

https://www.arduino.cc/en/Tutorial/Foundations/DigitalPins

where it states "This also means however, that pins configured as pinMode(pin, INPUT) with nothing connected to them, or with wires connected to them that are not connected to other circuits, will report seemingly random changes in pin state, picking up electrical noise from the environment, or capacitively coupling the state of a nearby pin."

Anything seems possible when you don't know what you're talking about.


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

Hi @will and @jeffreyjene,

 Returning to my previous comments and Will's last answer message, I should like to clarify a couple of points .. and apologise for any confusion I added.

  • The question of which reference voltage for the ADC you were using wasn't obvious (to me anyway) from the information, but because your code had vin = 5V, I assumed you had arranged the code to selecte the AVCC pin input, which on the UNO board schematic I found, appears to be connected to the general 5V power used for both digital and analogue parts.  My analysis depends upon this choice!

 

  • To explain, if the voltage across both resistors of your voltage divider is the same as the ADC reference, then that voltage is equivalent to an ADC reading of 1024. Thus the reading at the junction of the voltage divider resistor, divided by 1024, will be the value of the unknown resistor divided by the sum of the resistor values -- ie the obvious simple voltage divider ratio. This result is rather convenient, since it means that the accuracy of the measurement is only related to the ADC (linearity etc.) and the 'known' resistor ... the actual value of the ADC reference voltage does not affect the accuracy of the measurement, and hence does not need to be a precise value. (Obviously the limits of the devices concerned must be respected.)

 

  • If the voltage applied to the voltage divider is any way changed, but that change is not also reflected to the ADC reference, such as by supplying the voltage divider from a digital output pin of either the Arduino processor or a shift register, then this will introduce an error. Such outputs from CMOS technology devices (like Arduino processor or HC family logic chips)  may be very close to the rail voltage when current flow is only a few microamps, but may fall appreciably (up to say 0.5V) when a milliamp or more flowing. Furthermore, the relationship between voltage drop and current flow is only poorly specified, as voltage drops of 0.5V and more are of no consequence in terms of logic levels. One might consider trying to get around this by some sort of calibration/correction calculation if only low accuracy is required, but in general I wouldn't recommend going down that path.

 

  • I suggested you investigate analogue switches as a means of mitigating the last concern. I noted that these switches would not replace shift registers or other means of expanding the number of contol signals from an Arduino, but instead complement them, so each part did what it was designed to do .. the shift registers provide logic functionlity, the analogue switches switch the analogue voltages onto the resistors, etc.

I hope that is a little clearer.

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

I haven't had the time to look at your more recent discussion with Will, although I notice you are using an external reference. I am sure he can provide some good advice! Clearly the ADC should be able to function as specified, and failure to do that suggests an issue to be resolved.

When you get the ADC to measure accurately, please remember the important thing is to be able to measure/determine both the voltage across the whole divider and the voltage across the unknown resistor, so that you can apply the simple ratio calculation. The absolute accuracy of these measurements is irrelevant, (e.g they could both be 50% high .. though of course they shouldn't be!), providing the values retain the correct ratio to each other.

Be careful when applying voltages from other sources ... I haven't checked this case but ADCs  can be susceptible to damage if the voltage exceeds the reference voltage .. eg by applying a 5V input whilst the reference is 4V, even though it would be quite happy if the reference voltage was 5V!

Good luck! Dave


   
Inst-Tech reacted
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2533
 
Posted by: @davee

Hi @will and @jeffreyjene,

Be careful when applying voltages from other sources ... I haven't checked this case but ADCs  can be susceptible to damage if the voltage exceeds the reference voltage .. eg by applying a 5V input whilst the reference is 4V, even though it would be quite happy if the reference voltage was 5V!

Apparently, judging fro his last post, he's now using a 4.096 V precision voltage as the reference and is powering his test circuits from 6V and 9V batteries.

Apparently, the backwards calculations are off by about 1V :/

So, your advice may be a little late.

Anything seems possible when you don't know what you're talking about.


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

Hi @will,

   I share your concerns; it prompted the comment but my crystal ball is too cloudy to have predicted that course of action in advance.

    Anyway, I hope it is in time ... silicon can be forgiving .. cats are supposed to have 9 lives ... Arduinos I am not sure about!

I am hoping the voltage discrepancy is because the DC is being fed a different voltage from expected or maybe being read incorrectly. Without accurate information as to what is happening, we are both in the dark.

Best wishes.


   
Inst-Tech reacted
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2533
 

@davee 

Concerned ... moi  ?

It ain't my silicon 🙂

Anything seems possible when you don't know what you're talking about.


   
Inst-Tech reacted
ReplyQuote
(@jeffreyjene)
Member
Joined: 4 years ago
Posts: 56
Topic starter  

@DaveE So you are saying if I'm using an external reference of 4.096 and I apply 5V through a voltage divider to an analog pin it will damage the Arduino? I've never heard that before. Interesting. Fortunately other than incorrect values (which have been happening since the beginning) I'm not seeing anything that would indicate damage.

I'm not "powering" the projects with 6 or 12 volts, I'm testing voltages through a divider to see what readings I'm getting from the analog pins. The project is being powered by 5V USB input.

@Will I just read the bit as well about the random noise on INPUT pins being common. That's probably what screwed up this idea from the very beginning. I wonder if there's a way to quiet that down. If so, I haven't found anything yet.

I'll pull together an experiment here and give you the exact results, maybe my math is off somewhere.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2533
 
Posted by: @jeffreyjene

@DaveE So you are saying if I'm using an external reference of 4.096 and I apply 5V through a voltage divider to an analog pin it will damage the Arduino? I've never heard that before. Interesting. Fortunately other than incorrect values (which have been happening since the beginning) I'm not seeing anything that would indicate damage.

I don't think that DaveE is GUARANTEEING a toasted Arduino, he's just warning you of the possibility.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
(@jeffreyjene)
Member
Joined: 4 years ago
Posts: 56
Topic starter  

I'm not sure how, but I came back and turned everything on and now the voltages are returning near perfectly. A 6V battery, 9V battery, and my power supply voltages all came back within a fraction or two from my multimeter. Very strange. I can't wait to see what it'll say tomorrow.

//the LCD display
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);

//for timing purposes to avoid delay()
unsigned long Timer;

#define ANALOG_IN_PIN A0

///////////Voltage meter///////////
//Floats for ADC voltage & Input voltage
float adc_voltage = 0.0;
float in_voltage = 0.0;
 
// Floats for resistor values in divider (in ohms)
float R1 = 30000.0;
float R2 = 7500.0; 

//Float for Reference Voltage
float ref_voltage = 4.096;
 
//Integer for ADC value
int adc_value = 0;

void setup()
{
  Serial.begin(9600);
  
  lcd.init();                      // initialize the lcd 
  lcd.backlight();

  analogReference(EXTERNAL);
  
}

void loop() {
  if (millis() - Timer >= 1000UL){
      Timer = millis();
      //Read the Analog Input
      adc_value = analogRead(ANALOG_IN_PIN);
      
      //Determine voltage at ADC input
      adc_voltage  = (adc_value * ref_voltage) / 1024.0; 
      
      //Calculate voltage at divider input
      in_voltage = adc_voltage / (R2/(R1+R2)); 

      if (in_voltage < 0.05){
        in_voltage = 0;
      }

      Serial.println(in_voltage);
      
      //Print results LCD
      lcd.setCursor(0, 1);
      lcd.print(in_voltage);
      lcd.print("V");
    }
  
}

   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2533
 

@jeffreyjene 

In the words of Mr Spock "Random chance has operated in your favour".

Anything seems possible when you don't know what you're talking about.


   
Inst-Tech reacted
ReplyQuote
Page 3 / 4