Analog pin always reads 1023 from voltage divider

I've got an ATMEGA328P-PU in a circuit (using 16MHz crystal oscillator, external), where the PC3 pin (equivalent - A3) is connected to a voltage divider which has a 12v source going through a 100kOhm resistor to the pin, then a 47kOhm resistor to ground (5v source and 14V max (the whole circuit is for charging a 12v NiCD battery) source share a ground, only connected in one place). I made a program to read the value of this pin and stop a certain operation when it reached 1023 or greater. I was having problems with this, so I set up PC1 (A1) to output whatever PC3 (A3) was reading, and even when I ground PC3, PC1 still outputs 5V (1023). I've tried changing around which ADC pins I was using, but same results each time. The actual voltage at the PC3 pin is 4.33v.

Any ideas? I'm probably missing something obvious. The rest of the code works great, just this measuring the value at the ADC pin part is temporarily stumping me.

int drvd = 5;
int drvc = 6;
int cLED = 7;
int dLED = 8;
float b;
float a;

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(drvd, OUTPUT);
  pinMode(drvc, OUTPUT);
  pinMode(cLED, OUTPUT);
  pinMode(dLED, OUTPUT);
}

void loop() {
  
  digitalWrite(drvc, HIGH); //drvc low-time
  a = analogRead(A3);
  b = a * (6.00/1023);
  analogWrite(A1, 0);
  delayMicroseconds(b * 1000);
  if(a < 1023)
  {
    digitalWrite(drvc, LOW); //control from low, drvc hightime
    delay(1);
    digitalWrite(dLED, LOW);
    digitalWrite(cLED, HIGH);
    analogWrite(A1, a);
  }
  else
  {
    digitalWrite(dLED, HIGH);
    digitalWrite(cLED, LOW);
    delay(100);
    analogWrite(A1, 0);
    digitalWrite(dLED, LOW);
    digitalWrite(cLED, LOW);
    delay(100);
    analogWrite(A1, a);
  }
}

A1 is not an analog output. It is an analog input. When you use analogWrite on it the code will default to digitalWrite LOW for anything less than 127 and HIGH for anything greater.

The analog outputs give you PWM, not a voltage you can measure but a square wave.

It will never ever ever be greater. Do you know why?

Instead of trying to write the voltage back out to another pin, which isn't going to work, try just printing the value you get from analog read and see what it is.

2 Likes

A1 is not PWM able output

1 Like

That divider will give you roughly 0-4V for a 0-12v input range.
So the input value shouldn’t get to 1023 unless the source is getting above about 14-15V

1 Like

yes, the resolution of the ADC. It gives a value between 0 and 1023, I meant to say 1022, I just did that in the code temporarily to make that if statement turn on. Though, it does make sense that A1 can't output an analog voltage signal, why it only gives the "high" value, I don't know why I didn't realize that lol. I will try it with a PWM pin later, I've got an oscilloscope so reading a square wave isn't a problem. I can't print the value through serial, there is no USB connection from the microcontroller to my PC, I am using an Arduino uno as an ISP to program the controller. That's why I was attempting to read the value of the pin through other means. Thank you for the input!

Yup, that's the same values I got. And that 14-15v range is exactly where I want it to cut off.

I found my problem. The AREF pin was tied to ground and after removing the ground from that pin it works great lol.

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