본문 바로가기

아두이노

아두이노 절전모드 (sleep mode

아두이노를 절전모드로 전환했다가 다시 켜는 방법

 

Arduino Pro Mini의 전력 소비량 비교

  • awake : 25mA
  • asleep : 0.57mA

 

ATmega328의 sleep mode

종류 키워드 비고
Idle SLEEP_MODE_IDLE 최소 절전모드
ADC Noise Reduction SLEEP_MODE_ADC  
Power-down SLEEP_MODE_PWR_DOWN 최대 절전모드
Power-save SLEEP_MODE_PWR_SAVE  
Standby SLEEP_MODE_STANDBY  

 

ATmega328의 sleep mode와 Wake-up 방법

(출처: https://static1.squarespace.com/static/55abde31e4b0249b70d593c9/t/5a6b4209c830255e65ed23b8/1516978711075/Atmel-42735-8-bit-AVR-Microcontroller-ATmega328-328P_Datasheet.pdf)

 

 

예제코드 

출처 : https://thekurks.net/blog/2018/1/24/guide-to-arduino-sleep-mode

 

/*
 * Author:Ab Kurk
 * version: 1.0
 * date: 24/01/2018
 * Description: 
 * This sketch is part of the beginners guide to putting your Arduino to sleep tutorial. 
 * It is to demonstrate how to put your arduino into deep sleep and how to wake it up.
 * Link To Tutorial http://www.thearduinomakerman.info/blog/2018/1/24/guide-to-arduino-sleep-mode
 */

#include <avr/sleep.h>   // this AVR library contains the methods that controls the sleep modes
#define interruptPin 2   // Pin we are going to use to wake up the Arduino


void setup() {
    Serial.begin(115200);                     // Start Serial Comunication
    pinMode(LED_BUILTIN, OUTPUT);             // We use the led on pin 13 to indecate when Arduino is A sleep
    pinMode(interruptPin, INPUT_PULLUP);      // Set pin d2 to input using the buildin pullup resistor
    digitalWrite(LED_BUILTIN, HIGH);          // turning LED on
}


void loop() {
   delay(5000);         // wait 5 seconds before going to sleep
   Going_To_Sleep();
}


void Going_To_Sleep(){
    sleep_enable();                        // Enabling sleep mode
    attachInterrupt(0, wakeUp, LOW);       // attaching a interrupt to pin d2
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);   // Setting the sleep mode, in our case full sleep
    digitalWrite(LED_BUILTIN,LOW);         // turning LED off
    delay(1000);                           // wait a second to allow the led to be turned off before going to sleep
    sleep_cpu();                           // activating sleep mode
    Serial.println("just woke up!");       // next line of code executed after the interrupt 
    digitalWrite(LED_BUILTIN,HIGH);        // turning LED on
  }


void wakeUp(){
    Serial.println("Interrrupt Fired");    // Print message to serial monitor
    sleep_disable();                       // Disable sleep mode
    detachInterrupt(0);                    // Removes the interrupt from pin 2;
}

 

코드 설명

pinMode(LED_BUILTIN, OUTPUT);

LED_BUILTIN 아두이노 온보드 LED 지정한 것으로 아두이노 우노/미니의 경우 13 핀에 해당함

 

pinMode(interruptPin, INPUT_PULLUP); 

인터럽트 핀을 floating 상태에서도 HIGH 유지할 있도록 INPUT_PULLUP으로 설정함

 

delay(5000); 

아두이노가 깨어있는 상태를 확인할 있는 시간으로 넉넉히 5초의 시간을 두었음

 

sleep_enable();

아두이노가 sleep mode 사용할 있으며, 인터럽트로 sleep mode에서 깨어날 있도록 세팅하는 함수

 

attachInterrupt(0, wakeUp, LOW);

attachInterrupt(digitalPinToInterrupt(interruptPin), wakeUp, LOW);으로 사용 권장

2번핀에 인터럽트를 설정하였고, ISR wakeUp(), interruptPin 풀업 상태(HIGH)이므로 핀의 상태가 LOW 바뀔때 인터럽트를 발생시킴

 

set_sleep_mode(SLEEP_MODE_PWR_DOWN);

ATmega328 sleep mode power-donw 모드로 설정함

 

sleep_cpu();

아두이노가 sleep mode 전환함

 

sleep_disable();

아두이노가 sleep mode에서 깨어남

 

detachInterrupt(0);  

detachInterrupt(digitalPinToInterrupt(interruptPin)); 으로 사용 권장

인터럽트를 해제함

 

 

동작

  1. 프로그램을 업로드 하기 전에 2핀에 점퍼선을 연결한다. 아두이노에 전원을 연결하고, 프로그램을 업로딩한다.
  2. 5초 후 아두이노의 LED가 꺼지고, 1초 후 sleep mode로 전환된다. 
  3. 2번 핀의 점퍼선을 아두이노의 그라운드(GND)에 연결하여 인터럽트를 발생시킨다.
  4. 아두이노는 sleep mode에서 깨어나고 LED가 켜진다. 
  5. 단계 2로 되돌아간다.

 

부가설명

Going_To_Sleep() 함수에서 sleep_cpu()에 의해 아두이노가 sleep mode로 전환된 후 인터럽트에 의해 sleep mode에서 깨어나면 sleep_cpu() 다음 줄의 코드를 수행한다.