Write and read I2C on arduino

57 Ansichten (letzte 30 Tage)
Benedikt Kniebel
Benedikt Kniebel am 20 Mär. 2020
Kommentiert: serhat YILDIZHAN am 17 Jun. 2021
Hey there,
I want to get the data out of the Sensirion SDP810-125Pa via my arduino. With a llibrary that I downloaded from github, the sensor works fine and gives me data via I2C and the serial moitor respectively with the Arduino IDE.
Now I wanna combine it with Matlab.
clear all
clc
myarduino = arduino('COM3','Uno','Libraries','I2C');
%scanI2CBus(myarduino,0);
pressureSensor = device(myarduino,'I2CAddress',0x25,'bitrate',100000);
write(pressureSensor, 0x361E, 'uint16');
data = read(pressureSensor, 2,'uint16');
disp(data);
I configured the I2CAddress but now it gives me the followin error.
Error using Untitled (line 6)
Error occurred while reading data from the I2C device. Check the connections between the hardware and device, clear the device object and create a
new one.
Thanks for your help!
  1 Kommentar
Danielle Park
Danielle Park am 20 Nov. 2020
Hey Benedikt,
Did you ever figure this out? I am trying to do the same but with a Sensirion SLF3S-1300F sensor.
-Danielle

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Aakriti Suman
Aakriti Suman am 27 Mär. 2020
I understand that you want to use I2C devices on Arduino hardware.
The address that you are using for I2C communication seems incorrect. The bus address used for I2C comunication on Arduino hardware is 0x48. You can also scan the I2C bus to get the I2C address. You can try the below code snippet.
% connect all the I2C devices with the arduino and then perform the following snippet
myarduino = arduino('COM4','Uno','Libraries','I2C') % assuming that the arduino hardware is connected in COM4 serial port.
add = scanI2CBus(myarduino);
Here is an example documentation of the usage of I2C devices on arduino hardaware. This will help you in understanding the I2C communication with arduino hardware in detail. https://in.mathworks.com/help/supportpkg/arduinoio/ug/measure-temperature-from-i2c-device-on-arduino-hardware.html
I am also attaching a document which will help you with the functions that can be used in reading and writing to I2C devices connected to arduino hardware. https://in.mathworks.com/help/supportpkg/arduinoio/i2c-sensors.html?s_tid=CRUX_lftnav
  4 Kommentare
Gongyan Du
Gongyan Du am 12 Jun. 2020
Hi @Aakriti Summan, My code is:
a=arduino('COM3','uno');
address = scanI2CBus(a,0);
disp(['i2c address: ', address]);
INA219 = '0x40';
Currentsensor = i2cdev(a,INA219);
INA219_ADC_CH0='40'; %CR 40
INA219_ADC_CH1='41'; % Shunt Voltage
INA219_ADC_CH2='42'; % Bus Voltage
INA219_ADC_CH3='43'; % Power
INA219_ADC_CH4='44';% Current
INA219_ADC_CH5='45';%
while 1
disp([' |CR|',' |Shunt V|',' |Bus V|',' |Power|',' |Current|',' |Cal|']);
write(Currentsensor,hex2dec(INA219_ADC_CH0));
read(Currentsensor,3,'uint16');
CR = read(Currentsensor,3,'uint16');
pause(0.1);
write(Currentsensor,hex2dec(INA219_ADC_CH1));
read(Currentsensor,3,'uint16');
V_shunt = read(Currentsensor,3,'uint16');
pause(0.1);
write(Currentsensor,hex2dec(INA219_ADC_CH2));
read(Currentsensor,3,'uint16');
V_bus = read(Currentsensor,3,'uint16');
pause(0.1);
write(Currentsensor,hex2dec(INA219_ADC_CH3));
read(Currentsensor,3,'uint16');
P = read(Currentsensor,3,'uint16');
pause(0.1);
write(Currentsensor,hex2dec(INA219_ADC_CH4));
read(Currentsensor,3,'uint16');
I = read(Currentsensor,3,'uint16');
pause(0.1);
write(Currentsensor,hex2dec(INA219_ADC_CH5));
read(Currentsensor,3,'uint16');
Cal = read(Currentsensor,3,'uint16');
pause(0.1);
disp([CR V_shunt V_bus P I Cal]);
end
serhat YILDIZHAN
serhat YILDIZHAN am 17 Jun. 2021
Hi Gungyan, I will take altitude, temperature and pressure data using the I2C connection with the bmp280 sensor and draw a graph. And I'm thinking of doing this by editing your code. But I couldn't understand some parts of the code. Can you help me?
Currentsensor = i2cdev(a,INA219); %How can I define it according to bmp280?
%Can you explain how you wrote this part?
INA219_ADC_CH0='40'; %CR 40
INA219_ADC_CH1='41'; % Shunt Voltage
INA219_ADC_CH2='42'; % Bus Voltage
INA219_ADC_CH3='43'; % Power
INA219_ADC_CH4='44';% Current
Thank you.

Melden Sie sich an, um zu kommentieren.


Benedikt Kniebel
Benedikt Kniebel am 21 Nov. 2020
Hey Danielle,
I just connected the sensor again this morning and reconstructed my setup.
I used the following code on the arduino (and cheked the results in the serial monitor):
#include <Wire.h>
#include <sdpsensor.h>
SDP8XXSensor sdp;
void setup() {
Wire.begin();
Serial.begin(9600);
delay(1000); // let serial console settle
do {
int ret = sdp.init();
if (ret == 0) {
Serial.print("init(): success\n");
break;
} else {
Serial.print("init(): failed, ret = ");
Serial.println(ret);
delay(1000);
}
} while(true);
}
void loop() {
int ret = sdp.readSample();
if (ret == 0) {
Serial.print(sdp.getDifferentialPressure());
Serial.write(13);
Serial.write(10);
// Serial.print("Differential pressure: ");
// Serial.print(" ");
// Serial.println(sdp.getTemperature());
} else {
Serial.print("Error in readSample(), ret = ");
Serial.println(ret);
}
delay(50);
}
This is just retuning the raw pressure sensor data / 20Hz.
Then I followed the steps here:
Make shure that you insert the correct terminator and COM-Port and maybe lower the if src.UserData.Count > 1001 for a reasonable amount of time between the measurements.
This woks for me and is giving me the serial data from the Arduino.
Hope I could help.
  1 Kommentar
Danielle Park
Danielle Park am 24 Nov. 2020
Yes! This is the route I ended up taking as well. Thank you so much!

Melden Sie sich an, um zu kommentieren.


Harsha Priya Daggubati
Harsha Priya Daggubati am 23 Mär. 2020
Hi,
You need to have either a Total Phase Aardvark host adapter or a NI USB-845x adapter board installed to use the i2c interface.
Refer to the following link for installation help:
You can also follow the steps listed here after the installation is successful:
These threads might be of help too:
  3 Kommentare
Harsha Priya Daggubati
Harsha Priya Daggubati am 23 Mär. 2020
I guess yes. Can you use write as follows similar to the one in the example.
write(pressureSensor, 0x361E, 'uint16');
Benedikt Kniebel
Benedikt Kniebel am 23 Mär. 2020
That is what I did and it throws the error above.
I also tried to use another I2C bus number.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Arduino Hardware finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by