How to interrupt Arduino with voltage input?

I already change for easy testing to:

attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), chargerConnected, HIGH);

Try digitalread and boolean variable in your interrupt.

Temp is now a 2 byte variable. The next interrupt may change it during reading...
You now may have a million interrupts per second...
So the rising edge combined with the button might be an option.

I try this:

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

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

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 = digitalRead(INTERRUPT_PIN);
}

Inspired to this working circuit:

int interrupcion = 0;
// Interrupt.0 se encuentra en el pin digital 2 
// Interrupt.1 se encuentra en el pin digital 3

volatile int numInterrupt = 0;

void setup() {

  Serial.begin(9600);

  /* Sintaxis interrupciones:
   attachInterrupt(pin,función_a_ejecutar,modo);
   Modos:
	LOW: se ejecuta siempre que el valor en el pin sea 0.
	CHANGE: se ejecuta siempre que se produzca un cambio.
	RISING: se ejecuta mientras el valor va de 0 a 1 (o de 0 a 5 Voltios).
	FALLING: se ejecuta mientras el valor va de 1 a 0 (o de 5 a 0 Voltios).
   */

  attachInterrupt(interrupcion, funcionInterrupcion, HIGH);
}

void loop() {

  /*Simulamos una tarea que requiera tiempo.
	Utilizamos for ya que siempre se cumplirá la
	condición y nunca dejará de ejecutar el código incluido.
  */
  for(int i = 0; i < 100; i++){
    Serial.print(i);
    Serial.print(", ");
    delay(2500);
  }

  Serial.println("nuevo LOOP");
}

void funcionInterrupcion() {
  
  Serial.print("Interrupcion numero ");
  Serial.println(numInterrupt);
  
  numInterrupt++; // Aumentamos el número almacenado en numInterrupt en una unidad.
}

I do not know what i did wrong.
I'm going to sleep... :sleeping: I will do more testing later.

You should set temp to true....
The interrupt pin is high (otherwise you would not be in the interrupt function), so there is no need to read the pin inside your interrupt.
Initialise temp as false.
See if it ever gets true.

Why the 1k resistor?
I meant to put the button in the battery power line....
So you can start with no battery and then (when the program runs) add the battery by pushing the button.

Ah yeah I forgot to set the bool.

void chargerConnected() {
  temp = true; // digitalRead(INTERRUPT_PIN);
}

still no change.
The 1k resistor I do not know why it's used on the working circuit but if I remove it or replace it with single wire it won't work, i was expecting you explain me.

You have set interrupt pin to pin 0.
Pin 0 and pin 1 are reserved for Serial communication..

If you want to trigger an interrupt when the power supply on the left goes below (or is it above ?) a certain threshold using a digital pin, i suggest you change this power supply in your simulation with a saw-tooth generator and have it slooooowly ramp down (or up if above) and read the generator voltage (via the voltmeter or an AnalogRead()) as the interrupt triggers.

But the digital pin won't trigger the interrupt at the same voltage level in simulation and in real life, trimmer required. It might even depend on temperature and Arduino PS voltage.

An other solution is to use a timer to wake the Arduino up from time to time and AnalogRead() the voltage... To be simulated with a saw-tooth generator as well.

Maybe the analog comparator would work for you. You'll have to experiment with the interrupt vs. sleep mode to be sure.

https://www.gammon.com.au/forum/?id=11916

1 Like

I do not know what I did wrong from my first circuit (post #19) but it's working now.

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

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

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 = true; // digitalRead(INTERRUPT_PIN);
  Serial.print("up");
}

Thanks All!

2.5 V is not really high (it is halfway). I do not know the exact definition of high on Arduino. But to be on the safe side, it is better to decrease the resistor to plus side of your battery.
Also, you are writing to Serial in your interrupt...

1 Like

Yes I did not forgot your comment on using serial inside interrupt function, I just put it back for testing. About the minimal voltage to be HIGH I tested and it's work from 2.3V but probably in real life it may vary, so I though 2.5V should be fine but I can go up to 3V because my MCU will be powered at 3.3v and add 0.1uF capacitor between Pin2 and GND.

A quick google search came up with an arduino forum post stating that 3V and higher is guaranteed as HIGH. So you are in the grey area now...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.