Analog circuit strange behavior?

I have a simple circuit I am troubleshooting. I am using an analog input from a sensor to switch on and off a digital pin based on the value. When my Arduino is plugged into my computer, with the sensor on its own 12v power source (common ground) it works perfectly. I can monitor the analog value in the serial monitor and the digital pin turns on and off at the appropriate values. However when I disconnect it from my computer and use the same 12v my sensor is using to power the Arduino for some reason it activates at a higher threshold (by a couple 0.01's of a volt). It will begin blinking before it finally fully turns the pin off(it's programmed to turn pin off under threshold). I am monitoring the analog voltage with my DMM and nothing is different when I change power sources. So, I am confused by the change in behavior. Any ideas or tips would be greatly appreciated.

const int analogPin = A15; // pin that the sensor is attached to
const int ledPin = 13; // pin that the LED is attached to
const int threshold = 205; //245 15psi with Arduino on 5volt supply 205 10 1.01v = 10psi 15psi = 1.21v psi an arbitrary threshold level that's in the range of the analog input

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communications:

void loop() {
// read the value of the potentiometer:
int analogValue = analogRead(analogPin);

// if the analog value is high enough, turn on the LED:
if (analogValue > threshold) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);

// print the analog value:
delay(1000); // delay in between reads for stability

Show us a good schematic and image of the actual wiring.

Show us your sketch using the code tags icon </> from the posting menu.

The analog reading is always relative to the reference voltage. When you power the Arduino board using your 12V supply, it uses a regulator to provide 5v to the microcontroller, and the reference voltage will be very close to 5v. However when you power from the USB port, whatever the USB voltage is powers the microcontroller and is the reference voltage. This voltage can often be as much as 10% off from 5v, so you might see as much as a 10% error in the analogRead value.

To be accurate, you either need to apply an accurate voltage to the AREF pin and execute analogReference(EXTERNAL) or you need to select an internal accurate reference. Since you mention using A15 it sounds like you are using an Arduino Mega board. You can select the INTERNAL2V56 reference, which limits you to 2.56 volts or less on the input, however it sounds like you don't expect more than 1.21 volts anyway.


I tried to capture a voltage variance with my DMM but everything looked identical. But, I will look into adding a voltage reference to my code/circuit I feel like this may solve my problems. Thank you for your help!

The A/D uses the 5V supply as a reference to convert the analog. My guess is the 5V supply is slightly different when connected via the 12V.

I suggest you use the internal 1.1 volt Reference. You may have to divide down the input signal if it is above 1.1 Volts.

Google changing the Arduino voltage reference to learn how to set it.

I will research that thank you. I was not able to solve the problem using the Aref feature. My analog signal will reach close to the max 5v. But for my programming purposes I am only concerned with the 1-2v range .

Could you use the 2.56V internal reference (you are using a Mega here, aren't you?) and then make a 2x voltage divider on the input so you signal would be in the range 0 to 2.5v?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.