Sending Serial Commands

I’m struggling with figuring out how to control an LG TV using Arduino. Here is an excerpt of the codes:
http://postimg.org/image/av2nsw7et/
For example, I want to send “ka 01 01” via the TX pin to turn on the TV. I’m really lost with how to accomplish this. Does it need to be a string? My C writing skills are poor but if you can get me started I can work from there. I’m just so confused after looking at an ASCII table noting makes sense, there is no “ka”.

I’m writing in v1.6.6 using an UNO. This is what I’ve come up with:

#include <SoftwareSerial.h>

SoftwareSerial TV_Serial(0, 1); // Arduino RX, Arduino TX

void setup() {
  TV_Serial.begin(9600);  // software serial port
}

void loop() {
  
  String cmd = ("ka 01 01");

 TV_Serial.println (cmd);

  delay(1);
}

Am I on the right track? This didn’t work to turn on the TV btw.

Does it need to be a string?

Does "ka 01 01" look like a string? It does to me.

SoftwareSerial TV_Serial(0, 1); // Arduino RX, Arduino TX

Do NOT do SoftwareSerial on the hardware serial pins.

  String cmd = ("ka 01 01");

That is NOT a string. The parentheses are useless.

Am I on the right track?

No, but the right track is in the neighborhood.

PaulS:
Does "ka 01 01" look like a string? It does to me.

  String cmd = ("ka 01 01");

That is NOT a string. The parentheses are useless.

So how do I write ka 01 01 into a string? Without parentheses the code does not compile: 'ka' was not declared in this scope. I don't understand what ka 01 01 even is. Doesn't look like hex, binary, or anything I can figure. How do I drive into the right neighborhood?

Where did you get this ka 01 01 code from? Maybe there is some information there on what that should look like.

It needs a carriage return at the end, according to my 10 seconds of research.
Codes are ASCII, so "ka 01 01\r" should do it.

TV_Serial.print("ka 01 01\r");

