Borrar filtros
Borrar filtros

connect I2c light meter sensor to MATLAB with arduino

3 visualizaciones (últimos 30 días)
ali alizadeh
ali alizadeh el 7 de Nov. de 2021
Hi
I have serius problem with arduino code and MATLAB code. I connect a light meter sesnor to arduino Uno that it's work properly with code on below ( on Arduino app):
/****************************************/
// Distributed with a free-will license.
// Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
// MAX44009
// This code is designed to work with the MAX44009_I2CS I2C Mini //Module available from ControlEverything.com.
// https://www.controleverything.com/products
#include<Wire.h>
// MAX44009 I2C address is 0x4A(74)
#define Addr 0x4A
void setup()
{
// Initialise I2C communication as MASTER
Wire.begin();
// Initialise serial communication, set baud rate = 9600
Serial.begin(9600);
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select configuration register
Wire.write(0x02);
// Continuous mode, Integration time = 800 ms
Wire.write(0x40);
// Stop I2C transmission
Wire.endTransmission();
delay(300);
}
void loop()
{
unsigned int data[2];
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select data register
Wire.write(0x03);
// Stop I2C transmission
Wire.endTransmission();
// Request 2 bytes of data
Wire.requestFrom(Addr, 2);
// Read 2 bytes of data
// luminance msb, luminance lsb
if (Wire.available() == 2)
{
data[0] = Wire.read();
data[1] = Wire.read();
}
// Convert the data to lux
int exponent = (data[0] & 0xF0) >> 4;
int mantissa = ((data[0] & 0x0F) << 4) | (data[1] & 0x0F);
float luminance = pow(2, exponent) * mantissa * 0.045;
// Output data to serial monitor
Serial.print("Ambient Light luminance :");
Serial.print(luminance);
Serial.println(" lux");
delay(300);
}
and this code have result like this ( attention on Lux unit):
In this code, a binary number has been converted to obtain data with a lux unit.
I dont know how to get these results in MATLAB.
I tried to get the results with the following code, but the data obtained differs from the Arduino data.
a = arduino('COM3', 'Uno', 'Libraries', 'I2C');
I = i2cdev(a, '0x4A');
write(I, 0, 'uint8');
data = read(I, 2, 'uint16');
data1 = data(1)
Please help me.

Respuestas (0)

Categorías

Más información sobre MATLAB Support Package for Arduino Hardware en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by