Wake up from sleeping mode doesn't work

Hello everybody,
I can’t wake up Arduino UNO from sleeping mode using external voltage signal (5V) on digital pin 2. Sleeping goes well, since led diode is turned off and data from sensor aren’t printed on serial monitor after condition is met. Voltage impulse doesn’t wake it up. Meaning, led diode isn’t lighted up and data from sensor are not read.

I enclose my whole code. Thank you for any suggestions.

#include <Wire.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include "SparkFunISL29125.h"

int ledPin = 13;            // LED connected to digital pin 13
int count = 0;


void wakeUpNow()        // here the interrupt is handled after wakeup
{
  sleep_disable();
  detachInterrupt (0);
}


SFE_ISL29125 RGB_sensor;

void setup()
{

  pinMode(ledPin, OUTPUT);         // sets the digital pin as output

  Serial.begin(9600);
  Wire.begin();

  digitalWrite(ledPin, HIGH);
  delay (500);

 // Inicialization of sensor
  if (RGB_sensor.init())
  {
    Serial.println("Sensor Initialization Successful\n\r");
  }

}

void sleepNow()
{
  set_sleep_mode (SLEEP_MODE_PWR_DOWN);   
  sleep_enable ();          // enables the sleep bit in the mcucr register
  attachInterrupt (0,wakeUpNow, LOW);  // wake up on low level on D2
  sleep_mode ();            // here the device is actually put to sleep!!
}  // end of sleepNow


void loop()
{
// Read data from sensor
  unsigned int red = RGB_sensor.readRed();

  Serial.print("Red: "); Serial.println(red, DEC);
  Serial.println();
  
// Write data into EEPROM memory of NFC tag
  Wire.beginTransmission(25); 
  Wire.write(0x40); 
  Wire.write(0x55); 
  Wire.write(0x00); 
  Wire.write(red); 
  Wire.write(red>>8); 
  Wire.write(0x00); 
  Wire.write(0x00); 
  Wire.endTransmission();      

  count++;

  delay(2000); 
  
  if (count==2){
  digitalWrite(ledPin, LOW);
  delay(100);
  sleepNow();
  }
}

Have you a pull up resistor (say 10k) on D2 ? Then grounding D2 should force a wake up.
I guess you need digitalWrite(ledPin, HIGH); somewhere in your loop() also.

Thank you. It was really because of pull-up resistors. Now, it wakes up and stays waked up even pin 2 is not grounded. Does it mean, that it can be put into sleep just once or I have some other mistake in the code? Do you know how to solve this?

Nevermind, I just figure it out. Thank you for your help again :slight_smile:

Nuramon:
Nevermind, I just figure it out. Thank you for your help again :slight_smile:

Always a good idea to say what the problem was, just in case anyone searches the forum looking for similar help …

Good point! I just set count to “0” in my condition “if”. It works well, when I attach smartphone to NFC tag, Arduino is triggered - sensor messures and led is lighted up.

But another problem occured. When I leave smartphone on the tag for longer time (Pin 2 stays LOW even after condition is met - Arduino is triggered) and then I take it from the NFC tag and return it back. Arduino is not triggered again. I don’t know why shouldn’t be triggered.

My code is following:

#include <Wire.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include "SparkFunISL29125.h"

int ledPin = 13;            // LED connected to digital pin 13
int count = 0;


void wakeUpNow()        // here the interrupt is handled after wakeup
{
  sleep_disable();
  detachInterrupt (0);
}



SFE_ISL29125 RGB_sensor;

void setup()
{

  pinMode(ledPin, OUTPUT);         // sets the digital pin as output
  pinMode(2,INPUT_PULLUP);
 
  Serial.begin(9600);
  Wire.begin();

 // Inicialization of sensor
  if (RGB_sensor.init())
  {
    Serial.println("Sensor Initialization Successful\n\r");
  }

}

void sleepNow()
{
  set_sleep_mode (SLEEP_MODE_PWR_DOWN);   
  sleep_enable ();          // enables the sleep bit in the mcucr register
  attachInterrupt (0,wakeUpNow, LOW);  // wake up on low level on D2
  sleep_mode ();            // here the device is actually put to sleep!!
}  // end of sleepNow



void loop()
{
// Light up led diode and read from RGB sensor
  detachInterrupt (0); 
  digitalWrite(ledPin, HIGH);
  delay (500);
  unsigned int red = RGB_sensor.readRed();

  Serial.print("Red: "); Serial.println(red, DEC);
  Serial.println();
  
// Write to NFC tag EEPROM memory
  Wire.beginTransmission(25); 
  Wire.write(0x40); 
  Wire.write(0x55); 
  Wire.write(0x00); 
  Wire.write(red);  
  Wire.write(red>>8); 
  Wire.write(0x00);
  Wire.write(0x00); 
  Wire.endTransmission();     

  count++;

  delay(2000); 
  
  if (count==2){
  digitalWrite(ledPin, LOW);
  digitalWrite(2, HIGH);
  count = 0;
  delay(100);
  sleepNow();
  }
}