[Tuto] Mode sommeil sur Trinket

Posté sur: juil. 19, 2019
Catégories: Arduino
Les modes sommeil permettent de réduire énormément la consommation des cartes électroniques. Sur le Trinket d'Adafruit, il est souvent recommandé de passer par les librairies Adafruit_sleepydog dans cet exemple nous allons passer par l'utilisation d'une librairie plus bas niveau.
Le cœur de l'Adafruit Trinket est un microcontrôleur Attiny 85. Celui-ci est basé sur une architecture de type AVR. Nous allons donc passer dans notre cas par les librairies pour Arduino avr/sleep.h et avr/interrupt.h
Pour faire nos essais, nous allons nous baser sur le sketch exemple "Button" inclue dans l'IDE Arduino. Après quelques modifications de pinout, il est compatible avec le Trinket:
const int buttonPin = 0; // the number of the pushbutton pin const int ledPin = 1; // the number of the LED pin int buttonState = 0; // variable for reading the pushbutton status void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); } void loop() { // read the state of the pushbutton value: buttonState = digitalRead(buttonPin); // check if the pushbutton is pressed. If it is, the buttonState is HIGH: if (buttonState == HIGH) { // turn LED on: digitalWrite(ledPin, HIGH); } else { // turn LED off: digitalWrite(ledPin, LOW); } }
Le mode sleep va fonctionner comme ceci:
- Lorsque le bouton n'est pas pressé
- La LED s'allume 3 secondes
- Le mode sleep s'enclenche avec la LED qui reste allumée
- Lors de l'appuie sur le bouton le Trinket sort du mode sleep
Voici le code que cela donne:
#include <avr/sleep.h> #include <avr/interrupt.h> #include <avr/power.h> const int buttonPin = 0; // the number of the pushbutton pin const int ledPin = 1; // the number of the LED pin int buttonState = 0; // variable for reading the pushbutton status void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT_PULLUP); } void sleep() { GIMSK |= _BV(PCIE); // Enable Pin Change Interrupts PCMSK |= _BV(PCINT0); // Use PB0 as interrupt pin 0 ADCSRA &= ~_BV(ADEN); // ADC off set_sleep_mode(SLEEP_MODE_PWR_DOWN); // replaces above statement sleep_enable(); // Sets the Sleep Enable bit in the MCUCR Register (SE BIT) sei(); // Enable interrupts sleep_cpu(); // sleep cli(); // Disable interrupts PCMSK &= ~_BV(PCINT0); // Turn off PB0 as interrupt pin sleep_disable(); // Clear SE bit ADCSRA |= _BV(ADEN); // ADC on sei(); // Enable interrupts } // sleep ISR(PCINT0_vect) { // This is called when the interrupt occurs, but I don't need to do anything in it } void loop() { // read the state of the pushbutton value: buttonState = digitalRead(buttonPin); // check if the pushbutton is pressed. If it is, the buttonState is HIGH: if (buttonState == HIGH) { // turn LED on: digitalWrite(ledPin, HIGH); delay(3000); sleep(); } else { // turn LED off: digitalWrite(ledPin, LOW); } }
A titre de comparaison, sans le mode sleep LED allumée le Trinket consomme 22mA sous 12V avec le mode sleep cela tombe à 13mA soit une économie de 9mA.
Connectez-vous pour commenter
Se connecter