Hardware serial code not working with softwareSerial arduino mega

This code sends allows me to send AT commands through the serial monitor to a wifi board and recieve an output. The code which uses Serial1 works, but I can't get it to work using software serial. The baud rate is correct.

Hardware Serial code:

void setup() {
  // Initialize the Serial communication
  Serial.begin(600);
  // Initialize the Serial1 communication
  Serial1.begin(600);
  // Wait for the sensor to reboot
  delay(1000);
}

void loop() {
  if (Serial.available()) {    // If anything comes in from the computer
    Serial1.write(Serial.read());  // send it to the sensor
  }

  if (Serial1.available()) {    // If anything comes in from the sensor
    Serial.write(Serial1.read());  // send it to the computer
  }
}

software serial code:

#include <SoftwareSerial.h>

// Create a software serial object with pin 6 as RX and pin 7 as TX
SoftwareSerial sensorSerial(6, 7);

void setup() {
  // Initialize the Serial communication with the computer
  Serial.begin(600);
  // Initialize the software serial communication with the sensor
  sensorSerial.begin(600);
  // Wait for the sensor to reboot
  delay(1000);
}

void loop() {
  if (Serial.available()) {    // If anything comes in from the computer
    sensorSerial.write(Serial.read());  // send it to the sensor
  }

  if (sensorSerial.available()) {    // If anything comes in from the sensor
    Serial.write(sensorSerial.read());  // send it to the computer
  }
}

Any help would be much appreciated :slight_smile:

Why 600 instead of 9600? Does it need to be that slow?

I used a lower baud rate for softwareserial just incase

The code looks ok, are the Tx/Rx backwards?

Nope, I'ts all good there

Ok then next question would be do you have a way to test the wifi board separately to make sure it works?

Yes I send AT commands through hardware serial. Gimme one second, I tried it with the hardware serial code just now and am not getting a response back.

Important but not obvious for beginners.

For the OP. How are things powered? Way too often lack of power makes trouble. It spells schematics.. Pen and paper usually works well enough.

It is powered via usb from a laptop to an arduino mega and then 5v-> Vin on the wifi board

Its the pins on the Arduino mega you are using.

See the Limitations section here.

1 Like

the reason it didn't work for a second is because I had only changed the Current baud rate to 600 and might have accidentally reset it. Now have changed AT+UART_DEF:600,8,0,0 which flashes the baud rate to the wifi board

Thank you, I didn't see that part :slight_smile: works great

1 Like

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