colinodowd
Member
Hi,
I do not understand how to use the <i2c_t3.h> library for my application because one of my devices is using <Wire.h> through an instance of LightSensor. I need to run a secondary device on Wire1 and have tried using pins 29/30 and 16/17 in the below code.
The first I2C device (Light sensor) is working on pins 17/18 and my second I2C device (accelerometer) is not working. When I plug the accelerometer into pins 17/18 it works but the LightSensor has to be unplugged. Any guidance would be very helpful. Thanks.
This is my code:
I do not understand how to use the <i2c_t3.h> library for my application because one of my devices is using <Wire.h> through an instance of LightSensor. I need to run a secondary device on Wire1 and have tried using pins 29/30 and 16/17 in the below code.
The first I2C device (Light sensor) is working on pins 17/18 and my second I2C device (accelerometer) is not working. When I plug the accelerometer into pins 17/18 it works but the LightSensor has to be unplugged. Any guidance would be very helpful. Thanks.
This is my code:
Code:
#define HWSERIAL Serial1
#include <MFRC522.h>
#include <SPI.h>
#include <Wire.h>
#include "BH1750FVI.h"
#define VIB_PIN 6
#define RST_PIN 9
#define SS_PIN 10
#define SCK_PIN 13
BH1750FVI LightSensor;
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
const int MPU_ADDR = 0x68; // I2C address of the MPU-6050. If AD0 pin is set to HIGH, the I2C address will be 0x69.
int16_t accelerometer_x, accelerometer_y, accelerometer_z; // variables for accelerometer raw data
int16_t temperature; // variables for temperature data
char tmp_str[7]; // temporary variable used in convert function
void RFID_reader() {
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
//Show UID on serial monitor
HWSERIAL.print("UID = ");
String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
HWSERIAL.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
HWSERIAL.print(mfrc522.uid.uidByte[i], HEX);
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
HWSERIAL.println();
}
long TP_init(){
long measurement=digitalRead(VIB_PIN); //wait for the pin to get HIGH and returns measurement
return measurement;
}
char* convert_int16_to_str(int16_t i) { // converts int16 to string. Moreover, resulting strings will have the same length in the debug monitor.
sprintf(tmp_str, "%6d", i);
return tmp_str;
}
void setup() {
pinMode(VIB_PIN, INPUT);
HWSERIAL.begin(9600);
SPI.setSCK(SCK_PIN);
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
LightSensor.begin();
LightSensor.SetAddress(0x23);
LightSensor.SetMode(Continuous_H_resolution_Mode);
Wire1.begin();
Wire1.beginTransmission(MPU_ADDR); // Begins a transmission to the I2C slave (GY-521 board)
Wire1.write(0x6B); // PWR_MGMT_1 register
Wire1.write(0); // set to zero (wakes up the MPU-6050)
Wire1.endTransmission(true);
}
void loop() {
RFID_reader();
long measurement = TP_init();
HWSERIAL.print("Vib = ");HWSERIAL.print(measurement);
HWSERIAL.println();
delay(100);
uint16_t lux = LightSensor.GetLightIntensity();
HWSERIAL.print("Lux = ");HWSERIAL.print(lux);
HWSERIAL.println();
Wire1.beginTransmission(MPU_ADDR);
Wire1.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) [MPU-6000 and MPU-6050 Register Map and Descriptions Revision 4.2, p.40]
Wire1.endTransmission(false); // the parameter indicates that the Arduino will send a restart. As a result, the connection is kept active.
Wire1.requestFrom(MPU_ADDR, 7*2, true); // request a total of 7*2=14 registers
// "Wire.read()<<8 | Wire.read();" means two registers are read and stored in the same variable
accelerometer_x = Wire1.read()<<8 | Wire1.read(); // reading registers: 0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L)
accelerometer_y = Wire1.read()<<8 | Wire1.read(); // reading registers: 0x3D (ACCEL_YOUT_H) and 0x3E (ACCEL_YOUT_L)
accelerometer_z = Wire1.read()<<8 | Wire1.read(); // reading registers: 0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L)
temperature = Wire1.read()<<8 | Wire1.read();
HWSERIAL.print("aX = ");HWSERIAL.print(convert_int16_to_str(accelerometer_x));
HWSERIAL.println();
delay(100);
HWSERIAL.print("aY = ");HWSERIAL.print(convert_int16_to_str(accelerometer_y));
HWSERIAL.println();
delay(100);
HWSERIAL.print("aZ = ");HWSERIAL.print(convert_int16_to_str(accelerometer_z));
HWSERIAL.println();
delay(100);
HWSERIAL.print("Temp = ");HWSERIAL.print(temperature/340.00+36.53);
HWSERIAL.println();
delay(100);
}