How To Use digitalRead on Serial Pins 0(RX)/1(TX) on Arduino Uno

Hello,
I'm quite a newbie with Arduino boards and I became desperate on an issue:
I'm using an Arduino Uno SMD for a speed measurement with two light barriers.
I have a lot of components (3x4 number pad = 7pins, 2x16 display = 7 pins, light barrier input = 2 pins, microSD card for storing results = 4 pins). So all in all I want to use all the 20 I/O-Pins of the Arduino.

Pin 1 (TX) is used for the Display and works fine.
My problem is Pin 0 (RX) which I use for a column of my number pad. I simply can't read it out with the digitalRead() function because it will always return 0...

I don't use Serial communication and even tried to additionally add Serial.end(); to my initialization. Using the board with an external voltage supply instead of the USB cable didn't change the result, too.
Basically, I my source code is like this:

void setup() {
  //Serial.end();//optional, doesn't help anyway
  pinMode(0, INPUT);//COL4 <-- "problem pin"
  //...
}
void loop() {
  reading = digitalRead(0);
  //reading will be 0
  //it works with any other pin, but I need them all for other purposes
}

I use 0,44V as "low" and 4,8V as "high" input voltage. But connecting the Pin with GND didn't help either.

I used google and the forum search but I only find that advice that "you can't use Pins 0 and 1 as digital Pins if you use Serial communication". I don't use Serial. Promise!
I also read that the FTDI Serial chip would be the problem? Can I disable it, maybe unsolder it? Or is it inside the atmega? Can I still use the USB cable to program the Arduino?
Is there a solution? I could change the firmware if that helps?

I could change the pin mapping so that I use the pin as an output, if that will work? But it would be a less-than-ideal solution because my breakout shield is already produced and populated.

Thank you in advance.

Greetings,
Matthias

Edit:
simplified circuit layout (image & eagle)

simplified.png

simplified.sch (158 KB)

Are you powering the arduino by the usb cable? May want to try another power source.
Have you used up all the analog pins, as they can be used ad digital of course.

You are using an older Uno that has the FTDI chip on it?