might do what you want.
(Or maybe without the spaces, but I think they're needed.) Yes - needed.

https://www.lg.com/us/commercial/documents/m3701c-ba-rs232c.pdf

http://activatethespace.freshdesk.com/support/solutions/articles/1000105664-serial-control-command-strings-for-lg-tv-s

But not on the hardware serial pins, as Paul says.

Is the serial connection on the tv TTL or RS232? If it is RS232 then you will need to address the conversion.

image.jpg

zoomkat:
Is the serial connection on the tv TTL or RS232? If it is RS232 then you will need to address the conversion.

image.jpg

Very good point - I overlooked that. :frowning:

LG TV RS232C.JPG

The below seems to indicate RS232 protocol.

https://www.google.com/search?as_q=lg+tv+serial+commands&as_epq=&as_oq=&as_eq=&as_nlo=&as_nhi=&lr=&cr=&as_qdr=all&as_sitesearch=&as_occt=any&safe=images&as_filetype=&as_rights=

zoomkat:
The below seems to indicate RS232 protocol.

lg tv serial commands - Google Search

Yep. The pdf that I linked to earlier says that it's RS232C. The image I posted in my last reply was from that LG pdf.

zoomkat:
Is the serial connection on the tv TTL or RS232? If it is RS232 then you will need to address the conversion.

image.jpg

Do you mean physically? I’m actually working with an LG-LW100 wireless transmitter. The arduino will simulate the remote control from my tv via a 3.5mm pin in the service port of the transmitter box. It’s all proprietary hardware that is only compatible with LG. So there will be 2 pins designated as software serial (not 0 and 1) and ground connected to TX, RX, and GND of the 3.5mm pin. I just said I’m working with an LG for simplicity. The coding is all the same and that’s the part I’m struggling with. I’ve got the hardware all figured out. I’m still working off RS232 protocol. Here’s what I’ve been working off of:

ok ive figured out how to switch inputs without needing an LGTV or pressing buttons on the front panel.

i made a serial cable out of these parts:

any 3.5mm stereo TRS jack or cable
db9 serial cable or connector (only need 3 wires)
paperclip + conductivity tester w/ leads or probes to test cable

connect tip of 3.5mm to pin 3 of db9
connect ring of 3.5mm to pin 2 of db9
connect sleeve of 3.5mm to pin 5 of db9

connect TRS side to “service-only” port on rear of an-wl100 main base unit (where devices connect), and db9 to serial or usbTTy device.
set baud rate to 115200 / 8bit / no parity / stop bit 1 / flow control off
turn unit on

you will not get interactive output during bootup (or when pressing enter) but when you issue the following commands you should get acknowledge and input switch:

xb 00 10 = TV ??? Very odd. I get static audio as if this thing would be capable of NTSC tuning!
xb 00 11 = DTV ??? Wonder if it can tune ATSC?
xb 00 20 = AV1
xb 00 21 = AV2
xb 00 40 = Component1
xb 00 41 = Component2
xb 00 60 = RGB/VGA
xb 00 70 = HDMI1
xb 00 71 = HDMI2
xb 00 72 = HDMI3
xb 00 73 = HDMI4

Instead of using the hyperteminal on PC I want to use arduino to transmit the commands through the 3.5mm pin. Others have said that 9600 is the correct baud rate.

With the carriage return the code complies, and I see that it is needed.

Thanks for all your help! I’ll keep working on this and report back.

I still haven’t been successful with this project. I’ve tested my cable and the serial commands using Hyperterminal in my PC and everything works. My problem is getting arduino to send command.

I’m using software serial and connecting arduino to MAX3232 serial to TTL converter. Here’s the sketch I’ve been trying:

#include <SoftwareSerial.h>
SoftwareSerial TV_Serial(10, 11); // Arduino RX, Arduino TX
void setup() {
  // set the data rate for the SoftwareSerial port, baud rate confirmed 9600 via hypeterminal
  TV_Serial.begin(9600);
  TV_Serial.println("xb 00 71\r"); //to change input to HDMI2
}
void loop() {}

The unit is also supposed to send back a confirmation over serial (b 01 OK71x). This is received in hyperterminal but I’ve never seen it with the arduino serial monitor (using a different sketch).
I need to send ASCII text command ‘xb 00 71’ and ‘enter’ (carriage return) with the arduino software serial TX. I’ve tried modifying this sketch several ways with no success. Any more suggestions?

Are you 100% sure you're connecting the Arduino's RX/TX pins to the USB to TTL converter the right way around?
ie Arduino RX to converter TX, and Arduino TX to converter RX.
That's a common mistake, and an easy one to make.

Aside from that, I can't think of anything further. If Hyperterminal is working, then with the USB to TTL converter correctly connected, the Arduino should be able to do the same.

Edit: Just a thought - when successfully using Hyperterminal, do you have it set to also send a 'newline' (LF) character with the 'carriage return' (CR)? According to the documentation, I don't think it's necessary, but thought I'd better ask anyway.

If so, try this:-

TV_Serial.println("xb 00 71\r\n"); //to change input to HDMI2

OldSteve:
Are you 100% sure you're connecting the Arduino's RX/TX pins to the USB to TTL converter the right way around?
ie Arduino RX to converter TX, and Arduino TX to converter RX.
That's a common mistake, and an easy one to make.

Thanks for that suggestion. I was following the instcutions:
"connect tip of 3.5mm to pin 3 of db9
connect ring of 3.5mm to pin 2 of db9
connect sleeve of 3.5mm to pin 5 of db9"

I swapped pin 2 and 3 and that did it! The original author for that pinout must've got his wires crossed :stuck_out_tongue:
The sketch works, my inputs are changing!

alphacheese:
Thanks for that suggestion. I was following the instcutions:
"connect tip of 3.5mm to pin 3 of db9
connect ring of 3.5mm to pin 2 of db9
connect sleeve of 3.5mm to pin 5 of db9"

I swapped pin 2 and 3 and that did it! The original author for that pinout must've got his wires crossed :stuck_out_tongue:
The sketch works, my inputs are changing!

Excellent. And it was only a shot in the dark on my part, too.
I'm pleased that it's working. Took a while, but we got there. :slight_smile:

Re: crossed wires.
Good catch OldSteve, useful to have crossed eyes :wink:

34f5a50f39fdeadc98ec01f950922afc.jpg

LarryD:
Re: crossed wires.
Good catch OldSteve, useful to have crossed eyes :wink:

34f5a50f39fdeadc98ec01f950922afc.jpg

Haha, me at my finest. :smiley:

Just to finalize this thread here is my sketch I wrote to change the inputs of an LG AN-WL100W wireless transmitter. You will need to make a 3.5mm stereo TRS cable to plug into the back of the transmitter via the service port. As stated, for my unit this is the correct pinout:

connect tip of 3.5mm to pin 2 of db9 (not 3 as stated by previous author)
connect ring of 3.5mm to pin 3 of db9 (not 2 as stated by previous author)
connect sleeve of 3.5mm to pin 5 of db9" (ground)

In lieu of db9 connections you might be using a mini MAX3232 RS232 to TTL converter. Connect the Arduino’s software serial RX pin to the TX pin of the MAX3232 and Arduino’s software serial TX pin to the RX pin of the MAX3232. It’s hard to tell which is which on the mini as they are labeled with arrows not text. Also, the correct baud rate for my transmitter was 9600.

I am using a 4x4 matrix keypad for buttons and only wiring buttons 1-9 to save space on my Arduino for other pins. If you need more pins there are tutorials to setup the keypad using only one analog input :slight_smile: I plan to add an LCD at some point to confirm button press and input change but for now your receiver unit will display what input has been selected via the HDMI display its plugged in to. The front of the transmitter unit will not indicate what input has selected and the soft buttons do not work, hence the purpose for this sketch. Maybe the next step will be to open the sucker up and wire in the soft buttons and LEDs to the Arduino! Oooh that sounds exciting ;D

#include <Keypad.h>
#include <SoftwareSerial.h>

SoftwareSerial TV_Serial(10, 11); // Arduino RX, Arduino TX

const byte ROWS = 3; //four rows
const byte COLS = 3; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  };
byte rowPins[ROWS] = {9, 8, 7}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

void setup(){
  Serial.begin(9600);
  TV_Serial.begin(9600);
}
  
void loop(){
  char customKey = customKeypad.getKey();
  
  if (customKey == '1'){
    TV_Serial.println("xb 00 20\r");
    Serial.println("AV1");
    }
    if (customKey == '2'){
    TV_Serial.println("xb 00 21\r");
    Serial.println("AV2");
    }
    if (customKey == '3'){
    TV_Serial.println("xb 00 40\r");
    Serial.println("Component1");
    }
    if (customKey == '4'){
    TV_Serial.println("xb 00 41\r");
    Serial.println("Component2");
    }
    if (customKey == '5'){
    TV_Serial.println("xb 00 60\r");
    Serial.println("RGB/VGA");
    }
    if (customKey == '6'){
    TV_Serial.println("xb 00 70\r");
    Serial.println("HDMI1");
    }
    if (customKey == '7'){
    TV_Serial.println("xb 00 71\r");
    Serial.println("HDMI2");
    }
    if (customKey == '8'){
    TV_Serial.println("xb 00 72\r");
    Serial.println("HDMI3");
    }
    if (customKey == '9'){
    TV_Serial.println("xb 00 73\r");
    Serial.println("HDMI4");
  }
}