2

I have a sensor that communicates via serial communication. CTi Sensors TILT-05. It transmits an ASCII string with data separated by commas. I want to be able to read the incoming ASCII data in the Arduino IDE's Serial Monitor in real time (the device is an accelerometer).

I am struggling to convey this in the code. Currently, I have the sensor's RX connected to an Arduino Uno's TX port (digital 1) and sensor TX-> Arduino RX (digital 0). The sensor is powered and grounded via the Arduino.

I have searched for serial communication methods but can't find any that seem to work. How can I receive and interpret the incoming data from the sensor?

Basic setup. Not complicated at all, just need some clarification. Closer view of the connections.

1

You don't mention the sensor brand or a datasheet so this will be a very general answer to the problem.

Firstly, how you have the sensor connected now should just spool info into the Serial monitor of the Arduino IDE without any code on the Arduino.

What are the data speed and the exact way the Serial comes out from the sensor?

If you want to interpret the data as it is coming through you should use Software Serial as you can't relay info to the PC with an Arduino's RX TX lines connected to something else.

What you can do is below(Untested):

#include <SoftwareSerial.h>

SoftwareSerial mySer(3, 4); //RX TX

//these are char areay because its from Serial
char accel[3] = {'0'};
uint8_t place = 0;

// array to print in sync with the accelerometer values array
const char* SerialText[3] = {
   "\nX Axis: ",
   "\nY Axis: ",
   "\nZ Axis: ",
};


char inData = '0';
char separator = ','; // separator you described



void setup(){
//set to the normal baud, mines for the example
     Serial.begin(9600); 

     mySer.begin(9600);

}//~ end setup

void loop(){

    if(mySer.available()){

     inData = mySer.read(); // get the value at the instance of the serial data being there

        if(inData != separator){
          accel[place] = inData;
          Serial.print(SerialText[place]); // print the current values identifier
          Serial.print(accel[place]); // print value
          place++; // increment for the next time
     }
   }

if(place >= 3) place = 0;
}//~ end loop

The above gives this output at the moment, with an input of $CSACC,-0156.4,-0226.3,+0981.2*49

X Axis: $
Y Axis: C
Z Axis: S
X Axis: A
Y Axis: C
Z Axis: C
X Axis:  
Y Axis: -
Z Axis:  
X Axis: 0
Y Axis: 1
Z Axis: 5
X Axis: 6
Y Axis: .
Z Axis: 4
X Axis:  
Y Axis: -
Z Axis:  
X Axis: 0
Y Axis: 2
Z Axis: 2
X Axis: 6
Y Axis: .
Z Axis: 3
X Axis: +
Y Axis: 0
Z Axis: 9
X Axis: 8
Y Axis: 1
Z Axis: .
X Axis: 2
Y Axis: *
Z Axis: 4
X Axis: 9
Y Axis:  
Z Axis: 
X Axis: 

The code doesn't filter this the way it should but is a stepping stone. I will update it to do it properly when I have a chance.

| improve this answer | |
  • There are other ways, but I will add some info later if needed or after clarification from the OP. – RSM Jun 27 '16 at 22:41
  • Hi there, thank you so much for trying to help. For clarification, this is the sensor I'm trying to use: ctisensors.com/wp-content/uploads/2016/03/…. It transmits an ASCII string at a baud rate of 115200. I've tried your code but have encountered a few errors, primarily: error: array must be initialized with a brace-enclosed initializer char accel[3] = 0; – F Young Jun 27 '16 at 23:05
  • @FYoung thanks for the sensor info, i will amend my answer to that and also to fix the error of the code. – RSM Jun 28 '16 at 7:20
  • As a side note, as the device uses NMEA 0183, here is a link to way to split it nicely – RSM Jun 28 '16 at 7:45
0

With RSM's help I've managed to get the sensor reading into the serial monitor semi-reliably. I have yet to solder the headers onto the sensor yet and am just doing prototyping (basically just me pushing a wire against the sensor output hole) which is unreliable. I believe once I get some headers soldered on the output will be much more reliable. For those searching, this code worked for me.

Digital pins 3/4 function as your RX/TX pins respectively. Connect the TX of your sensor to the RX (3) of the Arduino and likewise for the TX. I used a baud rate of 115200 but you can change it to whatever works in the code. This is simply a simplification of RSM's code in his previous answer and all credit/kudos goes to him.

#include <SoftwareSerial.h>

SoftwareSerial mySer(3, 4);

char inData = '0';

void setup(){
     Serial.begin(115200); 
      mySer.begin(115200);
}

void loop(){
    if(mySer.available()){
     inData = mySer.read(); 
     Serial.print(inData);
  }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.