Communication problem with IMU (9dof) and Nano using hardware serial

Hi

This is my first post here on the arduino forum so go easy on me please.

I have started a quadrocopter project with the intention of learning alot of embedded systems such as microcontrollers and different mechanical actuators. The goal of the project is to in the end make it fly autonomous outdoor with a mounted camera to film the ride. This is a project i plan on doing for many months forward and still in the beginning of the project.

The problem:
The problem is that i would like to communicate with the IMU card using the hardware serial pins on the Nano (RX0 and TX1). Which for some reason does not work as i expected (will come to that later just so you have an idea of what im doing)

I have the following components:

  • IMU (9dof) - flashed with Razor AHRS
  • Levelshifter from 3.3v to 5v
  • Arduino Nano
  • Arduino Uno
  • FTDI 5v cable

The levelshifter is created by me using this scheme Sparkfun level converter. It is a 50% voltage cut on the TX side and a transistor boost on the RX side.

The code that i have flashed into the IMU is taking different commands so i can manipulate the output from it. On the IMU there is also a LED that is on if the IMU is in continious sending mode and off otherwise.
The different commands i am trying to send to the IMU is

  • #o0 //This stops continuous output from the IMU
  • #o1 //This starts continuous output from the IMU

By sending those commands one after eachother i can make the IMU blink (if i do it with some delay in between). But here is where i get into problems. When using the code below the IMU does blink perfectly when i put the RX pin from IMU card to pin 9 (Soft transmit pin), but when i change it to the hardware pin (TX1) it does not work as expected. The wierdest part is that it works “sometimes” say 1/50 times it actually changes value, i checked this by just increasing the frequency i changed the value with to like 10hz instead of 1hz and count seconds between diode on and off by observing myself.

Code put into the Nano

#include <AltSoftSerial.h>
#include <HardwareSerial.h>

// AltSoftSerial always uses these pins:
//
// Board          Transmit  Receive   PWM Unusable
// -----          --------  -------   ------------
// Arduino Uno        9         8         10

AltSoftSerial altSerial;
unsigned long time=0;
boolean p=true;

void setup() {
  Serial.begin(57600);
  altSerial.begin(57600);
}

void loop() {

		//change value every 1 second
  if(time<millis()){
    time=millis()+1000;
    if(p){
      p=false;
      altSerial.write("#o0");
      Serial.write("#o0");
    }else{
      p=true;
      altSerial.write("#o1");
      Serial.write("#o1");
    }
  }
}

Other experiments i have done

  1. I have used a FTDI 5v cable connected to the IMU by the level converter. Then everything works just fine. When i write “#o0” and diode turns off and when i write “#o1” the diode turns on.

  2. Connected an Arduino UNO in paralell to the TX1 pin of the Nano (Nano → Uno+IMU), and then read it directly using the console to confirm that the Nano actually sends correct serial information. Here on the IMU i get same behaviour sometimes it works(1/50), but on the Uno i get correct value all the time.

"Question"
Why does the hardware pins behave like this? Is there some trick to serial communications using hardware pins? Is this a problem with signal levels (5v, 2.5v)?

//Regards KufrA

New tests trying to solve this problem.

  1. Connecting oscilloscope to the outputs of both the serial outputs HW pin and SW pin, both is stable at 5v. So no problem with signal level

  2. Connected the uno to a logic tester (Saleae logic) and sampled the signal in Megahertz. By using their software i could analyze the results and confirm that both pins send the exact same thing using the code above and also that there is nothing wrong with the logical converter.

This is a very wierd error. if you know any good way to try to analyze this please tell me.

If I understand, the software serial TX pin is working but the hardware serial is not? Do the RX pins work for both software and hardware serial?

How do you send the commands to the nano? How do you send commands to the IMU? (BTW, I am not familiar with what an IMU is). Maybe if your set up is simple enough you could post a clear photo of how everything is connected.

With respect to the IMU, what sort of communication protocol does it expect? 9600,n,8, 1? Or does it in fact respond at 57.6k baud? Is it TTL, RS-232? What voltage does it expect? The fact that the uno and nano communicate effectively suggests to me that we need to look at the IMU. What is a levelshifter? Is it possible that the shifting of voltages has poor fidelity?

