Has anyone used the Adafruit MPR121 Touch Sensor Breakout with the Propeller?

Hi All,

Has anyone got any code to drive the Adafruit MPR121 Touch Sensor Breakout? It is an I2C device.

Thanks!

Jim

Comments

  • 18 Comments sorted by Date Added Votes
  • Mike GreenMike Green Posts: 22,753
    edited June 3 Vote Up0Vote Down
    It’s only a general I2C interface, but “Basic I2C Driver” in the OBEX would let you access the device’s registers. You can get the datasheet from AdaFruit. I don’t know if they also provide the sources for their Arduino library. You also might look at their CircuitPython library for ideas.

    There are other I2C interfaces in the OBEX. This one is pretty straightforward to use.
  • JonnyMacJonnyMac Posts: 6,090
    edited June 3 Vote Up0Vote Down
    I don't have one of those devices but as Mike points out, it's a pretty simple interface with I2C. Adafruit does provide a simple Arduino library for the part and there was no problem porting that to Spin.

    FAIR WARNING: the attached code compiles but I haven't tested it as I don't have the board.
    Jon McPhalen
    Hollywood, CA
    It's Jon or JonnyMac -- please do not call me Jonny.
  • Whoops... while looking through the code I found a missing CR. Fixed in this version -- but I still haven't run the code.
    Jon McPhalen
    Hollywood, CA
    It's Jon or JonnyMac -- please do not call me Jonny.
  • Mike and Jon - thanks for your help! Jon - that jm_mpr121.spin code looks like just what I need. I did look at the Adafruit library some time ago, and I can recognize bits of it even though I am new to Spin. I should have no problem getting it going. BTW - very, very nice code! Easy to follow.
  • JonnyMacJonnyMac Posts: 6,090
    edited June 4 Vote Up0Vote Down
    Thanks for the kind words, and I do hope the code helps.

    Looking through it again I found a copy-and-paste gotcha. The rd_reg8() method should use i2c#NAK instead of i2c#ACK in the read line -- like this:
    pub rd_reg8(reg)
    
    '' Read 8-bit value from specified register
    
      i2c.start
      i2c.write(devid)
      i2c.write(reg)
      i2c.start
      i2c.write(devid | 1)
      result.byte[0] := i2c.read(i2c#NAK)
      i2c.stop
    

    I will order one of those boards so that I can test the code. Once I've done that I will upload the final object/demo to ObEx. In the meantime, do let me know if you have any troubles or suggestions.
    Jon McPhalen
    Hollywood, CA
    It's Jon or JonnyMac -- please do not call me Jonny.
  • Thanks Jon - that's fantastic! I think many people will find the board useful. I use them quite a lot with MIDI instruments, but I have only used them with the Arduino, and not yet with the Propeller. I am working on a MIDI Autoharp that has 12 carbon fibre strings, and a 24 button chord keyboard. My existing Arduino design uses an Uno to connected to the MPR121 to scan the strings and output MIDI, and a separately mounted ATMega328p to scan the keyboard. It is a clumsy design, so I want to move it to the Propeller where I can have separate cores handling the different tasks. One for the strings, one for the keyboard and one for the MIDI output. You never know, I might even add video output to it... There are so many more possibilities with the Propeller.
  • That sounds very cool. I play guitar and recently bought and inexpensive keytar so that I had a standard MIDI output to send into a Propeller board (I use the EFX-TEK HC-8+, but then, I designed it!).

    Since the MPR121 is I2C you could use a couple MCP23017s (also I2C) to monitor your buttons -- assuming, of course, that they are simple closures and not velocity sensitive. Depending on scan rate you might want to switch to a PASM I2C interface. If you decided to do that, let me know. I would have to update (well, create a secondary version of) my MPR121 and MCP23017 objects to use an external I2C object so that you're only consuming one cog and not creating issues with collisions.

    I hope you'll post the project when you get it working.
    Jon McPhalen
    Hollywood, CA
    It's Jon or JonnyMac -- please do not call me Jonny.
  • Keytars are great fun! The missing link between the guitar and the keyboard.

    Yes, the buttons are just simple closures, so I could use MCP23017s. I'll consider it in the future. The thing is I am trying to keep latency down to a minimum so I want one core monitoring the strings and a different one monitoring the keyboard. The interface between the ATMega328p keyboard scanner and the Uno is 5 bit parallel and one interrupt, so that will be fast. All the chord keyboard does is output a binary number that corresponds to a specific chord.

    In fact, I should do some investigation, because I know that latency can be a serious problem but I don't really know the extent of the problem, or where the latency is. It seems to be that if the Uno tries to do much else apart from scan the strings using the MPR121, and send some MIDI, everything gets too unresponsive. Throwing another ATMega328p at the problem is a lazy solution, but it will work.

    Actually, I just got my chord keyboard PCBs this morning, so I'll be able to see how it works with the ATMega328p shortly.

    EFX-TEK HC-8+ - I took a look at your site - you've got some good looking stuff there.
  • JonnyMacJonnyMac Posts: 6,090
    edited June 6 Vote Up0Vote Down
    The nice thing about the Propeller is that you can throw another processor at the Propeller without worry of processor-to-processor coms. Scanning 24 buttons could be handled with 10 pins (4 rows of 6 columns) -- you might even do it in Spin like this:
    var
    
      long  kcog
      long  kstack[32]
    
      long  keys                                                    ' global keys mask
    
    
    pri scan_keyboard | t, tkeys, scan
    
    '' Scan 4x6 matrix keyboard every millisecond
    
      outa[ROW3..ROW0] := %1111                                     ' prep rows for high output
    
      t := cnt
      repeat
        tkeys := -1                                                 ' enable all keys
        repeat 4
          waitcnt(t += constant(MS_001 >> 2))                       ' wait 1/4ms
          dira[ROW3..ROW0] := %0001                                 ' scan row-by-row
          scan := ina[COL5..COL0]        
          dira[ROW3..ROW0] := %0010      
          scan |= ina[COL5..COL0] <<  6  
          dira[ROW3..ROW0] := %0100      
          scan |= ina[COL5..COL0] << 12  
          dira[ROW3..ROW0] := %1000      
          scan |= ina[COL5..COL0] << 18
          dira[ROW3..ROW0] := %0000  
          tkeys &= scan                                             ' debounce
        keys := tkeys                                               ' update global keys
    
    You would launch this method into its own cog with:
      kcog := cognew(scan_keyboard, @kstack) + 1
    
    Since this is in Spin and you have control of the design, it uses contigous groups for the rows and columns -- this helps keep the code efficient. The scan code is unrolled to help it be as fast as possible, too.

    Fair warning: I've only been up for about an hour am just on one cup of coffee!

    Again, this sounds like such a fun project -- I'm looking forward to seeing (and hearing) the end result.
    Jon McPhalen
    Hollywood, CA
    It's Jon or JonnyMac -- please do not call me Jonny.
  • Yes - that code is pretty much what I have on the ATMega328p! In fact, what I originally wanted was a couple of 74922 keypad driver ICs, but these are now obsolete, so I programmed the ATMega chip to kind of emulate them. Shame - they were nice chips. I actually have 3 rows x 8 columns (not that it matters), after the Oscar Schmidt "Americana" Autoharp keyboard (http://www.oscarschmidt.com/products/autoharps/os11021ae.asp). There are 7 chords in each row, so that leaves me with 1 user programmable key per row. I might use 2 of these for octave up and down, and the 3rd to switch from Autoharp mode (chordal) to lyre mode (diatonic).

    With regards to seeing the end result, I have an article in the next edition of Nuts & Volts on a MIDI Lyre, which is the basis of the MIDI Autoharp. I'm planning on 2 versions of the MIDI Autoharp. An Arduino version, that will be an add-on to the MIDI Lyre - this will be published in Nuts & Volts also, and a Propeller version. I can post the Propeller version here. With regards to how it sounds, the main thing design point is that it is a very responsive instrument. So you can stum the strings quite rapidly. This is why I am keeping the string scanning loop as tight as possible.
  • Jim,

    This sounds like a pretty cool project! I'm a MIDI guy from the 80's. Look forward to N&V articles.
    Infernal Machine
  • PublisonPublison Posts: 10,323
    edited June 8 Vote Up0Vote Down
    JonnyMac wrote: »
    That sounds very cool. I play guitar and recently bought and inexpensive keytar so that I had a standard MIDI output to send into a Propeller board (I use the EFX-TEK HC-8+, but then, I designed it!).

    Jon, Did you get one of the new Alexsis Wireless Keyboards?
    http://www.alesis.com/products/view2/vortex-wireless-2
    I put one on my cart to play with. I sold all of my Rolands and Casios a few years back. Still kept my Moog Liberation, but that's not MIDI.


    Infernal Machine
  • JonnyMacJonnyMac Posts: 6,090
    edited June 9 Vote Up0Vote Down
    Jon, Did you get one of the new Alexsis Wireless Keyboards?
    No, no, no. When I said inexpensive, I meant it.
    -- https://www.amazon.com/gp/product/B003RS19XE/ref=oh_aui_detailpage_o04_s00?ie=UTF8&th=1
    This was recommended by my friend Ryan Clarke (for a dirt-cheap MIDI controller). I wanted a standard MIDI out port which is really hard to find on budget instruments.
    Ryan has a neat channel on Twitch that concerns itself with electronics and electronic music.
    --

    Jon McPhalen
    Hollywood, CA
    It's Jon or JonnyMac -- please do not call me Jonny.
  • JonnyMac wrote: »
    Jon, Did you get one of the new Alexsis Wireless Keyboards?
    No, no, no. When I said inexpensive, I meant it.
    -- https://www.amazon.com/gp/product/B003RS19XE/ref=oh_aui_detailpage_o04_s00?ie=UTF8&th=1
    This was recommended by my friend Ryan Clarke (for a dirt-cheap MIDI controller). I wanted a standard MIDI out port which is really hard to find on budget instruments.
    Ryan has a neat channel on Twitch that concerns itself with electronics and electronic music.
    --

    WOW! That is cheap for a MIDI!

    Infernal Machine
  • I really expected it to be crap, but it feels quite substantial. That said, I haven't powered it up (on the road) so I will save final judgement for later.
    Jon McPhalen
    Hollywood, CA
    It's Jon or JonnyMac -- please do not call me Jonny.
  • Erco deal alert!

    I got an ebay clone of a retired Sparkfun MPR121 touchpad - this has the chip conveniently set up on a board with a touch keypad:
    https://www.sparkfun.com/products/retired/10250

    And tried it out with Jon's code. It worked the first time!

    San Mateo, CA
  • Jeff - fantastic! I haven't had a chance to try it out yet myself but look forward to doing so as soon as I have finished the Arduino version.
  • And tried it out with Jon's code. It worked the first time!
    I rule! :lol:
    Jon McPhalen
    Hollywood, CA
    It's Jon or JonnyMac -- please do not call me Jonny.
Sign In or Register to comment.