solved: mystery of analogRead()

hi,
my sketch shows a very curious behaviour.
With this sketch I want to perform a pretest for a circuit to charge an accumulator. The diagram is shown in the attachment.
A p-channel MOSFET shall switch power via R2 to the accu.
The variable gate voltage is generated by PWM and filtered with R1, C.

For testing the gate voltage is varied slowly up and down. When this voltage goes down it reaches a level when the MOSFET is switched on and provides current to the accu which causes the accu voltage to rise somewhat.

The following sketch shows the varying voltage levels measured on the analog inputs A3, A5, A7 on the Serial Plotter.

byte pwm=255, dpwm=-5;
word V_batt, V_d, V_g, V_0;
unsigned long tm, dtm;

void setup()
{
  Serial.begin(38400);

  pinMode(3,OUTPUT);
  analogWrite(3,pwm);
  delay(20);
  tm=millis();
}

void loop()
{
  // pinA5=V_batt, pinA7=V_d, pinA3=V_g
 
  if (millis()-tm > 50) { // every 50ms new PWM-value
    pwm += dpwm;
    if ((pwm < 5) || (pwm > 250)) dpwm = -dpwm;
    analogWrite(3,pwm);
    tm=millis();
  }

    V_batt=analogRead(A5);
    delay(2);
    V_d=analogRead(A7);
    delay(2);
    V_g=analogRead(A3);
    Serial.print(0);Serial.print(" ");      // blue plot
    Serial.print(1025);Serial.print(" ");   // red plot
    Serial.print(V_batt);Serial.print(" "); // green plot
    Serial.print(V_d);                      // brown plot
    Serial.print(" ");
    Serial.println(V_g);                    // magenta plot
    Serial.flush();
}

The plot analogReadA5_A7.JPG for this version of the sketch is shown in the attachment.
When the gate voltage (magenta) is lower than about 700 charging of the accu starts. This is the expected behaviour. When charging starts, the voltage at the drain (brown) always should be higher than the voltage on the accu which not always seems to be true. Well, in the moment I have no explanation why sometimes this is not true. But this is not the problem.

The problem I don’t understand at all comes up when the sequence of reading the voltages from A5 and A7 is switched.

void loop()
{
  // pinA5=V_batt, pinA7=V_d, pinA3=V_g
 
  if (millis()-tm > 50) { // every 50ms new PWM-value
    ...
  }

    V_d=analogRead(A7);   // before it was V_batt=analogRead(A5)
    delay(2);
    V_batt=analogRead(A5);   // before it was V_d=analogRead(A7)
    delay(2);
    V_g=analogRead(A3);
    ...

Now the plot is shown in analogReadA7_A5.JPG.
It is impossible that V_d nearly follows V_g, but it does!
What is happening here?

A totally confused SupArdu :confused:

CircuitDiag.JPG

As you are discovering, that circuit doesn't work. For one, you need a logic level MOSFET.

Google images "pmos high side switch" for typical circuits.

At first, I want to note that my first post contained an error in the example code for the switched sequence of analog reading.
Now this error is corrected in the post and corresponds to the code which generated the plot in analogReadA7_A5.JPG.

Now my answer to jremington:

The circuit in my post was intended only for testing, not as a final circuit.
In the final version the MOSFET shall not only switch on/off but shall work as a variable resistor controlling the amount of current for charging. Therefore the gate voltage has to be controlled by the filtered analog voltage from PWM.

I know that my circuit contains problems for the intended use.
But the problem I wanted to show here is that switching the sequence of reading analog inputs in the sketch on my Arduino Nano results in totally different voltage readings.

Hi,
Please post your entire circuit, including the Arduino and your power supplies.

Thanks.. Tom.. :slight_smile:

Try reading each input twice and discarding the first reading

(the MOSFET) shall work as a variable resistor controlling the amount of current for charging.

If you want the current to be predictably related to the output voltage, a voltage controlled current source using an op amp would be simple, and a much better choice.

hi all,
thanks for your tips.
The mystery is solved: One connection in my breadboard had no contact. It was the connection between V_d and the analog input of the Nano. Therefore the input was floating!