Beginner Serial Communication

JayStel: The data sheet for the LASER unit details the RS232 signal pins use 3.0v logic level. Are the voltage levels on your Arduino serial pins 3v RS232 logic level? Or 5v ?

They are 3.3V...it's all working now.

How you put your Arduino serial to 3.3V? Did you use a ttl converter, eg https://www.sparkfun.com/products/8745?

ivoferreira: How you put your Arduino serial to 3.3V? Did you use a ttl converter, eg https://www.sparkfun.com/products/8745?

No, I didn't use a TTL converter. I ended up using software serial on digital IO pins 10 and 11. The RX pin (10) is receiving data at 3.5V from the LR4 interface board while the TX pin (11) is transmitting at 4.5V (~5V standard).

Bjornert Hello, can you please show the final sketch for ARDUINO connect LR4. I'm having the same problem. Thank you very much.

jtcueli:
Bjornert Hello, can you please show the final sketch for ARDUINO connect LR4. I’m having the same problem.
Thank you very much.

I’ve attached some simple code that takes the measurement string from the LR4 and outputs an integer to your serial monitor. Notice that it is using software serial on pins 10 and 11 rather than the RX and TX pins 0 and 1 which are being used by the USB programming cable. This code was also design using the Arduino Leonardo. If you use another controller, like a Due for example, there are many more serial pins and you do not need to use software serial.

Hope this helps! Good luck.

#include <SoftwareSerial.h>

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

byte goByte = 0x67;                //g
byte stopByte = 0x73;              //s
byte inByte = B00000000;           //recieved bytes via RX
String inString = "";              //string to hold input

void setup(){  
  Serial.begin(9600);              //9600 as per the datasheet
  while(!Serial){;}                //wait for serial port to connect
  mySerial.begin(9600);            //Initialize LRF software serial
  mySerial.write(goByte);          //Start The Laser
  delay(1000);                     //Delay 1s
}

void loop(){  
  
  if(mySerial.available()) {
    int inChar = mySerial.read();
    if(isDigit(inChar)){
       inString += (char)inChar; 
    }
    if(inChar == '\n') {
       Serial.print("Distance: ");
       int distance = inString.toInt();       
       Serial.print(inString.toInt());
       Serial.println("mm");
       inString = ""; 
    }
  }
 }

bjornert:

ivoferreira: How you put your Arduino serial to 3.3V? Did you use a ttl converter, eg https://www.sparkfun.com/products/8745?

No, I didn't use a TTL converter. I ended up using software serial on digital IO pins 10 and 11. The RX pin (10) is receiving data at 3.5V from the LR4 interface board while the TX pin (11) is transmitting at 4.5V (~5V standard).

There are at least 3 ways to get that 4.5V-5V TX signal down to nominal 3.3V. One uses a diode and a resistor and the device 3.3V output, if it has one. One uses a voltage divider with one resistor at half the value of the other, and the third is to put 2 diodes in series between Arduino TX and device RX.

In the datasheet specifies that Rx and Tx pins used 3V, but the pins 10 and 11 in Arduino UNO are 5V and I try to connect directly, so it might not work?

Much obliged.

jtcueli: In the datasheet specifies that Rx and Tx pins used 3V, but the pins 10 and 11 in Arduino UNO are 5V and I try to connect directly, so it might not work?

Much obliged.

5V TX going into a 3V RX may work or it may burn up the RX circuit either quickly or slowly.

You can make a voltage divider to change the 5V level closer to 3V. I used 3 4.7k resistors because 2 of those in series made a 9.4k and I have no 9.4k's. So my 5V TX goes into 1 4.7k resistor and at the other end I connect my wire to the 3V RX. Same place as the wire connects I have the 2 4.7k resistors in series also connected and the other end of those (one after the other, in series) connects to ground. 1/3 of the voltage is grounded while 2/3 goes to RX.

Regular diodes drop .7V. Two in a row drops 1.4V. 4.5V - 1.4V = 3.1V.

The diode and resistor method requires 3.3V at the device end and uses that to make a pullup for RX. It starts with a diode connected to the Arduino TX wire, pointed to stop flow from TX to RX. The other side of the diode connects to the RX pin and also connected is 3.3V through a resistor like 4.7k or 10k. When TX is HIGH the RX pin gets 3.3V. When TX is low the pullup current flows through the diode and RX is pulled LOW.

The last one is probably the most energy-efficient. All of them are cheap and easy.

HazardsMind:

