아두이노를 절전모드로 전환했다가 다시 켜는 방법
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 |
예제코드
출처 : 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)); 으로 사용 권장
인터럽트를 해제함
동작
- 프로그램을 업로드 하기 전에 2핀에 점퍼선을 연결한다. 아두이노에 전원을 연결하고, 프로그램을 업로딩한다.
- 5초 후 아두이노의 LED가 꺼지고, 1초 후 sleep mode로 전환된다.
- 2번 핀의 점퍼선을 아두이노의 그라운드(GND)에 연결하여 인터럽트를 발생시킨다.
- 아두이노는 sleep mode에서 깨어나고 LED가 켜진다.
- 단계 2로 되돌아간다.
부가설명
Going_To_Sleep() 함수에서 sleep_cpu()에 의해 아두이노가 sleep mode로 전환된 후 인터럽트에 의해 sleep mode에서 깨어나면 sleep_cpu() 다음 줄의 코드를 수행한다.
'아두이노' 카테고리의 다른 글
아두이노에서 가변저항을 이용한 DC 모터 속도 제어 (0) | 2021.05.31 |
---|---|
아두이노 릴레이 제어 (0) | 2021.05.18 |
아두이노를 이용한 자동차 후방 센서 구현 : 초음파 거리센서+LED+피에조 (0) | 2021.05.16 |
아두이노와 2개의 7세그먼트를 이용한 카운터 구현 (0) | 2021.05.16 |
아두이노와 7세그먼트를 이용한 16진수 카운터 (0) | 2021.05.16 |