Arduino Leonardo Communication Issue

Hi,

I have an Arduino Leonardo.
I’ve just connected my Arduino to my color sensor. The ADJD-S311 sensor. I’ve been constantly trying to get the serial work both the ways. The sketch uploads, but when it comes to communicate with the PC after the upload through the Serial class, it doesn’t work. Neither on the second port, Serial1.

On the other hand, on the PC everything looks fine, the COM is okay.

I don’t know what else to try. I’ve looked over some other topics, but it’s not clear to me.
Just to eliminate any uncertainty, here’s the source code, which most of it is a copy-paste from bildr.com

#include <ADJDS311.h>
#include <Wire.h>

int sensorLed_pin = 13;
ADJDS311 colorSensor(sensorLed_pin);

void setup()
{
  Serial.begin(9600);
  while(!Serial);
  colorSensor.init();
  colorSensor.ledOn(); 
  colorSensor.calibrate(); 
}

void loop()
{
  RGBC color = colorSensor.read();
  
  Serial.print(color.red);
  Serial.print(" | ");
  Serial.print(color.green);
  Serial.print(" | ");
  Serial.print(color.blue);
  Serial.print(" | ");
  Serial.println(color.clear);
  
  delay(200); 
}

Thank you for your assistance,
RobertEagle

How is the sensor connected to the Arduino?

Why not put a Serial.print() in setup() to see that it gets to the end, and a Serial.print() before and after the read() call, to see that it gets to, and past, or not, the read() call?

Okay.

So, I've tested the board only by using the serial, meaning no other functions in the sketch. It worked. Then, gradually, I've started to include the sensor's functions. The sensor uses an I2C protocol. I've made the class instance. Turned on the LED. Till now, everything okay. But when I try to calibrate the sensor, the program ,,hangs". I've tried to see what happened.

I've searched into the sensor's source code, and find out that it stops in the moment it tries to communicate trough I2C. Then I soldered the I2C connections, hoping for a result. Nothing. I had some time ago a similar problem, but back then the connections were weak.

I'm thinking for a solution, but my mind doesn't want to follow me. Maybe there is something wrong with the sensor's library. On the bildr tutorial I only see positive reviews.

The I2C pins are not the same on all Arduinos. Are you connecting to the right pins on the Leonardo? Are you connecting ground, too?

Checked!

There are those 2 special pins on the Leonardo, called SDA and SCl, just to be clear. There's where I've connected the sensor. Also the ground is connected. What the hack isn't working? I'm trying to eliminate all the factors, but the problem is not to be seen.

EDIT: If the read and calibrate instructions are removed, everything else works.

Perhaps it's time to post a link to the sensor, and a schematic showing how it is connected.

Okay.

Here's the tutorial from bildr: http://bildr.org/2012/01/adjd-s311_arduino/ Here's the sparkfun product page: https://www.sparkfun.com/products/10701

And, from what I have seen, some people are having the exact same problem as I have with this sensor. They were saying about switching the serial pins. I've switched the SDA and SCL, hoping the given schematic was wrong. Nothing happened. My wiring is exactly the same as it is in the tutorial.

EDIT: Also checked on a Duemilanove. The same as on the Leonardo.

Pin 13 has a resistor in series with it. That can sometimes cause problems. If you use another pin, do you get different results?

I tried with other pins than 13, and I have the same result.

If you look in the cpp file of the sensor's library, you'll see a function called performMeasurement(). If I debug with Serial.print, it prints on the monitor wherever I put a Serial.print in the function. The funny thing is that, if I put a Serial.print exactly after the function's call, nothing appears on the screen.

This is the performMeasurement function:

void ADJDS311::performMeasurement(){ 
  writeRegister(0x01, 0x00); 
 // start sensing
  while(readRegister(0x00) != 0)
    ; // waiting for a result//
}

So, it would appear that readRegister() never gets a non-zero value.

You could hack the library to print out the result of the call to readRegister(). The serial output would slow the retry loop down, but you'll just have to hope that doesn't matter. Once you know what result you're getting back, you could look at the data sheet for your device and see what that result means.

Okay. Another strange activity. I observed the sensor's LED blinks some times, strangely, not when I reset the board, both Serial/button.

Another strange fact is that the same library works for some one, and for some other isn't working. I mean, might be the solderings? The solderings I made are quite strong. And a last one is that instead of seeing on the monitor "Init", I sometimes see "In" or "Ini", and sometimes I don't even see it. The word "init" suggests the Arduino has communicated with the sensor.

Based on what I have rechecked, is that the program somehow hangs at the Wire.endTransmission() at some points. Do you know, why the Arduino would do that?

I have the same problem with the I2C protocol on arduino leonardo

I try to use a I2C lcd with my arduino but it doesn't show anting

this is my mounted board, I notice that the I2C works, sending information because just with that address (0x20) the lcd blinks once an then it turn off. when I load a sketch that uses the serial protocol, it blinks when I send data to the lcd.

the problem is the same the back light is turn off and the first lien is white, in the picture I use a led lamp just for emulate the light of the lcd that is turn off.

I don't know about this, but my guess is that the problem is on the wire library or in the clock speed of the leonardo.