My brother and I have been trying to get this working for days now and we just can't figure out what we are doing wrong, we could really use some help please!
What we're trying to accomplish is reading in data from a light sensor on an RPI expansion board through our own program written in C, using the BCM2835 library.
This is the light sensor we are using: TSL2561 https://cdn-shop.adafruit.com/datasheets/TSL2561.pdf
We are using a raspberry pi B+ model with Raspbian installed on it (through noobs).
This is the C library we are using: http://www.airspayce.com/mikem/bcm2835/
I activated I2c through the raspian configuration.
and I detected the light sensor through i2ctools and it shows up correctly with adress 0x29.
We get 0 values back with our reads, and we tested our commands with an osciloscope and it seems he does ACK the write commands. he just doesn't send anything back...
Can someone please help us ?
#include <bcm2835.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
uint16_t clk_div = BCM2835_I2C_CLOCK_DIVIDER_148;
uint8_t slave_address = 0x29; //0101001 - this is the sensor address
uint64_t delay = 70000;
int main(int argc, char **argv)
{
/*
DATA0LOW Ch 7:0 ADC channel 0 lower byte
DATA0HIGH Dh 7:0 ADC channel 0 upper byte
*/
char buffer[10]={0};
char addr;
uint16_t data;
uint8_t data2;
int i =0;
char writeBuff[1] = {0x8C}; ////Address the Ch0 lower data register
char readBuff[10];
char writeBuff2[2] = {0x8D}; ////Address the Ch0 upper data register
char readBuff2[10];
char *wb_ptr,*r_ptr,*wb_ptr2,*r_ptr2;
wb_ptr = writeBuff;
wb_ptr2 = writeBuff2;
r_ptr = readBuff;
r_ptr2 = readBuff2;
printf("Running ... \n");
bcm2835_init():
bcm2835_i2c_begin();
bcm2835_i2c_setSlaveAddress(slave_address); //0x29
printf("Clock divider set to: %d\n", clk_div);
printf("Slave address set to: %d or %X\n",slave_address,slave_address);
//needed according to datasheet to read although unsure if it needs to be sent in two writes or in one ? 0x83 instead of 0x80 + 0x03 ?
bcm2835_i2c_write(0x80, 1); //command register
bcm2835_i2c_write(0x03, 1); //command itself
bcm2835_delayMicroseconds(delay);
//--------------------------
while (1)
{
printf("reading data from light sensor\n");
bcm2835_i2c_write(wb_ptr, 1); // 0x8C
bcm2835_delayMicroseconds(delay);
data = bcm2835_i2c_read(readBuff,1);
bcm2835_delayMicroseconds(delay);
printf("Read Result 1 = %d\n", data);
bcm2835_i2c_write(wb_ptr2, 1); //0x8D
bcm2835_delayMicroseconds(delay);
data2 = bcm2835_i2c_read(readBuff2,1);
bcm2835_delayMicroseconds(delay);
printf("Read Result 2 = %d\n", data);
bcm2835_delay(1000);
}
bcm2835_i2c_end();
bcm2835_close();
printf("... done\n");
return 0;
}
This is a Quick edit
#include <bcm2835.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
uint16_t clk_div = BCM2835_I2C_CLOCK_DIVIDER_148;
uint8_t slave_address = 0x29; //0101001 - this is the sensor address
uint64_t delay = 70000;
int main(int argc, char **argv)
{
/*
DATA0LOW Ch 7:0 ADC channel 0 lower byte
DATA0HIGH Dh 7:0 ADC channel 0 upper byte
*/
/*
enum bcm2835I2CReasonCodes { BCM2835_I2C_REASON_OK = 0x00, BCM2835_I2C_REASON_ERROR_NACK = 0x01, BCM2835_I2C_REASON_ERROR_CLKT = 0x02, BCM2835_I2C_REASON_ERROR_DATA = 0x04 }
*/
char buffer[10]={0};
char addr;
uint16_t data;
uint8_t data2;
uint8_t error = 0xff;
int i =0;
char writeBuff[1] = {0x8C}; ////Address the Ch0 lower data register
char readBuff[10];
char writeBuff2[2] = {0x8D}; ////Address the Ch0 upper data register
char readBuff2[10];
char writeBuff3[2] = {0x80};
char writeBuff4[2] = {0x03};
char *wb_ptr,*r_ptr,*wb_ptr2,*r_ptr2,*r_ptr3,*r_ptr4;
wb_ptr = writeBuff;
wb_ptr2 = writeBuff2;
r_ptr = readBuff;
r_ptr2 = readBuff2;
r_ptr3 = writeBuff3;
r_ptr4 = writeBuff4;
printf("Running ... \n");
bcm2835_init();
bcm2835_i2c_begin();
bcm2835_i2c_setSlaveAddress(slave_address); //0x29
printf("Clock divider set to: %d\n", clk_div);
printf("Slave address set to: %d or %X\n",slave_address,slave_address);
//needed according to datasheet to read although unsure if it needs to be sent in two writes or in one ? 0x83 instead of 0x80 + 0x03 ?
bcm2835_i2c_write(r_ptr3, sizeof(r_ptr3)); //command register
bcm2835_i2c_write(r_ptr4, sizeof(r_ptr4)); //command itself
bcm2835_delayMicroseconds(delay);
//--------------------------
// Blink
while (1)
{
printf("reading data from light sensor\n");
error = bcm2835_i2c_write(wb_ptr, sizeof(wb_ptr)); // 0x8C
bcm2835_delayMicroseconds(delay);
data = bcm2835_i2c_read(readBuff,sizeof(readBuff));
bcm2835_delayMicroseconds(delay);
printf("readbuff1 = 0x%02X \n",readBuff);
printf("error result = 0x%02X\n", error);
printf("Read Result 1 = 0x%02X\n", data);
error = bcm2835_i2c_write(wb_ptr2, sizeof(wb_ptr2)); //0x8D
bcm2835_delayMicroseconds(delay);
data2 = bcm2835_i2c_read(readBuff2,sizeof(readBuff2));
bcm2835_delayMicroseconds(delay);
printf("readbuff2 = 0x%02X \n",readBuff2);
printf("error result = 0x%02X\n", error);
printf("Read Result 2 = 0x%02X\n", data2);
bcm2835_delay(1000);
}
bcm2835_i2c_end();
bcm2835_close();
printf("... done\n");
return 0;
}
I managed to fix it, I had several problems , one of which was that I was adressing the wrong adress to send commands, it has to be 0xa0 instead of 0x80.
#include <bcm2835.h>
#include <stdio.h>
int main(void)
{
/*
DATA0LOW Ch 7:0 ADC channel 0 lower byte
DATA0HIGH Dh 7:0 ADC channel 0 upper byte
*/
/*
enum bcm2835I2CReasonCodes { BCM2835_I2C_REASON_OK = 0x00, BCM2835_I2C_REASON_ERROR_NACK = 0x01, BCM2835_I2C_REASON_ERROR_CLKT = 0x02, BCM2835_I2C_REASON_ERROR_DATA = 0x04 }
*/
if (!bcm2835_init())
return 1;
char uitgelezenTempWaarde[1];
int totalTemp[2];
int error =0;
bcm2835_i2c_begin(); // start i2c
bcm2835_i2c_setSlaveAddress(0x29); // slave address
bcm2835_i2c_set_baudrate(1000); // default
//----------- turn channels on for measurement ------
uitgelezenTempWaarde[0] = 0xa0;
error = bcm2835_i2c_write(uitgelezenTempWaarde,1);
if(error != 0x00)
{
printf("i2c error! : 0x%02X \n",error);
}
uitgelezenTempWaarde[0] = 0x03;
error = bcm2835_i2c_write(uitgelezenTempWaarde,1);
if(error != 0x00)
{
printf("i2c error! : 0x%02X \n",error);
}
bcm2835_delay(500);
error = bcm2835_i2c_read(uitgelezenTempWaarde,1);
if(error != 0x00)
{
printf("i2c error! : 0x%02X \n",error);
}
//----------- read data ------
uitgelezenTempWaarde[0] = 0xac; //DATA0LOW Ch 7:0 ADC channel 0 lower byte
error = bcm2835_i2c_write(uitgelezenTempWaarde,1);
if(error != 0x00)
{
printf("i2c error! : 0x%02X \n",error);
}
error = bcm2835_i2c_read(uitgelezenTempWaarde,1);
if(error != 0x00)
{
printf("i2c error! : 0x%02X \n",error);
}
totalTemp[1]= (int)uitgelezenTempWaarde[0];
uitgelezenTempWaarde[0] = 0xad; //DATA0HIGH Dh 7:0 ADC channel 0 upper byte
error = bcm2835_i2c_write(uitgelezenTempWaarde,1);
if(error != 0x00)
{
printf("i2c error! : 0x%02X \n",error);
}
error = bcm2835_i2c_read(uitgelezenTempWaarde,1);
if(error != 0x00)
{
printf("i2c error! : 0x%02X \n",error);
}
totalTemp[0] = (int)uitgelezenTempWaarde[0]; //
totalTemp[0] *= 256; //hex conversion for the highest byte so it is seen as a high number (16 bits)
printf("The light value is :%d\n",totalTemp[0]+totalTemp[1]);
bcm2835_i2c_end();
bcm2835_close();
return 0;
}