Why is Arduino saying pin 8 is HIGH when I simply put a cable in it?

I have an Arduino Mega with a 433MHz RF Module Receiver and a Uno with the Transmitter. it is setup so that when the Mega receives the message "Hello World", it triggers the transistor to light an LED. meanwhile, I am trying to get the Uno to transmit the message when I push a button connected to pin 8, however, there are two problems.

  1. the Arduino reads putting a cable (with no voltage running through it) in the pin socket as "HIGH" and will then send the message

  2. the Arduino UNO will send two of the messages (one once then the second four seconds later) each time I put the cable in the pin socket

If anyone could help me it would be appreciated - here is the code:


Transmitter (Arduino UNO):

`

#include <RH_ASK.h> // Include RadioHead Amplitude Shift Keying Library

#include <SPI.h> // Include dependant SPI Library

// Create Amplitude Shift Keying Object

RH_ASK rf_driver;

void setup()

{

// Initialize ASK Object

rf_driver.init();

// Setup Serial Monitor

Serial.begin(9600);

}

void loop()

{

if (digitalRead(8)==HIGH){

const char *msg = "Hello World";

rf_driver.send((uint8_t *)msg, strlen(msg));

rf_driver.waitPacketSent();

// Message Transmitted

Serial.println("Message Transmitted: Hello World");

digitalWrite(13,HIGH);

delay(4000);

}

}
` ` `
` ` `
Receiver (Arduino Mega):


#include <RH_ASK.h> // Include RadioHead Amplitude Shift Keying Library

#include <SPI.h> // Include dependant SPI Library

// Create Amplitude Shift Keying Object

RH_ASK rf_driver;

void setup()

{

// Initialize ASK Object

rf_driver.init();

// Setup Serial Monitor

Serial.begin(9600);

}

void loop()

{

// Set buffer to size of expected message

uint8_t buf[11];

uint8_t buflen = sizeof(buf);

// Check if received packet is correct size

if (rf_driver.recv(buf, &buflen))

{

// Message received with valid checked-sum

Serial.print("Message Received: ");

Serial.println((char*)buf);

digitalWrite(6,HIGH);

delay(500);

digitalWrite(6,LOW);

}

}


` ` `

Please edit your post to add code tags ("</>" editor button).

An unconnected port pin "floats" and can be read as HIGH or LOW, intermittently.

Describe your wiring: best to post a hand drawn diagram.

1 Like

code tags?

A wire just connected to the pin may act like an antenna.

Also... you need to use pinMode() before using the I/O pins.

So you need a resistor (either external or INTERNAL_PULLUP) to your digital input to define it HIGH or LOW. Than, by touching 5V or ground you can make it HIGH or LOW on your command.
Of course you can wire a button to make this more neat.

Sorry about the code, I don't know how to fix it.

Edit your post. look at the top of the box and you will see this:

image

Select your code and click on the </> sign

In addition to the solution, you also need to set the pinMode of pin 13. On startup, all pins default to INPUT. You need to set pin 13 to OUTPUT in setup().

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.