Hi,
Im using an arduino pro micro and i would like to use the sleep mode.
I used the lowpower library with an exemple code.
But when i uploaded this code, i can't access to the Serial monitor in order to verify if it works.
Did you know why ?
Thank you for your help.
#include <LowPower.h>
void setup() {
Serial.begin(115200);
}
void lowPowerSleep(int minutes)
{
int seconds = minutes * 60;
int sleeps = seconds / 8;
for (int i = 0 ; i < sleeps ; i++) {
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
}
}
void loop() {
lowPowerSleep(1);
Serial.println("HELLO");
delay(1000);
}
according the the code you posted, your arduino will be put to sleep BEFORE it even gets to the Serial.print point.
also the other thing that I don't understand is you "lowPowerSleep" routine... why do you have a 'for' loop to repeatedly call the 'powerDown' functions.... IMHO it only needs to be called ONCE at which point the sleep(powerdown) process would be triggered.
The Pro Micro does USB in the main processor. If you put that in power-down sleep mode, almost everything is deactivated, the USB subsystem too. So you loose the USB connection and you're responsible to activate that again, once you wake up. If you immediately print something to that system it will be lost. Your operating system must handle a lost and reconnect correctly to be able to get anything from that device too.
The maximum time for the arduino sleeping with the lowpower library is 8s. So if i want to sleep during 10 minutes for exemple i need to make a loop right ? My RTC cant deliver any external pulse to wake up the arduino.
I set the Serial.print before putting the arduino to sleep. The objective is to wake up the arduino after a minute and print a "hello". After one minute, the arduino LED flashes but nothing is printed on the serial monitor. Is it because I need to wait longer or add something?
Hello,
Thank you for your answer. It is for try if the arduino wakes up properly.
At the end, my main objective is to write on the sd card some datas every 10 minutes.
I will try to write datas on my sd card after the arduino sleeps.
Then connect a USB2Serial adapter to the D0/D1 pins and use Serial1 for debugging. The UART is available almost immediately after wake-up.
Getting USB serial ready after a sleep is not worth the effort if it's used only for debugging.
Remember: you have to find a solution on your PC too, once you fixed the Arduino problem. If you're using Windows there, I cannot help.
Here is an example of using software serial comms between to 2 arduinos where one always sleeps until it receives a wakeup LOW pulse from other arduino.
sleepy arduino:
//Sleeping arduino
/*
When the arduino is in SLEEP_MODE_PWR_DOWN the only way to wake it is with either
- a watchdog timer interrupt,
- a level interrupt on pins 2 or 3, or
- a Pin Change interrupt
*/
#include <avr/sleep.h>
#include <SoftwareSerial.h>
#define RXPIN 2
#define TIMEOUT 1000UL
SoftwareSerial mySerial(RXPIN, 4); //pin2 RX, pin4 TX
unsigned long oldtime = 0;
void wakeUpNow() // here the interrupt is handled after wakeup
{
sleep_disable(); // first thing after waking from sleep:
// disable sleep...
detachInterrupt(digitalPinToInterrupt(RXPIN)); // disables interrupt 0 on pin 2 so the
Serial.println("Woken!");
sei();
mySerial.begin(9600);
mySerial.print("w"); //tx 'w' so indicate device in awake and ready to receive
oldtime = millis();
while (millis() - oldtime < TIMEOUT) { //wait for 1000ms for sender to reply
if (mySerial.available()) {
char c = mySerial.read();
if (c == 'W') {
Serial.println("WakeUp Acknowledged!");
break;
}
}
}
if (millis() - oldtime > TIMEOUT){
sleepNow();
}
}
void sleepNow() {
sleep_enable();
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
/* Trigger mode of the interrupt pin. can be:
LOW a low level triggers
CHANGE a change in level triggers
RISING a rising edge of a level triggers
FALLING a falling edge of a level triggers
In all but the IDLE sleep modes only LOW can be used.
*/
Serial.println("Sleeping Now");
delay(500);
mySerial.end();
attachInterrupt(digitalPinToInterrupt(RXPIN), wakeUpNow, LOW); // wakeUpNow when pin 2 gets LOW
sleep_mode();
}
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
pinMode(RXPIN, INPUT_PULLUP);
}
void loop() {
// put your main code here, to run repeatedly:
if (millis() - oldtime < TIMEOUT) { //go to sleep if no data received by TIMEOUT
if (mySerial.available()) {
oldtime = millis();
char c = mySerial.read();
Serial.println(c); //print RX data in serial monitor (as ASCII)
}
}
else {
mySerial.print("s"); //tells sender it is going to sleep
sleepNow();
}
}
waking arduino
#include <SoftwareSerial.h>
SoftwareSerial mySerial(11, 12);
unsigned long oldtime = 0;
void sendData() {
char data[9] = "abcdefgh";
for (char i = 0; i < 8; ++i) {
mySerial.print(data[i]);
delay(50);
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
mySerial.begin(9600);
Serial.println("READY");
}
void loop() {
char otwp;
// put your main code here, to run repeatedly:
if (mySerial.available()) {
otwp = mySerial.read();
Serial.println(otwp);
}
if (otwp == 'w') {
mySerial.print("W");
sendData();
}
if (millis() - oldtime > 5000) { //send a 2ms LOW pulse (wakeup command)
digitalWrite(12, LOW);
delay(2); //at least 2ms LOW is required to cause other arduino to wake up
digitalWrite(12, HIGH);
oldtime = millis();
}
}
Thank you for your answer.
Actually the serial monitor is deactivated and does not reactivate itself. It's not important in my case because I want to access the serial monitor during the initialization to calibrate my weight sensor. However, it writes to my SD card every minute.
#include <LowPower.h>
#include <SPI.h>
#include <SD.h>
File myFile;
void setup() {
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
SD.begin(4);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
}
void lowPowerSleep(int minutes)
{
int seconds = minutes * 60;
int sleeps = seconds / 8;
for (int i = 0 ; i < sleeps ; i++) {
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
}
}
void loop() {
myFile = SD.open("toto.txt", FILE_WRITE);
if (myFile) {
myFile.println("testing 1, 2, 3.");
myFile.close();
}
delay(2000);
lowPowerSleep(1);
//delay(1000);
}
Yes, the arduino wrote on the sd card "testing 1, 2, 3." each minutes
I dont know if that was your question.
You can find my code with an application on weight sensor in this post.