Interrupts and analog inputs

Hi there, firstly, let me just introduce myself to you guys, hi, I’m Rick and I’m coming from an Arduino background but have bought two of these Spark cores cos they look great.

However, they are a bit sparce on pins so I have had to rethink a control panel I originally designed for use on an Arduino Mega. I have a panel of 8 buttons which I want to connect using a resistor ladder to get an analog output. I have other stuff on the other pins so I need my 8 buttons to use as fewer pins as possible so the resistor ladder seems like a good solution.

If I use pin A0 as my reader for my button array, will it trigger the interrupt if my array only produces say .5v?

I have not received my cores yet so can’t test this out, it’s more of a feasibilty study currently, I see the core only outputs 3.3v so if anyone can tell me if this will work I’d greatly appretiate it.

Many thanks, Rick.


Something like a Parallel In Serial Out register would help you save on the pins :smiley:

0.5V will not trigger the interrupt since it’s almost like 0. What you can do is to read the analog value on A0 instead and set a threshold to recognize it as triggered.

Since you are interfacing buttons to the :spark: core, it’s essential an INPUT and there’s no issue with the pins only OUTPUTTING 3.3v. You won’t need the pins to be outputting a voltage.

1 Like

Awsome, I was hoping someone would suggest a chip I could use, I have found this one 74HC165N, would this mean that I can connect 8 buttons to it and then I would presumably get a serial (uart?) out. Is there a way I could get the chip to say trigger the interup then in the ISR read the serial output of the chip or am I getting it all a bit wrong with my understanding. Thanks for the reply :slight_smile:

Maybe someone would be better to suggest a chip since i haven’t used one. I only tried 595 which works the other way round.

You probably won’t need ISR since you can simply check for Serial.available() for process them in the loop()

But i guess it’s definitely possible to go the ISR way as well.

Ok, yes, I want to process my user controls via ISRs because it frees up my main loop to do the meat of the functionality.

I come from a .NET desktop developement background and so am used to handling events and using interrupts just feels much more natural to me for handling user input than constantly polling switches and buttons etc.

Can anyone else suggest an elegant way to handle an array of buttons, preferably via an interrupt and preferably hardware debounced. I am currently using R/C network and schmit trigger to debounce each button and having individual pins on the mega but, hmm, my knowledge of what components are out there / electronics design patterns is letting me down :frowning:

Why not try Software debounce since you are more to the software side? :wink:

The 2 buttons on the :spark: core is software debounced and would be lovely to take a look at how it’s done!

Hey, sorry to keep banging this drum but would this work :smile:

Then in the ISR attatched to A6 I could read A7. I however have no idea what transistor I’d need but with my limited electronics knowledge the small output of the ladder would make the transistor send 3.3v to A6 no :slight_smile:

rickpbush, I am not sure you need an interrupt in this case. With equal resistors, you will get about 0.4V discrimination per switch assuming all resistors are the same. That is an ADC count per switch of 511 or so.

By sampling the ADC in loop(), you can detect a single button push using an if/elseif ladder. Assuming loop() runs fast, your sampling negates the need for an interrupt. Another approach is using an Arduino library for creating debounced analog buttons. A quick look tells me it should be easy to port to the Spark. If that interests you, let me know. :smile:

UPDATE: I have the library posted but not tested. :smile:

Thanks peekay, I actually bought two sparks after reading through the Digole thread that you played a major part in, I was unaware of the spark prior to that and stumbled upon your thread while looking for help with the Digole Serial oled, as a direct result of your library work they sold 2 spark cores to me :slight_smile:

Anyway, I know I can put the button polling in the main loop, it’s just potentially I’ve got lots going on,

  • rotary encoder,
  • 8 buttons,
  • a 4 position selector switch,- a hall effect joystick
  • and all the drawing routines for displaying the menus, capturing user data etc.

All the controls are currently handled by ISRs to keep that stuff out of my way while processing the main loop. I may be wrong but I feel designing a system to use interrupts is preferential to polling constantly, if only that it ‘feels’ more comfortable to me to interrupt the main program flow when something ‘non normal’ happens, like the user wants attention, ie he pressed a button or something.

However, I was entirely unaware of the AnalogButtons library. I took a look and it is exactly what I’m trying to do :), I would however need 8 buttons so I may need to tweek it a bit, and there I was thinking I was being clever using a resistor ladder :slight_smile:

Another option for reducing pins would be a multiplexer. You could read 8 buttons with 4 digital pins (3 address lines, one data input). More pins that it would take with serial, but much simpler to read. It’s all about trade-offs, right? :smile:

rickpbush, I am glad you discoverd Spark because of me!

Another possiblity for your dilemma are I/O expanders that use the I2C bus - the MCP23017 and the SX1509 that also includes an LED driver and keyboard scanner with debounce.


Ok, I have decided that my best option is simply to use the library and a few resistors, I’ll poll in the loop as suggested by peekay and de-bounce in software, it just seems like the best solution to me and won’t require the procurement of exotic hardware. Thanks for your help on this guys :slight_smile: I now have a different set of questions for which I will create a new thread because they relate to expanding my basic (non existent) knowledge of the sparks capabilities/design.

rickpbush, the analogButtons library does the debouncing for you :smile:

Hi @rickpbush

I see that you also have a rotary encoder on your list, which is great. I don’t know if you mean the kind used with a motor to resolve position or the kind used with a knob as a user-input mechanism.

You will want to use interrupts for that for sure! I have the user-input knob kind working fine with interrupts (I can post the code) but I had to give up on software debouncing–just adding two small caps was much less frustrating. There is some good Arduino code out there, but there is also a lot of bad code for dealing with rotary encoders.

The motor kind of encoder can bring different challenges due to the higher speed requirements.

bko, from what I can see, rickpbush is doing menus on a display so I suspect the “rotary encoder” is most likely the manual kind. :smile:

Hi bko, thanks for the input, it’s a user input knob type.

I have implemented my rotary encoder using an RC network and schmit trigger, total overkill but it works brilliantly on an interrupt and I wanted to learn about debouncing which is why I did it that way.

I only needed to use one interrupt on the up cycle and a digital pin on the down because both up and down fire when the knob is turned, just out of sync with each other as I’m sure you know, I simply read both pins and compare when the interrupt is fired to deturmine turn direction.

I love these encoders used in conjunction with an LCD as a means of cycling alph/numeric character to capture user input strings, my encoder has a push button on top so entering strings feels really natural. By adjusting the cap/resistor value (ie taw), I can get really fast acurate debounced input.

At some point I’ll do a blog for those who are interested, I’m still a novice but I do get quite excited about making stuff work and getting feedback makes me feel less of a loner :slight_smile:

1 Like

Gotta love schmitt triggers! Nothing like debouncing the old fashioned way :ok_hand:

1 Like

Just for the sake of it, here’s what I’ve got, I know its on an Arduino but using this forum I intend to reduce that big ole’ mega and shield with a diddy little core :slight_smile:


That is exactly how I use my rotary encoder knob too–it scrolls the LCD display through choices and you push to select.

1 Like

@bko would you mind posting the rotary encoder code you use. I made a topic for it here: