Connecting to a sensor - which pin to choose?

Hi,

I am just trying to connect an arduino to a gas sensor which has the following pins but not sure what the difference is and which to choose?

Any clues would be great :slight_smile:

TTL RXD
TTL TXD
I2C SCL
I2C SDA

It looks like the gas sensor can be controlled by I2C or through TTL serial.
I would use the I2C which is analogue pins 4 & 5. You will need the I2C library called wire and the data sheet for the gas sensor to know what address it is at and what commands it responds to.

Wow Mike - that has to be the fastest reply ever :slight_smile:

Would the I2C option be best for a noob like me? At the moment I'm looking for quick and easy - maybe look at an elegant solution when I know what I'm doing !

I think it is the best for you because the Arduino uses the serial lines to talk back to the computer and using it for other purposes is tricky as you are left with no form of feedback for debugging or transferring data back to the computer.

OK, great. I will start trying to find out how I2C works - looks like this is a good starting point: Arduino Playground - I2C

Thanks again.

Be sure you locate a ground signal connection on your sensor so that you can wire it to the Arduino's ground pin.

Lefty

Thanks folks, much appreciated.

I'm trying to connect to the sensor for the first time but not getting too far!!! Using the code below but might be completely wrong?

Here is some info on the sensor but I'm not totally sure what it all means yet:

The sensor doesn't have a handshake.
It has a 10bit data format
Operates in slave mode: Address 0x31
Slave address byte: slave address (0x31) 7bit + R/W 1 bit
R/W bit: Raed=1/ Write=0
Reading data - slave address bit = 0x63
Writing data - slave address bit = 0x62

I guess I'm going badly wrong but not quite sure how or why - any I2C wizzes out there?

Thanks.

// Wire Master Reader
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device
// Refer to the "Wire Slave Sender" example for use with this

// Created 29 March 2006

#include <Wire.h>

void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop()
{
  Wire.requestFrom(2, 10);    // request 6 bytes from slave device #2

  while(Wire.available())    // slave may send less than requested
  { 
    char c = Wire.receive(); // receive a byte as character
    Serial.print(c);         // print the character
  }

  delay(500);
}

First of all PLEASE post you code between the brackets you get when you click the hash icon in the reply box.

You have to send to the address of the device the line:-
Wire.requestFrom(2, 10);
Is requesting 10 bytes of data from a device address of 2
you should be using:-
Wire.requestFrom(0x31, 10);

However the rest of the code does not look right as you are not sending anything to the device in the first place. You normally set up a register with something like:-
Wire.beginTransmission(address);
Wire.send(register);
Wire.endTransmission();

Can you put us out of our misery and say what device you are trying to talk to .

Hi Mike, Sorry, a bit slow this end.

The sensor is for gas and samples automatically every 3 seconds and transmits the data. As such I have tried just picking up the data as it's transmitted with the code below but I'm just getting '0's.... I guess I'm still not sure what the hex values are that I am addressing? The data sheet says the SLAVE ADDRESS is 0x31 but also 'when reading the data, Slave Address Byte is 0x63... which is infact 31 with and extra 0 at the end? I have tried both 0x31 & 0x63.

I can see an led blinking on the sensor every 3 seconds so I guess it is working.

#include "Wire.h"
//wire library

#define delayC 5000
//delay count in ms

#define baudrate 9600
//baudrate for communication

void setup()
{
  Wire.begin();
  Serial.begin(baudrate);
}

void loop()
{
  Serial.print("gas PPM: ");

  int gas;
  
  Wire.requestFrom(0x31,4);
   {
    gas = Wire.receive();
    Serial.println(gas);
  }
  
  delay(delayC);
}

I can see I'm going quite badly wrong here !!! Just been reading about pullup resistors which I'm not using.

Can anyone enlighten me?.... are these required and how do I know which value to use? I guess there is a better chance of getting the code working if I have the hardware setup right !!!

Pull up resistors. 4K7 from both A4 & A5 to +5V. It will work without it but it is so much better for noise margin if you have them.

Code.
Please look at the I2C examples in the libraries. You need to wait until data is available before reading it. If you request 4 bytes of data then you have to read 4 bytes of data before requesting more. You normally send something to an I2C device before requesting any data from it, please read the data sheet for the device you have and see what needs to be sent first.

Check the address is correct, this is a 7 bit address not an 8 bit one. If the address includes the read / write bit at the end remove it and shift the address bit pattern one place to the right.

Thanks again Mike, I'm slowly getting the picture. I think the problem might be the inclusion of the read/write bit as I have tried many permutations of code - I will try shifting the address as explained (along with adding a couple of pullups)

Have a good weekend.