Discussion Forums

High power consumption after analog write

#1

Hellow,

I am working with leds and for power consumption I am using analog write to power the led, the problem is that after the led is powered off by analogWrite(pin, 0) the ble is consuming 300 ua, even if I remove the led and with the ble in sleep mode (through ble.waitforevent)

My code:

#include <nRF5x_BLE_API.h>

#define PIN_TO_DIGITAL(p) (p)    
#define IS_PIN_PWM(p)     ( (p) == 3 || (p) == 5 || (p) == 6 )    
#define PIN_TO_PWM(p)     PIN_TO_DIGITAL(p)

#define ADC_TIMES_IN_50_hz 650
#define PIN_ADC D1
#define LED_RED D2
// #define LED_RED A2
// #define LED_GREEN D3
// 32700 [samples/s] / 50 [1/s] = 650

#define ADVERTIZING_PERIOD_SECONDS 0.5
#define SAMPLE_TIME 7

bool state = true;

uint8_t beaconPayload[] =
{
  0x4C,0x00,                                                                       // Company Identifier Code = Apple
  0x02,                                                                            // Type = iBeacon
  0x15,                                                                            // Following data length
  0x71,0x3d,0x00,0x77,0x50,0x3e,0x4c,0x75,0xba,0x94,0x31,0x31,0x31,0x31,0x33,0x33, // Beacon UUID
  0x00,0x00,                                                                       // Major
  0x00,0x00,                                                                       // Minor
  0xC5                                                                             // Measure Power
};

BLE ble;
Ticker ticker_task1;

float read_voltage() // for 50hz wave rms value
{
  long int sum = 0;
  int it = 0;
  for(it = 0; it < ADC_TIMES_IN_50_hz; it++)
  {
    sum += analogRead(PIN_ADC);
  }
  float voltage = (sum/float(ADC_TIMES_IN_50_hz))  * (1.0/1024);
  return voltage;
}

void turn_off_led()

{
  analogWrite(LED_RED, 0);
  
  //digitalWrite(LED_RED, LOW);

  // digitalWrite(LED_GREEN, LOW);
  ticker_task1.detach();
  ticker_task1.attach(turn_on_led, SAMPLE_TIME);
}

void turn_on_led()
{
  
  float voltage = read_voltage();
  float limit_voltage = 0.90;
  bool current_state;
  if (voltage < limit_voltage)
  {
    current_state = true;
    if(state != current_state){
      if(read_voltage() < limit_voltage){
          state = current_state;
      }
    }
  }
  else
  {
    current_state = false;
    if(state != current_state){
      if(read_voltage() >= limit_voltage){
          state = current_state;
      }
    }
  }

  if(state)
  {
    state = true;
    analogWrite(LED_RED, 120);
    digitalWrite(LED_RED, HIGH);
    // digitalWrite(LED_GREEN, LOW);
  }
  else
  {
    state = false;
    // analogWrite(LED_RED, 0);
    digitalWrite(LED_RED, LOW);
    // digitalWrite(LED_GREEN, HIGH);
  }
  ble.clearAdvertisingPayload();
  // beaconPayload[20:21] -> major beaconPayload[22:23] -> minor
  beaconPayload[20] = 0;
  beaconPayload[21] = int(voltage * 100.0);

  beaconPayload[22] = 0;
  beaconPayload[23] = 0;

  ble.accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, beaconPayload, sizeof(beaconPayload));

  // Serial.println(voltage);
  ticker_task1.detach();
  ticker_task1.attach(turn_off_led, 0.13);
}


int main(void)
{
  pinMode(PIN_TO_PWM(LED_RED), OUTPUT);
  NRF_POWER->DCDCEN = 0x00000001;
  ticker_task1.detach();

  pinMode(PIN_TO_PWM(LED_RED), OUTPUT);
  pinMode(PIN_ADC, INPUT);
  pinMode(LED_RED, OUTPUT);

  // pinMode(LED_GREEN, OUTPUT);
  ticker_task1.attach(turn_on_led, SAMPLE_TIME);

  ble.init();
  // Beacon Things
  //=========================================================================================
  ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
  ble.accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, beaconPayload,  sizeof(beaconPayload));
  ble.setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
  ble.setAdvertisingInterval(160 * 10 * ADVERTIZING_PERIOD_SECONDS);
  // ble.setTxPower(4);
  ble.setAdvertisingTimeout(0);
  ble.startAdvertising();

  while(true)
  {
      ble.waitForEvent();
  }

  return 0;
}

I am using arduino IDE.

Thanks!

#2

Hi,

Did you try to change it back to digital out and set it to low?