# Problems reading ADC

**URL:** <https://forum.arduino.cc/t/problems-reading-adc/1349478>\
**Category:** General Guidance\
**Created:** [February 1, 2025, 4:58pm UTC](https://forum.arduino.cc/t/problems-reading-adc/1349478 "2025-02-01T16:58:48Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![oli170](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/oli170/32/1192968_2.png) [@oli170](https://forum.arduino.cc/u/oli170)\
**Post date:** [February 1, 2025, 4:58pm UTC](https://forum.arduino.cc/t/problems-reading-adc/1349478/1 "2025-02-01T16:58:48Z")

</div>

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?

```arduino
// 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
  }
}

```

---

<div class="post-metadata">

**Author:** ![markd833](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/markd833/32/1186928_2.png) [@markd833](https://forum.arduino.cc/u/markd833)\
**Post date:** [February 1, 2025, 5:21pm UTC](https://forum.arduino.cc/t/problems-reading-adc/1349478/2 "2025-02-01T17:21:26Z")

</div>

> [@oli170](#):
>
> ```arduino
> // 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);
> }
> 
> ```

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:

```arduino
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%
}

```

---

<div class="post-metadata">

**Author:** ![Paul\_KD7HB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_kd7hb/32/1127958_2.png) [@Paul\_KD7HB](https://forum.arduino.cc/u/Paul_KD7HB)\
**Post date:** [February 1, 2025, 5:22pm UTC](https://forum.arduino.cc/t/problems-reading-adc/1349478/3 "2025-02-01T17:22:45Z")

</div>

> [@oli170](#):
>
> I have enclosed code, how can I show a picture if that helps?

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?

---

<div class="post-metadata">

**Author:** ![oli170](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/oli170/32/1192968_2.png) [@oli170](https://forum.arduino.cc/u/oli170)\
**Post date:** [February 1, 2025, 5:25pm UTC](https://forum.arduino.cc/t/problems-reading-adc/1349478/4 "2025-02-01T17:25:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![oli170](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/oli170/32/1192968_2.png) [@oli170](https://forum.arduino.cc/u/oli170)\
**Post date:** [February 1, 2025, 5:27pm UTC](https://forum.arduino.cc/t/problems-reading-adc/1349478/5 "2025-02-01T17:27:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![oli170](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/oli170/32/1192968_2.png) [@oli170](https://forum.arduino.cc/u/oli170)\
**Post date:** [February 1, 2025, 5:29pm UTC](https://forum.arduino.cc/t/problems-reading-adc/1349478/6 "2025-02-01T17:29:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![oli170](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/oli170/32/1192968_2.png) [@oli170](https://forum.arduino.cc/u/oli170)\
**Post date:** [February 1, 2025, 5:32pm UTC](https://forum.arduino.cc/t/problems-reading-adc/1349478/7 "2025-02-01T17:32:37Z")

</div>

Sorry, here's some pics

 ![tester from tinx](https://europe1.discourse-cdn.com/arduino/original/4X/2/f/e/2fef8ac813707bedb4017d7caeed0b6fcaeff0bd.png)  
 ![tester photo](https://europe1.discourse-cdn.com/arduino/original/4X/0/4/d/04d22a08df8cd8ac461137629ebb56da62cae6c3.jpeg)

---

<div class="post-metadata">

**Author:** ![markd833](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/markd833/32/1186928_2.png) [@markd833](https://forum.arduino.cc/u/markd833)\
**Post date:** [February 1, 2025, 5:33pm UTC](https://forum.arduino.cc/t/problems-reading-adc/1349478/8 "2025-02-01T17:33:24Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![oli170](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/oli170/32/1192968_2.png) [@oli170](https://forum.arduino.cc/u/oli170)\
**Post date:** [February 1, 2025, 5:35pm UTC](https://forum.arduino.cc/t/problems-reading-adc/1349478/9 "2025-02-01T17:35:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![oli170](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/oli170/32/1192968_2.png) [@oli170](https://forum.arduino.cc/u/oli170)\
**Post date:** [February 1, 2025, 5:35pm UTC](https://forum.arduino.cc/t/problems-reading-adc/1349478/10 "2025-02-01T17:35:47Z")

</div>

> 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 🙂

---

<div class="post-metadata">

**Author:** ![Paul\_KD7HB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_kd7hb/32/1127958_2.png) [@Paul\_KD7HB](https://forum.arduino.cc/u/Paul_KD7HB)\
**Post date:** [February 1, 2025, 5:40pm UTC](https://forum.arduino.cc/t/problems-reading-adc/1349478/11 "2025-02-01T17:40:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![oli170](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/oli170/32/1192968_2.png) [@oli170](https://forum.arduino.cc/u/oli170)\
**Post date:** [February 1, 2025, 5:42pm UTC](https://forum.arduino.cc/t/problems-reading-adc/1349478/12 "2025-02-01T17:42:48Z")

</div>

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...

---

<div class="post-metadata">

**Author:** ![Paul\_KD7HB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_kd7hb/32/1127958_2.png) [@Paul\_KD7HB](https://forum.arduino.cc/u/Paul_KD7HB)\
**Post date:** [February 1, 2025, 5:45pm UTC](https://forum.arduino.cc/t/problems-reading-adc/1349478/13 "2025-02-01T17:45:43Z")

</div>

> [@oli170](#):
>
> The code I believe works on percentages of the supply voltage,

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?

---

<div class="post-metadata">

**Author:** ![oli170](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/oli170/32/1192968_2.png) [@oli170](https://forum.arduino.cc/u/oli170)\
**Post date:** [February 1, 2025, 5:54pm UTC](https://forum.arduino.cc/t/problems-reading-adc/1349478/14 "2025-02-01T17:54:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![EmilyJane](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/emilyjane/32/821537_2.png) [@EmilyJane](https://forum.arduino.cc/u/EmilyJane)\
**Post date:** [February 1, 2025, 6:07pm UTC](https://forum.arduino.cc/t/problems-reading-adc/1349478/15 "2025-02-01T18:07:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![EmilyJane](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/emilyjane/32/821537_2.png) [@EmilyJane](https://forum.arduino.cc/u/EmilyJane)\
**Post date:** [February 1, 2025, 6:29pm UTC](https://forum.arduino.cc/t/problems-reading-adc/1349478/16 "2025-02-01T18:29:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![oli170](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/oli170/32/1192968_2.png) [@oli170](https://forum.arduino.cc/u/oli170)\
**Post date:** [February 1, 2025, 6:42pm UTC](https://forum.arduino.cc/t/problems-reading-adc/1349478/17 "2025-02-01T18:42:45Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![van\_der\_decken](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/van_der_decken/32/1006976_2.png) [@van\_der\_decken](https://forum.arduino.cc/u/van_der_decken)\
**Post date:** [February 1, 2025, 6:50pm UTC](https://forum.arduino.cc/t/problems-reading-adc/1349478/19 "2025-02-01T18:50:37Z")

</div>

Your power connections don't match.

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/c/5/1/c51c6a80f51de7ea47419e7cb604bebb9e0e4b4b.png)

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/9/3/7/9379709a7fa3c486e78d1df6e3ae704357b2e2a2.jpeg)

And what voltage are you putting in Vin?

---

<div class="post-metadata">

**Author:** ![EmilyJane](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/emilyjane/32/821537_2.png) [@EmilyJane](https://forum.arduino.cc/u/EmilyJane)\
**Post date:** [February 1, 2025, 6:50pm UTC](https://forum.arduino.cc/t/problems-reading-adc/1349478/20 "2025-02-01T18:50:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Terrypin](https://avatars.discourse-cdn.com/v4/letter/t/a3d4f5/32.png) [@Terrypin](https://forum.arduino.cc/u/Terrypin)\
**Post date:** [February 1, 2025, 8:38pm UTC](https://forum.arduino.cc/t/problems-reading-adc/1349478/21 "2025-02-01T20:38:22Z")

</div>

See post #9

[Next page](https://forum.arduino.cc/t/problems-reading-adc/1349478.md?page=2)
