Example Temperature I2C with MCP9803

I2C temperature sensor with MCP9803

For this example we will use the integrated MCP9803 microchip, it is a temperature sensor with I2C communication.



The PCF8591 has an integrated temperature sensor plus a dedicated alert pin. It has three pins to select the address of the I2C bus.

In this example we will only use the temperature sensor.

It can be seen in the diagram that we have put all the address pins to GND, that means that our peripheral I2C will have the address 000, which translated to our code 0x48.

The diagram of our sensor in PCB is as follows:




The following image shows what we are in the PCB, the 4 analog inputs and the communication connector with the power supply, which is the one that mainly interests us.pcb-mcp9803


The PCB that I designed for the sensor has the pins that are compatible with the I2C connectors that are prepared in the Shiny Mioio Motor, just like the Srf04 ultrasonics.

img_20140121_175050 img_20140121_175108

To put it into action in our program we must include the following:

The program is very similar to the PCF8591 converter, just change a line of code.

1. The library that has the I2C communication:

import ioio.lib.api.TwiMaster;

2. In the setup: (Note how I2C port number 1 is used, which corresponds to pins 47 and 48 of the MIOIO).

twi = ioio_.openTwiMaster(1, TwiMaster.Rate.RATE_100KHz, true);

3. In the loop:

final float sensorain1 = Readmcp9803(0x48, twi);

4. The functions used are as follows:

//Para la lectura de la conversión:
public float Readmcp9803(int address, TwiMaster port) {
float returnval = 0;
byte[] request = new byte[] { 0x30 };
 byte[] tempdata = new byte[2];
 try {
 port.writeRead(address, false, request,request.length,tempdata,tempdata.length);
float ain1 = tempdata[0];
//La siguiente linea es la que cambia respecto del ejemplo del 
//conversor analógico PCF8591
ain1+=(float)tempdata[1] / 256;
return ain1;
 } catch (ConnectionLostException e) {
 // TODO Auto-generated catch block
 } catch (InterruptedException e) {
 // TODO Auto-generated catch block
 } finally {}
return returnval;
//La función settext para la visualización del dato en un textView que nos hayamos creado.
//private void setText(final String str) {
 runOnUiThread(new Runnable() {
 public void run() {

Leave a Reply

Your email address will not be published.