1

I have Arduino Uno (sender) and Arduino Uno Wifi rev 2 (receiver). I want to send the ping value to the other one via serial - jumper wires. Arduino Uno is using the newping example, and I want the Uno Wifi to receive the sensor value and print it to serial. I am using SoftwareSerial.h jumper wires go TX<->RX , RX <->TX, GRD-GRD according to the pins in the sketch below.

The problem is the sketch code for the Uno WiFi (receiver), it keeps printing garbage and seems like no carriage return sometimes.

13
⸮10
⸮52
⸮5LC⸮1⸮H⸮10
5⸮H⸮51
13C⸮10
⸮5⸮H⸮5LC⸮13
10
⸮52
51C⸮13
10
⸮52
5LC⸮13
⸮10C⸮52
⸮51
13
10
⸮52
50

Code on Uno (sender):

// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------
#include <SoftwareSerial.h>
#include <NewPing.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
SoftwareSerial mySerial(9, 10); // RX, TX

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

 Serial.println("Hello World Serial");
 mySerial.begin(4800);
 mySerial.println("Hello World, mySerial");
}
void loop() {
  delay(500);                     // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");
  mySerial.print(sonar.ping_cm());
  mySerial.println();
  }

code on uno wifi (reciever):

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  // set the data rate for the SoftwareSerial port
  mySerial.begin(4800);
  
}

void loop() { // run over and over
 
    Serial.println(mySerial.read());
    delay(1000);
  
}

I can see the Uno (sender) with NewPing is printing the correct data to it's serial but I can't get the Uno wifi (listener) to get the same value. Its just garbage. I am new to this, what am I doing wrong?

3
  • reduce the sender code ... only send fixed values ... remove all code that is not associated with sending mySerial data ... no Serial, no sonar
    – jsotola
    Mar 20, 2021 at 23:07
  • Try replacing the receiving Serial line with if(mySerial.available()) Serial.write(mySerial.read());
    – chrisl
    Mar 21, 2021 at 0:12
  • Well I have gotten a bit farther. I used Serial.write and on the recieving Arduino I get values just not the correct one. The serial on the sender is sending value- 32, on the recieving arduino with - SonarReading=(mySerial.read()); I get numbers like 240, 241, ...
    – foxtrot22
    Mar 22, 2021 at 10:55

1 Answer 1

1

Well had a bit of luck after spending all day on this.

Arduino UNO (sender): Like mentioned by chrisl, use Serial.write and not Serial.print

mySerial.write(sonar.ping_cm());

UNO Wifi (receiver):

Serial.println(mySerial.readString());

So, instead of mySerial.read I used mySerial.readString. This is most likely a obvious mistake to almost everybody here; took me two days. Sigh.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.