How do I use INPUT_PULLUP on pin 13 (LED)?

Status
Not open for further replies.

heyjudemw

Member
Hi folks, do I need to treat pin 13 differently because it has an LED? Can I use the same INPUT_PULLUP? I wrote a little code to test some buttons I connected to the Teensy 3.2 just to verify that everything is connected correctly and 13 is behaving strangely. The serial monitor is often showing it pressed when it isnt. I did about 30 min of web searching to find an answer and I read you have to knock the LED off with a soldering iron. Is that really true? Thanks for your help.
Code:
void setup() {                
  Serial.begin(38400);
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
  pinMode(13, INPUT_PULLUP);
  pinMode(14, INPUT_PULLUP);
  pinMode(15, INPUT_PULLUP);
  pinMode(16, INPUT_PULLUP);
  pinMode(17, INPUT_PULLUP);
  pinMode(18, INPUT_PULLUP);
  pinMode(19, INPUT_PULLUP);
  pinMode(20, INPUT_PULLUP);
  pinMode(21, INPUT_PULLUP);
  pinMode(22, INPUT_PULLUP);
  pinMode(23, INPUT_PULLUP); 
}

void loop()                     
{
  if (digitalRead(0) == LOW) {
    Serial.println("0");}
  if (digitalRead(1) == LOW) {
    Serial.println("1");}
  if (digitalRead(2) == LOW) {
    Serial.println("2");}
  if (digitalRead(3) == LOW) {
    Serial.println("3");}
  if (digitalRead(4) == LOW) {
    Serial.println("4");}
  if (digitalRead(5) == LOW) {
    Serial.println("5");}
  if (digitalRead(6) == LOW) {
    Serial.println("6");}
  if (digitalRead(7) == LOW) {
    Serial.println("7");}
  if (digitalRead(8) == LOW) {
    Serial.println("8");}
  if (digitalRead(9) == LOW) {
    Serial.println("9");}
  if (digitalRead(10) == LOW) {
    Serial.println("10");}
  if (digitalRead(11) == LOW) {
    Serial.println("11");}
  if (digitalRead(12) == LOW) {
    Serial.println("12");}
  if (digitalRead(13) == LOW) {
    Serial.println("13");}
  if (digitalRead(14) == LOW) {
    Serial.println("14");}
  if (digitalRead(15) == LOW) {
    Serial.println("15");}
  if (digitalRead(16) == LOW) {
    Serial.println("16");}
  if (digitalRead(17) == LOW) {
    Serial.println("17");}
  if (digitalRead(18) == LOW) {
    Serial.println("18");}
  if (digitalRead(19) == LOW) {
    Serial.println("19");}
  if (digitalRead(20) == LOW) {
    Serial.println("20");}
  if (digitalRead(21) == LOW) {
    Serial.println("21");}
  if (digitalRead(22) == LOW) {
    Serial.println("22");}
  if (digitalRead(23) == LOW) {
    Serial.println("23");}
  delay(250);
}
 
The LED acts as an external pull-down, which is much stronger than any internal pull-up, so it electrically overrides that. So yes, it is rather not possible to use pin13 in input pull-up mode without removing the LED.

A workaround could be either not using pin13 in input pull-up mode (hasn’t the Teensy enough other pins you can use?) or using an external buffer like the 74lvc125 in conjunction with an external pull-up resistor at its input.
 
Hi Theremingenieur, thanks for your answer. Your responses are always appreciated. Im using all of the digital and analog inputs that are accessible through holes on the Teensy(as opposed to the ones that solder to the back of Teensy) except for DAC/A14. I can move the analog input from 23(A9) to DAC/A14 and move 13(LED) to 23. That'll work fine as long as DAC doesn't interfere with A14 in some way that I didn't anticipate like the LED interfering with 13. I'm going to look up info on DAC/A14 pin now and see what goes on there. Thanks!
 
The DAC is disabled on boot up, so it won't interfere by default and pin A14 will behave like any other analog input pin. Just take care to not accidentally activate the DAC, i.e. by having a analogWrite(A14) in your code.

BTW: Pin A14 is a pure analog pin without any GPIO functionality and without 5V tolerance. Configuring it as input_pullup and doing digitalRead() won't work, so wiring an external pull-up resistor and then reading the 10bit analog value with analogRead() and an additional line of code to convert this into a simple hi/lo will be required.

A simpler way might be maintaining pin13 and adding an external 470R resistor from that pin to +3.3V in order to reinforce the pull-up effect.

How many digital input pins can one need? I'm curious to learn more about your project...
 
Or an alternative solution is to solder a wire to one of the pads underneath the Teensy 3.2.

Another alternative solution is to move up to a Teensy 3.5 or 3.6 that gives you more pins brought out on the outer row.

Alternatively consider moving some of the digital and analog pins to separate i2c/spi boards.
  • There are i2c boards that provide either digital input or output such as the MCP23017 that gives you 16 digital inputs or outputs.
  • There are separate boards like the ADS1015 that gives you 4 additional analog inputs.
  • You would typically want to use i2c for things like buttons and leds, where you don't need the fastest speed.
  • For things like PWM pins, there are other i2c boards, that provides PWM support for running servos, etc. (i.e. you tell the board how fast to cycle the pin, rather than having the Teensy do it).
  • To use i2c you would need to connect the 2 i2c pins (usually pins 18/A4 and 19/A5), plus a wire connecting the 3.3v power and ground pins to the i2c chips.
  • I2c can be chained together, as long as each i2c device has a separate address.
  • You might need pull-up resistors between each of the two i2c pins and 3.3v (usually 2.2k resistors are recomended).

If you have a bare Teensy (or one with the pins soldered on, but you take off the plastic insulation), and you want to use the underneath pads, you can get various PCBs either pre-made or made to order by a PCB house like oshpark.com that give you access to the bottom pins when you solder it to the Teensy. Here are a few that I know about:
 
Thanks for the advice, guys. I moved a couple of pins around and its working great. Thank you for the recommendations for options that can give me more inputs. I had just enough inputs to get it working but in the future I'll try one of these ideas.

My project is a tiny MIDI controller thats about wallet sized. There's just enough room in there for the 23 buttons, the 3 little pots, the wire and the teensy 3.2 and thats about it. It's working great. Thanks, guys.
 
Status
Not open for further replies.
Back
Top