Do you have the nano connected to your PC using the micro usb connector? I believe you can only use one or the other.

If you go through the altsoftserial documentation, I believe it states that you need to be careful in choosing the baud rate. It's particularly an issue with softwareserial, which you don't appear to be using, but it is still possible that the two libraries are stumbling over each other at times. You might try using different baud rates for the two ports. You could also try it without the altsoftserial library. (http://www.pjrc.com/teensy/td_libs_AltSoftSerial.html)

Typically hardware serial is the most effective approach for serial communication. Do you need two serial ports for your project?

Cloverleaf: If I understand, the software serial TX pin is working but the hardware serial is not? Do the RX pins work for both software and hardware serial?

No not at all, the software library i am using has defined pin 8 and 9 for software serial (as shown in the code) and the hardware serial is using TX and RX pins as usual.

How do you send the commands to the nano? How do you send commands to the IMU? (BTW, I am not familiar with what an IMU is). Maybe if your set up is simple enough you could post a clear photo of how everything is connected.

The code i put into the nano at the moment is only for debugging, and what it does is that it is sending out "#o1" followed by "#o0" and repeating. If the IMU (IMU means Inertial Measurement Unit and in my case it consists of an atmega328p + 9 sensors connected to a I2C buss) receive it correctly it will light the onboard diode if "#o1" is received and turn off if "#o0" is received. I also has a blog (mainly for my own project and myself) where i put information about my project. Is a picture how i plan everything to be connected later on in the project (now only the bottom part is the goal).

With respect to the IMU, what sort of communication protocol does it expect? 9600,n,8, 1? Or does it in fact respond at 57.6k baud? Is it TTL, RS-232? What voltage does it expect? The fact that the uno and nano communicate effectively suggests to me that we need to look at the IMU. What is a levelshifter? Is it possible that the shifting of voltages has poor fidelity?

The code i programmed into the IMU defined the communication to 57600 baud rate. It is UART Asynchronous Serial communication (standard for what i know). Voltages expected is 3.3v for the IMU and 5v for the arduino, this was one of my thoughts what could be wrong but when i checked with the occiloscope today there were no different at all between the hardware pins and the software pins in either voltage level or rising times (from 0 to 1). The levelshifter in this way is really two resistors of equal size and a cable in between them. so basically i get 2.5v out. I actually had thoughts of this aswell. But as it turned out today the voltage is exactly the same if i use the hardware or software does not matter the signal looks exactly the same. This implies that nothing is really wrong but still it does not work. (To be clear what is same is the voltages and the timings, since i debugged it using this cool sample thing as i posted above). So i do believe they are not the same in some sense but i can not understand how. again when i use software serial it works but i want to use the hardware serial to not put to much stress to the cpu, and the hardware communication does not reach the IMU as the software one does...

Do you have the nano connected to your PC using the micro usb connector? I believe you can only use one or the other.

I have tried to connect the IMU to my FTDI cable then it works all fine. I have tried to power the arduino by external power not using the usb at all and try the hardware pins but still not working.

If you go through the altsoftserial documentation, I believe it states that you need to be careful in choosing the baud rate. It's particularly an issue with softwareserial, which you don't appear to be using, but it is still possible that the two libraries are stumbling over each other at times. You might try using different baud rates for the two ports. You could also try it without the altsoftserial library. (http://www.pjrc.com/teensy/td_libs_AltSoftSerial.html)

If i just comment the altsoftserial away it still does not work. and the altsoftserial communication is actually working. but again i do wish to use the hardware serial to minimize the load of the CPU. One thing i have not tried yet is to reprogram the IMU card with a different baud rate and test if that works. I will do this until my next post. good idea =)

Typically hardware serial is the most effective approach for serial communication. Do you need two serial ports for your project?

Yes that is the point i do not need the soft serial at all but since the hard serial was not working i tried many different ways of debugging and when i found out about soft serial i tried it and it worked. So it is part of my debugging that i have done, if the hardware serial communication just worked i would not need it. T_T

Well... I reprogrammed the IMU using 115200 baudrate instead of 57600 and now it works for both Software serial and Hardware serial. wow this was one of the wierdest faults i have ever seen. I have no idea at all why it is working but now it actually is working! thanks "Cloverleaf" for trying to help!