Reading values from IR sensor using I2C

I am a beginner at using I2C communication to read data from sensors. I highly appreciate it if you could help me with this question. I have the sample which can work in STM32F103.
First, write a value of 0X00 to register 0X30, then write a value of 0X08 to register 0X30, and turn on the state machine, so that data conversion will begin inside the chip.
The temperature output register is three 8-bit registers, with a total of 24 data bits. The first ten digits are the integer part, the WD variable is the integer result, and the WENDU variable is the decimal result.
They told me the device address is 0X7f, but I can not find it in the sample code as below:
[Sample code]

#include "stm32f10x_gpio.h"             // Keil::Device:StdPeriph Drivers:GPIO
#include "stm32f10x_rcc.h"              // Keil::Device:StdPeriph Drivers:RCC
#include "systick.h"
#include "iic.h"

signed char adc,adc1,adc2,adc3;
signed long wd,wd1;
u8 yd1=3,yd2=3,yd3=3,yd4=3;
float wendu;
int main(void)
{
	SYSTICK_CONFIG();	    	   
	delay_init(72);
	IIC_Init();

	delay_ms(3);

	IIC_Start();													//initialize cmd										
	IIC_Send_Byte(0Xfe);
	IIC_Wait_Ack();
	IIC_Send_Byte(0x30);
	IIC_Wait_Ack();
	IIC_Send_Byte(0x00);
	IIC_Wait_Ack();	 
	IIC_Stop();

	IIC_Start();											//turn on the state machine												
	IIC_Send_Byte(0Xfe);
	IIC_Wait_Ack();
	IIC_Send_Byte(0x30);                 
	IIC_Wait_Ack();
	IIC_Send_Byte(0x08);									
	IIC_Wait_Ack();	 
	IIC_Stop();
	 
	while(1)
	{
	delay_ms(200);
	 IIC_Start();													//read register
	 IIC_Send_Byte(0Xfe);
	 IIC_Wait_Ack();
	 IIC_Send_Byte(0x10);
	 IIC_Wait_Ack();

	 IIC_Start();
	 IIC_Send_Byte(0Xff);
	 IIC_Wait_Ack();
	 adc=IIC_Read_Byte(0);
	 IIC_Stop();
	 

	 IIC_Start();													//read register
	 IIC_Send_Byte(0Xfe);
	 IIC_Wait_Ack();
	 IIC_Send_Byte(0x11);
	 IIC_Wait_Ack();

	 IIC_Start();
	 IIC_Send_Byte(0Xff);
	 IIC_Wait_Ack();
	 adc1=IIC_Read_Byte(0);
	 IIC_Stop();
	 

	 IIC_Start();													//read register
	 IIC_Send_Byte(0Xfe);
	 IIC_Wait_Ack();
	 IIC_Send_Byte(0x12);
	 IIC_Wait_Ack();

	 IIC_Start();
	 IIC_Send_Byte(0Xff);
	 IIC_Wait_Ack();
	 adc2=IIC_Read_Byte(0);
	 IIC_Stop();
	 
	 WD=(adc<<2)+(adc1>>6);
	 wd1=(adc<<24)+(adc1<<16)+(adc2<<8);
	 WENDU=(float)wd1/64/65536; 
	 delay_ms(100);

I connect the sense with the Nano. But I can not read the correct value , I do not know why. And this is my code

const int rs = 13, en = 12, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
float num;
byte temp, temp1;

void setup() {
  Wire.begin();
  Wire.beginTransmission(0X7f);
  Wire.write(0x30);
  Wire.write(0x00);
  Wire.endTransmission();
  Wire.beginTransmission(0X7f);
  Wire.write(0x30); 
  Wire.write(0x08);
  Wire.endTransmission();

  Serial.begin(9600);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
//  lcd.print("hello, world!");
}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  //lcd.print(millis() / 1000);
  Wire.beginTransmission(0X7f);
  Wire.write(0x10); 
  Wire.requestFrom(0X7f,1);
  byte x0 = Wire.read();
  Wire.endTransmission();
  Wire.beginTransmission(0X7f);
  Wire.write(0x11); 
  Wire.requestFrom(0X7f,1);
  byte x1 = Wire.read();
//  byte x2 = Wire.read();
  unsigned int temp =  ((unsigned int) x1 >> 6) | ((unsigned int) x0 << 2);
 Wire.endTransmission();
  byte wd=(temp<<2)+(temp1>>6);
    lcd.print(wd);
}

Please correct your post and add code tags around your code.

There is a small pencil image below your existing posts.

  • click on this pencil ➜ that will let you edit your post.
  • Select the part of the text that corresponds to the code
  • Click on the <code/> icon in the toolbar to indicate that it is code
  • click image Save Edit

(Also make sure to properly indent the code in the IDE before copying and pasting it here. This can be done by pressing ctrlT on a PC or cmdT on a Mac)

To determine if you are using the correct I2C address, you might try running an I2C scanner such as the one from Adafruit.

What sensor are you using?

Is there an existing Arduino support library for it? If so, and there are working examples, try running one of those.

It's here in this bit of code:

It helps to understand a bit about IIC (or I2C) communication. The 7-bit device address is encoded into the upper 7 bits of this byte. The LSB of this byte is the read/write bit that tells the remote device whether you are wanting to perform a read or a write. In this case it's a 0 so it's a write command.

Sending 0xFE, 0x30, 0x00 is telling the device at address 0x7F that you want to write the value 0x00 to its internal address 0x30.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.