Maintaining PWM output during sleep/hibernate modes using Snooze library?

Status
Not open for further replies.

iikushan

Member
Hi everyone,

I have a project (it will use either Teensy 3.2 or LC, still TBD) where I need to output PWM for a about a second, and then possibly update the PWM value, wait another second, and so forth. I want to put the MCU into a sleep mode in order to save power during the wait period. Is this possible with Teensy? I did a quick test using the Snooze library, but it does not seem to work.

This is the code I tried:

Code:
/*
	AnalogWrite with snooze test
	01/01/2017
 */
#include <Snooze.h>
SnoozeTimer timer;
SnoozeBlock config(timer);


void setup() {
  pinMode(3, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  timer.setTimer(1000);
}

void loop() {
	analogWrite(3, 32);
	Snooze.hibernate(config);
	analogWrite(3, 0);
	blink();
	analogWrite(3, 64);
	Snooze.hibernate(config);
	analogWrite(3, 0);
	blink();
	analogWrite(3, 128);
	Snooze.hibernate(config);
	analogWrite(3, 0);
	blink();
	analogWrite(3, 256);
	Snooze.hibernate(config);
	analogWrite(3, 0);
	blink();
}

void blink() {
	for (int i = 0; i < 2; i++) {
		digitalWrite(LED_BUILTIN, HIGH);
		delay(200);
		digitalWrite(LED_BUILTIN, LOW);
		delay(200);
	}
}

This code is supposed to sweep the PWM output of pin 3 after a wait period of a second. It also blinks the onboard LED two times between transitions.
When I run this, I don't see the PWM rate updating. What I see instead is that pin 3 will be stuck to either high or low during the sleep. It seems like that pin gets stuck at whatever the PWM output was just as the MCU entered the sleep mode.

Any insights and tips are appreciated. Thank you and a happy new year!
 
ever heard of a free running pca9685? it's a set and forget pwm i2c chip, set the pwm duty cycle and just make your micro goto sleep, done. 16 free-running pwm channels for you. :)
if your timing the pwm like your doing it can be done with this as well, mcu can wake up, do the pwm, stop it, then goto sleep, repeat etc
 
Hi everyone,

I have a project (it will use either Teensy 3.2 or LC, still TBD) where I need to output PWM for a about a second, and then possibly update the PWM value, wait another second, and so forth. I want to put the MCU into a sleep mode in order to save power during the wait period. Is this possible with Teensy?
Try using 'sleep' instead of 'hibernate' or 'deepsleep' which shut down pretty much everything on the Teensy. I'll see what it does tonight but 'sleep' should allow some functionality while in low power mode but I can't say for sure that pwm will still work with Snooze's current configuration. Also what version of Snooze are you using?
 
duff:

I am using v6.2.4. I just tried using sleep, and I am getting the same results.

tonton81:

My application is space constrained, so I don't want to add any additional ICs to my PCB unless I absolutely have to.
 
duff:

I am using v6.2.4. I just tried using sleep, and I am getting the same results.

tonton81:

My application is space constrained, so I don't want to add any additional ICs to my PCB unless I absolutely have to.

I'm looking into this now, the problem is 'sleep' changes the cpu speed to 2 MHz while the pwm configured at whatever speed you compiled your sketch so unless you compiled it at 2 MHz the pwm will have the wrong register values. The good new is that pwm will work during sleep now I just need make the pwm transition to 2 MHz not glitch which might not be easy.
 
I'm looking into this now, the problem is 'sleep' changes the cpu speed to 2 MHz while the pwm configured at whatever speed you compiled your sketch so unless you compiled it at 2 MHz the pwm will have the wrong register values. The good new is that pwm will work during sleep now I just need make the pwm transition to 2 MHz not glitch which might not be easy.

Oh awesome, I will try compiling my code at 2MHz and see if it works. Thanks for looking into this!
 
Oh awesome, I will try compiling my code at 2MHz and see if it works. Thanks for looking into this!
I updated Snooze and now it has an example w/ driver for maintaining the right PWM phase even during 'sleep' mode. It's not a perfect transition from F_CPU to 2 MHz and back but as you will see in the pics below there is a small glitch during these transitions:

This pic shows the PWM signal on pin 3 transition from F_CPU to 2 MHZ glitch, the yellow signal is the PWM, blue is my trigger.
F_CPU_to_2MHz_PWM.png


This pic shows the PWM signal on pin 3 transition from 2 MHZ to F_CPU glitch, the yellow signal is the PWM, blue is my trigger.
2MHz_to_F_CPU_PWM.png


I'm still working on this but it will have to take a back seat to some other projects I have going on now. I also added a new driver for USB Serial and an example. Let me know if this works for you?
 
I updated Snooze and now it has an example w/ driver for maintaining the right PWM phase even during 'sleep' mode. It's not a perfect transition from F_CPU to 2 MHz and back but as you will see in the pics below there is a small glitch during these transitions:

This pic shows the PWM signal on pin 3 transition from F_CPU to 2 MHZ glitch, the yellow signal is the PWM, blue is my trigger.
View attachment 9297

This pic shows the PWM signal on pin 3 transition from 2 MHZ to F_CPU glitch, the yellow signal is the PWM, blue is my trigger.
View attachment 9298

I'm still working on this but it will have to take a back seat to some other projects I have going on now. I also added a new driver for USB Serial and an example. Let me know if this works for you?

I just tried your example. It works great, thank you! Good to know about the glitch, but I think this shouldn't matter for my application. Thanks again!
 
I updated Snooze and now it has an example w/ driver for maintaining the right PWM phase even during 'sleep' mode. It's not a perfect transition from F_CPU to 2 MHz and back but as you will see in the pics below there is a small glitch during these transitions:

This pic shows the PWM signal on pin 3 transition from F_CPU to 2 MHZ glitch, the yellow signal is the PWM, blue is my trigger.
View attachment 9297

This pic shows the PWM signal on pin 3 transition from 2 MHZ to F_CPU glitch, the yellow signal is the PWM, blue is my trigger.
View attachment 9298

I'm still working on this but it will have to take a back seat to some other projects I have going on now. I also added a new driver for USB Serial and an example. Let me know if this works for you?

One more thing: I realized that whatever PWM frequency I set before gets overwritten by 400-something Hertz once I call the Snooze PWM driver. Would it be possible to fix this, or at least point me in the right direction about fixing it myself if you are too busy with other projects? Thanks!
 
One more thing: I realized that whatever PWM frequency I set before gets overwritten by 400-something Hertz once I call the Snooze PWM driver. Would it be possible to fix this, or at least point me in the right direction about fixing it myself if you are too busy with other projects? Thanks!
Hmm not sure what you mean, can you post an example that shows this? All it does is recofigure the FTM for 2 MHz operation. 488.28 is the default frequency for analogWrite is that what your talking about?
 
Hmm not sure what you mean, can you post an example that shows this? All it does is recofigure the FTM for 2 MHz operation. 488.28 is the default frequency for analogWrite is that what your talking about?

Yes. Can't send code at the moment, but what I saw was that that even though I set the analogWriteFrequency in the setup() function, I still see 488.28 as the PWM frequency during sleep. I will try to send code once I get to my lab.
 
Yes. Can't send code at the moment, but what I saw was that that even though I set the analogWriteFrequency in the setup() function, I still see 488.28 as the PWM frequency during sleep. I will try to send code once I get to my lab.
I see, the code I posted was only for analogWrite at the default carrier frequency (488.28Hz). It's going to be a few days before I can tackle this unless you get to it first:) This was just a partial solution to the problem, thanks for following up on this!
 
I see, the code I posted was only for analogWrite at the default carrier frequency (488.28Hz). It's going to be a few days before I can tackle this unless you get to it first:) This was just a partial solution to the problem, thanks for following up on this!

Sounds good :) We'll try to get it to work this week as well, and I'll post a solution here if we have any luck.
 
Duff any luck yet? I'm trying to generate 4 PWM's, two are 50kHz @ 50% DC, and the other
two are 15kHz max. with a varing duty cycle. The maximum frequency I can attain is about
1.19kHz. I'm using the snooze PWM. Is this possible, or should I be looking into an external
part like the Si5351?
 
Duff any luck yet? I'm trying to generate 4 PWM's, two are 50kHz @ 50% DC, and the other
two are 15kHz max. with a varing duty cycle. The maximum frequency I can attain is about
1.19kHz. I'm using the snooze PWM. Is this possible, or should I be looking into an external
part like the Si5351?
No I haven't yet though I'll update this thread when I do get to this.
 
At the moment I'm even looking for a way someone might have by register manipulation
to set the correct registers before sleep. (The manual way)
 
Status
Not open for further replies.
Back
Top