1

I'm trying to use the Capacitive Sensing Library by Paul Badger in conjunction with one or more 74HC4067 multiplexers to achieve a lot of capacitive touch inputs. I came across this youtube video where the poster seems to have accomplished what I'm trying but I can't seem to replicate this.

Here is the setup I'm using with an arduino nano, some 1MΩ resistors and a CD74HC4067 breakout :

enter image description here

Here is my Arduino code :

#include <CapacitiveSensor.h>

#define muxS0Pin 7
#define muxS1Pin 6
#define muxS2Pin 5
#define muxS3Pin 4
#define muxSignalPin 3
#define muxEnPin 8
#define capSenseSendPin 2
#define sampleRate 5

int touch[4] = {};

CapacitiveSensor capSense = CapacitiveSensor( capSenseSendPin, muxSignalPin );

void setup() {
    digitalWrite( muxEnPin, LOW );
    Serial.begin( 115200 );
}

void loop(){
    byte touchIndex = 0;
    for ( byte channel = 0; channel < 4; ++channel ) {
        digitalWrite( muxS0Pin, ( channel & 1 ) >> 0 );
        digitalWrite( muxS1Pin, ( channel & 2 ) >> 1 );
        digitalWrite( muxS2Pin, ( channel & 4 ) >> 2 );
        digitalWrite( muxS3Pin, ( channel & 8 ) >> 3 );
        touch[touchIndex] = capSense.capacitiveSensor( sampleRate );
        ++touchIndex;
    }
    for ( byte i = 0; i < 10; ++i ) {
        Serial.print( touch[i] );
        Serial.print("\t");
    }
    Serial.println();
}

This example attempts only four capacitive touch pads but eventually I plan to use all 16 and then another multiplexer, but right now I'm stuck on getting anything, I seem to be getting some readings on touching the last pad but it's not what I expect to see, and it affects all the reading.

I've successfully got a version of this running without a multiplexer just on ten IO pins and it worked great. I also got the multiplexer running fine in a separate example with 16 push buttons.

Could the problem be due to pad charge times or the send-recieve capsense signals getting stopped in the multiplexer?

Any and all help would be appreciated.

  • Here is the link to the library : playground.arduino.cc/Main/CapacitiveSensor – juliusbangert Jul 21 '17 at 16:14
  • And here's a video of my example not working : youtube.com/watch?v=4bGJw1UrgOk. The setup in the video is slightly different than the illustration above; I have pads connected to C6 to C15 on the multiplexer and I loop through them backwards to display them in order on the serial monitor. But the point is, it seems to be only the last multiplexer pin that affects all the readings. – juliusbangert Jul 21 '17 at 17:15
  • Might need to set the mux enable pin (muxEnPin) mode to output in setup(). – Mikael Patel Jul 21 '17 at 20:30
1

This is way late, but for anyone who runs across this, here was my solution. The two main thing is explicitly using analog input pins (which could be quantized to digital values in the software, and making sure you have resitors on each channel being sensed. Also, ground the EN pin on the multiplexer

#include <CapacitiveSensor.h>

#define muxS0Pin 9  // I used these rather than 4-7
#define muxS1Pin 10
#define muxS2Pin 11
#define muxS3Pin 12

#define muxSignalPin A0      // explicitly set ro analog
#define capSenseSendPin A2   // 


#define sampleRate 10
#define n_inputs 16

int touch[n_inputs] = {};

CapacitiveSensor capSense = CapacitiveSensor( capSenseSendPin, muxSignalPin );


int readMux(int channel) {
  int controlPin[] = {muxS0Pin, muxS1Pin, muxS2Pin, muxS3Pin};

  int muxChannel[16][4] = {
    {0, 0, 0, 0}, //channel 0
    {1, 0, 0, 0}, //channel 1
    {0, 1, 0, 0}, //channel 2
    {1, 1, 0, 0}, //channel 3
    {0, 0, 1, 0}, //channel 4
    {1, 0, 1, 0}, //channel 5
    {0, 1, 1, 0}, //channel 6
    {1, 1, 1, 0}, //channel 7
    {0, 0, 0, 1}, //channel 8
    {1, 0, 0, 1}, //channel 9
    {0, 1, 0, 1}, //channel 10
    {1, 1, 0, 1}, //channel 11
    {0, 0, 1, 1}, //channel 12
    {1, 0, 1, 1}, //channel 13
    {0, 1, 1, 1}, //channel 14
    {1, 1, 1, 1} //channel 15
  };

  //loop through the 4 contol pins
  for (int k = 0; k < 4; k ++) {
    digitalWrite(controlPin[k], muxChannel[channel][k]);
  }
  //read the value at the SIG pin
  return capSense.capacitiveSensor( sampleRate );
}

void setup() {
  // Very important to set control pins to output mode
  pinMode(muxS0Pin, OUTPUT);
  pinMode(muxS1Pin, OUTPUT);
  pinMode(muxS2Pin, OUTPUT);
  pinMode(muxS3Pin, OUTPUT);

  digitalWrite(muxS0Pin, LOW);
  digitalWrite(muxS1Pin, LOW);
  digitalWrite(muxS2Pin, LOW);
  digitalWrite(muxS3Pin, LOW);
  Serial.begin( 9600 );  
}

void loop() {
  byte touchIndex = 0;
  for ( byte channel = 0; channel < n_inputs; ++channel ) {
    Serial.print( readMux(channel));
    Serial.print("\t");
  }
  Serial.println();
}


| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.