Transform I2C to SPI | Arduino Micro

Hi everyone!

I'm new in the forum. I have a TCS34725 (RGB sensor) and an Arduino Micro and I get the data perfectly via i2c. I want to know if it is posible to transform i2c to SPI by using the Arduino because I want to send the data to a Xbee.

If someone can provide me a sketch or part of a code it would be great! I would also appreciate instructions of how to wire the arduino micro with the xbee.

Thanks!

I want to know if it is posible to transform i2c to SPI by using the Arduino because I want to send the data to a Xbee.

To answer your question: yes, this is possible. But it doesn't make sense in this context. An XBee (you didn't specify which type of Xbee you're writing about) usually is connected by a UART interface, although a few XBee products do also have an SPI interface. As XBee are communications interfaces (tranceivers) you don't convert I2C traffic to SPI traffic but you have to write some application specific code that read the relevant values and transfers them using some XBee module.

Sorry for not explaining which model of XBee I have, the model is XBee SX868 (https://www.digi.com/resources/documentation/digidocs/pdfs/90001538.pdf).

The sensor communicates the arduino by using I2C, and the XBee SX868 has two interfaces UART and SPI. I understand that is easier transforming I2C to SPI than UART ( maybe I’m mistaken), once I have this conversion maden I was thinking about using XCTU software to send this data to a Gateway (XCTU because I don’t have a XBee shield).

So, according about what are you saying. Is it not necessary to do that conversion? In that case, What XBee pins should I use and how it should be the wiring?

I attach the code I use for the RGB sensor:


#include <Wire.h>
#include <Adafruit_TCS34725.h>

/* Initialise with default values (int time = 2.4ms, gain = 1x) */
// Adafruit_TCS34725 tcs = Adafruit_TCS34725();

Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X);

void setup(void) {
Serial.begin(9600);

if (!tcs.begin())
{
Serial.println(“Error al iniciar TCS34725”);
while (1) {
delay(1000);
Serial.println(“error”);

}
}

}

void loop(void) {
uint16_t r, g, b, c, colorTemp, lux;

tcs.getRawData(&r, &g, &b, &c);
colorTemp = tcs.calculateColorTemperature(r, g, b);
lux = tcs.calculateLux(r, g, b);

Serial.print(“Temperatura color: “); Serial.print(colorTemp, DEC); Serial.println(” K”);
Serial.print("Lux : "); Serial.println(lux, DEC);
Serial.print("Rojo: "); Serial.println(r, DEC);
Serial.print("Verde: "); Serial.println(g, DEC);
Serial.print("Azul: "); Serial.println(b, DEC);
Serial.print("Clear: “); Serial.println(c, DEC);
Serial.println(” ");
delay(1000);
}

Thanks!

Please use code tags

I understand that is easier transforming I2C to SPI than UART ( maybe I'm mistaken),

In the posted sketch you're already doing the conversion from I2C to UART (Serial is the UART object). So why should the SPI version be easier?

Ok, I believe that I can use UART to send the data to the XBee. Is it enough to use the TX and RX pins or do I need also the CTS and RTS pins? Can there be erroneous values in what the xbee receives due to the UART connection (USB) between the PC and the arduino?

Thanks

Is it enough to use the TX and RX pins or do I need also the CTS and RTS pins?

It should be enough to connect the TX/RX pins and GND.

Can there be erroneous values in what the xbee receives due to the UART connection (USB) between the PC and the arduino?

On the Micro (ATmega32U4) the USB connection is on Serial while the hardware serial interface (D0/D1) is on Serial1, so the two connections shouldn't interfere.