How can i print power consumption?

I have 4 LEDs that light up in different brightnesses and durations at the same time. It causes 13 mA at 5V and 7mA at 1V. I want to print the total energy consumed by the LEDs during their operation. How can I do it?

You can use Serial.println(yourvariable); where yourvariable is variable containing your total consumption value

Do you want to use average (fixed) values or actual (measured) values in the calculation of energy consumption ?

How will you, op, be detecting the current of the circuit during operations?

Once the current is detected, then detect the voltage applied. Then multiply the voltage times the current to get the power then Serial.println( power ).

void fReadCurrent( void * parameter )
{
  const TickType_t xFrequency = 1000; //delay for mS
  const float mVperAmp        = 185.0f;
  float    ADbits             = 4096.0f;
  float    ref_voltage        = 3.3f;
  float    mA                 = 0.0f;
  float    adcValue           = 0.0f;
  float    Voltage            = 0.0f;
  float    Power              = 0.0f;
  float    offSET             = 0.0f;
  int      printCount         = 0;
  uint64_t TimePastKalman     = esp_timer_get_time(); // used by the Kalman filter UpdateProcessNoise, time since last kalman calculation
  SimpleKalmanFilter KF_I( 1.0f, 1.0f, .01f );
  /*
     185mv/A = 5 AMP MODULE
     100mv/A = 20 amp module
     66mv/A = 30 amp module
  */
  String powerInfo = "";
  powerInfo.reserve( 150 );
  while ( !MQTTclient.connected() )
  {
    vTaskDelay( 250 );
  }
  TickType_t xLastWakeTime = xTaskGetTickCount();
  for (;;)
  {
    adc1_get_raw(ADC1_CHANNEL_3); // read once discard reading
    adcValue = ( (float)adc1_get_raw(ADC1_CHANNEL_3) );
    //log_i( "adcValue I = %f", adcValue );
    Voltage = ( (adcValue * ref_voltage) / ADbits ) + offSET; // Gets you mV
    mA = Voltage / mVperAmp; // get amps
    KF_I.setProcessNoise( (esp_timer_get_time() - TimePastKalman) / 1000000.0f ); //get time, in microsecods, since last readings
    mA = KF_I.updateEstimate( mA ); // apply simple Kalman filter
    TimePastKalman = esp_timer_get_time(); // time of update complete
    printCount++;
    if ( printCount == 60 )
    {
      xSemaphoreTake( sema_CalculatedVoltage, portMAX_DELAY);
      Power = CalculatedVoltage * mA;
      //log_i( "Voltage=%f mA=%f Power=%f", CalculatedVoltage, mA, Power );
      printCount = 0;
      powerInfo.concat( String(CalculatedVoltage, 2) );
      xSemaphoreGive( sema_CalculatedVoltage );
      powerInfo.concat( ",");
      powerInfo.concat( String(mA, 4) );
      powerInfo.concat( ",");
      powerInfo.concat( String(Power, 4) );
      xSemaphoreTake( sema_MQTT_KeepAlive, portMAX_DELAY );
      MQTTclient.publish( topicPower, powerInfo.c_str() );
      xSemaphoreGive( sema_MQTT_KeepAlive );
      powerInfo = "";
    }
    xLastWakeTime = xTaskGetTickCount();
    vTaskDelayUntil( &xLastWakeTime, xFrequency );
  }
  vTaskDelete( NULL );
} //void fReadCurrent( void * parameter )
////


void fReadBattery( void * parameter )
{
  float adcValue = 0.0f;
  const float r1 = 50500.0f; // R1 in ohm, 50K
  const float r2 = 10000.0f; // R2 in ohm, 10k potentiometer
  float Vbatt = 0.0f;
  int printCount = 0;
  float vRefScale = (3.3f / 4096.0f) * ((r1 + r2) / r2);
  uint64_t TimePastKalman  = esp_timer_get_time(); // used by the Kalman filter UpdateProcessNoise, time since last kalman calculation
  SimpleKalmanFilter KF_ADC_b( 1.0f, 1.0f, .01f );
  TickType_t xLastWakeTime = xTaskGetTickCount();
  const TickType_t xFrequency = 1000; //delay for mS
  for (;;)
  {
    adc1_get_raw(ADC1_CHANNEL_0); //read and discard
    adcValue = float( adc1_get_raw(ADC1_CHANNEL_0) ); //take a raw ADC reading
    KF_ADC_b.setProcessNoise( (esp_timer_get_time() - TimePastKalman) / 1000000.0f ); //get time, in microsecods, since last readings
    adcValue = KF_ADC_b.updateEstimate( adcValue ); // apply simple Kalman filter
    Vbatt = adcValue * vRefScale;
    xSemaphoreTake( sema_CalculatedVoltage, portMAX_DELAY );
    CalculatedVoltage = Vbatt;
    xSemaphoreGive( sema_CalculatedVoltage );
    
      printCount++;
      if ( printCount == 3 )
      {
      log_i( "Vbatt %f", Vbatt );
      printCount = 0;
      }
    
    TimePastKalman = esp_timer_get_time(); // time of update complete
    xLastWakeTime = xTaskGetTickCount();
    vTaskDelayUntil( &xLastWakeTime, xFrequency );
    //log_i( "fReadBattery %d",  uxTaskGetStackHighWaterMark( NULL ) );
  }
  vTaskDelete( NULL );
}
////

