12 Key Capacitive Touch board interfering with servo

Breakout boards, sensors, other Adafruit kits, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
tdicola
 
Posts: 1074
Joined: Thu Oct 17, 2013 9:11 pm

Re: 12 Key Capacitive Touch board interfering with servo

Post by tdicola »

Hrm, I just tried both an Uno and Leonardo (same processor as the Micro) and couldn't reproduce any jittering or issues with the MPR121. My setup was a servo on pin 9 and the MPR121 on the I2C bus pins. I put together both the servo sweep example and MPR121 test example to make a program that sweeps the servo and reads inputs. You can see the code below:

Code: Select all

#include <Servo.h> 
#include <Wire.h>
#include "Adafruit_MPR121.h"

// You can have up to 4 on one i2c bus but one is enough for testing!
Adafruit_MPR121 cap = Adafruit_MPR121();

// Keeps track of the last pins touched
// so we know when buttons are 'released'
uint16_t lasttouched = 0;
uint16_t currtouched = 0;

Servo myservo;  // create servo object to control a servo 
                // twelve servo objects can be created on most boards
 
int pos = 0;    // variable to store the servo position 

void setup() {
  Serial.begin(9600);
  Serial.println("Adafruit MPR121 Capacitive Touch sensor test"); 
  
  myservo.attach(9);
  
  // Default address is 0x5A, if tied to 3.3V its 0x5B
  // If tied to SDA its 0x5C and if SCL then 0x5D
  if (!cap.begin(0x5A)) {
    Serial.println("MPR121 not found, check wiring?");
    while (1);
  }
  Serial.println("MPR121 found!");
}

void checkTouches() {
  // Get the currently touched pads
  currtouched = cap.touched();
  
  for (uint8_t i=0; i<12; i++) {
    // it if *is* touched and *wasnt* touched before, alert!
    if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
      Serial.print(i); Serial.println(" touched");
    }
    // if it *was* touched and now *isnt*, alert!
    if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
      Serial.print(i); Serial.println(" released");
    }
  }

  // reset our state
  lasttouched = currtouched;
}

void loop() {
  
  for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
    checkTouches();
  } 
  for(pos = 180; pos>=0; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
    checkTouches();
  } 
}
It's odd if you're seeing jittering, since like Bill mentioned the MPR library just uses I2C to communicate with the device and shouldn't the timers at all. I wonder if something else like perhaps another library is being included and accidentally messing with the servo PWM timers. If you take my sketch above and connect a servo signal pin to pin 9 on the board and the MPR121 to the I2C pins do you see the same jittering issues?

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: 12 Key Capacitive Touch board interfering with servo

Post by adafruit_support_bill »

The other possibility is that the servos are picking up noise from the cap-touch board. Make sure that your servo wiring is routed well away from the cap touch.

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Products from Adafruit”