bgoodwine / SeniorDesign

Code for Senior Design II
0 stars 1 forks source link

I2C with new current sensors #23

Open bgoodwine opened 1 year ago

bgoodwine commented 1 year ago

ACS725 3.3V current sensor monitors the ESP's IMU on pin 9 = I34 and Pi's IMU on pin 7 = I39 ACS724 5V current sensor monitors the Pi on pin 10 = I35

Not super legit code, incorrect for loop (?) so maybe just adjust the ACS725 code?

bgoodwine commented 1 year ago

IMU Current Sensor Code

#define RefVal 3.3

// IMU on pin 9 = I34
#define ESP_IMU 34
#define PI_IMU 39

// Take the average of 500 times
const int averageValue = 500;

long int sensorValue = 0;

// datasheet- 264mV/A(Typ.) sensitivity i.e. 1000.0 mA / 264.0 mV
float sensitivity = 1000.0 / 264.0; 

// Vref is zero drift value, change to MEASURED value 
float Vref = 322; 

void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
  // read sensor data multiple times (for accuracy)
  for (int i = 0; i < averageValue; i++)
  {
    sensorValue += analogRead(ESP_IMU);
    // TODO: read in from Pi as well 
    //piSensorValue += analogRead(PI_IMU);
    delay(2); // 2ms before next loop 
  }

  sensorValue = sensorValue / averageValue;
  //piSensorValue = piSensorValue / averageValue;

  // ADC is 10-bits = 2^10 = 1024.0 values
  // different power supply = different reference sources
  // example: 2^10 = 1024 -> 5V / 1024 ~= 4.88mV & unitValue= 5.0 / 1024.0*1000 ;
  // so 2^10 = 1024 -> 3.3V/1024 ~= 3.22mV
  float unitValue= RefVal / 1024.0*1000 ; // volts per bit in analog input
  float voltage = unitValue * sensorValue; // volts from sensor  

  // when no load, Vref=initialValue
  Serial.print("initialValue: ");
  Serial.print(voltage);
  Serial.println("mV"); 

  // calculate the corresponding current
  float current = (voltage - Vref) * sensitivity;

  // print voltage (mV) on pin corresponding to current 
  voltage = unitValue * sensorValue-Vref;
  Serial.print(voltage); // unit volts per bit ? 
  Serial.println("mV");

  // print current (mA)
  Serial.print(current);
  Serial.println("mA");
  Serial.print("\n");

  // reset the sensorValue for the next reading
  sensorValue = 0;
  // read it once per second
  delay(1000);
}