Hi,
I'm using this sensor with an arduino board.
On page 2, they give you 3 options to read the sensor using the analogue pin, the pulse pin and a serial pin.
How do you read a serial pin in arduino?
Many thanks
Hi,
I'm using this sensor with an arduino board.
On page 2, they give you 3 options to read the sensor using the analogue pin, the pulse pin and a serial pin.
How do you read a serial pin in arduino?
Many thanks
With SoftwareSerial
Hi,
Thanks for you reply.
The datasheet say it has a TTL output.
The output is an ASCII capital “R”, followed by four ASCII character digits representing the range in millimeters,
followed by a carriage return (ASCII 13). The maximum range reported is 9998 mm (10-meter
models). The serial data format is 9600 baud, 8 data bits, no parity, with one stop bit (9600-8-N-1).
Because the data is presented in a binary data format, the serial output is most accurate .
How would you turn this into a integer value? Apologies, I'm quite new to arduino.
Thanks
conor1:
How would you turn this into a integer value? Apologies, I'm quite new to arduino.
The Arduino IDE provides are examples on using serial communication. I don't know which example shows how to read in numeric values.
Robin2 posted "Serial Input Basics". I'm pretty sure the thread includes an example of reading numeric data.
zoomkat posted an example of reading numeric value from serial input in this post.
conor1:
Hi,Thanks for you reply.
The datasheet say it has a TTL output.
How would you turn this into a integer value? Apologies, I'm quite new to arduino.
Thanks
conor1:
Hi,Thanks for you reply.
The datasheet say it has a TTL output.
How would you turn this into a integer value? Apologies, I'm quite new to arduino.
Thanks
In the format Rxxxx, xxxx is a 4 digit decimal integer. Were you asking instead, how to translate that to an "int" variable? The answer to that could be atoi() or other C++ functions that are documented here.
conor1:
The datasheet say it has a TTL output.
That refers to the voltage of the serial signal. TTL is 0-5v and is what the Arduino likes.
The alternative for serial signals is RS232 voltages which are too high for an Arduino and need a MAX232 chip to convert them to TTL levels.
...R
I looked at the data sheet and if you have the TTL (MB7380 series) of sensor, you should be able to easily communicate with the arduino. As a simple test setup, you might try the following:
load the below dummy program on the arduino.
connect the sensor 5v wire to the arduino 5v pin, the sensor ground to arduino ground, and the sensor tx wire to the arduino tx.
open the serial monitor and see if the range data is being displayed in the serial monitor.
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
Hi;
- connect the sensor 5v wire to the arduino 5v pin, the sensor ground to arduino ground, and the sensor tx wire to the arduino tx.
- connect the sensor 5v wire to the arduino 5v pin, the sensor ground to arduino ground, and the sensor tx wire to the arduino Rx.
Tom... ![]()
- connect the sensor 5v wire to the arduino 5v pin, the sensor ground to arduino ground, and the sensor tx wire to the arduino Rx.
Bzzzztttt! Know and understand how your arduino is built. 8)
Just as an update if anybody is curious, the reason why the sensor tx is connected to the arduino tx pin is the USB/serial chip rx is also connected to the arduino tx pin. This is convenient if one wants to make a direct connection to a TTL device without involving the arduino chip itself. Similar setup for the arduino rx pin. Apparently the the arduino tx/rx are at a high impedance when they are not set for serial communication by code running on the arduino.
Hi,
I have connected the sensor and I'm using the code below. However the output on the serial window is "-1" repeated. Any ideas?
void setup()
{
Serial.begin(9600);
}
void loop()
{
int data = Serial.read();
Serial.println(data);
delay (1000);
}
Thanks
However the output on the serial window is "-1" repeated.
That's what Serial.read returns when there's nothing to read.
Did you read reply #6?
Hi,
My apologies, I had it connected to the wrong pin.
I have data now on the serial monitor using this code.
void setup()
{
Serial.begin(9600);
}
void loop()
{
int data = Serial.read();
Serial.println(data);
delay (1000);
}
However, the data doesn't make sense. All that is outputted is '82' which gather corresponds to capital R in ascii. Should I be declaring the variable data as another type? How to convert the ascii characters to a string?
Thanks
Hi,
So I tried this code, which I think is nearly there.
I'm just wondering how to I print out the actual distance. If the distance is four ascii characters 1:4. should I declare the variable data as an int.
void setup()
{
Serial.begin(9600);
}
void loop()
{
byte DataToRead [6];
Serial.readBytesUntil(char(13), DataToRead, 6);
int data = DataToRead [1:4];
Serial.println(data);
delay (1000);
}
I accept that you may consider me biased but I believe the examples in Serial Input Basics will be helpful - probably the second example. @DuanDegn gave you a link to that in Reply #3. Unlike Serial.readBytesUntil() my examples don't block the Arduino while waiting for data. And, as posted, they are a convenient way just to see what data is being received.
...R