[SOLVED]4051 multiplexer not working correctly with teensy 3.2 but works with arduino

Status
Not open for further replies.

mmark

New member
Hi all!
I hooked up a 74HCT4051 to my arduino and teensy. My test setup (picture below) works perfectly with my arduino uno.
When I connect it te same way (same pins) to my teensy, it get 'floaters'. There should appear one 0 on my serial terminal but 3 appear.
I followed roughly this page: https://www.pjrc.com/teensy/td_midi.html
I've tried multple 74HCT4051's - all different floaters. :confused:

Capture.JPG
DSC_0001.jpg


The code:
Code:
const int selectPins[3] = {10, 11, 12}; // S0~2, S1~3, S2~4
const int zInput = 9; // Connect common (Z) to A0 (analog input)

void setup() 
{
  Serial.begin(9600); // Initialize the serial port
  // Set up the select pins as outputs:
  for (int i=0; i<3; i++)
  {
    pinMode(selectPins[i], OUTPUT);
    digitalWrite(selectPins[i], HIGH);
  }
  pinMode(zInput, INPUT_PULLUP); // Set up Z as an input

  // Print the header:
  Serial.println("Y0\tY1\tY2\tY3\tY4\tY5\tY6\tY7");
  Serial.println("---\t---\t---\t---\t---\t---\t---\t---");
}

void loop() 
{
  // Loop through all eight pins.
  for (byte pin=0; pin<=7; pin++)
  {
    selectMuxPin(pin); // Select one at a time
    int inputValue = digitalRead(zInput); // and read Z
    Serial.print(String(inputValue) + "\t");
    delay(100);
  }
  Serial.println();
  delay(500);
}

// The selectMuxPin function sets the S0, S1, and S2 pins
// accordingly, given a pin from 0-7.
void selectMuxPin(byte pin)
{
  for (int i=0; i<3; i++)
  {
    if (pin & (1<<i))
      digitalWrite(selectPins[i], HIGH);
    else
      digitalWrite(selectPins[i], LOW);
  }
}
 
Thanks for your reply. I get the same problem when I connect the VCC to the USB 5v pin on the teensy.
Also, when I connect the 74HCT4051 to the arduino; either 3.3 and 5v works fine.
 
Last edited:
Maybe the Teensy is too fast? Please try to add a "delay(1);" :
Code:
void selectMuxPin(byte pin)
{
  for (int i=0; i<3; i++)
  {
    if (pin & (1<<i))
      digitalWrite(selectPins[i], HIGH);
    else
      digitalWrite(selectPins[i], LOW);
  }
  [COLOR=#ff0000][B]delay(1);[/B][/COLOR]
}

Perhaps a shorter delay "delayMicroseconds(x)" would work, too.
 
Frank is right, you need at least a couple microseconds delay between changing the mux and reading the pin. The pin is using INPUT_PULLUP mode and the pullup resistor is weak. If the pin is floating low, the pullup resistor takes a very brief time to bring the voltage high again.

You should really do the same on Arduino. But regular 8 bit Arduino runs much slower than Teensy 3.2. Arduino's digitalRead() code also has a lot of extra overhead, which gives a brief delay even if you have no explicit delay in your code.

Teensy 3.2 is so much faster than 8 bit Arduino that test too-fast issues sometimes come up with code that seemed to always work perfectly on slow boards.
 
SOLVED: So easy, but i never could come up with it myself. Thanks guys!
A delay of 1 was still to short; 5 works perfectly!
 
Status
Not open for further replies.
Back
Top