How to interrupt Arduino with voltage input?

Hi guys,

I just would like to detect the power of charger when connected.
I saw few recommendations to use a voltage comparator but for simplicity and reduce component I'm finding simpler.
I try this:


Why it's not working? :face_with_raised_eyebrow: even with a Vout voltage close to 5V (4.5V).
Maybe because it's not an analog pin?

Edit:

const int INTERRUPT_PIN = 2; // Set interrupt pin

void setup() {
  pinMode(INTERRUPT_PIN, INPUT);
  attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), chargerConnected, RISING);
  Serial.begin(9600);
}

void loop() {
  // Check if chargeis connected
  Serial.print(digitalRead(INTERRUPT_PIN));
  delay(1000);
}

void chargerConnected() {
  // Interrupt service routine
  // Charger is connected, handle it here
  Serial.print("UP");
}

Hello kamoba

We need some additional information.
Post your sketch, well formated, with well-tempered comments and in so called code tags "< code >" and a real schematic to see how we can help.

Have a nice day and enjoy coding in C++.

Sorry paulpaulson, I Updated the post.

Digital read will not give true when Vpin == 0.5 Vcc.
The measured 2.5 V is not according to your resistors...
Where is the shared ground line to arduino?
How does arduino get power? Probably from usb... (that would be ok).

Hello kamoba

Yes.

There is no need to use an interrupt function.

You can read the voltage via an analog input pin.

Actually I would like to detect that while the MCU is on sleep mode, that's why I need an interrupt pin.

I read some where it should be at least 60% of Vcc but i tested with 4.5V by adjusting resistors without change.
And yes it's powered by usb.

It is waiting for a rising edge. Did you disconnect / connect your battery while looking at Serial?

You should not do Serial.print inside your interrupt. Serial uses interrupts itself....
Instead you should set a volatile boolean flag.

Hello kamoba

Consider this voltage monitor sketch.

constexpr uint8_t PotPin {A8}; 
constexpr uint16_t LowVoltage {1000}; // milli volt
constexpr uint16_t HighVoltage {4500}; // milli volt

void setup() 
{
  Serial.begin(115200);
  Serial.println(F("voltage monitor is running")); 
}
void loop() 
{
  uint16_t milliVoltage=map(analogRead(PotPin),0,1023,0,5000);
  static uint16_t milliVoltageOld=2500;
  milliVoltage=constrain(milliVoltage,LowVoltage,HighVoltage);
  switch (milliVoltage) 
  {
    case LowVoltage: 
    if (milliVoltageOld!=milliVoltage)
    {
      milliVoltageOld=milliVoltage;
      Serial.println(F("down")); 
    }
    break;
    case HighVoltage: 
    if (milliVoltageOld!=milliVoltage)
    {
      milliVoltageOld=milliVoltage;
      Serial.println(F("up")); 
    }
    break;
  }
}

Wake up the Arduino and run it.

Have a nice day and enjoy coding in C++.

Reply #4, no common ground.

Ok you are right changed for volatile.

const int INTERRUPT_PIN = 2; // Set interrupt pin
volatile int temp = 0;

void setup() {
  pinMode(INTERRUPT_PIN, INPUT);
  attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), chargerConnected, HIGH);
  Serial.begin(9600);
}

void loop() {
  // Check if chargeis connected
  Serial.print(digitalRead(INTERRUPT_PIN));
  Serial.println(temp);
  delay(1000);
}

void chargerConnected() {
  // Interrupt service routine
  // Charger is connected, handle it here
  temp = analogRead(INTERRUPT_PIN);
}

Still not working.
Anyway digitalRead(INTERRUPT_PIN) in loop should be HIGH if it's works.

Do you mean it won't work if separated power source?

It could work if i check it constantly, but i would like to use interrupt because i will be in sleep mode.

Yes.

To wake up the Arduino is one task.
The scond is to run the voltage monitor.
These are two different task to be done.
Or I´m wrong? confused

Do you have a common ground now???

Euhh actually no because it's a separated power source.

1 Like

Good reading, I admit I always warring to mix connections of different voltage.
Is it risk if voltage battery is higher than voltage supported by the Arduino (12V BAT for example)?
I still need to read about common ground.

Now HIGH detected in loop with 2.5V but interrupt still not working (even with 4.55V).

Maybe add a push button in your battery line.
You are waiting for a rising edge. But it is already high before you start your sketch.