Arduino: attachInterrupt events linked to a range of voltage from -12 to +12V

Hi there, I would like to have attachInterrupt events in Arduino related to the following voltage values: -12V, 0V, 6V, 9V, 12V ( the range is from -12V to +12V). Considering that the Arduino is not able to read a such high range of voltage I suppose that probably I have to use a voltage divider but I don't know how to calculate the resistors to use. Can anyone help me?

THK, Filippo

What do you think an attachInterrupt event might be?

Vout = Vin * Rl / (Ru + Rl)

Ru = upper resistor Rl = lower resistor

For dual polarity a more complex arrangement of resistors is needed.

[ 18k to +5V, 27k to gnd, 56k to the incoming voltage might work well for a +/-15V range mapped to 0--5V ]

Or use opto isolators

Do you have the link of any good example/tutorial about it? Does the SparkFun opto isolator works also for negative incoming Voltages?

I don't really need to use the attachInterrupt() even the simple analogRead() can be fine. However, reading constantly the the value of a specific single pin I need to recognize the different passed voltages (-12V, 0V, 6V, 9V, 12V) to trigger some functions. THK

filippoaiello: I don't really need to use the attachInterrupt() even the simple analogRead() can be fine.

No, you really don't!

Whenever I see a post which includes the word "interrupt", I instinctively cringe.

Seems you - and many other "newbies" of course - do not understand what an interrupt is.

An interrupt is a mechanism for performing a task that can be performed virtually instantaneously, that must be performed virtually immediately, and that is not in itself intended to affect the flow of the main program (or delay it) in any way. Servicing a pushbutton for example - a common expectation - essentially tends to fail all three criteria. The terms "instantaneously" and "immediately" are here defined in microprocessor, not human context, that is in terms of microseconds.

You wish to "interrupt" the operation of a particular part of your code. This is quite different to the above; to do this, you must insert decisions into the code where you wish it to be "interrupted".

The trick is to write "non-blocking" code. This prohibits the use of "While" loops or equivalent constructs (which automatically precludes "delay()"), but is instead, a chain of "If" constructs within the main loop(). Which incidentally, is a state machine. The important thing is that many different tasks are allowed to be performed apparently simultaneously given that each only does what it must at any given instant, promptly passing on to the next.

Yes you can wire an opto to handle negative voltage - the input side of a run of the mill opto is just an led.

Using 1 dual op amp do the following:

!) Create inverting amplifier with a gain of 1, but insert a offset of 12V to the + (non inverting pin), this will max your -12 to 12V range change to a -24-0V range.

2) To the output of stage 1, connect another inverting amplifier with gain of 0.2 you would get a a positive voltage range of 0-4.8V which is fine to work with the arduino's 0-5V ADC tolerance.

aqibi2000, could you please share the drawing with the configuration of the suggested setting and the components I should purchase? THK