Testing a transistor - wierd input pin behavior

In my tests to learn all these new things, I have the following sketch running on a Mega 2560 :

// transistor test

const int buttonPin = 22; // the number of the pushbutton pin
const int TransPin = 7; // the number of the Transistor pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int LASTbuttonState = 0;

void setup() {

// start serial port

pinMode(TransPin, OUTPUT);
digitalWrite(TransPin, LOW);
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, LOW); // disable the internal pull-up


void loop(){

// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

if (buttonState != LASTbuttonState){
LASTbuttonState = buttonState;

if (buttonState == HIGH) {
// turn TRANSISTOR on:
digitalWrite(TransPin, HIGH);
if (buttonState == LOW) {
digitalWrite(TransPin, LOW);


I am powering from the USB cable.
No connections made to the board, except a wire ( 3 inches long ) to pin 22. Other end of the wire is not yet connected to anything.

The Serial Monitor is showing a constant non-stop stream of "HIGH" messages, but the wire in pin 22 is not connected to anything.
Reset button on the board does not change anything.
If I remove the wire from the pin 22, the Serial Monitor stops spewing out the "HIGH" messages.
Plug in the wire, and it starts again.
I have tried a number of different pins for the input, all with the same result.

What have I done wrong here ?

an input pin with no resistor will pick up anything from passing taxis to your favourite radio station
even a high value resistor will stop this silly behaviour

any reason why you don't want to use the built-in?

mmcp42 -- you're a freaking genius !!


Been stumped by this for 2 hours. I never thought a small piece of wire could pick up enough current to register as an input. Was starting to think that I had damaged my board.

Didn't go for the internal pull-up as the test I am doing requires a constant LOW, with the action of the code only happening if the input pin receives a HIGH signal.

Am going to be moving the cell phone a bit further away from the bed-side table from tonight !!

Thanks again.

Haha... dude, if you have nothing connected to an I/O pin - not even a pull-up or pull-down resistor - it'll be a "floating" pin, meaning it reads literally completely random values. I mean... you could seriously initialize a random function using a floating pin as input. It's totally random when there's nothing connected. And a pushbutton has two states: open and closed. So if it's open (not pushed), it's connected to nothing... still floating. Only when it's connected (e.g. to ground), it has a definite state - 0, ground. Any other time, it floats.

That's why we use internal pull-ups on buttons and call them "active" when they return 0:
pinMode(22, INPUT); // set as input (default anyway)
digitalWrite(22, HIGH); // set pull-up resistor on port 22


Things are explained here:-

Thanks all.

Will chalk this one up to my first school lesson.

Have been reading sooo much and probably did read about the floating mode of the input pins, but it got lost in all the other stuff I have read.

Just goes to show the value of actual physical wiring to make the written material sink home.

Will be changing things to make the switch value Low instead of High, and enable the internal pull-ups.

Maybe even start my setup code with something like this to set all as inputs and enable all internal pull-ups :

for (int x = 0; x <= 53; x++){
pinMode(x, INPUT);
digitalWrite(x, HIGH);

Note all pins are inputs by default but it dose no harm to do this specifically.