I am running this code against this circuit.
#include <Arduino.h>
const int _selectPins[3] = {36, 37, 38}; // S0~2, S1~3, S2~4
const int _dataInput = 4; // Connect common (Z) to A0 (analog input)
unsigned long previousMillis = 0;
const long interval = 1000;
void selectMuxPin(byte pin);
void setup()
{
Serial.begin(115200);
for (int i=0; i<3; i++)
{
pinMode(_selectPins[i], OUTPUT);
digitalWrite(_selectPins[i], HIGH);
}
pinMode(_dataInput, INPUT);
selectMuxPin(0);
}
float GetVoltage(int _batteryVoltageRawValue) { // Calculate Voltage Sensor Value from Battery
float R1 = 30000.0;
float R2 = 7500.0;
float vout = (float(_batteryVoltageRawValue) * 3.3) / 4096.0;
// float result = vout / (R2 / (R1 + R2));
float result = vout*(R1+R2)/R2;
// Calibration
result = result * 1.01595;
return result;
}
void loop()
{
unsigned long currentMillis = millis();
int rawVoltValue = analogRead(_dataInput);
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
Serial.print("Volt raw value : "); Serial.println(rawVoltValue);
Serial.print("Volt value : "); Serial.print(GetVoltage(rawVoltValue)); Serial.println("V");
}
}
// The selectMuxPin function sets the S0, S1, and S2 pins
// accordingly, given a pin from 0-7.
void selectMuxPin(byte pin)
{
for (int i=0; i<3; i++)
{
if (pin & (1<<i)) {
digitalWrite(_selectPins[i], HIGH);
} else {
digitalWrite(_selectPins[i], LOW);
}
}
}
circuit schema
When i plug/unplug the battery, the ESP32 stop running.
If i put a voltmeter between +5v and GND, it does not happen.
Tried to add a 10K pull down resistor on Y0, but it seems to act like a voltage divider bridge.
What can i do to found the issue ?
thank you.
New contributor
Steeve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.