Serial communications with XY-LPWM using Serial1

This is my first time posting on here, so if I screwed up anything in this post let me know and I will try to fix it.

I bought an XY-LPWM PWM generator to use for work. It can generate PWM signals as high as 30V. The frequency and duty cycle of the generator can be set using serial control. I am trying to get it to work on Serial1 of my Mega 2560, but I cannot. If I connect the RX & TX to Pins 0 and 1 I can open the serial monitor and send commands to it which set it properly

String setPWM;

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(Serial.available() >0) {
    setPWM = Serial.readString();
    Serial.println(setPWM);
    Serial1.println(setPWM);
    
  }
}

but if I try using it on Serial1

String setPWM;

void setup() {
  Serial1.begin(9600);
}

void loop() {
  setPWM = "F105";  
  Serial1.println(setPWM);    
  delay(5000);
}

it does not change the settings of the generator. I have tried it with the generator RX connected to the Mega RX and the generator TX connected to the Mega TX and also with RX-TX and TX-RX, but neither way worked. Any thoughts on this issue would be appreciated.

Thanks

ground to ground!

Mark

You have not explained how you have connected everything in the two different scenarios. Please make some simple pencil drawings of the connections in the two cases and post photos of the drawings. See this Simple Image Guide

And you have not told us exactly what the Arduino receives in the first case.

...R

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html

Have you got the Tx of the generator connected to Rx of the Mega, and Rx of the generator connected to Tx of the Mega?

Have you got gnd of the generator and Mega connected together?

Tom.. :slight_smile:

Initially I had the generator connected to Serial. Then I ran this code. With this code running I then opened the Serial Monitor. I could then type a command ("F101") and the PWM frequency was set to 100. So, I know that the PWM Generator communication is working.

String setPWM;

void setup() {
  Serial.begin(9600);
}

void loop() {
  if(Serial.available() >0 {
    setPWM = Serial.readString();
    Serial.println(setPWM);
  }
}

As I stated in the original post I then tried connecting the generator to Serial1 I tried it with the Gen TX to Mega TX & Gen RX to Mega RX first (Serial1.jpg).

Using the code below this did not work, so I flipped it to Gen TX to Mega RX & Gen RX to Mega TX (Serial1Flipped.jp).

That did not work either. In both cases the Gen ground was connected to the Mega ground.

String setPWM;

void setup() {
  Serial1.begin(9600);
}

void loop() {
  setPWM = "F105";  
  Serial1.println(setPWM);    
  delay(5000);
}

Initially I had the generator connected to Serial. Then I ran this code. With this code running I then opened the Serial Monitor. I could then type a command ("F101") and the PWM frequency was set to 100. So, I know that the PWM Generator communication is working.

String setPWM;

void setup() {
  Serial.begin(9600);
}

void loop() {
  if(Serial.available() >0 {
    setPWM = Serial.readString();
    Serial.println(setPWM);
  }
}

As I stated in the original post I then tried connecting the generator to Serial1 I tried it with the Gen TX to Mega TX & Gen RX to Mega RX first (Serial1.jpg).

Using the code below this did not work, so I flipped it to Gen TX to Mega RX & Gen RX to Mega TX (Serial1Flipped.jp).

That did not work either. In both cases the Gen ground was connected to the Mega ground.

String setPWM;

void setup() {
  Serial1.begin(9600);
}

void loop() {
  setPWM = "F105";  
  Serial1.println(setPWM);    
  delay(5000);
}

could it be a problem with the end of line? try

setPWM = "F105\r\n";

Hi there!

I see you tried Serial1 connections both with Tx-Tx and Tx-Rx, but with Serial it seems you only did Tx-Tx. Can you try Serial with Tx-Rx or have you already done that?

I tried it with setPWM = "F105\r\n";. That did not work either.

One more thing to be clear. I need to be able to communicate with this board using Serial1 because I need Serial to be open for communication with my PC.

Having said that, I did try to send commands to the generator using Serial and code (no Serial Monitor). It did not work with either Tx-Tx, Rx-Rx of Tx-Rx, Rx-Tx.

dougrider:
I tried it with setPWM = "F105\r\n";. That did not work either.

One more thing to be clear. I need to be able to communicate with this board using Serial1 because I need Serial to be open for communication with my PC.

Having said that, I did try to send commands to the generator using Serial and code (no Serial Monitor). It did not work with either Tx-Tx, Rx-Rx of Tx-Rx, Rx-Tx.

That seems very strange. I did some research and found this Arduino Forum article that may give some clues as to what can solve your issue.

That seems very strange. I did some research and found this Arduino Forum article that may give some clues as to what can solve your issue.

Unfortunately I have read all of that several times trying to find some help there, but no.

Hi,

Initially I had the generator connected to Serial. Then I ran this code. With this code running I then opened the Serial Monitor. I could then type a command ("F101") and the PWM frequency was set to 100. So, I know that the PWM Generator communication is working.

I hope you realise that the code in the Mega had nothing to do with this occurring.
You were sending serial commands directly to the generator from the Serial Monitor because they were connected to the same Serial port terminals.

Tom... :slight_smile:

Hi,
Look at this link;

Especially the use of Serial.write instead of Serial.print

Tom.. :slight_smile:

Thanks to everyone for your input.
I got it working now.
I needed to convert my string to a char array and then write (not print) the array to Serial1.

Thanks again.

Hanzz23:
can you help me with code? my arduino with module doesnt work

What module?

What code have you tried?

Exactly what happens when you run your program?

...R

I have arduino mega 2560 and LCD PWM generator with ttl. i would like to control 4pin fan controlling PWM with arduino.

my code is:

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
Serial1.begin(9600);
}

char buf[40];
int count=0;

void loop() { // run over and over
if (Serial1.available()) {
Serial.write(Serial1.read());
}
if (Serial.available()) {
char ch = Serial.read();
if(ch=='\r'){
buf[count]=0;
Serial1.print(buf);
count = 0;
}else if(ch=='\n'){
;
}else{
buf[count++] = ch;
}
}

}

comunication doestn work :confused:

I try to modify the code but it still does not work. on yesterday's communication attempt FAIL .....

@Hanzz23, I have suggested to the Moderator to move your question to its own Thread as there is no sign of an answer emerging in the present Thread

The words "doesn't work" give us no information from which to help you. Tell us in as much detail as possible what happens when you run your program.

Aslo make a drawing showing how you have everything connected and post a photo of the drawing. See this Simple Image Guide

...R

are you transmitting '\r' to the program?
you don't appear to be adding an end of line (\r or \n) to your buffer before transmission over Serial1

I want to connect the arduino mega and pwm module with ttl communication (arduino pin RX18, TX19) did not send the wiring diagram. as it appeared in the previous post. my colleague has managed to solve the problem. i thought it would sound ..... i would like to control the pwm module with 4pin PC fan 12v and controlling pc ... after uploading the code to arduin, nothing will appear on serial communication. i can write command read and display FAIL. in the code I was inspired by youtube

I am sorry for my bad English...

thanks everbody! my code its works. I did not have Both NL & CR on serial communication.......