PHOTON ADC not converting properly?

Good day!

I’m a new user of Photon and I have a question regarding the ADC measurement accuracy. I’m getting erroneous measurement from my ADC conversion. My circuit is shown below, with A5 connected to a diode and A3 is at GND.


A5 is expected to be around 0.7V (which is confirmed by DMM) and A3 should be at 0V. However, my cloud measurement shows that A5 is 1.204V and A3 is at 1.670V. I’ve included a test variable to check if web publishing is done.

I have included my code below (created using Web IDE):

#include "Particle.h"

// Variable declaration
int analogPinA5 = A5;
int analogPinA3 = A3;
double measA5V = 0.0;
double measA3V = 0.0;
double testVar = 0.0;
int a5Voltage = 0;
int a3Voltage = 0;

void setup() 
{
  // Put initialization like pinMode and begin functions here.
  // Make all in-line pinS of A5/A3 as digital output and low
  pinInitializations();

  Particle.variable("measureA5", measA5V);    
  Particle.variable("measureA3", measA3V);  
  Particle.variable("testConstant", testVar);
   
}

void loop() 
{
  // Measure voltage at A5 and A3
  a5Voltage = analogRead(analogPinA5);
  a3Voltage = analogRead(analogPinA3);
  
  // Calculate measured voltages by multiplying the INT result
  // to the ADC resolution (step size is approximately 800uV)
  measA5V = (3.3/4096)*a5Voltage;
  measA3V = (3.3/4096)*a3Voltage;
  
  // Calculate "dummy" test variable then publish to cloud
  // expected result should be 1.65
  testVar = 2048 * (3.3/4096);

  // Publish results
  Particle.publish("A5 Measurement", String(measA5V,3) + "V");
  Particle.publish("A3 Measurement", String(measA3V,3) + "V");
  Particle.publish("Test Variable", String(testVar,3) + "V");

  // To see change in measurement
  delay(1000);
}

void pinInitializations()
{
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A4, OUTPUT);
  pinMode(DAC, OUTPUT);
  pinMode(WKP, OUTPUT);
  pinMode(RX, OUTPUT);
  pinMode(TX, OUTPUT);

  digitalWrite(A0, LOW);
  digitalWrite(A1, LOW);
  digitalWrite(A2, LOW);
  digitalWrite(A4, LOW);
  digitalWrite(DAC, LOW);
  digitalWrite(WKP, LOW);
  digitalWrite(RX, LOW);
  digitalWrite(TX, LOW);
}

Please help me in identifying what caused the measurement errors. Thanks! :slight_smile:

For better precision I'd change that to

  measA5V = (a5Voltage * 3.3) / 4095;
  measA3V = (a3Voltage * 3.3) / 4095;

Also your delay(1000) after the burst of three publishes is too little. After the fourth publish, the rate limit will hit.

Hi ScruffR,

Thanks for the advice. I’ve tried the code that you suggested, but my my measurements are still off from my DMM measurement.

What do you mean by: Also your delay(1000) after the burst of three publishes is too little. After the fourth publish, the rate limit will hit.

Do I need to change it to a higher delay value?..e.g. 2seconds?

Per the docs you’d need to add a 4sec “cooldown phase” after a burst of 4 events to keep the average publish rate below 1 per second.
My comment about precision was not actually meant for the ADC issue but just as a general coding rule.

I don’t see how it’s possible to get those results unless something is wrong with your Photon (or your actual setup is not what your post shows). Have you tried different pins instead of A3 and A5 or a different Photon? I tested your code and setup with a 220 ohm resistor and a 1N4001 diode, and I got nearly identical readings on A5 between my DMM and what’s reported on the console. I get 0.002V for A3 Measurement and 0.693V for A5 Measurement.

2 Likes

Thanks a lot for the response, I appreciate it. I did try on other analog pins, with the same results. Unfortunately, i don’t have a spare unit yet…

Since you confirmed it with your Photon setup, I think mine is a defective unit. Again, thanks for your reply.

@LeeT, change your diode to a 1N4001 if you can and test again.

You can also try other A pins. If one or two are dead, you may still find some working ones. Just make sure never to put more than 3.3V on a pin in ADC mode (or on A3 & A6/DAC at any time)!

If your diode is dead or was used in reverse polarity, you will have the full 5V on A5 inevitably frying the ADC.

To be on the safe side, rather use 3V3 instead of Vin and adjust your resistor accoridingly.

2 Likes

Another possibility that has not been mentioned yet is if your breadboard is high quality or not. A low quality or damaged breadboard may add some resistance to your connections and cause a difference in readings.

In your photo, you have your meter leads connected at the diode. If you move the leads to connect to the Photon pins and the meter reading changes, then your breadboard may be the issue.

Good day guys! Thanks a lot for all the inputs that you have given. It is confirmed, my old Photon unit is defective. It doesn’t convert the expected levels at all it’s analog inputs and it also is pretty hot, reaching as high as 61°C.

My new unit arrived yesterday, I tested it using the debug setup and plug-it in to my application board correct measurements.