RP2040 connect TX,RX not functioning

Im tryin to connect two rp2040s thrugh rx and tx. Im using IDE 2.0.4 and im attempting to light up an LED on and off just to check for a connection.
my transmitter code is:

void setup() 
{
    Serial1.begin(9600);
    delay(100);
    
}//setup

void loop() 
{
    Serial1.print("1");
    delay(1000);
    Serial1.print("2");
    delay(1000);
    
}

//#define PIN_SERIAL_TX (0ul)
//#define PIN_SERIAL_RX (1ul)
//...
//#define SERIAL_HOWMANY 1
int ledPin = 13; // the pin that the LED is attached to
int incomingByte; // a variable to read incoming serial data into
int i = 0;
void setup() {
// initialize serial communication:
//Serial.begin(9600);

Serial1.begin(9600);
//Serial.setTimeout(50);
delay(100);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
}

void loop() {

// see if there's incoming serial data:
while(Serial1.available()){

delay(2);
incomingByte = Serial1.readString();
// if it's a capital H (ASCII 72), turn on the LED:
if (incomingByte == 2) {
  digitalWrite(ledPin, HIGH);
}
// if it's an L (ASCII 76) turn off the LED:
if (incomingByte == 1) {
  digitalWrite(ledPin, LOW);
}

}
}

Your comments do not fit to the values

You are sending the bytes as ASCII-code

but check for integers

if (incomingByte == 2) 

a ASCII-coded 1 has decimal value 49
a ASCII-coded 2 has decimal value 50

so you have to change your code either to

if (incomingByte == '2') 

or substract 48 from the value

incomingByte = Serial1.readString();
incomingByte = incomingByte - 48;

best regards Stefan

Ill try rn, Thanks a lot

Update. Still not working

Please insert your revised codes using the code tags

I guess you have tried everything possible, just solve the RxD and TxD problem for my self and one thing I can tell you is, unplug and plugin the board again, expecially if you are "crussing" around different apps, etc.

In general, make sure that the connections are right, that means, e.g. when reading the serial via the rxd, txd on tiny, then the ftdi connection is the same (rxd->rxd, etc.). If you are using the "external" not arduino serial port reader app then make sure your bauds, etc. are properly set, in most cases, the other settings are ok! In addition, if you are using the ftdi, etc. make sure that your driver is ok...

and do not bother with code too much, it should works straight via the Serial.begin(); and Serial.println() within the main loop, for example.

Good luck and take a break :slight_smile:

Transmiter:

void setup() 
{
    Serial1.begin(9600);
    delay(100);
    
}//setup

void loop() 
{
    Serial1.print("1");
    delay(1000);
    Serial1.print("2");
    delay(1000);
    
}//loop

Reciever:

int ledPin = 13;  // the pin that the LED is attached to
char incomingByte;       // a variable to read incoming serial data into
int i = 0;
void setup() {
  // initialize serial communication:
  //Serial.begin(9600);


  Serial1.begin(9600);
  //Serial.setTimeout(50);  
  delay(100);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  delay(1000);
  digitalWrite(ledPin, LOW);
}

void loop() {
  
  // see if there's incoming serial data:
  while(Serial1.available()){
    
    delay(2);
    incomingByte = Serial1.read();
    // if it's a capital H (ASCII 72), turn on the LED:
    if (incomingByte == '2') {
      digitalWrite(ledPin, HIGH);
    }
    // if it's an L (ASCII 76) turn off the LED:
    if (incomingByte == '1') {
      digitalWrite(ledPin, LOW);
    }
}  
}

Thanks a lot to everyone for helping out

What is your connections?

You should add serial debug output to the serial monitor
THis will make visible what your code is doing (or not doing)
and this will guide you to the bug

similar to this

void setup() {
    Serial.begin(115200);
    Serial.println("Setup-Start");
    Serial1.begin(9600);
    delay(100);
    
}//setup

void loop() {
    Serial1.print("1");
    Serial.println("Serial1 send 1");
    delay(3000);
    Serial1.print("2");
    Serial.println("Serial1 send 2");
    delay(3000);
    
}//loop

receiver

int ledPin = 13;  // the pin that the LED is attached to
char incomingByte;       // a variable to read incoming serial data into
int i = 0;
void setup() {
  // initialize serial communication:
  //Serial.begin(9600);

  Serial.begin(115200);
  Serial.println("Setup-Start");

  Serial1.begin(9600);
  //Serial.setTimeout(50);  
  delay(100);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  delay(1000);
  digitalWrite(ledPin, LOW);
}

void loop() {
  
  // see if there's incoming serial data:
  while(Serial1.available()){
    
    delay(2);
    incomingByte = Serial1.read();
    Serial.print("incomingByte=");
    Serial.println(incomingByte);

    // if it's a capital H (ASCII 72), turn on the LED:
    if (incomingByte == '2') {
      Serial.print("received a 2");
      digitalWrite(ledPin, HIGH);
    }
    // if it's an L (ASCII 76) turn off the LED:
    if (incomingByte == '1') {
      Serial.print("received a 1");
      digitalWrite(ledPin, LOW);
    }
}  
}

best regards Stefan

Sincerely grateful for the help!

I get the message "17:01:07.735 -> incomingByte=" and a little empty square next to the =. The transmitter is writing 0 and 1 and I see the Serial.print("incomingByte="); which i gess means that Serial 1 is available?

the function-call Serial1.available() results in "true" if there is minimum one byte in the receive-buffer

are you sure that the transmitter is writing 0 and 1 ?

The code I posted sends a "1" and and a "2"
as ASCII-Code -string

best regards Stefan

How can I check for that other than watching the serial monitor? it seems to write Serial1.print("1"); Serial.println("Serial1 send 1") perfectly and Ive checked wiring, Im completely lost

posting the exact sender and the exact receiver-code
posting what you get in the serial monitor as a code-section too

so that others can analyse what you received in the serial monitor

using looping back from

serial Tx to serial1 Rx
serial Rx to serial1 Tx
of one and the same microcontroller
with using the same baudrate on both serial interfaces

using the command serial.write() to send really a single byte instead
of a string

printing the value of the received byte as

  • binary
  • decimal

using a
USB-to-COM-Port-adapter and receiving the sended bytes with a terminal software and printing the received bytes as decimal or hexadecimal values

connecting 8 LEDs to the receivers IO-pins and switch on / off the 8 IO-pins according to the bits of the received byte

best regards Stefan

Ill provide all the information tomorrow, your help means a lot!

moth later. Sorry for not writting back, had many exams to deal with(im in uni). Bought a new RP2040, copied your code and its all working!!! Thousand thanks

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