//Gyro Adafruit MPU6050
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
Adafruit_MPU6050 mpu;
bool accelInitialized = false;
float pitch, roll, temperature, voltage;
float calibrationX = -0.0;
float calibrationY = -0.0;
#define ANALOG_IN_PIN 35//36 // ESP32 pin GPIO36 (ADC0) connected to voltage sensor
#define REF_VOLTAGE 3.3
#define ADC_RESOLUTION 4096.0
#define R1 12000.0//30000.0 // resistor values in voltage sensor (in ohms)
#define R2 2000.0//7500.0 // resistor values in voltage sensor (in ohms)
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
InitializeVoltageMeasuring();
MPU6050Begin();
}
void loop() {
// put your main code here, to run repeatedly:
delay(500); // this speeds up the simulation
GetCurrentVoltage();
getLevel();
}
void InitializeVoltageMeasuring(){
pinMode(ANALOG_IN_PIN, INPUT);
// set the ADC attenuation to 11 dB (up to ~3.3V input)
analogSetPinAttenuation(ANALOG_IN_PIN, ADC_11db);
}
float GetCurrentVoltage(){
// read the analog input
word adc_value = analogRead(ANALOG_IN_PIN);
// determine voltage at adc input
float voltage_adc = ((float)adc_value * REF_VOLTAGE) / ADC_RESOLUTION;
// calculate voltage at the sensor input
float voltage_in = voltage_adc * (R1 + R2) / R2;
// print results to serial monitor to 2 decimal places
Serial.print("Voltage:");
Serial.print(voltage_in, 2);
Serial.print(",Raw_Input:");
Serial.print(adc_value);
Serial.print(",ADC:");
Serial.println(voltage_adc);
return voltage_in;
}
void logPrintLn(const String &msg){
logPrint(msg, true);
}
void logPrint(const String &msg, bool linebreak) {
if(linebreak){
Serial.println(msg);
}
else {
Serial.print(msg);
}
}