How do you get leds to light up at 1 volt ?

Is the question about leds relevant to this topic ?

int led = 3;
int ldr = A5;
int ir = A0;
void setup()
pinMode (led,OUTPUT);
pinMode (ldr,INPUT);
pinMode (ir,INPUT);
void loop()
{
Serial.println(analogRead(A5));
int ldrStatus = analogRead (ldr);
if (ldrStatus <=500)
{

   digitalWrite(led, HIGH);
   analogWrite(led,51);

if (analogRead(A0)<300) // IR 1 CODE
{
digitalWrite(led,HIGH);
analogWrite(led,255);

            delay(500);// micro second
           } 
        else 
            {
              digitalWrite(led,HIGH);
              analogWrite(led,51);

Here is my code simply. I know it's easy but i am just a beginner so sorry for all these :frowning: I want to print the calculation of all LEDs. When something appears in front of IR, LED will be 5V when it's not it'll be 1v. Due to unstable bright up and bright down times i can't create the code.

With "analogWrite(led,255/5)" code.

No Serial.begin…?

I want to write a piece of code that collects the 13mA and 7mA currents caused by the leds at different brightnesses at the same time and gives it to the screen.

Will the current only ever be 13mA or 7mA, controlled by the PWM output from the Arduino or can it vary for some other reason and, if so, what ?

Yes only 13 mA and 7mA. I measured the led's current. When it's on high statement it causes 13 mA when is low (i mean 1v) 7mA. Only 2 values. But there are 4 leds. And they're working with IR sensors. So their statements only 13 mA and 7mA too. I want to calculate and print the "total" currents for a certain time. (For example 30 sec)

You are setting the current used by each of the LEDs that you are controlling by writing a voltage to them. Save the value of millis() when you start writing HIGH to an LED and when you write LOW to it subtract the start time from the current value of millis() to get the time spent at 13 mA and add it to the 13 mA total value.

Do the same with the LOW state to get the total LOW state time. When 30 seconds have elapsed, save the time at the current state to the relevant total. Now you have 2 totals

Put the LED pin numbers in an array and you can iterate through the LEDs to add to the totals for all of the LEDs

At the end of 30 seconds do the calculations on the 2 totals to get the total consumption

Why is LOW 1V rather than 0V or close to it ?

1 Like

You only ever apply 5V to the LED!

'energy consumed' is in Watt-seconds. Watts is Volts times Amps: 5V * 0.013A = 0.065W, 1V * 0.007A = 0.007W. Multiply that by time in seconds to get watt-seconds (or by milliseconds to get Watt-milliseconds.

For a single LED:

float Accumulator = 0.0;
unsigned long TotalPowerConsumed = 0;  // Watt-Milliseconds

void SetBrightness(int brightness)
{
   unsigned long currentMillis = millis();
   static int lastBrightness = 0;
   static unsigned long lastMillis = 0;
   if (brightness == lastBrightness)
     return;  // No change in brightness
   unsigned long elapsedMillis = currentMillis - lastMillis;
   if (lastBrightness > 0 && elapsedMillis > 0)
  {
    float Watts = 0.065 * (lastBrightness/255.0);
    float WattMilliseconds = Watts * elapsedMillis;
    Accumulator += WattMilliseconds;
    int fullWmS = Accumulator;
    if (fullWmS > 0)
    {
      TotalPowerConsumed += fullWmS;
      Accumulator -= fullWmS;
    }
  }

  analogWrite(LEDPin, brightness);
  lastBrightness = brightness;
  lastMillis = currentMillis;
}
1 Like

I really appreciate it. Thank you for your lovely support. :slight_smile:

Thank you for your time... I will edit my sketch with your support again. :slight_smile:

Uh, sorry. I mean (high,51) accidentally typed "low".

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.