Problems reading ADC

I have made a circuit that reads ADC from a potential divider. So, <16%, >16% & <50%, >50% & <82%, and >82% have different outputs.

One of them (A3) is bad, good, good on battery power, when i want bad, bad, good. Essentially the outputs are not responding as required according to the analogue input. When I power them off USB they are bad, bad, bad! Can anyone suggest what is going wrong?

I have enclosed code, how can I show a picture if that helps?

// Pin Definitions
const int d3 = 3;
const int d4 = 4;
const int d5 = 5;
const int d6 = 6;
const int d7 = 7;
const int d8 = 8;
const int d9 = 9;
const int d10 = 10;
const int d11 = 11;
const int a1 = A1;
const int a2 = A2;
const int a3 = A3;

void setup() {
  // Initialize digital pins as outputs
  pinMode(d3, OUTPUT);
  pinMode(d4, OUTPUT);
  pinMode(d5, OUTPUT);
  pinMode(d6, OUTPUT);
  pinMode(d7, OUTPUT);
  pinMode(d8, OUTPUT);
  pinMode(d9, OUTPUT);
  pinMode(d10, OUTPUT);
  pinMode(d11, OUTPUT);

  // Set all outputs low initially
  digitalWrite(d3, LOW);
  digitalWrite(d4, LOW);
  digitalWrite(d5, LOW);
  digitalWrite(d6, LOW);
  digitalWrite(d7, LOW);
  digitalWrite(d8, LOW);
  digitalWrite(d9, LOW);
  digitalWrite(d10, LOW);
  digitalWrite(d11, LOW);
  
  delay(1000); // Pause for 1 second

  // Set d4, d7, and d10 high
  digitalWrite(d4, HIGH);
  digitalWrite(d7, HIGH);
  digitalWrite(d10, HIGH);
  delay(1000); // Pause for 1 second

  // Set all outputs low
  digitalWrite(d4, LOW);
  digitalWrite(d7, LOW);
  digitalWrite(d10, LOW);
}

void loop() {
  // Loop1 start - Repeat 3 times only
  for (int i = 0; i < 3; i++) {
    digitalWrite(d4, HIGH);
    delay(100); // Pause for 0.25 seconds
    digitalWrite(d4, LOW);
    digitalWrite(d7, HIGH);
    delay(100); // Pause for 0.25 seconds
    digitalWrite(d7, LOW);
    digitalWrite(d10, HIGH);
    delay(100); // Pause for 0.25 seconds
    digitalWrite(d10, LOW);
    delay(100); // Pause for 0.25 seconds
  }
  
  // After Loop1 runs 3 times, set d4, d7, and d10 high again
  digitalWrite(d4, HIGH);
  digitalWrite(d7, HIGH);
  digitalWrite(d10, HIGH);

  // Now begin Loop2, which runs indefinitely
  while(true) {
    int voltageA3 = analogRead(a3); // Read A3
    float voltageA3Percentage = (voltageA3 / 1023.0) * 100; // Convert to percentage

    // Check A3 voltage levels and adjust outputs accordingly
    if (voltageA3Percentage < 16) {
      digitalWrite(d5, LOW);
      digitalWrite(d3, LOW);
      digitalWrite(d4, HIGH);
    } else if (voltageA3Percentage >= 16 && voltageA3Percentage < 50) {
      digitalWrite(d4, LOW);
      digitalWrite(d5, LOW);
      digitalWrite(d3, HIGH);
    } else {
      digitalWrite(d3, LOW);
      digitalWrite(d4, LOW);
      digitalWrite(d5, HIGH);
    }

    int voltageA2 = analogRead(a2); // Read A2
    float voltageA2Percentage = (voltageA2 / 1023.0) * 100; // Convert to percentage

    // Check A2 voltage levels and adjust outputs accordingly
    if (voltageA2Percentage < 16) {
      digitalWrite(d6, LOW);
      digitalWrite(d8, LOW);
      digitalWrite(d7, HIGH);
    } else if (voltageA2Percentage >= 16 && voltageA2Percentage < 50) {
      digitalWrite(d6, LOW);
      digitalWrite(d7, LOW);
      digitalWrite(d8, HIGH);
    } else if (voltageA2Percentage >= 50 && voltageA2Percentage < 82) {
      digitalWrite(d7, LOW);
      digitalWrite(d8, LOW);
      digitalWrite(d6, HIGH);
    } else {
      digitalWrite(d7, LOW);
      digitalWrite(d6, LOW);
      digitalWrite(d8, HIGH);
    }

    int voltageA1 = analogRead(a1); // Read A1
    float voltageA1Percentage = (voltageA1 / 1023.0) * 100; // Convert to percentage

    // Check A1 voltage levels and adjust outputs accordingly
    if (voltageA1Percentage < 16) {
      digitalWrite(d9, LOW);
      digitalWrite(d11, LOW);
      digitalWrite(d10, HIGH);
    } else if (voltageA1Percentage >= 16 && voltageA1Percentage < 90) {
      digitalWrite(d9, LOW);
      digitalWrite(d10, LOW);
      digitalWrite(d11, HIGH);
    } else {
      digitalWrite(d10, LOW);
      digitalWrite(d11, LOW);
      digitalWrite(d9, HIGH);
    }

    // Loop2 will continue running indefinitely
  }
}