It now has nothing to do with my problem as the LR4 has been hooked up to the Arduino via the TX, RX, 5V and GND pins.

If that's the case, then you need to get an Arduino with more than one set of TX/RX pins. You can maybe use Software Serial, or get an Arduino Mega.

Does the Leonardo, which is what the OP is using, not have 2 serial interfaces ? One via USB to the serial monitor and a second one via pins 0 and 1

From http://arduino.cc/en/Main/ArduinoBoardLeonardo

Serial: 0 (RX) and 1 (TX). Used to receive (RX) and transmit (TX) TTL serial data using the ATmega32U4 hardware serial capability. Note that on the Leonardo, the Serial class refers to USB (CDC) communication; for TTL serial on pins 0 and 1, use the Serial1 class.

If you are initially just interested in turning on and off the laser, take the

  if(Serial.available() > 0);{
   inByte = Serial.read();          //Read what's on the RX line
   delay(500);
   Serial.print("Incoming Byte: ");  
   Serial.println(inByte);   
   delay(500);
  }
}

out, and test it. It could be causing confusion.

jackwp: If you are initially just interested in turning on and off the laser, take the

  if(Serial.available() > 0);{
   inByte = Serial.read();          //Read what's on the RX line
   delay(500);
   Serial.print("Incoming Byte: ");  
   Serial.println(inByte);   
   delay(500);
  }
}

out, and test it. It could be causing confusion.

See reply #24 for the updated code. It's better.

I have made a voltage divider to pass Tx 3V, the output from pin 11 but the laser does not start. I am using the sketch # 24 but with ARDUINO UNO. Has anyone tested on Arduino UNO?

Thank you in advance!!

I don't have the laser device that you do, just lasers. Did you test the voltage with a meter? Did you give it 5V before and it worked? Is there some message you can send it end get a message back?

Do you have two arduino boards? Can they send/receive serial between them selves?

I do not know exactly where it is wrong. I tried directly to connect the pin to 5V but not start the laser. Arduino I have no time to check that communication is good. I have found that Arduino pin 10 receives signal LR4 Tx. For now I'd settle for that arrancase laser.

jtcueli: I do not know exactly where it is wrong. I tried directly to connect the pin to 5V but not start the laser. Arduino I have no time to check that communication is good. I have found that Arduino pin 10 receives signal LR4 Tx. For now I'd settle for that arrancase laser.

The wiring looks okay to me. Testing serial voltage is easy. When not sending, the line is HIGH. Just meter the voltage between LR4 RX and ground. I would use larger value resistors for the divider just to draw less current but that is me. 10k is not too much.

Do you want the laser for range finding or just to have a laser? http://dx.com/p/12mm-5mw-red-laser-diode-modules-black-dc-4-5v-165078 I bought a bag of these thinking they were just the diodes. Then I bought another bag when I found they are diode, optics and constant current circuit able to run on 5V. They won't pull more than 20mA, safe for Arduino pins. No, I haven't run a test to find the lifetime of one. At 46 cents each, I got extras.

GoForSmoke: Do you want the laser for range finding or just to have a laser? http://dx.com/p/12mm-5mw-red-laser-diode-modules-black-dc-4-5v-165078 I bought a bag of these thinking they were just the diodes. Then I bought another bag when I found they are diode, optics and constant current circuit able to run on 5V. They won't pull more than 20mA, safe for Arduino pins. No, I haven't run a test to find the lifetime of one. At 46 cents each, I got extras.

Do those produce a bright red dot at a reasonable distance? I see some fun with those!

It's a 5mW laser where a cheap pointer is 2 or 3mW. They claim 1000m visible which okay, maybe at night.. with a telescope? Or maybe better than that. With the right setup I am sure they'd be good for laser-modem or even laser-IR-remote at long range.

Swinging around the wall, the trails are bright red in a well lit room. I haven't tried a double-slit yet.

As for range finding, I wouldn't even try. I might put a laser with an ultrasonic sensor to show where the sensor is pointing but really a bright led more approximates the sound cone and is less dangerous to eyes.

What does this part of code means: " int inChar = mySerial.read(); if(isDigit(inChar)){ inString += (char)inChar; } "

What does this part of code means:

What do you think it means?

     int inChar = mySerial.read();

Read a character...

    if(isDigit(inChar)){

Check to see if it is a digit (0, 1, ... 9, or + or -)...

       inString += (char)inChar;

If it is, do a useless cast, then piss away resources using a String when a NULL terminated char array is just as useful, to add the digit to the collection.