Or a more recent board that uses the ATmega8U2 or 16U2 chip? With those, you can tie their reset pin low (it's on a seperate header) to put its pins into Input state and totally free up the D0/D1 line so your offboard device can freely drive the pin.

As long as you don't use the Serial library, pin 0 and 1 as just line other pins.
If the usb chip is not disabled as CrossRoads wrote, it has an output on pin 0 (Arduino RX input).
I prefer to keep pin 0 and 1 for the Serial library for the serial monitor.

You can use the 4 datalines of the display for other things.
Is the keypad library interrupt driven ? If not, you can perhaps use 4 outputs of the Arduino for both the datalines of the display and also 4 output lines for the keypad. I'm not sure of it, I would have to check the libraries.

A better way is to use a I2C lcd display. Once you have a I2C bus, you can also connect other devices to it, like sensors, I/O expanders and so on.

I went and read the Uno page - "The Uno differs from all preceding boards in that it does not use the FTDI USB-to-serial driver chip. Instead, it features the Atmega16U2 (Atmega8U2 up to version R2) programmed as a USB-to-serial converter."
If your external circuit cannot supply 0.5mA to drive the IO pins, than putting the 8U2/16U2 into reset would seem the best option.

Thank you for the very fast reply! :slight_smile:

jack wp: external power unit didn't help, too. Yes, all 14 digital + 6 analog pins are used.

CrossRoads: I don't know which version... (see photos attached) The upper left µC is a Atmega8U2 chip. I can't read the lower right one's name...
Do you mean the Reset Pin of the Arduino (which is next to the 3,3V pin?) or the one of the other µC?
Both Reset ports are represented in one of the two 2x3 ICSP Header. Which one do I have to use?

Erdin:

As long as you don't use the Serial library, pin 0 and 1 as just line other pins.
If the usb chip is not disabled as CrossRoads wrote, it has an output on pin 0 (Arduino RX input).

You mean I could use 0/Rx as an output pin but not as an input pin?

You can use the 4 datalines of the display for other things.

Good idea... hmm I have to rethink that. It wouldn't be a very good solution but it could work...

20130903_223843[1].jpg

20130903_223852[1].jpg

At this point, you have lots of pins used. I think you have two (or more) choices.

  1. add another arduino.
  2. convert to I2C for more I/O pins.

I connected the Reset pin of the Atmega8U2 (upper left µC next to USB-Connector) which functions as the USB-to-Serial Chip with Ground.
The Reset pin is the very upper left pin of the upper left 6-pin-ICSP-Header.
I measured 0,12V at the 0/Rx pin, no matter if I pressed the button or not. :~

I think the Arduino8U2 pulls down the Pins while being reseted. As a result, my 0/Rx pin is connected to low with a 1K-resistor connecting PD3 of Atmega8U2 with Atmega328 PD0 which is my input pin. I will try to replace the upper 4x1K resistor with 3 1K resistors, skipping the connection that annoys me.
Because I think I still need the Rx pin for programming, I will replace my 1K resistor with a 1K resistor in series with a jumper or switch, connecting them with a tiny cable to its initial position...
You think that will work? I'll keep you up to date...
(Buying another arduino or I2C I/O Pin breakout board would last too long, I need a quick fix. But it would be advisable for someone who doesn't want to fiddle with SMD parts)
Another option would be to have a look at the firmware of the Atmega8U2 in order to let its PD3 pin run in high impedance mode while I don't need it. For example, I could set an interrupt on one of the pins that are used for programming...
Any other ideas?

No other ideas, I think you about covered it all. Good luck.

Your board: Arduino Uno Rev3 SMD — Arduino Official Store

I look at the schematic, and there are the 1k resistors you mentioned.
So if you use both as output, you have no problem (it could cost an extra 5mA on the pin).
Pin 13 drives the led, use also Pin 13 as an output.
Everything else can be input without any attachments.

Pulling the reset of the usb chip (ATmega8U2) to ground, must set the pins floating.
That is a feature of all avr chips.
Could you try that once more ? it is a nice and quick fix.

You can make a jumper in the RX line, but it is a dirty fix. You have to set the jumper for uploading, and remove the jumper to make the sketch work.

Changing the firmware might get you in a swamp of troubles.

Erdin:
You can use the 4 datalines of the display for other things.
Is the keypad library interrupt driven ? If not, you can perhaps use 4 outputs of the Arduino for both the datalines of the display and also 4 output lines for the keypad. I'm not sure of it, I would have to check the libraries.

You are correct. If matzeatweb is using the Keypad library Arduino Playground - HomePage and the LiquidCrystal library [Sketch -> Import Library... -> LiquidCrystal] then yes, they are written so that the 4 data pins from the LCD and the Row pins of the keypad can be connected together.

I measured 0,12V at the 0/Rx pin

Where is 12V coming from? That's gotta be your external device. 12V will fry the pin.

0.12V (I think).

Hello,
0.12V, sorry...

Erdin:
I look at the schematic, and there are the 1k resistors you mentioned.
So if you use both as output, you have no problem (it could cost an extra 5mA on the pin).
Pin 13 drives the led, use also Pin 13 as an output.
Everything else can be input without any attachments.

Yes and that is where I remained. The jumper solution was too fiddly so I decided to change the layout of my board I plugged onto the Arduino board. Now I use 0/Rx and 1/Tx Pins as Data Input for the Display, i.e. I use the Arduino Pins 0 and 1 as OUTPUT.
Problem solved! This works brilliantly, thank you all for your help :slight_smile:

For those who came here because of the same problem: The easiest solution is to simply use Rx/Tx as an output which is sufficient for me. I didn't have to set the Reset-Pin of the upper left Atmega8U2 (USB-to-serial chip) to low.
If you pay attention to the 1K resistors and/or you have big enough input currents, maybe you can use the as inputs. But I had a 100k/10k voltage divider which was influenced by the 1K resistor connecting Rx or Tx with the PD2 or PD3 of the Atmega8U2 - making it useless for my purpose.

Very nice. Thanks for letting us know.