Inaccurate Voltage readings on Analog Pin

I have a bench top power supply that I'm using to send anywhere from 8V to 12V to a L7805CV that drops the voltage to <=5V before it reaches the arduino uno's Vin pin. Running from the negative and positive from the bench top power supply is a voltage divider, that also drops the 12V to ~4.9V.

The divider then leads to pin A0, to call the AnalogRead function and use the formula in the code below to print a reading.

The Problem: The first problem is the readings are all over the place at times. If I drop the voltage on the power supply to 8, it'll hover around the expected ~3.5. Other times, it'll spit out the 3.5 and on the next loop reads 4.7 consistently. It seems to read the voltages correctly after not being on for awhile and then starts acting up when I've uploaded the code a few different times with changes.

It isn't a cheap power supply either. I picked this one up from Circuit Specialists and have confirmed the voltages coming from it with a multimeter to be accurate. I have also tried putting 2 capacitors with the voltage dividers to smooth out the readings before it connects to the Arduino, but that did not do anything either.

The second issue is digital pin 8. This pin is connected to a TIP100 that is acting as a switch between the power supply and the negative on the LED strip. For whatever reason it is always connecting the gate, leaving the LEDs always on, regardless of the input from the hall effect sensor or LDR. I've been experimenting with both, so that's why I have both of those in the code.

Upon removing the connection between pin 8 and the TIP100, the LEDs go off, so the TIP100 is working, it's something with the code/arduino.

int ldrPin = A4;   // This is the pin connected between teh resiststor and the LDR. This reads the value coming from the LDR.
int sensorValue = 0;
const int hallPin = 2;
int hallState = 0;
const float referenceVolts = 4.904348; //
const int batteryPin = A0; // This is the variable that stores teh value coming form the sensor.
int blinkDelay = 300;
bool doneFlag = false;


void setup() {
  pinMode(8, OUTPUT); //This pin is what gets turned on or off based on the value from the LDR using the logic in the lines below. In this example, it is connected to the TIP100 that turns the LEDs on or off. This line defines pin 6 as the OUTPUT.
  pinMode(ldrPin, INPUT); // This line defines pin A2 as the input, where we will take our receiving values from. Remember we set pin A2 above as int ldrPin = A2
  pinMode(12, OUTPUT); // This pin is connected to the single LED that blinks, indicating low battery
  pinMode(hallPin, INPUT);
  // analogReference(DEFAULT);
  Serial.begin(9600); //sets serial port for communication
}

void loop() {
  // read the value from the sensor:
 sensorValue = analogRead(ldrPin);    // this sets the variable "sensorValue" as whatever the reading coming from pin A2 is. 
 Serial.println(sensorValue); //prints the values coming from the sensor on the screen in the IDE
hallState = digitalRead(hallPin);
Serial.println("HallState");
Serial.println(hallState);


  int val = analogRead(batteryPin);
  float volts = (val / 1023.0) * referenceVolts;
  Serial.println(volts);

  if(hallState = 1);
  digitalWrite(6, LOW);
  
 if(hallState = 0) //if that value is above 100, then do....
 digitalWrite(6, HIGH); //turn relay ON, by allowing power to flow from pin 6

  if (hallState = 0) { 
    if(doneFlag == false) {
      if(volts < 3.85) {
    digitalWrite(12, HIGH);
    delay(blinkDelay);
    digitalWrite(12, LOW);
    delay(blinkDelay);
    digitalWrite(12, HIGH);
    delay(blinkDelay);
    digitalWrite(12, LOW);
    doneFlag = true;
      }
    }
  }
  
  else digitalWrite(6, LOW); //turn relay OFF, by cutting power from pin 6

  digitalWrite(12, LOW);
  
  delay(1000); // wait 100ms then repeat.

Please find and read the voltage limits for the Vin pin.

Paul

Running from the negative and positive from the bench top power supply is a voltage divider, that also drops the 12V to ~4.9V.

Let's see a schematic of the voltage divider circuit and how it's connected.

the code below to print a reading.

Your code is missing. (Click the </> above to create "code tags" and put your code inside the tags.)

It may also help to "simplify" your code... Just run the basic [u]Read Analog Voltage Example[/u].

The second issue is digital pin 8. This pin is connected to a TIP100 that is acting as a switch between the power supply and the negative on the LED strip. For whatever reason it is always connecting the gate, leaving the LEDs always on,

You can test the transistor & LED strip by running the Blink Example (modified for pin 8 ).

If the LED strip is on with nothing connected to the base (not gate) then the transistor is shorted (burned-out) or something is connected wrong.

If that's working, write a little test program to read the Hall effect sensor and LDR. (Display the results on using the Serial Monitor, like the analog voltage example.)

If you need more help you'll have to show us the schematics.

DVDdoug:
Let's see a schematic of the voltage divider circuit and how it's connected.
Your code is missing. (Click the </> above to create "code tags" and put your code inside the tags.)

It may also help to "simplify" your code... Just run the basic [u]Read Analog Voltage Example[/u].
You can test the transistor & LED strip by running the Blink Example (modified for pin 8 ).

If the LED strip is on with nothing connected to the base (not gate) then the transistor is shorted (burned-out) or something is connected wrong.

If that's working, write a little test program to read the Hall effect sensor and LDR. (Display the results on using the Serial Monitor, like the analog voltage example.)

If you need more help you'll have to show us the schematics.

Good ideas.... sometimes it's easy to forget simple troubleshooting methods.

Thanks - will update when I run these.

bmfischer3:
I have a bench top power supply that I'm using to send anywhere from 8V to 12V to a L7805CV that drops the voltage to <=5V before it reaches the arduino uno's Vin pin.

As Paul_KD7HB hinted, V-in of an Uno needs a minimum of about 7volt.
If you have 5volt, then connect it to the 5volt pin.
Or use the buildin 5volt regulator of the Uno, by connecting 8-12volt directly to the DC socket.
(assuming you don't draw much from the Uno for sensors etc.)

DVDdoug:
Just run the basic [u]Read Analog Voltage Example[/u].

And what a horrible example that is.
Can't measure a voltage accurately with a ratiometric A/D without knowing what the 5volt supply actually is.
It could be 4.6volt (Nano on USB supply), making that voltage display meaningless.

const float referenceVolts = 4.904348; // and how stable is that going to be with flashing LEDs etc.
The last five nonsence digits can be removed if you're going to use default Aref.

Voltages should be measured with a reference voltage (the Uno has an internal one).
Calculate the battery voltage divider for ~1volt, and switch to the internal 1.1volt Aref in setup().
analogReference(INTERNAL); // enable 1.1volt Aref
Leo..

Did you remember to connect the Arduino Ground to the Emitter of the transistor? Did you put a current limiting resistor between the Arduino pin and the Base? Did you remember to set the pin mode to OUTPUT?