You don't need to check if the voltage is >=16 as it must be >=16 if the first IF statement hasn't been satisfied.

You can structure your IF statements like this:

if (voltageA3Percentage < 16) {
  // voltage < 16%
} else if (voltageA3Percentage < 50) {
  // voltage is >= 16% but < 50%
} else if (voltageA3Percentage < 82) {
  // voltage is >= 50% but < 82%
} else {
  // voltage is >= 82%
}

A picture of a hand made block diagram showing all the connections you have and all the resistor values and the voltage at the top end of the divider will replace the word salad with meaningful information. Perhaps then someone can offer advice. What Arduino are yo using?

1 Like

Thanks Mark - sorry for my tedious code, I have been helped significantly along the way! May I ask if your helpful suggestion will affect the result?

It's a £5 ebay nano knock off! I don't think I'm allowed to upload a picture yet?

Word salad LoL! Sorry, I'm just not sure whether it's a coding issue or I'm doing something wrong with the components...

Sorry, here's some pics


I'm a tad confused. Did you intend for the A3 "channel" code to only handle 3 distinct bands rather than the 4 bands you gave in your initial post?

What I'm doing is a tester that will gauge if earth, neutral and live cables have been connected correctly (NB not for connection to mains!) I'm using a PD to connect to the simulated mains supply to differentiate the outputs

Did you intend for the A3 "channel" code to only handle 3 distinct bands rather than the 4 bands you gave in your initial post?

yes :slightly_smiling_face:

Did you confirm the voltages with a DVM, digital volt meter so only the code could be at fault?

Yes. The 3 x AA seem to be supplying 4.2v, even though they are new. The PD is quite accurate too, perhaps unsurprisingly. The code I believe works on percentages of the supply voltage, so should work if/when the batteries degrade...

No! the A/D converter works on the voltage supplied to the Nano. That is the reference voltage. What voltage are you running the Nano with?

3 x AA batteries, which seem to be delivering 4.2v at the moment

Lovely job of breadboarding your circuit, by the way. Give a look at a schematic capture program like KiCad and consider creating actual schematic diagrams instead of the Fritzing-like pictures.

2 Likes

You seem to be powering the Nano from the Vin input. On an authentic Nano, that input voltage voltage should be a minimum of 7V so I don't know what voltage the processor is running on but it's not 4.2V, probably around 3V.

You seem to be powering the Nano from the Vin input.

Is there a better way to power it? (Sorry, @rduino noob here :joy:)

Your power connections don't match.

And what voltage are you putting in Vin?

If you can arrange for an input voltage higher than 7V that way is acceptable. Otherwise, for testing purposes you could rely on your USB connection. You'd need to power your breadboard from the 5V output from the nano.

See post #9