i2c: Alternative to using Serial2/UART in Photon

I have two circuit that send sensor data to photon via serial communication. Using one of them is fine with the help of Photon’s TX/RX pins and Serial1 port. How do I use both of them together? I looked into the Serial2 and looks like I need to do some soldering/unsoldering and I was suggested that there SHOULD be some easier alternative to this.

How do I make serial communication with 2 different sensor-circuits possible at the same time with the help of photon?

Hi @ishitadg, using Serial2 on the Photon is indeed difficult. Have you considering using something like a mux to switch between both sensors?

1 Like

No I had not until now because I need both the sensor data almost at the same time. How fast does the switching work? Do you think mux is the only solution?

You can invest into a mux, but you might also like to look into RedBear Duo.
RedBear is a Particle partner and the Duo is widely Photon compatible, use the Particle infrastructure, have also BLE on board and most importantly for you has two UARTs (RX/TX & A0/A1) readily available.

http://redbear.cc/wp-content/uploads/2015/11/Duo-pin-chart-page-R3.jpg

2 Likes

The mux is fairly quick (see datasheet here): http://www.ti.com/lit/ds/symlink/cd74hc4067.pdf. Switching time is on the order of 100s of nanoseconds.

2 Likes

@harrisonhjones, @ScruffR

So, the pH circuit I’m trying to serially communicate is this one. I have no idea about usage of i2c but I tried using that option as an alternative to UART limitations here. As of now, I am just trying to get the pH circuit to communicate with photon using i2c communication. I am running the sample code provided by them (I made the necessary Arduino to Photon changes and removed the #include lines as I figured we can automatically call Wire functions). I set up the circuit as guided by them here. Circuit’s SCL,SDA connected to D1 and D0 respectively.
However, no matter what I repeatedly get NO DATA (that is my first byte is always 255) in my Serial Monitor. What am I doing wrong?
Also, I looked at some other threads regarding i2c and people have talked about logic analyzer. Where can I monitor that?

A logic analyzer is a device that gets hooked up to your logic lines (in your case SDA/SCL) and interprets the electrical signals according to the respective protocol to make interpretation of the signals easier than with a mere oszilloscope.
But you’d monitor that on its own display.

Page 8 of the datasheets tell this

I’d suggest 4k7 pull-ups.
These pull-up resistors should be connected between your respective data pin and the voltage level you are powering the sensor with (5V … Vin pin / 3.3V … 3V3 pin)

3 Likes

It worked. Thank you :smile:

3 Likes

My next step is to send http post using the sensor data I have collected from the sensors (say pH, temp). But, unlike UART, in I2C, the sensors/slaves aren’t continuously sending data to the master (photon). Also, since the slaves depend upon the master to initiate communication, I have to manually send a serial command to extract reading from the slaves. How can I automate this (an alternative to manually sending serial commands) so that the sensors keep sending data that can be sent to my web server via http posts?

Hmm, I don’t quite follow.
Even with UART you’d need to Serial1.read() “manually” all the time, so if you just imagine the whole cycle required to trigger an I2C reading as something like Sensor.read() I don’t really see that much of a difference.
Actually I prefere to only get data when I want it than having to watch a stream of bytes and having to do the extracting of the desired info myself.

The difference is that I am dependent on serialEvent() to receive data in case of I2C:

  1. In UART, I do a Serial1.available(), to keep checking if the slave has any data avaialble on its port (Serial1) and keep receiving it and posting it. Even if there is no Serial event, I keep receiving sensor data.
  2. In the case for I2C, I have to manually send a command in the serial monitor (In my case"R", Page 49) each time I want to receive a reading. Thus, each time I am dependent on a serialEvent() to receive the sensor data.

I get it that this gives me the flexibility to receive a reading when I want to but my concern is that since I plan to deploy this for an IoT app, I would want to get rid of someone manually typing a command into the serial monitor to create a serialEvent.

@ishitadg, I’m not sure what serialEvent has to do with I2C as it only relates to serial ports. To continuously update the I2C based sensor values, simply have your loop() code send the “R” query command at regular intervals (say every second) and update, gather the data and send it to your web server. Perhaps it’s time to post your code so we can give you more specific advice! :grinning:

1 Like

It worked. I am sorry I think I was confusing the relation between I2C and serial ports as a mandatory one for communication.

1 Like

@ishitadg, so things are working?

Yes :slight_smile:

1 Like