# Inaccurate voltage readings on an esp32 as opposed to multimeter

#### Arjuna_1000

##### New Member
I am working on detecting the amplitude of a signal coming from a guitar pickup. I am using an esp32 and an LM386 module for amplifying the signal.

I get quite stable readings with a multimeter, but the readings on the Arduino IDE show peaks and also peaks come when no signal should be there.

Here is a link for short videos of both the esp32 readings and the multimeter:

Simple code:

Code:
int ampedVoltagePin = 12;

void setup() {
pinMode(ampedVoltagePin, INPUT);
Serial.begin(9600);
}

void loop() {
delay(100);
}
Diagram:

Thanks!

Last edited:

#### Nigel Goodwin

##### Super Moderator
It doesn't help you're trying to measure an AC signal with a DC converter, you're never going to get a sensible reading - you first need to convert the signal to DC.

#### Arjuna_1000

##### New Member
I solved it by averaging the values over 500 cycles.

New code:

Code:
int ampedVoltagePin = 12;
int counter = 0;
int sumOfVoltages = 0;
int sampleSize = 500;
int average;

void setup() {
pinMode(ampedVoltagePin, INPUT);
Serial.begin(9600);
}

void loop() {

if (counter > sampleSize -2) {
average = sumOfVoltages/sampleSize;
Serial.println(average);
sumOfVoltages = 0;
counter = 0;
}

counter ++;
}

Les.

#### Pommie

##### Well-Known Member
int ampedVoltagePin = 12;
Why do people do this? If should be a define!! I guess the optimizer takes care of it. But better to get it right in the first place.

Mike.

#### Nigel Goodwin

##### Super Moderator
Why do people do this? If should be a define!! I guess the optimizer takes care of it. But better to get it right in the first place.

Mike.
Certainly a very strange thing to do, and regardless of the optimiser it's going to be no better (and probably worse) than doing it properly.

I would imagine a 'better' compiler would flag it as an error?.

#### Pommie

##### Well-Known Member
I would imagine a 'better' compiler would flag it as an error?.
Not sure how a compiler would work out it's a pin and not just a constant. Still annoys me though.

Mike.

#### Nigel Goodwin

##### Super Moderator
Not sure how a compiler would work out it's a pin and not just a constant. Still annoys me though.

Mike.
I was thinking of it been a variable (int) rather than just a text name for a pin.

#### gophert

##### Well-Known Member
Why would it not work? its even used in the lesson on variables at Arduino.cc
I'm beginning to think OCD is a real thing.

#### dougy83

##### Well-Known Member
Why do people do this? If should be a define!! I guess the optimizer takes care of it. But better to get it right in the first place.
You can use constexpr to mark the typed variable as a compile-time constant, if you wanted; this also honours variable namespaces/scoping/etc., unlike #define.

#### Visitor

##### Well-Known Member
I believe I've read the ADC of the ESP32 isn't extremely linear in the first place.

#### Pommie

##### Well-Known Member
You can use constexpr to mark the typed variable as a compile-time constant, if you wanted; this also honours variable namespaces/scoping/etc., unlike #define.
But the OP had it as a global variable. Obviously, the Arduino compiler optimises it to the same code but if you did this on XC8 (without optimisation as most people use it) it will reserve two bytes of memory(RAM) for it. However, I do like this method.

Mike.

#### dougy83

##### Well-Known Member
But the OP had it as a global variable.
Locally-scoped variables (or class members) with the same name as the global variable are possible. Try that where a #define exists. As an example:
C++:
#ifdef USE_DEFINE
#define dataPin 1    // <-- this will cause issues if 'dataPin' is used elsewhere in code
#else
constexpr int dataPin = 1;  // <-- this is fine
#endif

class MyHardware
{
int dataPin;
public:
MyHardware(int dataPin) : dataPin(dataPin) {}
};
Obviously, the Arduino compiler optimises it to the same code but if you did this on XC8 (without optimisation as most people use it) it will reserve two bytes of memory(RAM) for it.
True; they probably compile to the same code. I'd almost swear that XC8 purposely puts in garbage code to incentivise paying for the compiler.

#### Pommie

##### Well-Known Member
I'd almost swear that XC8 purposely puts in garbage code to incentivise paying for the compiler.
I used to believe that but now think it's just the way it compiles and leaves it to the optimiser to tidy up. I have a friend who wrote a C compiler and that is the method he used.

BTW, I always define constants in upper case and variables in camel case.
So in my case (N.P.I.) I'd have,
Code:
#define DATAPIN 1
or, more probably
#define DATA_PIN 1
And, of course, variables as in your example (camel case).

Mike.

#### dougy83

##### Well-Known Member
I also think that's the case. I remember the output of the old hitech picc was better than the current XC8 output; they must've stripped out _ALL_ of the optimisations.
Fair enough; there'll be no name conflicts that way. I'll still avoid preprocessor substitution where possible.

#### Nigel Goodwin

##### Super Moderator
I believe I've read the ADC of the ESP32 isn't extremely linear in the first place.
It's not, as I recall the bottom end is very non-linear.